ic-query 0.37.1

Internet Computer query library for CloudEngine, NNS, SNS, ICRC, system canisters, and public network metadata
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
//! Module: ic::text
//!
//! Responsibility: render IC Dashboard reports as human-facing text.
//! Does not own: REST calls, report construction, JSON serialization, or command output.
//! Boundary: keeps raw values intact in JSON while making nullable text fields readable.

use crate::{
    human_quantity::decimal_cycle_rate_text,
    ic::{
        IcBoundaryNodeDataCentersReport, IcCanisterCountReport, IcCanisterFilters,
        IcCanisterPageReport, IcCanisterReport, IcDailyStatsReport, IcDashboardReportProvenance,
        IcIcrcAccountInfoReport, IcIcrcAccountListReport, IcIcrcHolderListReport,
        IcIcrcIndexedCountReport, IcIcrcTokenValueReport, IcIcrcTotalSupplyReport, IcMetricKind,
        IcMetricReport, IcNodeProviderRewardHistoryReport, IcNodeProviderRewardInfoReport,
        IcNodeProviderRewardListReport, IcReplicaVersionInfoReport, IcReplicaVersionListReport,
    },
    table::{ColumnAlign, render_table},
    text_value::{optional_text, optional_u64_text, sanitize_text, truncate_text, yes_no},
};

const IC_REPLICA_VERSION_TITLE_TEXT_LIMIT: usize = 80;
const IC_REPLICA_VERSION_SUMMARY_TEXT_LIMIT: usize = 240;

pub fn dashboard_provenance_lines(provenance: &IcDashboardReportProvenance) -> Vec<String> {
    vec![
        format!("schema_version: {}", provenance.schema_version),
        format!("network: {}", sanitize_text(&provenance.network)),
        format!("authority: {}", sanitize_text(&provenance.authority)),
        format!(
            "source_endpoint: {}",
            sanitize_text(&provenance.source_endpoint)
        ),
        format!("fetched_at: {}", sanitize_text(&provenance.fetched_at)),
        format!("fetched_by: {}", sanitize_text(&provenance.fetched_by)),
        format!("certified: {}", yes_no(provenance.certified)),
        format!(
            "point_in_time_guaranteed: {}",
            yes_no(provenance.point_in_time_guaranteed)
        ),
    ]
}

/// Render one official boundary-node data-center report as human-facing text.
#[must_use]
pub fn ic_boundary_node_data_centers_report_text(
    report: &IcBoundaryNodeDataCentersReport,
) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!("data_center_count: {}", report.data_center_count),
        format!("total_node_count: {}", report.total_node_count),
    ]);
    append_report_footer(&mut lines, &report.provenance);

    if !report.rows.is_empty() {
        lines.push(String::new());
        lines.push("boundary_node_data_centers:".to_string());
        lines.extend(report.rows.iter().map(|row| {
            format!(
                "  {}  name={}  owner={}  region={}  latitude={}  longitude={}  nodes={}",
                sanitize_text(&row.dc_id),
                sanitize_text(&row.name),
                sanitize_text(&row.owner),
                sanitize_text(&row.region),
                sanitize_text(&row.latitude),
                sanitize_text(&row.longitude),
                sanitize_text(&row.total_nodes),
            )
        }));
    }
    lines.join("\n")
}

