1use crate::table::*;
6use fuchsia_inspect::{
7 self as inspect, ArithmeticArrayProperty, ArrayProperty, DoubleArrayProperty,
8 DoubleExponentialHistogramProperty, DoubleLinearHistogramProperty, Inspector, IntArrayProperty,
9 IntExponentialHistogramProperty, IntLinearHistogramProperty, Node, UintArrayProperty,
10 UintExponentialHistogramProperty, UintLinearHistogramProperty,
11};
12use futures::FutureExt;
13use std::ops::AddAssign;
14use structopt::StructOpt;
15use {
16 fidl_fuchsia_inspect as finspect, fidl_fuchsia_inspect_deprecated as fdeprecated,
17 fuchsia_async as fasync,
18};
19
20pub mod deprecated_fidl_server;
21pub mod table;
22
23pub struct PopulateParams<T> {
24 pub floor: T,
25 pub step: T,
26 pub count: usize,
27}
28
29pub fn populated<H: inspect::HistogramProperty>(histogram: H, params: PopulateParams<H::Type>) -> H
30where
31 H::Type: AddAssign + Copy,
32{
33 let mut value = params.floor;
34 for _ in 0..params.count {
35 histogram.insert(value);
36 value += params.step;
37 }
38 histogram
39}
40
41#[derive(Debug, StructOpt, Default)]
42#[structopt(
43 name = "example",
44 about = "Example component to showcase Inspect API objects, including an NxM nested table"
45)]
46pub struct Options {
47 #[structopt(long)]
48 pub rows: usize,
49
50 #[structopt(long)]
51 pub columns: usize,
52
53 #[structopt(long = "extra-number")]
55 pub extra_number: Option<i64>,
56}
57
58#[derive(Default)]
59pub struct ExampleInspectData {
60 int_array: IntArrayProperty,
61 uint_array: UintArrayProperty,
62 double_array: DoubleArrayProperty,
63 int_linear_hist: IntLinearHistogramProperty,
64 uint_linear_hist: UintLinearHistogramProperty,
65 double_linear_hist: DoubleLinearHistogramProperty,
66 int_exp_hist: IntExponentialHistogramProperty,
67 uint_exp_hist: UintExponentialHistogramProperty,
68 double_exp_hist: DoubleExponentialHistogramProperty,
69 table: Option<Table>,
70}
71
72impl ExampleInspectData {
73 pub fn write_to(&mut self, node: &Node) {
78 reset_unique_names();
81 self.table.replace(new_example_table(node));
82 self.int_array = new_example_int_array(node);
83 self.uint_array = new_example_uint_array(node);
84 self.double_array = new_example_double_array(node);
85 self.int_linear_hist = new_example_int_linear_hist(node);
86 self.uint_linear_hist = new_example_uint_linear_hist(node);
87 self.double_linear_hist = new_example_double_linear_hist(node);
88 self.int_exp_hist = new_example_int_exp_hist(node);
89 self.uint_exp_hist = new_example_uint_exp_hist(node);
90 self.double_exp_hist = new_example_double_exp_hist(node);
91
92 new_example_lazy_double(node);
95 new_example_lazy_uint(node);
96 }
97
98 pub fn has_node_object(&self) -> bool {
100 self.table.is_some()
101 }
102
103 pub fn get_node_object(&self) -> NodeObject {
104 self.table.as_ref().unwrap().get_node_object()
105 }
106}
107
108pub async fn serve_deprecated_inspect(stream: fdeprecated::InspectRequestStream, node: NodeObject) {
109 deprecated_fidl_server::spawn_inspect_server(stream, node);
110}
111
112pub async fn serve_inspect_tree(stream: finspect::TreeRequestStream, inspector: &Inspector) {
113 let scope = fasync::Scope::new_with_name("serve_inspect_tree");
114 let settings = inspect_runtime::TreeServerSendPreference::default();
115 inspect_runtime::service::spawn_tree_server_with_stream(
116 inspector.clone(),
117 settings,
118 stream,
119 &scope,
120 );
121 scope.join().await;
122}
123
124pub fn new_example_table(node: &Node) -> Table {
125 let table_node_name = unique_name("table");
126 Table::new(3, 3, &table_node_name, node.create_child(&table_node_name))
127}
128
129pub fn new_example_int_array(node: &Node) -> IntArrayProperty {
130 let int_array = node.create_int_array(unique_name("array"), 3);
131 int_array.set(0, 1);
132 int_array.add(1, 10);
133 int_array.subtract(2, 3);
134 int_array
135}
136
137pub fn new_example_uint_array(node: &Node) -> UintArrayProperty {
138 let uint_array = node.create_uint_array(unique_name("array"), 3);
139 uint_array.set(0, 1u64);
140 uint_array.add(1, 10);
141 uint_array.set(2, 3u64);
142 uint_array.subtract(2, 1);
143 uint_array
144}
145
146pub fn new_example_double_array(node: &Node) -> DoubleArrayProperty {
147 let double_array = node.create_double_array(unique_name("array"), 3);
148 double_array.set(0, 0.25);
149 double_array.add(1, 1.25);
150 double_array.subtract(2, 0.75);
151 double_array
152}
153
154pub fn new_example_int_linear_hist(node: &Node) -> IntLinearHistogramProperty {
155 populated(
156 node.create_int_linear_histogram(
157 unique_name("histogram"),
158 inspect::LinearHistogramParams { floor: -10, step_size: 5, buckets: 3 },
159 ),
160 PopulateParams { floor: -20, step: 1, count: 40 },
161 )
162}
163
164pub fn new_example_uint_linear_hist(node: &Node) -> UintLinearHistogramProperty {
165 populated(
166 node.create_uint_linear_histogram(
167 unique_name("histogram"),
168 inspect::LinearHistogramParams { floor: 5, step_size: 5, buckets: 3 },
169 ),
170 PopulateParams { floor: 0, step: 1, count: 40 },
171 )
172}
173
174pub fn new_example_double_linear_hist(node: &Node) -> DoubleLinearHistogramProperty {
175 populated(
176 node.create_double_linear_histogram(
177 unique_name("histogram"),
178 inspect::LinearHistogramParams { floor: 0.0, step_size: 0.5, buckets: 3 },
179 ),
180 PopulateParams { floor: -1.0, step: 0.1, count: 40 },
181 )
182}
183
184pub fn new_example_int_exp_hist(node: &Node) -> IntExponentialHistogramProperty {
185 populated(
186 node.create_int_exponential_histogram(
187 unique_name("histogram"),
188 inspect::ExponentialHistogramParams {
189 floor: -10,
190 initial_step: 5,
191 step_multiplier: 2,
192 buckets: 3,
193 },
194 ),
195 PopulateParams { floor: -20, step: 1, count: 40 },
196 )
197}
198
199pub fn new_example_uint_exp_hist(node: &Node) -> UintExponentialHistogramProperty {
200 populated(
201 node.create_uint_exponential_histogram(
202 unique_name("histogram"),
203 inspect::ExponentialHistogramParams {
204 floor: 0,
205 initial_step: 1,
206 step_multiplier: 2,
207 buckets: 3,
208 },
209 ),
210 PopulateParams { floor: 0, step: 1, count: 40 },
211 )
212}
213
214pub fn new_example_double_exp_hist(node: &Node) -> DoubleExponentialHistogramProperty {
215 populated(
216 node.create_double_exponential_histogram(
217 unique_name("histogram"),
218 inspect::ExponentialHistogramParams {
219 floor: 0.0,
220 initial_step: 1.25,
221 step_multiplier: 3.0,
222 buckets: 5,
223 },
224 ),
225 PopulateParams { floor: -1.0, step: 0.1, count: 40 },
226 )
227}
228
229pub fn new_example_lazy_uint(node: &Node) {
230 node.record_lazy_child("lazy-node", move || {
231 async move {
232 let inspector = inspect::Inspector::default();
233 inspector.root().record_uint("uint", 3);
234 Ok(inspector)
235 }
236 .boxed()
237 });
238}
239
240pub fn new_example_lazy_double(node: &Node) {
241 node.record_lazy_values("lazy-values", move || {
242 async move {
243 let inspector = inspect::Inspector::default();
244 inspector.root().record_double("lazy-double", 3.25);
245 Ok(inspector)
246 }
247 .boxed()
248 });
249}