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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::time::SystemTime;
use dashmap::DashMap;
use probability::distribution::Inverse;
#[derive(Debug, Clone)]
pub struct EmaCalculator {
mean_accum: f64,
variance_accum: f64,
set: bool,
alpha: f64,
}
impl EmaCalculator {
pub fn new(initial_mean: f64, alpha: f64) -> Self {
Self {
mean_accum: initial_mean,
variance_accum: initial_mean / 10.0,
alpha,
set: true,
}
}
pub fn new_unset(alpha: f64) -> Self {
Self {
mean_accum: 0.0,
variance_accum: 0.001,
alpha,
set: false,
}
}
pub fn update(&mut self, point: f64) {
if !self.set {
self.mean_accum = point;
self.variance_accum = 0.0;
self.set = true
}
self.variance_accum = (1.0 - self.alpha)
* (self.variance_accum + self.alpha * (point - self.mean_accum).powi(2));
self.mean_accum = self.mean_accum * (1.0 - self.alpha) + self.alpha * point;
}
pub fn inverse_cdf(&self, frac: f64) -> f64 {
let dist =
probability::distribution::Gaussian::new(self.mean_accum, self.variance_accum.sqrt());
dist.inverse(frac)
}
pub fn mean(&self) -> f64 {
self.mean_accum
}
}
#[derive(Debug, Clone, Default)]
pub struct StatsGatherer {
mapping: Option<DashMap<String, TimeSeries>>,
}
impl StatsGatherer {
pub fn new_active() -> Self {
Self {
mapping: Some(Default::default()),
}
}
pub fn update(&self, stat: &str, val: f32) {
if let Some(mapping) = &self.mapping {
let mut ts = mapping
.entry(stat.to_string())
.or_insert_with(|| TimeSeries::new(10000));
ts.push(val)
}
}
pub fn increment(&self, stat: &str, delta: f32) {
if let Some(mapping) = &self.mapping {
let mut ts = mapping
.entry(stat.to_string())
.or_insert_with(|| TimeSeries::new(10000));
ts.increment(delta)
}
}
pub fn get_last(&self, stat: &str) -> Option<f32> {
let series = self.mapping.as_ref()?.get(stat)?;
series.items.get_max().map(|v| v.1)
}
pub fn get_timeseries(&self, stat: &str) -> Option<TimeSeries> {
Some(self.mapping.as_ref()?.get(stat)?.clone())
}
pub fn iter(&self) -> impl Iterator<Item = (String, TimeSeries)> {
self.mapping.clone().unwrap_or_default().into_iter()
}
}
#[derive(Debug, Clone, Default)]
pub struct TimeSeries {
max_length: usize,
items: im::OrdMap<SystemTime, f32>,
}
impl TimeSeries {
pub fn push(&mut self, item: f32) {
if self.same_as_last() {
return;
}
self.items.insert(SystemTime::now(), item);
self.may_decimate()
}
fn may_decimate(&mut self) {
if self.items.len() >= self.max_length {
let half_map: im::OrdMap<_, _> = self
.items
.iter()
.enumerate()
.filter_map(|(i, v)| if i % 2 != 0 { Some((*v.0, *v.1)) } else { None })
.collect();
self.items = half_map;
}
}
fn same_as_last(&self) -> bool {
let last = self.items.get_max();
if let Some(last) = last {
let last_time = last.0;
let dur = last_time.elapsed();
if let Ok(dur) = dur {
if dur.as_millis() < 5 {
return true;
}
}
}
false
}
pub fn increment(&mut self, delta: f32) {
if self.same_as_last() {
let (last_key, last_val) = self.items.get_max().unwrap();
let last_key = *last_key;
let new_last = *last_val + delta;
self.items.insert(last_key, new_last);
return;
}
let last_val = self.items.get_max().map(|v| v.1).unwrap_or_default();
self.items.insert(SystemTime::now(), delta + last_val);
self.may_decimate()
}
pub fn new(max_length: usize) -> Self {
Self {
max_length,
items: im::OrdMap::new(),
}
}
pub fn iter(&self) -> impl Iterator<Item = (&SystemTime, &f32)> {
self.items.iter()
}
pub fn after(&self, time: SystemTime) -> Self {
let (_, after) = self.items.split(&time);
Self {
items: after,
max_length: self.max_length,
}
}
pub fn get(&self, time: SystemTime) -> f32 {
self.items
.get_prev(&time)
.map(|v| *v.1)
.unwrap_or_default()
.max(self.items.get_next(&time).map(|v| *v.1).unwrap_or_default())
}
pub fn earliest(&self) -> Option<(SystemTime, f32)> {
self.items.get_min().cloned()
}
}