Skip to main content

metrics_procession/
chunk.rs

1use serde::{Deserialize, Serialize};
2use time::OffsetDateTime;
3
4use crate::event::Event;
5
6/// A chunk of metrics that represents all events emitted from the `reference_time`
7/// through 65 seconds after that reference time.
8#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
9pub struct Chunk {
10    /// The start time of this chunk
11    pub reference_time: OffsetDateTime,
12    /// The events that have happened within 65 seconds of the reference time
13    pub events: Vec<Event>,
14}
15
16impl Chunk {
17    /// Create a new chunk from the provided time
18    pub fn new(reference_time: OffsetDateTime) -> Self {
19        Self {
20            reference_time,
21            events: Default::default(),
22        }
23    }
24
25    /// Add a new event into this chunk
26    pub fn push(&mut self, event: Event) {
27        self.events.push(event);
28    }
29
30    /// A naive method for trying to determine the total memory size used by this chunk
31    pub fn memory_size(&self) -> usize {
32        use std::mem::size_of;
33        size_of::<Self>() + (self.events.len() * (size_of::<Event>()))
34    }
35}
36
37/// Create a new chunk with the reference time being [`time::OffsetDateTime::now_utc()`]
38impl Default for Chunk {
39    fn default() -> Self {
40        Self::new(OffsetDateTime::now_utc())
41    }
42}