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
use json::{stringify, stringify_pretty, JsonValue};
pub struct JsonConfig {
pub make_booleans_ints: bool,
pub pretty: Option<u16>,
}
impl Default for JsonConfig {
fn default() -> JsonConfig {
JsonConfig {
make_booleans_ints: false,
pretty: None,
}
}
}
#[derive(Debug, Clone)]
pub struct Snapshot {
pub items: Vec<(String, ItemKind)>,
}
impl Default for Snapshot {
fn default() -> Snapshot {
Snapshot { items: Vec::new() }
}
}
impl Snapshot {
pub fn push<K: Into<String>>(&mut self, k: K, v: ItemKind) {
self.items.push((k.into(), v))
}
pub fn to_default_json(&self) -> String {
self.to_json_internal(&JsonConfig::default())
}
pub fn to_json(&self, config: &JsonConfig) -> String {
self.to_json_internal(config)
}
fn to_json_internal(&self, config: &JsonConfig) -> String {
let data = self.to_json_value(config);
if let Some(indent) = config.pretty {
stringify_pretty(data, indent)
} else {
stringify(data)
}
}
fn to_json_value(&self, config: &JsonConfig) -> JsonValue {
let mut data = JsonValue::new_object();
self.items
.iter()
.for_each(|&(ref k, ref v)| data[k] = v.to_json_value(config));
data
}
}
#[derive(Debug, Clone)]
pub enum ItemKind {
Text(String),
Boolean(bool),
Float(f64),
UInt(u64),
Int(i64),
Snapshot(Snapshot),
}
impl ItemKind {
fn to_json_value(&self, config: &JsonConfig) -> JsonValue {
match *self {
ItemKind::Text(ref v) => v.clone().into(),
ItemKind::Boolean(v) => if config.make_booleans_ints {
if v {
1.into()
} else {
0.into()
}
} else {
v.into()
},
ItemKind::Float(v) => v.into(),
ItemKind::UInt(v) => v.into(),
ItemKind::Int(v) => v.into(),
ItemKind::Snapshot(ref snapshot) => snapshot.to_json_value(config),
}
}
}
#[derive(Debug, Clone)]
pub struct MeterSnapshot {
pub one_minute: MeterRate,
pub five_minutes: MeterRate,
pub fifteen_minutes: MeterRate,
}
impl MeterSnapshot {
pub fn put_snapshot(&self, into: &mut Snapshot) {
let mut one_minute = Snapshot::default();
self.one_minute.put_snapshot(&mut one_minute);
into.items
.push(("one_minute".to_string(), ItemKind::Snapshot(one_minute)));
let mut five_minutes = Snapshot::default();
self.five_minutes.put_snapshot(&mut five_minutes);
into.items
.push(("five_minutes".to_string(), ItemKind::Snapshot(five_minutes)));
let mut fifteen_minutes = Snapshot::default();
self.fifteen_minutes.put_snapshot(&mut fifteen_minutes);
into.items.push((
"fifteen_minutes".to_string(),
ItemKind::Snapshot(fifteen_minutes),
));
}
}
#[derive(Debug, Clone)]
pub struct MeterRate {
pub rate: f64,
pub count: u64,
}
impl MeterRate {
fn put_snapshot(&self, into: &mut Snapshot) {
into.items
.push(("rate".to_string(), ItemKind::Float(self.rate)));
into.items
.push(("count".to_string(), ItemKind::UInt(self.count)));
}
}
#[derive(Debug, Clone)]
pub struct HistogramSnapshot {
pub max: i64,
pub min: i64,
pub mean: f64,
pub stddev: f64,
pub count: u64,
pub quantiles: Vec<(u16, i64)>,
}
impl HistogramSnapshot {
pub fn put_snapshot(&self, into: &mut Snapshot) {
into.items
.push(("max".to_string(), ItemKind::Int(self.max)));
into.items
.push(("min".to_string(), ItemKind::Int(self.min)));
into.items
.push(("mean".to_string(), ItemKind::Float(self.mean)));
into.items
.push(("stddev".to_string(), ItemKind::Float(self.stddev)));
into.items
.push(("count".to_string(), ItemKind::UInt(self.count)));
let mut quantiles = Snapshot::default();
for &(ref q, ref v) in &self.quantiles {
quantiles.items.push((format!("p{}", q), ItemKind::Int(*v)));
}
into.items
.push(("quantiles".to_string(), ItemKind::Snapshot(quantiles)));
}
}