ic-query-cli 0.104.0

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Module: nns::operational_status
//!
//! Responsibility: shared CLI parsing and dispatch for observed node-status views.
//! Does not own: Dashboard collection, cache policy, report projection, or rendering.
//! Boundary: adds the same status operation beneath node, Subnet, and provider nouns.

use crate::cli::common::OutputFormat;
use crate::presentation::ic::ic_node_provider_status_report_text;
use crate::presentation::ic::ic_node_status_report_text;
use crate::presentation::ic::ic_subnet_status_report_text;
use crate::storage::cache_root;
use crate::{
    cli::common::{
        COLLECTION_MODE_CACHE_REFRESH_STALE, collection_help, current_unix_secs, write_text_or_json,
    },
    nns::NnsCommandError,
    progress::StderrQueryProgress,
};
use clap::{Args, Command as ClapCommand};
use ic_query::ic::DEFAULT_IC_DASHBOARD_SOURCE_ENDPOINT;
use ic_query::ic::IcNodeStatusReadRequest;
use ic_query::ic::IcNodeStatusView;
use ic_query::ic::build_ic_node_provider_status_report_with_source;
use ic_query::ic::build_ic_node_status_report_with_source;
use ic_query::ic::build_ic_subnet_status_report_with_source;

use std::path::PathBuf;

///
/// OperationalStatusSubject
///
/// NNS command noun selecting one projection over the shared node snapshot.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::nns) enum OperationalStatusSubject {
    Node,
    NodeProvider,
    Subnet,
}

impl OperationalStatusSubject {
    const fn command_name(self) -> &'static str {
        match self {
            Self::Node => "node",
            Self::NodeProvider => "node-provider",
            Self::Subnet => "subnet",
        }
    }

    const fn value_name(self) -> &'static str {
        match self {
            Self::Node => "node|node-prefix",
            Self::NodeProvider => "node-provider|node-provider-prefix",
            Self::Subnet => "subnet|subnet-prefix",
        }
    }

    const fn about(self) -> &'static str {
        match self {
            Self::Node => "Show observed operational status for IC nodes",
            Self::NodeProvider => "Show observed operational status grouped by node provider",
            Self::Subnet => "Show observed operational status and fault-distance by Subnet",
        }
    }

    const fn target_help(self) -> &'static str {
        match self {
            Self::Node => "Optional node principal or unique node-principal prefix",
            Self::NodeProvider => {
                "Optional node-provider principal or unique node-provider principal prefix"
            }
            Self::Subnet => "Optional Subnet principal or unique Subnet-principal prefix",
        }
    }
}

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

#[derive(clap::Args)]
#[group(skip)]
pub(super) struct StatusOptions {
    pub(super) target: Option<String>,
    #[arg(
        long,
        help = "Include fully-up rows or groups instead of attention rows only"
    )]
    pub(super) all: bool,
    #[arg(
        long,
        help = "Force a live snapshot refresh before rendering this view"
    )]
    pub(super) refresh: bool,
    #[command(flatten)]
    pub(super) source: crate::cli::common::SourceOutputOptions,
}

pub(in crate::nns) fn command(subject: OperationalStatusSubject) -> ClapCommand {
    configure_command(
        StatusOptions::augment_args(ClapCommand::new("status")),
        subject,
    )
}

pub(in crate::nns) fn configure_command(
    command: ClapCommand,
    subject: OperationalStatusSubject,
) -> ClapCommand {
    let command_name = subject.command_name();
    command.about(subject.about())
        .long_about(None)
        .after_help(collection_help(COLLECTION_MODE_CACHE_REFRESH_STALE,
            &format!("Examples:\n  icq nns {command_name} status\n  icq nns {command_name} status <target-or-prefix> --json\n  icq nns {command_name} status --all --refresh")))
        .mut_arg("source-endpoint", |arg| arg.default_value(DEFAULT_IC_DASHBOARD_SOURCE_ENDPOINT)
            .help("Official IC Dashboard API endpoint used for snapshot refreshes"))
        .mut_arg("target", |arg| arg.value_name(subject.value_name()).help(subject.target_help()))
}

/// Run one observed status projection over the shared cache identity.
pub(in crate::nns) async fn run(
    subject: OperationalStatusSubject,
    options: StatusOptions,
    network: &str,
) -> Result<(), NnsCommandError> {
    let (format, request) = status_request(options, network, cache_root()?, current_unix_secs()?);
    let mut progress = StderrQueryProgress::new();
    let source = ic_query::ic::LiveIcSource::default();

    match subject {
        OperationalStatusSubject::Node => {
            let report =
                build_ic_node_status_report_with_source(&request, &source, &mut progress).await?;
            write_text_or_json(format, &report, ic_node_status_report_text)
        }
        OperationalStatusSubject::NodeProvider => {
            let report =
                build_ic_node_provider_status_report_with_source(&request, &source, &mut progress)
                    .await?;
            write_text_or_json(format, &report, ic_node_provider_status_report_text)
        }
        OperationalStatusSubject::Subnet => {
            let report =
                build_ic_subnet_status_report_with_source(&request, &source, &mut progress).await?;
            write_text_or_json(format, &report, ic_subnet_status_report_text)
        }
    }
}

fn status_request(
    options: StatusOptions,
    network: &str,
    cache_root: PathBuf,
    now_unix_secs: u64,
) -> (OutputFormat, IcNodeStatusReadRequest) {
    let source = options.source;
    let format = source.format();
    let mut request =
        IcNodeStatusReadRequest::new(cache_root, network, source.source_endpoint, now_unix_secs);
    request.view = IcNodeStatusView {
        target: options.target,
        include_all: options.all,
    };
    request.force_refresh = options.refresh;

    (format, request)
}

#[cfg(test)]
mod tests;