ic-query-cli 0.101.0

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Module: nns::topology::capacity
//!
//! Responsibility: render NNS topology capacity reports as text.
//! Does not own: capacity calculation, source reads, or JSON output.
//! Boundary: combines summary and attention capacity tables for humans.

use super::{common::render_value_table, percent::ratio_percent_text};
use crate::presentation::text_value::table;
use ic_host_text::table::Alignment;

use crate::presentation::nns::topology::COMPACT_PRINCIPAL_CHARS;
use crate::presentation::text_value::compact_text;
use crate::presentation::text_value::optional_number_text;
use ic_query::nns::topology::NnsTopologyCapacityReport;

#[must_use]
/// Render the NNS topology capacity report as human-facing text.
pub fn nns_topology_capacity_report_text(report: &NnsTopologyCapacityReport) -> String {
    let lines = [
        render_capacity_summary_table(report),
        String::new(),
        render_capacity_attention_table(report),
    ];
    lines.join("\n")
}

fn render_capacity_summary_table(report: &NnsTopologyCapacityReport) -> String {
    render_value_table(
        ["FIELD", "VALUE"],
        [
            ("network", report.network.clone()),
            ("status", report.status.as_str().to_string()),
            ("node_operators", report.capacity.len().to_string()),
            (
                "total_node_allowance",
                report.total_node_allowance.to_string(),
            ),
            ("assigned_nodes", report.assigned_node_count.to_string()),
            (
                "available_node_slots",
                report.available_node_slots.to_string(),
            ),
            (
                "over_assigned_operators",
                report.over_assigned_operator_count.to_string(),
            ),
            (
                "over_assigned_nodes",
                report.over_assigned_node_count.to_string(),
            ),
            (
                "unknown_node_count_operators",
                report.unknown_node_count_operator_count.to_string(),
            ),
        ],
    )
}

fn render_capacity_attention_table(report: &NnsTopologyCapacityReport) -> String {
    let attention_rows = report
        .capacity
        .iter()
        .filter(|row| {
            matches!(
                row.status,
                ic_query::nns::topology::NnsTopologyCapacityStatus::Over
                    | ic_query::nns::topology::NnsTopologyCapacityStatus::Unknown
            )
        })
        .collect::<Vec<_>>();
    if attention_rows.is_empty() {
        let headers = ["STATUS", "DETAIL"];
        let rows = [[
            report.status.as_str().to_string(),
            "no capacity attention rows".to_string(),
        ]];
        let alignments = [Alignment::Left; 2];
        return table(&headers, &rows, &alignments);
    }

    let headers = [
        "NODE_OPERATOR",
        "NODE_PROVIDER",
        "DATA_CENTER",
        "ALLOWANCE",
        "NODES",
        "AVAILABLE",
        "OVER",
        "UTILIZATION",
        "STATUS",
    ];
    let rows = attention_rows
        .iter()
        .map(|row| {
            [
                compact_text(&row.node_operator_principal, COMPACT_PRINCIPAL_CHARS),
                compact_text(&row.node_provider_principal, COMPACT_PRINCIPAL_CHARS),
                row.data_center_id.clone(),
                row.node_allowance.to_string(),
                optional_number_text(row.assigned_node_count),
                optional_number_text(row.available_node_slots),
                optional_number_text(row.over_assigned_node_count),
                row.assigned_node_count.map_or_else(
                    || "-".to_string(),
                    |assigned| ratio_percent_text(assigned, row.node_allowance),
                ),
                row.status.as_str().to_string(),
            ]
        })
        .collect::<Vec<_>>();
    let alignments = [
        Alignment::Left,
        Alignment::Left,
        Alignment::Left,
        Alignment::Right,
        Alignment::Right,
        Alignment::Right,
        Alignment::Right,
        Alignment::Right,
        Alignment::Left,
    ];
    table(&headers, &rows, &alignments)
}