Skip to main content

ic_query_cli/presentation/nns/neuron/
mod.rs

1//! Module: nns::neuron
2//!
3//! Responsibility: render public NNS neuron reports as human-readable text.
4//! Does not own: live Governance calls, cache IO, or JSON serialization.
5//! Boundary: formats raw public neuron values without changing report data.
6
7use crate::presentation::text_value::field;
8use crate::presentation::text_value::optional_table;
9use crate::presentation::text_value::push_titled_table_section;
10use crate::presentation::text_value::table;
11use ic_host_text::duration::display_duration_seconds;
12use ic_host_text::table::Alignment;
13
14use crate::presentation::nns::governance::collection_context_lines as governance_collection_context_lines;
15use crate::presentation::nns::governance::context_lines as governance_context_lines;
16use crate::presentation::text_value::optional_number_text;
17use crate::presentation::text_value::yes_no;
18use crate::presentation::token_amount::e8s_decimal_text;
19use ic_query::nns::neuron::NnsNeuronDistributionReport;
20use ic_query::nns::neuron::NnsNeuronInfoReport;
21use ic_query::nns::neuron::NnsNeuronListReport;
22use ic_query::nns::neuron::NnsNeuronRow;
23use ic_query::nns::neuron::NnsNeuronState;
24use ic_query::nns::neuron::NnsNeuronType;
25use ic_query::nns::neuron::NnsNeuronVisibility;
26
27/// Render one portable public-neuron distribution without selecting a process output sink.
28#[must_use]
29pub fn nns_neuron_distribution_report_text(report: &NnsNeuronDistributionReport) -> String {
30    let mut lines = distribution_preamble(report);
31    push_titled_table_section(&mut lines, "states:", state_distribution_table(report));
32    push_titled_table_section(
33        &mut lines,
34        "visibilities:",
35        visibility_distribution_table(report),
36    );
37    push_titled_table_section(
38        &mut lines,
39        "neuron_types:",
40        neuron_type_distribution_table(report),
41    );
42    lines.join("\n")
43}
44
45fn distribution_preamble(report: &NnsNeuronDistributionReport) -> Vec<String> {
46    let mut lines = governance_collection_context_lines(&report.context);
47    lines.extend(crate::presentation::text_value::report_fields!(report;
48        collection_page_count = report.context.collection_page_count,
49        collected_neuron_count,
50        point_in_time_guaranteed = yes_no(report.context.point_in_time_guaranteed),
51        earliest_retrieved_at_timestamp_seconds = optional_number_text(report.earliest_retrieved_at_timestamp_seconds),
52        latest_retrieved_at_timestamp_seconds = optional_number_text(report.latest_retrieved_at_timestamp_seconds),
53        total_effective_stake_e8s,
54        total_effective_stake_icp = e8s_decimal_text(report.total_effective_stake_e8s),
55        reported_staked_maturity_neuron_count,
56        unreported_staked_maturity_neuron_count,
57        total_reported_staked_maturity_e8s_equivalent,
58        total_reported_staked_maturity_icp = e8s_decimal_text(report.total_reported_staked_maturity_e8s_equivalent),
59        reported_deciding_voting_power_neuron_count,
60        unreported_deciding_voting_power_neuron_count,
61        total_reported_deciding_voting_power,
62        reported_potential_voting_power_neuron_count,
63        unreported_potential_voting_power_neuron_count,
64        total_reported_potential_voting_power,
65        known_neuron_metadata_count,
66        neurons_fund_join_timestamp_present_count,
67    ));
68    lines
69}
70
71const DISTRIBUTION_ALIGNMENTS: [Alignment; 4] = [
72    Alignment::Right,
73    Alignment::Left,
74    Alignment::Right,
75    Alignment::Right,
76];
77
78fn state_distribution_table(report: &NnsNeuronDistributionReport) -> Option<String> {
79    optional_table(
80        &["STATE_CODE", "STATE", "NEURONS", "EFFECTIVE_STAKE_ICP"],
81        report.state_distribution.iter().map(|row| {
82            [
83                row.state.to_string(),
84                NnsNeuronState::from_code(row.state).to_string(),
85                row.neuron_count.to_string(),
86                e8s_decimal_text(row.effective_stake_e8s),
87            ]
88        }),
89        &DISTRIBUTION_ALIGNMENTS,
90    )
91}
92
93fn visibility_distribution_table(report: &NnsNeuronDistributionReport) -> Option<String> {
94    optional_table(
95        &[
96            "VISIBILITY_CODE",
97            "VISIBILITY",
98            "NEURONS",
99            "EFFECTIVE_STAKE_ICP",
100        ],
101        report.visibility_distribution.iter().map(|row| {
102            [
103                optional_number_text(row.visibility),
104                NnsNeuronVisibility::from_code(row.visibility).to_string(),
105                row.neuron_count.to_string(),
106                e8s_decimal_text(row.effective_stake_e8s),
107            ]
108        }),
109        &DISTRIBUTION_ALIGNMENTS,
110    )
111}
112
113fn neuron_type_distribution_table(report: &NnsNeuronDistributionReport) -> Option<String> {
114    optional_table(
115        &[
116            "NEURON_TYPE_CODE",
117            "NEURON_TYPE",
118            "NEURONS",
119            "EFFECTIVE_STAKE_ICP",
120        ],
121        report.neuron_type_distribution.iter().map(|row| {
122            [
123                optional_number_text(row.neuron_type),
124                NnsNeuronType::from_code(row.neuron_type).to_string(),
125                row.neuron_count.to_string(),
126                e8s_decimal_text(row.effective_stake_e8s),
127            ]
128        }),
129        &DISTRIBUTION_ALIGNMENTS,
130    )
131}
132
133/// Render one public NNS neuron-index page.
134#[must_use]
135pub fn nns_neuron_list_report_text(
136    report: &NnsNeuronListReport,
137    options: crate::presentation::TextOptions,
138) -> String {
139    let mut lines = governance_context_lines(&report.context);
140    lines.extend(crate::presentation::text_value::report_fields!(report;
141        from_cache = yes_no(report.provenance.cache_path().is_some()),
142        requested_limit,
143        exclusive_start_neuron_id = optional_number_text(report.exclusive_start_neuron_id),
144        next_start_neuron_id = optional_number_text(report.next_start_neuron_id),
145        total_neuron_count = optional_number_text(report.total_neuron_count),
146        point_in_time_guaranteed = yes_no(report.point_in_time_guaranteed),
147        returned_neuron_count = report.neurons.len(),
148        verbose = yes_no(options.verbose),
149    ));
150    if let Some(cache_path) = report.provenance.cache_path() {
151        lines.push(field("cache_path", &(cache_path)));
152    }
153    if !report.neurons.is_empty() {
154        lines.push(String::new());
155        lines.push(neuron_table(&report.neurons));
156    }
157
158    lines.join("\n")
159}
160
161/// Render one public NNS neuron detail report.
162#[must_use]
163pub fn nns_neuron_info_report_text(report: &NnsNeuronInfoReport) -> String {
164    let mut lines = governance_context_lines(&report.context);
165    lines.extend(crate::presentation::text_value::report_fields!(report;
166        from_cache = yes_no(report.provenance.cache_path().is_some()),
167    ));
168    if let Some(cache_path) = report.provenance.cache_path() {
169        lines.push(field("cache_path", &(cache_path)));
170    }
171    lines.push(String::new());
172    lines.push(neuron_table(std::slice::from_ref(&report.neuron)));
173    lines.join("\n")
174}
175
176fn neuron_table(neurons: &[NnsNeuronRow]) -> String {
177    table(
178        &[
179            "ID",
180            "STATE",
181            "VISIBILITY",
182            "STAKE ICP",
183            "DISSOLVE",
184            "DECIDING VP",
185            "NAME",
186        ],
187        &neurons
188            .iter()
189            .map(|neuron| {
190                [
191                    neuron.neuron_id.to_string(),
192                    NnsNeuronState::from_code(neuron.state).to_string(),
193                    NnsNeuronVisibility::from_code(neuron.visibility).to_string(),
194                    e8s_decimal_text(neuron.stake_e8s),
195                    display_duration_seconds(neuron.dissolve_delay_seconds),
196                    optional_number_text(neuron.deciding_voting_power),
197                    neuron
198                        .known_neuron_data
199                        .as_ref()
200                        .map_or_else(|| "-".to_string(), |known| known.name.clone()),
201                ]
202            })
203            .collect::<Vec<_>>(),
204        &[
205            Alignment::Right,
206            Alignment::Left,
207            Alignment::Left,
208            Alignment::Right,
209            Alignment::Right,
210            Alignment::Right,
211            Alignment::Left,
212        ],
213    )
214}