Skip to main content

ic_query_cli/presentation/ic/node_status/
mod.rs

1//! Module: ic::node_status
2//!
3//! Responsibility: human-readable observed node, Subnet, provider, and refresh reports.
4//! Does not own: source collection, cache policy, projection, or JSON output.
5//! Boundary: keeps compact operational display separate from raw report fields.
6
7use crate::presentation::text_value::field;
8use crate::presentation::text_value::table;
9use ic_host_text::escape::terminal_text;
10use ic_host_text::table::Alignment;
11
12use crate::presentation::text_value::yes_no;
13use ic_query::ic::IcNodeAssignmentStatusCounts;
14use ic_query::ic::IcNodeCountComparisonCounts;
15use ic_query::ic::IcNodeProviderStatusReport;
16use ic_query::ic::IcNodeStatusCounts;
17use ic_query::ic::IcNodeStatusObservation;
18
19use ic_query::ic::IcNodeStatusRefreshReport;
20use ic_query::ic::IcNodeStatusReport;
21use ic_query::ic::IcNodeStatusRow;
22use ic_query::ic::IcSubnetStatusReport;
23
24const COMPACT_PRINCIPAL_CHARS: usize = 8;
25
26/// Render a human-readable node-level operational status report.
27#[must_use]
28pub fn ic_node_status_report_text(report: &IcNodeStatusReport) -> String {
29    let mut sections = observation_lines(&report.observation);
30    sections.push(counts_line("nodes", &report.counts.statuses));
31    sections.push(assignment_status_line(&report.counts.assignment_statuses));
32    push_table_section(&mut sections, render_node_table(report.nodes.iter()));
33    sections.join("\n")
34}
35
36/// Render a human-readable Subnet operational status report.
37#[must_use]
38pub fn ic_subnet_status_report_text(report: &IcSubnetStatusReport) -> String {
39    let mut sections = observation_lines(&report.observation);
40    sections.push(format!(
41        "subnets: total={} attention={} returned={} assigned_nodes={}",
42        report.subnet_count,
43        report.attention_subnet_count,
44        report.subnets.len(),
45        report.assigned_node_count
46    ));
47    let headers = [
48        "SUBNET",
49        "NODES",
50        "UP",
51        "DEGRADED",
52        "DOWN",
53        "DISABLED",
54        "UNKNOWN",
55        "F",
56        "+DOWN >F",
57        "+NON-UP >F",
58    ];
59    let rows = report
60        .subnets
61        .iter()
62        .map(|row| {
63            [
64                compact(&row.subnet_id),
65                row.statuses.total.to_string(),
66                row.statuses.up.to_string(),
67                row.statuses.degraded.to_string(),
68                row.statuses.down.to_string(),
69                row.statuses.disabled.to_string(),
70                row.statuses.unknown.to_string(),
71                row.fault_tolerance_node_count.to_string(),
72                row.additional_down_nodes_to_exceed_fault_tolerance
73                    .to_string(),
74                row.additional_non_up_nodes_to_exceed_fault_tolerance
75                    .to_string(),
76            ]
77        })
78        .collect::<Vec<_>>();
79    push_table_section(
80        &mut sections,
81        table(
82            &headers,
83            &rows,
84            &[
85                Alignment::Left,
86                Alignment::Right,
87                Alignment::Right,
88                Alignment::Right,
89                Alignment::Right,
90                Alignment::Right,
91                Alignment::Right,
92                Alignment::Right,
93                Alignment::Right,
94                Alignment::Right,
95            ],
96        ),
97    );
98    let mut non_up = report
99        .subnets
100        .iter()
101        .flat_map(|subnet| subnet.non_up_nodes.iter())
102        .peekable();
103    if non_up.peek().is_some() {
104        sections.push(String::new());
105        sections.push("non-up node evidence:".to_string());
106        sections.push(render_node_table(non_up));
107    }
108    sections.join("\n")
109}
110
111/// Render a human-readable node-provider operational status report.
112#[must_use]
113pub fn ic_node_provider_status_report_text(report: &IcNodeProviderStatusReport) -> String {
114    let mut sections = observation_lines(&report.observation);
115    sections.push(format!(
116        "node_providers: total={} attention={} returned={}",
117        report.provider_count,
118        report.attention_provider_count,
119        report.providers.len()
120    ));
121    sections.push(format!(
122        "unassigned_vs_assigned providers (less/equal/greater): up={} non_up={}",
123        comparison_counts_text(&report.unassigned_up_vs_assigned_up_provider_counts),
124        comparison_counts_text(&report.unassigned_non_up_vs_assigned_non_up_provider_counts),
125    ));
126    let headers = [
127        "NODE PROVIDER",
128        "NAME",
129        "NODES",
130        "UP",
131        "DEGRADED",
132        "DOWN",
133        "DISABLED",
134        "UNKNOWN",
135        "ASSIGNED UP/NON-UP",
136        "UNASSIGNED UP/NON-UP",
137        "API BN UP/NON-UP",
138        "UNKNOWN ASN UP/NON-UP",
139        "UNASN VS ASN UP/NON-UP",
140    ];
141    let rows = report
142        .providers
143        .iter()
144        .map(|row| {
145            [
146                compact(&row.node_provider_id),
147                row.node_provider_name.clone(),
148                row.counts.statuses.total.to_string(),
149                row.counts.statuses.up.to_string(),
150                row.counts.statuses.degraded.to_string(),
151                row.counts.statuses.down.to_string(),
152                row.counts.statuses.disabled.to_string(),
153                row.counts.statuses.unknown.to_string(),
154                up_non_up(&row.counts.assignment_statuses.assigned),
155                up_non_up(&row.counts.assignment_statuses.unassigned),
156                up_non_up(&row.counts.assignment_statuses.api_boundary),
157                up_non_up(&row.counts.assignment_statuses.unknown),
158                format!(
159                    "{}/{}",
160                    row.unassigned_up_vs_assigned_up.as_str(),
161                    row.unassigned_non_up_vs_assigned_non_up.as_str()
162                ),
163            ]
164        })
165        .collect::<Vec<_>>();
166    push_table_section(
167        &mut sections,
168        table(
169            &headers,
170            &rows,
171            &[
172                Alignment::Left,
173                Alignment::Left,
174                Alignment::Right,
175                Alignment::Right,
176                Alignment::Right,
177                Alignment::Right,
178                Alignment::Right,
179                Alignment::Right,
180                Alignment::Right,
181                Alignment::Right,
182                Alignment::Right,
183                Alignment::Right,
184                Alignment::Left,
185            ],
186        ),
187    );
188    sections.join("\n")
189}
190
191fn push_table_section(sections: &mut Vec<String>, table: String) {
192    sections.push(String::new());
193    sections.push(table);
194}
195
196/// Render a human-readable forced node-status cache refresh report.
197
198#[must_use]
199pub fn ic_node_status_refresh_report_text(report: &IcNodeStatusRefreshReport) -> String {
200    [
201        field("network", &report.network),
202        field("fetched_at", &report.fetched_at),
203        field("source_endpoint", &report.source_endpoint),
204        counts_line("nodes", &report.counts.statuses),
205        assignment_status_line(&report.counts.assignment_statuses),
206        field(
207            "replaced_existing_cache",
208            &(yes_no(report.replaced_existing_cache)),
209        ),
210        field("cache_path", &report.cache_path),
211        field("refresh_lock_path", &report.refresh_lock_path),
212    ]
213    .join("\n")
214}
215
216fn observation_lines(observation: &IcNodeStatusObservation) -> Vec<String> {
217    let mut lines = vec![format!(
218        "observed node status: network={} fetched_at={} source={} certified={} point_in_time={}",
219        terminal_text(&observation.source.network),
220        terminal_text(&observation.source.fetched_at),
221        terminal_text(&observation.source.source_endpoint),
222        yes_no(observation.source.certified),
223        yes_no(observation.source.point_in_time_guaranteed)
224    )];
225    lines.push(format!(
226        "scope: {} cloud_engine_nodes_included={}",
227        observation.scope.as_str(),
228        yes_no(observation.cloud_engine_nodes_included)
229    ));
230    if let Some(cache) = &observation.cache {
231        lines.push(format!(
232            "cache: fresh={} age={}s stale_after={}s path={}",
233            yes_no(cache.cache_fresh),
234            cache.age_seconds,
235            cache.stale_after_seconds,
236            terminal_text(&cache.cache_path)
237        ));
238    }
239    lines
240}
241
242fn counts_line(label: &str, counts: &IcNodeStatusCounts) -> String {
243    format!(
244        "{label}: total={} up={} degraded={} down={} disabled={} unknown={} non_up={}",
245        counts.total,
246        counts.up,
247        counts.degraded,
248        counts.down,
249        counts.disabled,
250        counts.unknown,
251        counts.non_up()
252    )
253}
254
255fn assignment_status_line(counts: &IcNodeAssignmentStatusCounts) -> String {
256    format!(
257        "assignments (total/up/non_up): assigned={}/{}/{} unassigned={}/{}/{} api_boundary={}/{}/{} unknown={}/{}/{}",
258        counts.assigned.total,
259        counts.assigned.up,
260        counts.assigned.non_up(),
261        counts.unassigned.total,
262        counts.unassigned.up,
263        counts.unassigned.non_up(),
264        counts.api_boundary.total,
265        counts.api_boundary.up,
266        counts.api_boundary.non_up(),
267        counts.unknown.total,
268        counts.unknown.up,
269        counts.unknown.non_up()
270    )
271}
272
273fn up_non_up(counts: &IcNodeStatusCounts) -> String {
274    format!("{}/{}", counts.up, counts.non_up())
275}
276
277fn comparison_counts_text(counts: &IcNodeCountComparisonCounts) -> String {
278    format!("{}/{}/{}", counts.less, counts.equal, counts.greater)
279}
280
281fn render_node_table<'a>(nodes: impl IntoIterator<Item = &'a IcNodeStatusRow>) -> String {
282    let headers = ["NODE", "STATUS", "TYPE", "SUBNET", "PROVIDER", "ALERT"];
283    let rows = nodes
284        .into_iter()
285        .map(|node| {
286            [
287                compact(&node.node_id),
288                node.status.clone(),
289                node.node_type.clone(),
290                node.subnet_id
291                    .as_deref()
292                    .map_or_else(|| "-".to_string(), compact),
293                compact(&node.node_provider_id),
294                node.alert_name
295                    .as_deref()
296                    .map_or_else(|| "-".to_string(), str::to_string),
297            ]
298        })
299        .collect::<Vec<_>>();
300    table(&headers, &rows, &[Alignment::Left; 6])
301}
302
303fn compact(value: &str) -> String {
304    crate::presentation::text_value::compact_text(value, COMPACT_PRINCIPAL_CHARS)
305}