/// Render one bounded official Dashboard daily-statistics report as human-facing text.
#[must_use]
pub fn ic_daily_stats_report_text(report: &IcDailyStatsReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!("start_unix_secs: {}", report.query.start_unix_secs),
        format!("end_unix_secs: {}", report.query.end_unix_secs),
        format!("returned_day_count: {}", report.returned_day_count),
    ]);
    append_report_footer(&mut lines, &report.provenance);

    if !report.rows.is_empty() {
        lines.push(String::new());
        lines.push("daily_stats:".to_string());
        lines.extend(report.rows.iter().map(|row| {
            format!(
                "  {}  timestamp={}  avg_total={}  avg_update={}  avg_query={}  max_total={}  max_update={}  max_query={}  blocks_avg={}",
                sanitize_text(&row.day),
                row.timestamp_unix_secs,
                sanitize_text(&row.average_transactions_per_second),
                sanitize_text(&row.average_update_transactions_per_second),
                sanitize_text(&row.average_query_transactions_per_second),
                sanitize_text(&row.max_total_transactions_per_second),
                sanitize_text(&row.max_update_transactions_per_second),
                sanitize_text(&row.max_query_transactions_per_second),
                sanitize_text(&row.blocks_per_second_average),
            )
        }));
    }
    lines.join("\n")
}

/// Render one bounded official Dashboard node-provider reward page as human-facing text.
#[must_use]
pub fn ic_node_provider_reward_list_report_text(report: &IcNodeProviderRewardListReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!("limit: {}", report.query.limit),
        format!("offset: {}", report.query.offset),
        format!(
            "requested_max_reward_index: {}",
            optional_u64_text(report.query.max_reward_index)
        ),
        format!(
            "resolved_max_reward_index: {}",
            report.resolved_max_reward_index
        ),
        format!("total_reward_records: {}", report.total_reward_records),
        format!("returned_count: {}", report.returned_count),
        format!(
            "next_offset_hint: {}",
            optional_u64_text(report.next_offset_hint)
        ),
        format!("pages_may_overlap: {}", yes_no(report.pages_may_overlap)),
    ]);
    append_report_footer(&mut lines, &report.provenance);

    if !report.rows.is_empty() {
        lines.push(String::new());
        lines.push(render_table(
            &[
                "ID",
                "PROVIDER",
                "AMOUNT_E8S",
                "MODE",
                "REWARDED_AT",
                "PROPOSAL",
            ],
            &report
                .rows
                .iter()
                .map(|row| {
                    [
                        row.reward_id.to_string(),
                        row.node_provider_id.clone(),
                        row.amount_e8s.to_string(),
                        sanitize_text(&row.reward_mode),
                        row.reward_timestamp_unix_secs.to_string(),
                        optional_u64_text(row.proposal_id),
                    ]
                })
                .collect::<Vec<_>>(),
            &[
                ColumnAlign::Right,
                ColumnAlign::Left,
                ColumnAlign::Right,
                ColumnAlign::Left,
                ColumnAlign::Right,
                ColumnAlign::Right,
            ],
        ));
    }
    lines.join("\n")
}

/// Render one exact official Dashboard node-provider reward report as human-facing text.
#[must_use]
pub fn ic_node_provider_reward_info_report_text(report: &IcNodeProviderRewardInfoReport) -> String {
    let reward = &report.reward;
    let details = serde_json::to_string(&reward.details).unwrap_or_else(|_| "{}".to_string());
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!("reward_id: {}", reward.reward_id),
        format!("node_provider_id: {}", reward.node_provider_id),
        format!("amount_e8s: {}", reward.amount_e8s),
        format!("reward_mode: {}", sanitize_text(&reward.reward_mode)),
        format!(
            "reward_timestamp_unix_secs: {}",
            reward.reward_timestamp_unix_secs
        ),
        format!(
            "dashboard_updated_at: {}",
            sanitize_text(&reward.dashboard_updated_at)
        ),
        format!("proposal_id: {}", optional_u64_text(reward.proposal_id)),
        format!(
            "registry_version: {}",
            optional_u64_text(reward.registry_version)
        ),
        format!(
            "maximum_node_provider_rewards_e8s: {}",
            optional_u64_text(reward.maximum_node_provider_rewards_e8s)
        ),
        format!(
            "minimum_xdr_permyriad_per_icp: {}",
            optional_u64_text(reward.minimum_xdr_permyriad_per_icp)
        ),
        format!(
            "xdr_timestamp_unix_secs: {}",
            optional_u64_text(reward.xdr_conversion_rate.timestamp_unix_secs)
        ),
        format!(
            "xdr_permyriad_per_icp: {}",
            optional_u64_text(reward.xdr_conversion_rate.xdr_permyriad_per_icp)
        ),
        format!("details: {}", sanitize_text(&details)),
    ]);
    append_report_footer(&mut lines, &report.provenance);
    lines.join("\n")
}

