use crate::cli::common::OutputFormat;
use crate::storage::cache_root;
use crate::{
cli::{
clap::{flag_arg, string_option},
common::{
COLLECTION_MODE_CACHE_REFRESH_STALE, SourceOutputOptions, collection_help,
current_unix_secs, json_arg, source_endpoint_arg, write_text_or_json,
},
},
nns::NnsCommandError,
progress::StderrQueryProgress,
};
use clap::{ArgMatches, 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;
use ic_query::ic::build_ic_node_status_report;
use ic_query::ic::build_ic_subnet_status_report;
use ic_query_text::ic::ic_node_provider_status_report_text;
use ic_query_text::ic::ic_node_status_report_text;
use ic_query_text::ic::ic_subnet_status_report_text;
use std::path::PathBuf;
const ALL_ARG: &str = "all";
const REFRESH_ARG: &str = "refresh";
const TARGET_ARG: &str = "target";
#[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",
}
}
}
pub(in crate::nns) fn command(subject: OperationalStatusSubject) -> ClapCommand {
let command_name = subject.command_name();
ClapCommand::new("status")
.bin_name(format!("icq nns {command_name} status"))
.about(subject.about())
.arg(
crate::cli::clap::value_arg(TARGET_ARG)
.value_name(subject.value_name())
.help(subject.target_help()),
)
.arg(
flag_arg(ALL_ARG)
.long(ALL_ARG)
.help("Include fully-up rows or groups instead of attention rows only"),
)
.arg(
flag_arg(REFRESH_ARG)
.long(REFRESH_ARG)
.help("Force a live snapshot refresh before rendering this view"),
)
.arg(json_arg())
.arg(
source_endpoint_arg(DEFAULT_IC_DASHBOARD_SOURCE_ENDPOINT)
.help("Official IC Dashboard API endpoint used for snapshot refreshes"),
)
.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"
),
))
}
pub(in crate::nns) async fn run(
subject: OperationalStatusSubject,
matches: &ArgMatches,
network: &str,
) -> Result<(), NnsCommandError> {
let (format, request) = status_request(matches, network, cache_root()?, current_unix_secs()?);
let mut progress = StderrQueryProgress::new();
match subject {
OperationalStatusSubject::Node => {
let report = build_ic_node_status_report(&request, &mut progress).await?;
write_text_or_json(format, &report, ic_node_status_report_text)
}
OperationalStatusSubject::NodeProvider => {
let report = build_ic_node_provider_status_report(&request, &mut progress).await?;
write_text_or_json(format, &report, ic_node_provider_status_report_text)
}
OperationalStatusSubject::Subnet => {
let report = build_ic_subnet_status_report(&request, &mut progress).await?;
write_text_or_json(format, &report, ic_subnet_status_report_text)
}
}
}
fn status_request(
matches: &ArgMatches,
network: &str,
cache_root: PathBuf,
now_unix_secs: u64,
) -> (OutputFormat, IcNodeStatusReadRequest) {
let source = SourceOutputOptions::from_matches(matches);
let mut request =
IcNodeStatusReadRequest::new(cache_root, network, source.source_endpoint, now_unix_secs);
request.view = IcNodeStatusView {
target: string_option(matches, TARGET_ARG),
include_all: matches.get_flag(ALL_ARG),
};
request.force_refresh = matches.get_flag(REFRESH_ARG);
(source.format, request)
}
#[cfg(test)]
mod tests;