ic_query/nns/node/report/text/
list.rs1use crate::{
2 nns::{
3 node::report::NnsNodeListReport,
4 render::{compact_text, text_or_dash},
5 },
6 table::{ColumnAlign, render_table},
7 text_value::sanitize_text,
8};
9
10const COMPACT_PRINCIPAL_CHARS: usize = 5;
11
12#[must_use]
13pub fn nns_node_list_report_text(report: &NnsNodeListReport) -> String {
14 let mut lines = Vec::new();
15 lines.push(format!(
16 "nodes: {} count {} fetched_at {}",
17 sanitize_text(&report.network),
18 report.node_count,
19 sanitize_text(&report.fetched_at)
20 ));
21 if report.nodes.is_empty() {
22 lines.push("nodes: none".to_string());
23 return lines.join("\n");
24 }
25 let headers = ["NODE", "OPERATOR", "PROVIDER", "SUBNET", "KIND", "DC"];
26 let rows = report
27 .nodes
28 .iter()
29 .map(|node| {
30 [
31 compact_text(&node.node_principal, COMPACT_PRINCIPAL_CHARS),
32 compact_text(&node.node_operator_principal, COMPACT_PRINCIPAL_CHARS),
33 compact_text(&node.node_provider_principal, COMPACT_PRINCIPAL_CHARS),
34 compact_text(&node.subnet_principal, COMPACT_PRINCIPAL_CHARS),
35 node.subnet_kind.as_str().to_string(),
36 text_or_dash(Some(&node.data_center_id)),
37 ]
38 })
39 .collect::<Vec<_>>();
40 let alignments = [
41 ColumnAlign::Left,
42 ColumnAlign::Left,
43 ColumnAlign::Left,
44 ColumnAlign::Left,
45 ColumnAlign::Left,
46 ColumnAlign::Left,
47 ];
48 lines.push(String::new());
49 lines.push(render_table(&headers, &rows, &alignments));
50 lines.join("\n")
51}
52
53#[must_use]
54pub fn nns_node_list_report_verbose_text(report: &NnsNodeListReport) -> String {
55 let mut lines = Vec::new();
56 lines.push(format!(
57 "source_endpoint: {}",
58 sanitize_text(&report.source_endpoint)
59 ));
60 lines.push(format!("fetched_by: {}", sanitize_text(&report.fetched_by)));
61 if report.nodes.is_empty() {
62 lines.push("nodes: none".to_string());
63 return lines.join("\n");
64 }
65 let headers = [
66 "NODE",
67 "OPERATOR",
68 "PROVIDER",
69 "SUBNET",
70 "KIND",
71 "DC",
72 "REGISTRY_VERSION",
73 "FETCHED_AT",
74 ];
75 let rows = report
76 .nodes
77 .iter()
78 .map(|node| {
79 [
80 node.node_principal.clone(),
81 node.node_operator_principal.clone(),
82 node.node_provider_principal.clone(),
83 node.subnet_principal.clone(),
84 node.subnet_kind.as_str().to_string(),
85 text_or_dash(Some(&node.data_center_id)),
86 report.registry_version.to_string(),
87 report.fetched_at.clone(),
88 ]
89 })
90 .collect::<Vec<_>>();
91 let alignments = [
92 ColumnAlign::Left,
93 ColumnAlign::Left,
94 ColumnAlign::Left,
95 ColumnAlign::Left,
96 ColumnAlign::Left,
97 ColumnAlign::Left,
98 ColumnAlign::Right,
99 ColumnAlign::Left,
100 ];
101 lines.push(String::new());
102 lines.push(render_table(&headers, &rows, &alignments));
103 lines.join("\n")
104}