Skip to main content

frequenz_microgrid/
sample.rs

1// License: MIT
2// Copyright © 2025 Frequenz Energy-as-a-Service GmbH
3
4use chrono::{DateTime, Utc};
5
6/// Represents a measurement of a microgrid metric, made at a specific time.
7#[derive(Copy, Clone, Debug, Default)]
8pub struct Sample<Q: Copy + Clone + std::fmt::Debug + Default + std::fmt::Display> {
9    pub(crate) timestamp: DateTime<Utc>,
10    pub(crate) value: Option<Q>,
11}
12
13impl<Q: crate::quantity::Quantity> std::fmt::Display for Sample<Q> {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        write!(f, "Sample({}, ", self.timestamp)?;
16
17        if let Some(value) = self.value {
18            write!(f, " {})", value)
19        } else {
20            write!(f, " None)")
21        }
22    }
23}
24
25impl<Q: Copy + Clone + Default + std::fmt::Debug + std::fmt::Display> frequenz_resampling::Sample
26    for Sample<Q>
27{
28    type Value = Q;
29
30    fn new(timestamp: DateTime<Utc>, value: Option<Self::Value>) -> Self {
31        Self { timestamp, value }
32    }
33
34    fn timestamp(&self) -> DateTime<Utc> {
35        self.timestamp
36    }
37
38    fn value(&self) -> Option<Self::Value> {
39        self.value
40    }
41}
42
43impl<Q: Copy + Clone + Default + std::fmt::Debug + std::fmt::Display> Sample<Q> {
44    /// Creates a new `Sample` with the given timestamp and value.
45    pub fn new(timestamp: DateTime<Utc>, value: Option<Q>) -> Self {
46        Self { timestamp, value }
47    }
48
49    /// Returns the timestamp of the sample.
50    pub fn timestamp(&self) -> DateTime<Utc> {
51        self.timestamp
52    }
53
54    /// Returns the value of the sample.
55    pub fn value(&self) -> Option<Q> {
56        self.value
57    }
58}