/// Render one bounded official Dashboard node-provider reward history as human-facing text.
#[must_use]
pub fn ic_node_provider_reward_history_report_text(
    report: &IcNodeProviderRewardHistoryReport,
) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!("start_unix_secs: {}", report.query.start_unix_secs),
        format!("end_unix_secs: {}", report.query.end_unix_secs),
        format!("step_secs: {}", report.query.step_secs),
        format!(
            "requested_observation_limit: {}",
            report.requested_observation_limit
        ),
        format!(
            "returned_observation_count: {}",
            report.returned_observation_count
        ),
    ]);
    append_report_footer(&mut lines, &report.provenance);

    if !report.observations.is_empty() {
        lines.push(String::new());
        lines.push(render_table(
            &["TIMESTAMP", "AMOUNT_E8S"],
            &report
                .observations
                .iter()
                .map(|observation| {
                    [
                        observation.timestamp_unix_secs.to_string(),
                        observation.amount_e8s.to_string(),
                    ]
                })
                .collect::<Vec<_>>(),
            &[ColumnAlign::Right, ColumnAlign::Right],
        ));
    }
    lines.join("\n")
}

/// Render one bounded official Dashboard replica-version page as human-facing text.
#[must_use]
pub fn ic_replica_version_list_report_text(report: &IcReplicaVersionListReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!("limit: {}", report.query.limit),
        format!("offset: {}", report.query.offset),
        format!(
            "requested_max_proposal_index: {}",
            optional_u64_text(report.query.max_proposal_index)
        ),
        format!(
            "resolved_max_proposal_index: {}",
            report.resolved_max_proposal_index
        ),
        format!("total_proposals: {}", report.total_proposals),
        format!("returned_count: {}", report.returned_count),
        format!("next_offset: {}", optional_u64_text(report.next_offset)),
    ]);
    append_report_footer(&mut lines, &report.provenance);

    if !report.rows.is_empty() {
        lines.push(String::new());
        lines.push(render_table(
            &[
                "VERSION",
                "STATUS",
                "PROPOSAL",
                "EXECUTED_AT",
                "SUBNETS",
                "TITLE",
            ],
            &report
                .rows
                .iter()
                .map(|row| {
                    [
                        row.replica_version_id.clone(),
                        row.status.to_string(),
                        row.proposal_id.to_string(),
                        row.executed_timestamp_seconds.to_string(),
                        row.subnet_count.to_string(),
                        truncate_text(&row.title, IC_REPLICA_VERSION_TITLE_TEXT_LIMIT),
                    ]
                })
                .collect::<Vec<_>>(),
            &[
                ColumnAlign::Left,
                ColumnAlign::Left,
                ColumnAlign::Right,
                ColumnAlign::Right,
                ColumnAlign::Right,
                ColumnAlign::Left,
            ],
        ));
    }
    lines.join("\n")
}

