Skip to main content

ic_query_cli/presentation/cloud_engine/
mod.rs

1//! Module: cloud_engine
2//!
3//! Responsibility: render compact human-facing CloudEngine reports.
4//! Does not own: report construction, JSON output, live calls, or process output.
5//! Boundary: formats cycle amounts only for text while JSON retains raw decimal fields.
6
7use crate::presentation::text_value::report_fields;
8use crate::presentation::text_value::{push_optional_table_section, table};
9use ic_host_text::escape::terminal_text;
10use ic_host_text::table::Alignment;
11
12use crate::presentation::human_quantity::decimal_cycle_count_text;
13use crate::presentation::text_value::yes_no;
14use crate::presentation::text_value::{optional_bool_text, optional_number_text, optional_text};
15
16use ic_query::cloud_engine::CloudEngineListReport;
17use ic_query::cloud_engine::CloudEngineOperatorReport;
18use ic_query::cloud_engine::CloudEnginePricesReport;
19use ic_query::cloud_engine::CloudEngineReportContext;
20
21/// Render the Registry CloudEngine inventory and separate operator-binding observations.
22
23#[must_use]
24pub fn cloud_engine_list_report_text(report: &CloudEngineListReport) -> String {
25    let mut lines = list_context_lines(report);
26    lines.push(String::new());
27    lines.push("CloudEngine subnets".to_string());
28    if report.cloud_engines.is_empty() {
29        lines.push("none".to_string());
30        return lines.join("\n");
31    }
32    lines.push(list_table(report));
33    append_lookup_failures(report, &mut lines);
34    lines.join("\n")
35}
36
37fn list_context_lines(report: &CloudEngineListReport) -> Vec<String> {
38    let mut lines = Vec::from(report_fields!(report;
39        network,
40        registry_authority,
41        registry_canister_id,
42        registry_version,
43        registry_assurance = report.registry_assurance.as_str(),
44        registry_source_endpoints = report.registry_source_endpoints.join(", "),
45        catalog_fetched_at,
46        catalog_cache_disposition = report.catalog_cache_disposition.as_str(),
47        catalog_stale = yes_no(report.catalog_stale),
48    ));
49    lines.push(String::new());
50    lines.extend(report_fields!(report;
51        control_plane_authority,
52        control_plane_canister_id,
53        control_plane_source_endpoint,
54        control_plane_fetched_at,
55        control_plane_certified = yes_no(report.control_plane_certified),
56        control_plane_point_in_time_guaranteed = yes_no(report.control_plane_point_in_time_guaranteed),
57        control_plane_lookup_attempt_count,
58    ));
59    lines.push(format!(
60        "operator_bindings: {} resolved, {} absent, {} failed",
61        report.operator_binding_count,
62        report.missing_operator_binding_count,
63        report.operator_lookup_failure_count
64    ));
65    lines
66}
67
68fn list_table(report: &CloudEngineListReport) -> String {
69    let headers = ["Label", "Subnet", "Nodes", "Binding", "Operator"];
70    let alignments = [
71        Alignment::Left,
72        Alignment::Left,
73        Alignment::Right,
74        Alignment::Left,
75        Alignment::Left,
76    ];
77    let rows = report
78        .cloud_engines
79        .iter()
80        .map(|row| {
81            [
82                row.subnet_label.clone(),
83                row.subnet_id.clone(),
84                row.node_count
85                    .map_or_else(|| "unknown".to_string(), |count| count.to_string()),
86                match &row.operator {
87                    Ok(Some(_)) => "resolved",
88                    Ok(None) => "absent",
89                    Err(_) => "failed",
90                }
91                .to_string(),
92                row.operator
93                    .as_ref()
94                    .ok()
95                    .and_then(Option::as_ref)
96                    .map_or_else(|| "-".to_string(), ToString::to_string),
97            ]
98        })
99        .collect::<Vec<_>>();
100    table(&headers, &rows, &alignments)
101}
102
103fn append_lookup_failures(report: &CloudEngineListReport, lines: &mut Vec<String>) {
104    let failures = report
105        .cloud_engines
106        .iter()
107        .filter_map(|row| row.operator.as_ref().err().map(|error| (row, error)))
108        .collect::<Vec<_>>();
109    if !failures.is_empty() {
110        lines.push(String::new());
111        lines.push("Operator lookup failures".to_string());
112        lines.extend(
113            failures
114                .into_iter()
115                .map(|(row, error)| format!("  {}: {}", row.subnet_id, terminal_text(error))),
116        );
117    }
118}
119
120/// Render one CloudEngine Subnet-to-operator report.
121#[must_use]
122pub fn cloud_engine_operator_report_text(report: &CloudEngineOperatorReport) -> String {
123    let mut lines = context_lines(&report.context);
124    lines.extend(crate::presentation::text_value::report_fields!(report;
125        subnet_id,
126        operator_binding_present = yes_no(report.operator_canister_id.is_some()),
127        operator_canister_id = optional_text(report.operator_canister_id.as_ref()),
128        engine_owner = optional_text(report.engine_owner.as_ref()),
129        platform_admin = optional_text(report.platform_admin.as_ref()),
130        caffeine_enabled = optional_bool_text(report.caffeine_enabled),
131        claimed_domain_count = optional_number_text(report.claimed_domains.as_ref().map(Vec::len)),
132    ));
133
134    if let Some(domains) = report.claimed_domains.as_ref()
135        && !domains.is_empty()
136    {
137        lines.push(String::new());
138        lines.push("Claimed domains".to_string());
139        lines.extend(
140            domains
141                .iter()
142                .map(|domain| format!("  {}", terminal_text(domain))),
143        );
144    }
145    lines.join("\n")
146}
147
148/// Render one bounded CloudEngine marketplace price report.
149#[must_use]
150pub fn cloud_engine_prices_report_text(report: &CloudEnginePricesReport) -> String {
151    let mut lines = context_lines(&report.context);
152    lines.extend(crate::presentation::text_value::report_fields!(report;
153        network_fee,
154        price_count = report.prices.len(),
155    ));
156
157    push_optional_table_section(
158        &mut lines,
159        Some("Marketplace prices"),
160        &[
161            "Key",
162            "Node type",
163            "Data center",
164            "Provider",
165            "Net/month",
166            "Gross/month",
167            "Updated (ns)",
168        ],
169        report.prices.iter().map(|row| {
170            [
171                row.key.clone(),
172                row.node_type.to_string(),
173                row.data_center_id
174                    .as_deref()
175                    .map_or_else(|| "-".to_string(), str::to_string),
176                row.provider_id.clone().unwrap_or_else(|| "-".to_string()),
177                decimal_cycle_count_text(&row.net_cycles_per_month),
178                decimal_cycle_count_text(&row.gross_cycles_per_month),
179                row.updated_at_unix_nanos.to_string(),
180            ]
181        }),
182        &[
183            Alignment::Left,
184            Alignment::Left,
185            Alignment::Left,
186            Alignment::Left,
187            Alignment::Right,
188            Alignment::Right,
189            Alignment::Right,
190        ],
191    );
192    lines.join("\n")
193}
194
195fn context_lines(context: &CloudEngineReportContext) -> Vec<String> {
196    Vec::from(crate::presentation::text_value::report_fields!(context;
197        network,
198        authority,
199        engine_canister_id,
200        fetched_at,
201        source_endpoint,
202        fetched_by,
203        certified = yes_no(context.certified),
204        point_in_time_guaranteed = yes_no(context.point_in_time_guaranteed),
205        query_call_count,
206    ))
207}
208
209mod provider;
210pub use provider::*;
211
212mod node;
213pub use node::*;