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