/// Render one exact official Dashboard replica-version report as human-facing text.
#[must_use]
pub fn ic_replica_version_info_report_text(report: &IcReplicaVersionInfoReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!(
            "replica_version_id: {}",
            sanitize_text(&report.replica_version_id)
        ),
        format!("proposal_id: {}", report.proposal_id),
        format!(
            "executed_timestamp_seconds: {}",
            report.executed_timestamp_seconds
        ),
        format!("title: {}", text_or_dash(&report.title)),
        format!("url: {}", text_or_dash(&report.url)),
        format!("subnet_count: {}", report.subnet_count),
        format!(
            "summary: {}",
            truncate_text(&report.summary, IC_REPLICA_VERSION_SUMMARY_TEXT_LIMIT)
        ),
    ]);
    append_report_footer(&mut lines, &report.provenance);

    if !report.subnets.is_empty() {
        lines.push(String::new());
        lines.push("subnet_rollouts:".to_string());
        lines.extend(report.subnets.iter().map(|subnet| {
            format!(
                "  {}  proposal={}  executed_at={}",
                subnet.subnet_id, subnet.proposal_id, subnet.executed_timestamp_seconds
            )
        }));
    }
    lines.join("\n")
}

/// Render one bounded official Dashboard metric report as human-facing text.
#[must_use]
pub fn ic_metric_report_text(report: &IcMetricReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!("metric: {}", report.query.metric),
        format!("start_unix_secs: {}", report.query.start_unix_secs),
        format!("end_unix_secs: {}", report.query.end_unix_secs),
        format!("step_secs: {}", report.query.step_secs),
        format!("returned_series_count: {}", report.returned_series_count),
        format!(
            "returned_observation_count: {}",
            report.returned_observation_count
        ),
    ]);
    append_report_footer(&mut lines, &report.provenance);

    for series in &report.series {
        lines.push(String::new());
        lines.push(format!("{}:", sanitize_text(&series.name)));
        lines.extend(series.observations.iter().map(|observation| {
            format!(
                "  {}  {}",
                observation.timestamp_unix_secs,
                metric_observation_text(report.query.metric, &observation.value)
            )
        }));
    }
    lines.join("\n")
}

/// Render one official ICRC indexed-count report as human-facing text.
#[must_use]
pub fn icrc_indexed_count_report_text(report: &IcIcrcIndexedCountReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!(
            "ledger_canister_id: {}",
            sanitize_text(&report.ledger_canister_id)
        ),
        format!("kind: {}", report.kind),
        format!("total: {}", report.total),
    ]);
    append_report_footer(&mut lines, &report.provenance);
    lines.join("\n")
}

/// Render one bounded official ICRC account-index page as human-facing text.
#[must_use]
pub fn icrc_account_list_report_text(report: &IcIcrcAccountListReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!(
            "ledger_canister_id: {}",
            sanitize_text(&report.ledger_canister_id)
        ),
        format!("owner: {}", optional_text(report.query.owner.as_ref())),
        format!("sort_by: {}", report.query.sort_by),
        format!("limit: {}", report.query.limit),
        format!("returned_count: {}", report.returned_count),
        format!(
            "previous_cursor: {}",
            optional_text(report.previous_cursor.as_ref())
        ),
        format!(
            "next_cursor: {}",
            optional_text(report.next_cursor.as_ref())
        ),
    ]);
    append_report_footer(&mut lines, &report.provenance);

    if !report.rows.is_empty() {
        lines.push(String::new());
        lines.push("accounts:".to_string());
        let rows = report
            .rows
            .iter()
            .map(|row| {
                [
                    row.account_id.clone(),
                    row.owner.clone(),
                    row.balance_base_units.clone(),
                    row.total_transactions.to_string(),
                    row.created_at_unix_secs.to_string(),
                    row.latest_transaction_index.to_string(),
                ]
            })
            .collect::<Vec<_>>();
        lines.push(render_table(
            &[
                "ACCOUNT ID",
                "OWNER",
                "BALANCE",
                "TXS",
                "CREATED",
                "LATEST TX",
            ],
            &rows,
            &[
                ColumnAlign::Left,
                ColumnAlign::Left,
                ColumnAlign::Right,
                ColumnAlign::Right,
                ColumnAlign::Right,
                ColumnAlign::Right,
            ],
        ));
    }
    lines.join("\n")
}

