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::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::optional_text;
14use crate::presentation::text_value::yes_no;
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 = report
131                .caffeine_enabled
132                .map_or_else(|| "-".to_string(), |enabled| yes_no(enabled).to_string()),
133        claimed_domain_count = report
134                .claimed_domains
135                .as_ref()
136                .map(Vec::len)
137                .map_or_else(|| "-".to_string(), |count| count.to_string()),
138    ));
139
140    if let Some(domains) = report.claimed_domains.as_ref()
141        && !domains.is_empty()
142    {
143        lines.push(String::new());
144        lines.push("Claimed domains".to_string());
145        lines.extend(
146            domains
147                .iter()
148                .map(|domain| format!("  {}", terminal_text(domain))),
149        );
150    }
151    lines.join("\n")
152}
153
154/// Render one bounded CloudEngine marketplace price report.
155#[must_use]
156pub fn cloud_engine_prices_report_text(report: &CloudEnginePricesReport) -> String {
157    let mut lines = context_lines(&report.context);
158    lines.extend(crate::presentation::text_value::report_fields!(report;
159        network_fee,
160        price_count = report.prices.len(),
161    ));
162
163    if !report.prices.is_empty() {
164        let headers = [
165            "Key",
166            "Node type",
167            "Data center",
168            "Provider",
169            "Net/month",
170            "Gross/month",
171            "Updated (ns)",
172        ];
173        let alignments = [
174            Alignment::Left,
175            Alignment::Left,
176            Alignment::Left,
177            Alignment::Left,
178            Alignment::Right,
179            Alignment::Right,
180            Alignment::Right,
181        ];
182        let rows = report
183            .prices
184            .iter()
185            .map(|row| {
186                [
187                    row.key.clone(),
188                    row.node_type.to_string(),
189                    row.data_center_id
190                        .as_deref()
191                        .map_or_else(|| "-".to_string(), str::to_string),
192                    row.provider_id.clone().unwrap_or_else(|| "-".to_string()),
193                    decimal_cycle_count_text(&row.net_cycles_per_month),
194                    decimal_cycle_count_text(&row.gross_cycles_per_month),
195                    row.updated_at_unix_nanos.to_string(),
196                ]
197            })
198            .collect::<Vec<_>>();
199        lines.push(String::new());
200        lines.push("Marketplace prices".to_string());
201        lines.push(table(&headers, &rows, &alignments));
202    }
203    lines.join("\n")
204}
205
206fn context_lines(context: &CloudEngineReportContext) -> Vec<String> {
207    Vec::from(crate::presentation::text_value::report_fields!(context;
208        network,
209        authority,
210        engine_canister_id,
211        fetched_at,
212        source_endpoint,
213        fetched_by,
214        certified = yes_no(context.certified),
215        point_in_time_guaranteed = yes_no(context.point_in_time_guaranteed),
216        query_call_count,
217    ))
218}
219
220mod provider;
221pub use provider::*;
222
223mod node;
224pub use node::*;