use super::IcCommandError;
use crate::cli::common::OutputFormat;
use crate::cli::common::{
COLLECTION_MODE_LIVE, SourceOutputOptions, collection_help, current_unix_secs,
resolve_time_bounds, write_text_or_json,
};
use crate::presentation::ic::ic_boundary_node_data_centers_report_text;
use crate::presentation::ic::ic_daily_stats_report_text;
use clap::{Subcommand, builder::RangedU64ValueParser};
use ic_query::ic::DEFAULT_IC_BOUNDARY_NODE_DATA_CENTERS_SOURCE_ENDPOINT;
use ic_query::ic::DEFAULT_IC_DAILY_STATS_WINDOW_SECS;
use ic_query::ic::DEFAULT_IC_DASHBOARD_SOURCE_ENDPOINT;
use ic_query::ic::IcDailyStatsQuery;
use ic_query::ic::IcDailyStatsRequest;
use ic_query::ic::LiveIcSource;
use ic_query::ic::MIN_IC_DAILY_STATS_TIMESTAMP;
use ic_query::ic::build_ic_boundary_node_data_centers_report_with_source;
use ic_query::ic::build_ic_daily_stats_report_with_source;
const BOUNDARY_NODE_DATA_CENTERS_HELP_AFTER: &str = "\
Examples:
icq ic network boundary-node-data-centers
icq ic network boundary-node-data-centers --json
This command makes exactly one official Dashboard v4 request for the complete
boundary-node data-center resource. It does not issue per-location follow-up
calls or create a cache. Rows preserve the API's raw owner, region, coordinate,
and node-count strings, including locations that currently report zero nodes.
The Dashboard response is off-chain and non-certified.";
const DAILY_STATS_HELP_AFTER: &str = "\
Examples:
icq ic network daily-stats
icq ic network daily-stats --start 1784937600 --end 1785542400
icq ic network daily-stats --json
This command makes exactly one official Dashboard v3 request for an explicitly
bounded daily network-activity window. The default is the preceding seven days,
and every request is capped at one year and 366 returned rows. It never follows
up, paginates, or creates a cache. Rate values remain the raw strings returned
by this off-chain, non-certified API.";
pub(super) async fn run(command: NetworkCommand) -> Result<(), IcCommandError> {
match command {
NetworkCommand::BoundaryNodeDataCenters(options) => {
run_boundary_node_data_centers(options).await
}
NetworkCommand::DailyStats(options) => run_daily_stats(options).await,
}
}
async fn run_boundary_node_data_centers(
options: SourceOutputOptions,
) -> Result<(), IcCommandError> {
let (format, request) = options.into_dashboard_request(current_unix_secs()?);
let report =
build_ic_boundary_node_data_centers_report_with_source(&request, &LiveIcSource::default())
.await?;
write_text_or_json(format, &report, ic_boundary_node_data_centers_report_text)
}
async fn run_daily_stats(options: DailyStatsOptions) -> Result<(), IcCommandError> {
let (format, request) = daily_stats_request(options, current_unix_secs()?);
let report =
build_ic_daily_stats_report_with_source(&request, &LiveIcSource::default()).await?;
write_text_or_json(format, &report, ic_daily_stats_report_text)
}
#[derive(Subcommand)]
#[command(about = "Inspect bounded official Dashboard network analytics", long_about = None)]
pub(super) enum NetworkCommand {
#[command(about = "List official Dashboard boundary-node data-center aggregates", long_about = None, after_help = collection_help(COLLECTION_MODE_LIVE, BOUNDARY_NODE_DATA_CENTERS_HELP_AFTER))]
#[command(mut_arg("source-endpoint", |arg| arg.default_value(DEFAULT_IC_BOUNDARY_NODE_DATA_CENTERS_SOURCE_ENDPOINT).help("Official IC Dashboard API v4 base endpoint")))]
BoundaryNodeDataCenters(SourceOutputOptions),
#[command(about = "Query bounded official Dashboard daily network activity", long_about = None, after_help = collection_help(COLLECTION_MODE_LIVE, DAILY_STATS_HELP_AFTER))]
#[command(mut_arg("source-endpoint", |arg| arg.default_value(DEFAULT_IC_DASHBOARD_SOURCE_ENDPOINT).help("Official IC Dashboard API v3 base endpoint")))]
DailyStats(DailyStatsOptions),
}
#[derive(clap::Args)]
#[group(skip)]
pub(super) struct DailyStatsOptions {
#[arg(long, value_name = "unix-seconds", value_parser = RangedU64ValueParser::<u64>::new().range(MIN_IC_DAILY_STATS_TIMESTAMP..), help = "Inclusive start; defaults to seven days before end")]
pub(super) start: Option<u64>,
#[arg(long, value_name = "unix-seconds", value_parser = RangedU64ValueParser::<u64>::new().range(MIN_IC_DAILY_STATS_TIMESTAMP..), help = "Inclusive end; defaults to the current time")]
pub(super) end: Option<u64>,
#[command(flatten)]
pub(super) source: crate::cli::common::SourceOutputOptions,
}
fn daily_stats_request(
options: DailyStatsOptions,
now_unix_secs: u64,
) -> (OutputFormat, IcDailyStatsRequest) {
let (start_unix_secs, end_unix_secs) = resolve_time_bounds(
options.start,
options.end,
now_unix_secs,
DEFAULT_IC_DAILY_STATS_WINDOW_SECS,
0,
);
let (format, dashboard) = options.source.into_dashboard_request(now_unix_secs);
let request = IcDailyStatsRequest::new(
dashboard,
IcDailyStatsQuery::new(start_unix_secs, end_unix_secs),
);
(format, request)
}
#[cfg(test)]
mod tests;