Skip to main content

bctx_cloud_core/dashboard/
history.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub struct HistorySeries {
5    pub label: String,
6    pub points: Vec<DataPoint>,
7}
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct DataPoint {
11    pub timestamp: String,
12    pub value: f64,
13}
14
15impl HistorySeries {
16    pub fn new(label: impl Into<String>) -> Self {
17        Self {
18            label: label.into(),
19            points: Vec::new(),
20        }
21    }
22
23    pub fn push(&mut self, timestamp: impl Into<String>, value: f64) {
24        self.points.push(DataPoint {
25            timestamp: timestamp.into(),
26            value,
27        });
28    }
29
30    pub fn avg(&self) -> f64 {
31        if self.points.is_empty() {
32            return 0.0;
33        }
34        self.points.iter().map(|p| p.value).sum::<f64>() / self.points.len() as f64
35    }
36
37    pub fn peak(&self) -> f64 {
38        self.points.iter().map(|p| p.value).fold(0.0_f64, f64::max)
39    }
40}