/// Render one exact official ICRC account record as human-facing text.
#[must_use]
pub fn icrc_account_info_report_text(report: &IcIcrcAccountInfoReport) -> String {
    let account = &report.account;
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!("account_id: {}", sanitize_text(&account.account_id)),
        format!("owner: {}", sanitize_text(&account.owner)),
        format!("subaccount: {}", sanitize_text(&account.subaccount)),
        format!(
            "balance_base_units: {}",
            sanitize_text(&account.balance_base_units)
        ),
        format!("total_transactions: {}", account.total_transactions),
        format!("created_at_unix_secs: {}", account.created_at_unix_secs),
        format!(
            "created_at_subsec_nanos: {}",
            account.created_at_subsec_nanos
        ),
        format!(
            "ledger_canister_id: {}",
            sanitize_text(&account.ledger_canister_id)
        ),
        format!(
            "latest_transaction_index: {}",
            account.latest_transaction_index
        ),
        format!(
            "dashboard_updated_at: {}",
            sanitize_text(&account.dashboard_updated_at)
        ),
        format!(
            "active_fee_collector: {}",
            yes_no(account.active_fee_collector)
        ),
        format!(
            "fee_collector_block_range_count: {}",
            account.fee_collector_block_ranges.len()
        ),
    ]);
    append_report_footer(&mut lines, &report.provenance);
    lines.join("\n")
}

/// Render one bounded official ICRC holder-index page as human-facing text.
#[must_use]
pub fn icrc_holder_list_report_text(report: &IcIcrcHolderListReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!(
            "ledger_canister_id: {}",
            sanitize_text(&report.ledger_canister_id)
        ),
        format!("sort_by: {}", report.query.sort_by),
        format!("limit: {}", report.query.limit),
        format!("returned_count: {}", report.returned_count),
        format!(
            "previous_cursor: {}",
            optional_text(report.previous_cursor.as_ref())
        ),
        format!(
            "next_cursor: {}",
            optional_text(report.next_cursor.as_ref())
        ),
    ]);
    append_report_footer(&mut lines, &report.provenance);

    if !report.rows.is_empty() {
        lines.push(String::new());
        lines.push("holders:".to_string());
        let rows = report
            .rows
            .iter()
            .map(|row| {
                [
                    row.principal.clone(),
                    row.balance_base_units.clone(),
                    row.total_transactions.to_string(),
                    json_scalar_text(&row.percentage),
                    json_scalar_text(&row.value_usd),
                    row.created_at_unix_secs.to_string(),
                ]
            })
            .collect::<Vec<_>>();
        lines.push(render_table(
            &[
                "PRINCIPAL",
                "BALANCE",
                "TXS",
                "PERCENT",
                "VALUE USD",
                "CREATED",
            ],
            &rows,
            &[
                ColumnAlign::Left,
                ColumnAlign::Right,
                ColumnAlign::Right,
                ColumnAlign::Right,
                ColumnAlign::Right,
                ColumnAlign::Right,
            ],
        ));
    }
    lines.join("\n")
}

/// Render one bounded official ICRC token-value series as human-facing text.
#[must_use]
pub fn icrc_token_value_report_text(report: &IcIcrcTokenValueReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!(
            "ledger_canister_id: {}",
            sanitize_text(&report.ledger_canister_id)
        ),
        format!("start_unix_secs: {}", report.query.start_unix_secs),
        format!("end_unix_secs: {}", report.query.end_unix_secs),
        format!("limit: {}", report.query.limit),
        format!("returned_row_count: {}", report.returned_row_count),
        format!("limit_reached: {}", yes_no(report.limit_reached)),
    ]);
    append_report_footer(&mut lines, &report.provenance);

    if !report.rows.is_empty() {
        lines.push(String::new());
        lines.push("token_values:".to_string());
        lines.extend(report.rows.iter().map(|row| {
            format!(
                "  {}  price={}  volume_24h={}  price_usd={}  volume_24h_usd={}  source={}  source_url={}",
                row.timestamp_unix_secs,
                optional_text(row.price.as_ref()),
                optional_text(row.volume_24h.as_ref()),
                optional_text(row.price_usd.as_ref()),
                optional_text(row.volume_24h_usd.as_ref()),
                optional_text(row.source.as_ref()),
                optional_text(row.source_url.as_ref()),
            )
        }));
    }
    lines.join("\n")
}

