-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathmanage_chart_data.rs
More file actions
385 lines (355 loc) · 12 KB
/
Copy pathmanage_chart_data.rs
File metadata and controls
385 lines (355 loc) · 12 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use splines::{Interpolation, Key, Spline};
use crate::TrafficChart;
use crate::networking::types::info_traffic::InfoTraffic;
impl TrafficChart {
pub fn update_charts_data(&mut self, info_traffic_msg: &InfoTraffic, no_more_packets: bool) {
self.no_more_packets = no_more_packets;
if self.ticks == 0 {
self.first_packet_timestamp = info_traffic_msg.last_packet_timestamp;
}
#[allow(clippy::cast_precision_loss)]
let tot_seconds = self.ticks as f32;
self.ticks += 1;
#[allow(clippy::cast_precision_loss)]
let out_bytes_entry = -1.0 * info_traffic_msg.tot_data_info.outgoing_bytes() as f32;
#[allow(clippy::cast_precision_loss)]
let in_bytes_entry = info_traffic_msg.tot_data_info.incoming_bytes() as f32;
#[allow(clippy::cast_precision_loss)]
let out_packets_entry = -1.0 * info_traffic_msg.tot_data_info.outgoing_packets() as f32;
#[allow(clippy::cast_precision_loss)]
let in_packets_entry = info_traffic_msg.tot_data_info.incoming_packets() as f32;
let out_bytes_point = (tot_seconds, out_bytes_entry);
let in_bytes_point = (tot_seconds, in_bytes_entry);
let out_packets_point = (tot_seconds, out_packets_entry);
let in_packets_point = (tot_seconds, in_packets_entry);
// update sent bytes traffic data
update_series(
&mut self.out_bytes,
out_bytes_point,
self.is_live_capture,
no_more_packets,
);
self.min_bytes = get_min(&self.out_bytes);
// update received bytes traffic data
update_series(
&mut self.in_bytes,
in_bytes_point,
self.is_live_capture,
no_more_packets,
);
self.max_bytes = get_max(&self.in_bytes);
// update sent packets traffic data
update_series(
&mut self.out_packets,
out_packets_point,
self.is_live_capture,
no_more_packets,
);
self.min_packets = get_min(&self.out_packets);
// update received packets traffic data
update_series(
&mut self.in_packets,
in_packets_point,
self.is_live_capture,
no_more_packets,
);
self.max_packets = get_max(&self.in_packets);
}
pub fn push_offline_gap_to_splines(&mut self, gap: u32) {
for i in 0..gap {
#[allow(clippy::cast_precision_loss)]
let point = ((self.ticks + i) as f32, 0.0);
update_series(&mut self.in_bytes, point, false, false);
update_series(&mut self.out_bytes, point, false, false);
update_series(&mut self.in_packets, point, false, false);
update_series(&mut self.out_packets, point, false, false);
}
self.ticks += gap;
}
}
fn update_series(
series: &mut ChartSeries,
point: (f32, f32),
is_live_capture: bool,
no_more_packets: bool,
) {
// update spline
let spline = &mut series.spline;
let key = Key::new(point.0, point.1, Interpolation::Cosine);
if spline.len() >= 30 {
spline.remove(0);
}
spline.add(key);
// if offline capture, update all time data
if !is_live_capture {
let all_time = &mut series.all_time;
all_time.push(point);
// if we reached the end of the PCAP, reduce all time data into spline
if no_more_packets {
reduce_all_time_data(all_time);
let keys = all_time
.iter()
.map(|p| Key::new(p.0, p.1, Interpolation::Cosine))
.collect();
*spline = Spline::from_vec(keys);
}
}
}
/// Finds the minimum y value to be displayed in chart.
fn get_min(serie: &ChartSeries) -> f32 {
let mut min = 0.0;
for key in &serie.spline {
if key.value < min {
min = key.value;
}
}
min
}
/// Finds the maximum y value to be displayed in chart.
fn get_max(serie: &ChartSeries) -> f32 {
let mut max = 0.0;
for key in &serie.spline {
if key.value > max {
max = key.value;
}
}
max
}
#[derive(Default, Clone)]
pub struct ChartSeries {
/// Series to be displayed DURING live/offline capture
pub spline: Spline<f32, f32>,
/// Used to draw overall data, after the offline capture is over (not used in live captures)
pub all_time: Vec<(f32, f32)>,
}
fn reduce_all_time_data(all_time: &mut Vec<(f32, f32)>) {
// bisect data until we have less than 300 points
while all_time.len() > 300 {
let mut new_vec = Vec::new();
all_time.iter().enumerate().for_each(|(i, (x, y))| {
if i % 2 == 0 {
if let Some(next) = all_time.get(i + 1) {
new_vec.push((*x, (y + next.1) / 2.0));
}
}
});
*all_time = new_vec;
}
}
#[cfg(test)]
mod tests {
use splines::{Interpolation, Key, Spline};
use crate::chart::manage_chart_data::{ChartSeries, get_max, get_min};
use crate::networking::types::data_info::DataInfo;
use crate::utils::types::timestamp::Timestamp;
use crate::{ChartType, InfoTraffic, Language, StyleType, TrafficChart};
fn spline_from_vec(vec: Vec<(i32, i32)>) -> Spline<f32, f32> {
Spline::from_vec(
vec.iter()
.map(|&(x, y)| Key::new(x as f32, y as f32, Interpolation::Cosine))
.collect::<Vec<Key<f32, f32>>>(),
)
}
#[test]
fn test_chart_data_updates() {
let sent_vec = vec![
(0, -500),
(1, -1000),
(2, -1000),
(3, -1000),
(4, -1000),
(5, -1000),
(6, -1000),
(7, -1000),
(8, -1000),
(9, -1000),
(10, -1000),
(11, -1000),
(12, -1000),
(13, -1000),
(14, -1000),
(15, -1000),
(16, -1000),
(17, -1000),
(18, -1000),
(19, -1000),
(20, -1000),
(21, -1000),
(22, -1000),
(23, -1000),
(24, -1000),
(25, -1000),
(26, -1000),
(27, -1000),
(28, -1000),
];
let sent_spl = spline_from_vec(sent_vec);
let sent = ChartSeries {
spline: sent_spl,
all_time: vec![],
};
let received_vec = vec![
(0, 1000),
(1, 21000),
(2, 21000),
(3, 21000),
(4, 21000),
(5, 21000),
(6, 21000),
(7, 21000),
(8, 21000),
(9, 21000),
(10, 21000),
(11, 21000),
(12, 21000),
(13, 21000),
(14, 21000),
(15, 21000),
(16, 21000),
(17, 21000),
(18, 21000),
(19, 21000),
(20, 21000),
(21, 21000),
(22, 21000),
(23, 21000),
(24, 21000),
(25, 21000),
(26, 21000),
(27, 21000),
(28, 21000),
];
let received_spl = spline_from_vec(received_vec);
let received = ChartSeries {
spline: received_spl,
all_time: vec![],
};
let tot_data_info = DataInfo::new_for_tests(4444, 3333, 2222, 1111);
let mut traffic_chart = TrafficChart {
ticks: 29,
out_bytes: sent.clone(),
in_bytes: received.clone(),
out_packets: sent.clone(),
in_packets: received.clone(),
min_bytes: -1000.0,
max_bytes: 21000.0,
min_packets: -1000.0,
max_packets: 21000.0,
language: Language::default(),
chart_type: ChartType::Packets,
style: StyleType::default(),
thumbnail: false,
is_live_capture: true,
first_packet_timestamp: Timestamp::default(),
no_more_packets: false,
};
let mut info_traffic = InfoTraffic {
all_bytes: 0,
all_packets: 0,
tot_data_info,
dropped_packets: 0,
..Default::default()
};
assert_eq!(get_min(&sent), -1000.0);
assert_eq!(get_max(&received), 21000.0);
traffic_chart.update_charts_data(&info_traffic, false);
assert_eq!(get_min(&traffic_chart.out_packets), -3333.0);
assert_eq!(get_max(&traffic_chart.in_bytes), 21000.0);
let mut sent_bytes = sent.clone();
sent_bytes
.spline
.add(Key::new(29.0, -1111.0, Interpolation::Cosine));
let mut received_packets = received.clone();
received_packets
.spline
.add(Key::new(29.0, 4444.0, Interpolation::Cosine));
let mut sent_packets = sent;
sent_packets
.spline
.add(Key::new(29.0, -3333.0, Interpolation::Cosine));
let mut received_bytes = received;
received_bytes
.spline
.add(Key::new(29.0, 2222.0, Interpolation::Cosine));
// traffic_chart correctly updated?
assert_eq!(traffic_chart.ticks, 30);
assert_eq!(traffic_chart.min_bytes, -1111.0);
assert_eq!(traffic_chart.min_packets, -3333.0);
assert_eq!(traffic_chart.max_bytes, 21000.0);
assert_eq!(traffic_chart.max_packets, 21000.0);
assert_eq!(
traffic_chart.out_bytes.spline.keys(),
sent_bytes.spline.keys()
);
assert_eq!(
traffic_chart.in_packets.spline.keys(),
received_packets.spline.keys()
);
assert_eq!(
traffic_chart.out_packets.spline.keys(),
sent_packets.spline.keys()
);
assert_eq!(
traffic_chart.in_bytes.spline.keys(),
received_bytes.spline.keys()
);
info_traffic.tot_data_info = DataInfo::new_for_tests(990, 1, 2, 99);
traffic_chart.update_charts_data(&info_traffic, false);
info_traffic.tot_data_info = DataInfo::new_for_tests(1, 220, 0, 77);
traffic_chart.update_charts_data(&info_traffic, false);
sent_bytes.spline.remove(0);
sent_bytes.spline.remove(0);
sent_bytes
.spline
.add(Key::new(30.0, -99.0, Interpolation::Cosine));
sent_bytes
.spline
.add(Key::new(31.0, -77.0, Interpolation::Cosine));
received_packets.spline.remove(0);
received_packets.spline.remove(0);
received_packets
.spline
.add(Key::new(30.0, 990.0, Interpolation::Cosine));
received_packets
.spline
.add(Key::new(31.0, 1.0, Interpolation::Cosine));
sent_packets.spline.remove(0);
sent_packets.spline.remove(0);
sent_packets
.spline
.add(Key::new(30.0, -1.0, Interpolation::Cosine));
sent_packets
.spline
.add(Key::new(31.0, -220.0, Interpolation::Cosine));
received_bytes.spline.remove(0);
received_bytes.spline.remove(0);
received_bytes
.spline
.add(Key::new(30.0, 2.0, Interpolation::Cosine));
received_bytes
.spline
.add(Key::new(31.0, 0.0, Interpolation::Cosine));
// traffic_chart correctly updated?
assert_eq!(traffic_chart.ticks, 32);
assert_eq!(traffic_chart.min_bytes, -1111.0);
assert_eq!(traffic_chart.min_packets, -3333.0);
assert_eq!(traffic_chart.max_bytes, 21000.0);
assert_eq!(traffic_chart.max_packets, 21000.0);
assert_eq!(
traffic_chart.out_bytes.spline.keys(),
sent_bytes.spline.keys()
);
assert_eq!(
traffic_chart.in_packets.spline.keys(),
received_packets.spline.keys()
);
assert_eq!(
traffic_chart.out_packets.spline.keys(),
sent_packets.spline.keys()
);
assert_eq!(
traffic_chart.in_bytes.spline.keys(),
received_bytes.spline.keys()
);
}
}