ic-query-cli 0.104.1

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Module: ic::metrics
//!
//! Responsibility: parse and run bounded official Dashboard metric queries.
//! Does not own: Dashboard transport, report construction, or text rendering.
//! Boundary: exposes the metric command to the IC CLI facade.

use super::IcCommandError;
use crate::cli::clap::choice_parser;
use crate::cli::common::OutputFormat;
use crate::cli::common::{
    COLLECTION_MODE_LIVE, collection_help, current_unix_secs, resolve_time_bounds,
    write_text_or_json,
};
use crate::presentation::ic::ic_metric_report_text;
use clap::builder::RangedU64ValueParser;
use ic_query::ic::DEFAULT_IC_DASHBOARD_METRICS_SOURCE_ENDPOINT;
use ic_query::ic::DEFAULT_IC_METRIC_STEP_SECS;
use ic_query::ic::DEFAULT_IC_METRIC_WINDOW_SECS;
use ic_query::ic::IcMetricKind;
use ic_query::ic::IcMetricQuery;
use ic_query::ic::IcMetricRequest;
use ic_query::ic::LiveIcSource;
use ic_query::ic::MAX_IC_METRIC_STEP_SECS;
use ic_query::ic::MIN_IC_METRIC_TIMESTAMP;
use ic_query::ic::build_ic_metric_report_with_source;

const METRIC_KINDS: &[IcMetricKind] = &IcMetricKind::all();

const METRICS_HELP_AFTER: &str = "\
Examples:
  icq ic metrics instruction-rate
  icq ic metrics cycle-burn-rate --start 1700000000 --end 1700003600 --step 300
  icq ic metrics ic-node-count --json

This command makes exactly one official Dashboard Metrics API request for an
explicitly bounded window. It never follows up, paginates, or creates a cache.
The default is the preceding hour at a five-minute step, and every request is
capped at 1000 observations per returned series. Values are preserved as the
raw value strings returned by this off-chain, non-certified API.";

pub(super) async fn run(options: MetricOptions) -> Result<(), IcCommandError> {
    let (format, request) = metric_request(options, current_unix_secs()?);
    let report = build_ic_metric_report_with_source(&request, &LiveIcSource::default()).await?;
    write_text_or_json(format, &report, ic_metric_report_text)
}

///
/// MetricOptions
///
/// Typed arguments for the command operation.
///

#[derive(clap::Args)]
#[group(skip)]
#[command(about = "Query one bounded official Dashboard network metric time series", long_about = None, after_help = collection_help(COLLECTION_MODE_LIVE, METRICS_HELP_AFTER))]
#[command(mut_arg("source-endpoint", |arg| arg.default_value(DEFAULT_IC_DASHBOARD_METRICS_SOURCE_ENDPOINT).help("Official IC Dashboard Metrics API base endpoint")))]
pub(super) struct MetricOptions {
    #[arg(value_name = "metric", value_parser = choice_parser(METRIC_KINDS, IcMetricKind::as_str), help = "Official Dashboard metric path name")]
    pub(super) metric: IcMetricKind,
    #[arg(long, value_name = "unix-seconds", value_parser = RangedU64ValueParser::<u64>::new().range(MIN_IC_METRIC_TIMESTAMP..), help = "Inclusive start; defaults to one hour before end")]
    pub(super) start: Option<u64>,
    #[arg(long, value_name = "unix-seconds", value_parser = RangedU64ValueParser::<u64>::new().range(MIN_IC_METRIC_TIMESTAMP..), help = "Inclusive end; defaults to the current time")]
    pub(super) end: Option<u64>,
    #[arg(long, value_name = "seconds", default_value = DEFAULT_IC_METRIC_STEP_SECS.to_string(), hide_default_value = true, value_parser = RangedU64ValueParser::<u32>::new() .range(1..=u64::from(MAX_IC_METRIC_STEP_SECS)), help = "Observation step in seconds; defaults to 300")]
    pub(super) step: u32,
    #[command(flatten)]
    pub(super) source: crate::cli::common::SourceOutputOptions,
}

fn metric_request(options: MetricOptions, now_unix_secs: u64) -> (OutputFormat, IcMetricRequest) {
    let (start_unix_secs, end_unix_secs) = resolve_time_bounds(
        options.start,
        options.end,
        now_unix_secs,
        DEFAULT_IC_METRIC_WINDOW_SECS,
        0,
    );
    let (format, dashboard) = options.source.into_dashboard_request(now_unix_secs);
    let request = IcMetricRequest::new(
        dashboard,
        IcMetricQuery::new(options.metric, start_unix_secs, end_unix_secs, options.step),
    );
    (format, request)
}

#[cfg(test)]
mod tests;