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> {
9    pub(crate) timestamp: DateTime<Utc>,
10    pub(crate) value: Option<Q>,
11}
12
13impl<Q: Copy + Clone + Default + std::fmt::Debug> frequenz_resampling::Sample for Sample<Q> {
14    type Value = Q;
15
16    fn new(timestamp: DateTime<Utc>, value: Option<Self::Value>) -> Self {
17        Self { timestamp, value }
18    }
19
20    fn timestamp(&self) -> DateTime<Utc> {
21        self.timestamp
22    }
23
24    fn value(&self) -> Option<Self::Value> {
25        self.value
26    }
27}
28
29impl<Q: Copy + Clone + Default + std::fmt::Debug> Sample<Q> {
30    /// Creates a new `Sample` with the given timestamp and value.
31    pub fn new(timestamp: DateTime<Utc>, value: Option<Q>) -> Self {
32        Self { timestamp, value }
33    }
34
35    /// Returns the timestamp of the sample.
36    pub fn timestamp(&self) -> DateTime<Utc> {
37        self.timestamp
38    }
39
40    /// Returns the value of the sample.
41    pub fn value(&self) -> Option<Q> {
42        self.value
43    }
44}