/// Render one bounded official ICRC total-supply series as human-facing text.
#[must_use]
pub fn icrc_total_supply_report_text(report: &IcIcrcTotalSupplyReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!(
            "ledger_canister_id: {}",
            sanitize_text(&report.ledger_canister_id)
        ),
        format!("start_unix_secs: {}", report.query.start_unix_secs),
        format!("end_unix_secs: {}", report.query.end_unix_secs),
        format!("step_secs: {}", report.query.step_secs),
        format!(
            "requested_observation_limit: {}",
            report.requested_observation_limit
        ),
        format!(
            "returned_observation_count: {}",
            report.returned_observation_count
        ),
    ]);
    append_report_footer(&mut lines, &report.provenance);

    if !report.observations.is_empty() {
        lines.push(String::new());
        lines.push("total_supply_base_units:".to_string());
        lines.extend(report.observations.iter().map(|observation| {
            format!(
                "  {}  {}",
                observation.timestamp_unix_secs,
                sanitize_text(&observation.total_supply_base_units)
            )
        }));
    }
    lines.join("\n")
}

fn metric_observation_text(metric: IcMetricKind, value: &str) -> String {
    if metric == IcMetricKind::CycleBurnRate {
        decimal_cycle_rate_text(value)
    } else {
        sanitize_text(value)
    }
}

/// Render one official Dashboard canister report as human-facing text.
#[must_use]
pub fn ic_canister_report_text(report: &IcCanisterReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!("canister_id: {}", report.canister_id),
        format!("dashboard_id: {}", report.dashboard_id),
        format!("name: {}", text_or_dash(&report.name)),
        format!(
            "canister_type: {}",
            report
                .canister_type
                .as_deref()
                .map_or_else(|| "-".to_string(), text_or_dash)
        ),
        format!("subnet_id: {}", report.subnet_id),
        format!("controller_count: {}", report.controllers.len()),
        format!("language: {}", text_or_dash(&report.language)),
        format!("module_hash: {}", text_or_dash(&report.module_hash)),
        format!(
            "dashboard_updated_at: {}",
            sanitize_text(&report.dashboard_updated_at)
        ),
        format!(
            "upgrade_history_available: {}",
            yes_no(report.upgrades.is_some())
        ),
        format!(
            "upgrade_count: {}",
            report
                .upgrade_count
                .map_or_else(|| "-".to_string(), |count| count.to_string())
        ),
    ]);
    append_report_footer(&mut lines, &report.provenance);

    if !report.controllers.is_empty() {
        lines.push(String::new());
        lines.push("controllers:".to_string());
        lines.extend(
            report
                .controllers
                .iter()
                .map(|controller| format!("  {controller}")),
        );
    }

    if let Some(latest) = report
        .upgrades
        .as_ref()
        .and_then(|upgrades| upgrades.first())
    {
        lines.push(String::new());
        lines.push("latest_upgrade:".to_string());
        lines.push(format!("  proposal_id: {}", latest.proposal_id));
        lines.push(format!(
            "  executed_timestamp_seconds: {}",
            latest.executed_timestamp_seconds
        ));
        lines.push(format!("  module_hash: {}", latest.module_hash));
    }

    lines.join("\n")
}

/// Render one official Dashboard canister-count report as human-facing text.
#[must_use]
pub fn ic_canister_count_report_text(report: &IcCanisterCountReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.push(format!("total: {}", report.total));
    append_filters(&mut lines, &report.filters);
    append_report_footer(&mut lines, &report.provenance);
    lines.join("\n")
}

