scouter_types/custom/
types.rs

1use crate::util::ProfileFuncs;
2use chrono::{DateTime, Utc};
3use pyo3::prelude::*;
4use serde::{Deserialize, Serialize};
5use std::collections::BTreeMap;
6
7#[pyclass]
8#[derive(Debug, Clone, Serialize, Deserialize, Default)]
9pub struct BinnedCustomMetricStats {
10    #[pyo3(get)]
11    pub avg: f64,
12
13    #[pyo3(get)]
14    pub lower_bound: f64,
15
16    #[pyo3(get)]
17    pub upper_bound: f64,
18}
19
20#[pymethods]
21impl BinnedCustomMetricStats {
22    pub fn __str__(&self) -> String {
23        // serialize the struct to a string
24        ProfileFuncs::__str__(self)
25    }
26}
27
28#[pyclass]
29#[derive(Debug, Clone, Serialize, Deserialize, Default)]
30pub struct BinnedCustomMetric {
31    #[pyo3(get)]
32    pub metric: String,
33
34    #[pyo3(get)]
35    pub created_at: Vec<DateTime<Utc>>,
36
37    #[pyo3(get)]
38    pub stats: Vec<BinnedCustomMetricStats>,
39}
40
41#[pymethods]
42impl BinnedCustomMetric {
43    pub fn __str__(&self) -> String {
44        // serialize the struct to a string
45        ProfileFuncs::__str__(self)
46    }
47}
48
49#[pyclass]
50#[derive(Debug, Clone, Serialize, Deserialize, Default)]
51pub struct BinnedCustomMetrics {
52    #[pyo3(get)]
53    pub metrics: BTreeMap<String, BinnedCustomMetric>,
54}
55
56#[pymethods]
57impl BinnedCustomMetrics {
58    pub fn __str__(&self) -> String {
59        // serialize the struct to a string
60        ProfileFuncs::__str__(self)
61    }
62}
63
64impl BinnedCustomMetrics {
65    pub fn from_vec(metrics: Vec<BinnedCustomMetric>) -> Self {
66        let mapped: BTreeMap<String, BinnedCustomMetric> = metrics
67            .into_iter()
68            .map(|metric| (metric.metric.clone(), metric))
69            .collect();
70        BinnedCustomMetrics { metrics: mapped }
71    }
72}