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
use async_graphql::Object;
use chrono::{DateTime, Utc};

use crate::{
    config::ComponentKey,
    event::{Metric, MetricValue},
};

pub struct ReceivedBytesTotal(Metric);

impl ReceivedBytesTotal {
    pub const fn new(m: Metric) -> Self {
        Self(m)
    }

    pub fn get_timestamp(&self) -> Option<DateTime<Utc>> {
        self.0.timestamp()
    }

    pub fn get_received_bytes_total(&self) -> f64 {
        match self.0.value() {
            MetricValue::Counter { value } => *value,
            _ => 0.00,
        }
    }
}

#[Object]
impl ReceivedBytesTotal {
    /// Metric timestamp.
    pub async fn timestamp(&self) -> Option<DateTime<Utc>> {
        self.get_timestamp()
    }

    /// Total number of bytes received.
    pub async fn received_bytes_total(&self) -> f64 {
        self.get_received_bytes_total()
    }
}

impl From<Metric> for ReceivedBytesTotal {
    fn from(m: Metric) -> Self {
        Self(m)
    }
}

pub struct ComponentReceivedBytesTotal {
    component_key: ComponentKey,
    metric: Metric,
}

impl ComponentReceivedBytesTotal {
    /// Returns a new `ComponentReceivedBytesTotal`.
    ///
    /// Expects that the metric contains a tag for the component ID the metric is referenced to.
    pub fn new(metric: Metric) -> Self {
        let component_key = metric.tag_value("component_id").expect(
            "Returned a metric without a `component_id`, which shouldn't happen. Please report.",
        );
        let component_key = ComponentKey::from(component_key);

        Self {
            component_key,
            metric,
        }
    }
}

#[Object]
impl ComponentReceivedBytesTotal {
    /// Component ID.
    async fn component_id(&self) -> &str {
        self.component_key.id()
    }

    /// Metric for total bytes received.
    async fn metric(&self) -> ReceivedBytesTotal {
        ReceivedBytesTotal::new(self.metric.clone())
    }
}

pub struct ComponentReceivedBytesThroughput {
    component_key: ComponentKey,
    throughput: i64,
}

impl ComponentReceivedBytesThroughput {
    /// Returns a new `ComponentReceivedBytesThroughput` for the given component.
    pub const fn new(component_key: ComponentKey, throughput: i64) -> Self {
        Self {
            component_key,
            throughput,
        }
    }
}

#[Object]
impl ComponentReceivedBytesThroughput {
    /// Component ID.
    async fn component_id(&self) -> &str {
        self.component_key.id()
    }

    /// Throughput of bytes sent.
    async fn throughput(&self) -> i64 {
        self.throughput
    }
}