ic_query_cli/presentation/nns/neuron/
mod.rs1use 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_u64_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#[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_u64_text(report.earliest_retrieved_at_timestamp_seconds),
52 latest_retrieved_at_timestamp_seconds = optional_u64_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 row.visibility
104 .map_or_else(|| "-".to_string(), |value| value.to_string()),
105 NnsNeuronVisibility::from_code(row.visibility).to_string(),
106 row.neuron_count.to_string(),
107 e8s_decimal_text(row.effective_stake_e8s),
108 ]
109 }),
110 &DISTRIBUTION_ALIGNMENTS,
111 )
112}
113
114fn neuron_type_distribution_table(report: &NnsNeuronDistributionReport) -> Option<String> {
115 optional_table(
116 &[
117 "NEURON_TYPE_CODE",
118 "NEURON_TYPE",
119 "NEURONS",
120 "EFFECTIVE_STAKE_ICP",
121 ],
122 report.neuron_type_distribution.iter().map(|row| {
123 [
124 row.neuron_type
125 .map_or_else(|| "-".to_string(), |value| value.to_string()),
126 NnsNeuronType::from_code(row.neuron_type).to_string(),
127 row.neuron_count.to_string(),
128 e8s_decimal_text(row.effective_stake_e8s),
129 ]
130 }),
131 &DISTRIBUTION_ALIGNMENTS,
132 )
133}
134
135#[must_use]
137pub fn nns_neuron_list_report_text(
138 report: &NnsNeuronListReport,
139 options: crate::presentation::TextOptions,
140) -> String {
141 let mut lines = governance_context_lines(&report.context);
142 lines.extend(crate::presentation::text_value::report_fields!(report;
143 from_cache = yes_no(report.provenance.cache_path().is_some()),
144 requested_limit,
145 exclusive_start_neuron_id = optional_u64_text(report.exclusive_start_neuron_id),
146 next_start_neuron_id = optional_u64_text(report.next_start_neuron_id),
147 total_neuron_count = report
148 .total_neuron_count
149 .map_or_else(|| "-".to_string(), |count| count.to_string()),
150 point_in_time_guaranteed = yes_no(report.point_in_time_guaranteed),
151 returned_neuron_count = report.neurons.len(),
152 verbose = yes_no(options.verbose),
153 ));
154 if let Some(cache_path) = report.provenance.cache_path() {
155 lines.push(field("cache_path", &(cache_path)));
156 }
157 if !report.neurons.is_empty() {
158 lines.push(String::new());
159 lines.push(neuron_table(&report.neurons));
160 }
161
162 lines.join("\n")
163}
164
165#[must_use]
167pub fn nns_neuron_info_report_text(report: &NnsNeuronInfoReport) -> String {
168 let mut lines = governance_context_lines(&report.context);
169 lines.extend([field(
170 "from_cache",
171 &(yes_no(report.provenance.cache_path().is_some())),
172 )]);
173 if let Some(cache_path) = report.provenance.cache_path() {
174 lines.push(field("cache_path", &(cache_path)));
175 }
176 lines.push(String::new());
177 lines.push(neuron_table(std::slice::from_ref(&report.neuron)));
178 lines.join("\n")
179}
180
181fn neuron_table(neurons: &[NnsNeuronRow]) -> String {
182 table(
183 &[
184 "ID",
185 "STATE",
186 "VISIBILITY",
187 "STAKE ICP",
188 "DISSOLVE",
189 "DECIDING VP",
190 "NAME",
191 ],
192 &neurons
193 .iter()
194 .map(|neuron| {
195 [
196 neuron.neuron_id.to_string(),
197 NnsNeuronState::from_code(neuron.state).to_string(),
198 NnsNeuronVisibility::from_code(neuron.visibility).to_string(),
199 e8s_decimal_text(neuron.stake_e8s),
200 display_duration_seconds(neuron.dissolve_delay_seconds),
201 optional_u64_text(neuron.deciding_voting_power),
202 neuron
203 .known_neuron_data
204 .as_ref()
205 .map_or_else(|| "-".to_string(), |known| known.name.clone()),
206 ]
207 })
208 .collect::<Vec<_>>(),
209 &[
210 Alignment::Right,
211 Alignment::Left,
212 Alignment::Left,
213 Alignment::Right,
214 Alignment::Right,
215 Alignment::Right,
216 Alignment::Left,
217 ],
218 )
219}