/// Render one bounded official Dashboard canister page as human-facing text.
#[must_use]
pub fn ic_canister_page_report_text(report: &IcCanisterPageReport) -> String {
    let mut lines = report_header(&report.provenance);
    lines.extend([
        format!("returned_count: {}", report.returned_count),
        format!("requested_limit: {}", report.requested_limit),
        format!(
            "after: {}",
            report.after.as_deref().map_or("-", |value| value)
        ),
        format!(
            "before: {}",
            report.before.as_deref().map_or("-", |value| value)
        ),
        format!(
            "previous_cursor: {}",
            report.previous_cursor.as_deref().map_or("-", |value| value)
        ),
        format!(
            "next_cursor: {}",
            report.next_cursor.as_deref().map_or("-", |value| value)
        ),
    ]);
    append_filters(&mut lines, &report.filters);
    append_report_footer(&mut lines, &report.provenance);

    if !report.rows.is_empty() {
        lines.push(String::new());
        lines.push("canisters:".to_string());
        lines.extend(report.rows.iter().map(|row| {
            format!(
                "  {}  name={}  type={}  subnet={}  controllers={}  language={}  updated={}",
                row.canister_id,
                text_or_dash(&row.name),
                row.canister_type
                    .as_deref()
                    .map_or_else(|| "-".to_string(), text_or_dash),
                row.subnet_id,
                row.controllers.len(),
                text_or_dash(&row.language),
                sanitize_text(&row.dashboard_updated_at),
            )
        }));
    }
    lines.join("\n")
}

fn report_header(provenance: &IcDashboardReportProvenance) -> Vec<String> {
    vec![
        format!("network: {}", sanitize_text(&provenance.network)),
        format!("authority: {}", sanitize_text(&provenance.authority)),
    ]
}

fn append_report_footer(lines: &mut Vec<String>, provenance: &IcDashboardReportProvenance) {
    lines.extend([
        format!("certified: {}", yes_no(provenance.certified)),
        format!(
            "point_in_time_guaranteed: {}",
            yes_no(provenance.point_in_time_guaranteed)
        ),
        format!("fetched_at: {}", sanitize_text(&provenance.fetched_at)),
        format!(
            "source_endpoint: {}",
            sanitize_text(&provenance.source_endpoint)
        ),
    ]);
}

fn append_filters(lines: &mut Vec<String>, filters: &IcCanisterFilters) {
    if let Some(has_name) = filters.has_name {
        lines.push(format!("filter.has_name: {}", yes_no(has_name)));
    }
    if let Some(subnet_id) = filters.subnet_id.as_deref() {
        lines.push(format!("filter.subnet_id: {subnet_id}"));
    }
    if let Some(controller_id) = filters.controller_id.as_deref() {
        lines.push(format!("filter.controller_id: {controller_id}"));
    }
    if !filters.languages.is_empty() {
        lines.push(format!(
            "filter.languages: {}",
            filters
                .languages
                .iter()
                .map(|value| sanitize_text(value))
                .collect::<Vec<_>>()
                .join(",")
        ));
    }
    if !filters.canister_types.is_empty() {
        lines.push(format!(
            "filter.canister_types: {}",
            filters
                .canister_types
                .iter()
                .map(|value| sanitize_text(value))
                .collect::<Vec<_>>()
                .join(",")
        ));
    }
    if let Some(query) = filters.query.as_deref() {
        lines.push(format!("filter.query: {}", sanitize_text(query)));
    }
}

fn text_or_dash(value: &str) -> String {
    if value.is_empty() {
        "-".to_string()
    } else {
        sanitize_text(value)
    }
}

fn json_scalar_text(value: &serde_json::Value) -> String {
    match value {
        serde_json::Value::Null => "-".to_string(),
        serde_json::Value::String(value) => value.clone(),
        _ => value.to_string(),
    }
}