metrix/instruments/
polled.rs1use crate::snapshot::*;
7use crate::util;
8use crate::{Descriptive, PutsSnapshot};
9
10pub struct PollingInstrument<P> {
16 pub name: String,
19 pub title: Option<String>,
20 pub description: Option<String>,
21 pub poll: P,
22 pub create_group_with_name: bool,
23}
24
25impl<P> PollingInstrument<P>
26where
27 P: PutsSnapshot,
28{
29 pub fn new_with_defaults<T: Into<String>>(name: T, poll: P) -> PollingInstrument<P> {
30 PollingInstrument {
31 name: name.into(),
32 title: None,
33 description: None,
34 poll,
35 create_group_with_name: false,
36 }
37 }
38
39 pub fn get_name(&self) -> &str {
41 &self.name
42 }
43
44 pub fn set_name<T: Into<String>>(&mut self, name: T) {
49 self.name = name.into();
50 }
51
52 pub fn create_group_with_name(&self) -> bool {
55 self.create_group_with_name
56 }
57
58 pub fn set_create_group_with_name(&mut self, create_group: bool) {
62 self.create_group_with_name = create_group;
63 }
64
65 pub fn set_title<T: Into<String>>(&mut self, title: T) {
69 self.title = Some(title.into())
70 }
71
72 pub fn set_description<T: Into<String>>(&mut self, description: T) {
76 self.description = Some(description.into())
77 }
78
79 fn put_values_into_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
80 self.poll.put_snapshot(into, descriptive);
81 }
82}
83
84impl<P> Descriptive for PollingInstrument<P> {
85 fn title(&self) -> Option<&str> {
86 self.title.as_deref()
87 }
88
89 fn description(&self) -> Option<&str> {
90 self.description.as_deref()
91 }
92}
93
94impl<P> PutsSnapshot for PollingInstrument<P>
95where
96 P: PutsSnapshot,
97{
98 fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
99 util::put_postfixed_descriptives(self, &self.name, into, descriptive);
100 if self.create_group_with_name {
101 let mut new_level = Snapshot::default();
102 self.put_values_into_snapshot(&mut new_level, descriptive);
103 into.items
104 .push((self.name.clone(), ItemKind::Snapshot(new_level)));
105 } else {
106 self.put_values_into_snapshot(into, descriptive);
107 }
108 }
109}
110
111pub struct ConstantValue {
112 name: String,
113 title: Option<String>,
114 description: Option<String>,
115 value: ItemKind,
116}
117
118impl ConstantValue {
119 pub fn new<T: Into<String>, V: Into<ItemKind>>(name: T, value: V) -> Self {
120 ConstantValue {
121 name: name.into(),
122 title: None,
123 description: None,
124 value: value.into(),
125 }
126 }
127
128 pub fn set_name<T: Into<String>>(&mut self, name: T) {
133 self.name = name.into();
134 }
135
136 pub fn get_name(&self) -> &str {
138 &self.name
139 }
140
141 pub fn set_title<T: Into<String>>(&mut self, title: T) {
145 self.title = Some(title.into())
146 }
147
148 pub fn set_description<T: Into<String>>(&mut self, description: T) {
152 self.description = Some(description.into())
153 }
154}
155
156impl PutsSnapshot for ConstantValue {
157 fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
158 util::put_postfixed_descriptives(self, &self.name, into, descriptive);
159 into.items.push((self.name.clone(), self.value.clone()));
160 }
161}
162
163impl Descriptive for ConstantValue {
164 fn title(&self) -> Option<&str> {
165 self.title.as_deref()
166 }
167
168 fn description(&self) -> Option<&str> {
169 self.description.as_deref()
170 }
171}