use crate::event::payload::SupervisorEvent;
use crate::event::time::EventSequence;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EventJournal {
pub capacity: usize,
pub events: VecDeque<SupervisorEvent>,
pub dropped_count: u64,
pub last_sequence: Option<EventSequence>,
}
impl EventJournal {
pub fn new(capacity: usize) -> Self {
Self {
capacity,
events: VecDeque::with_capacity(capacity),
dropped_count: 0,
last_sequence: None,
}
}
pub fn push(&mut self, event: SupervisorEvent) -> u64 {
self.last_sequence = Some(event.sequence);
if self.capacity == 0 {
self.dropped_count = self.dropped_count.saturating_add(1);
return self.dropped_count;
}
if self.events.len() == self.capacity {
self.events.pop_front();
self.dropped_count = self.dropped_count.saturating_add(1);
}
self.events.push_back(event);
self.dropped_count
}
pub fn recent(&self, limit: usize) -> Vec<SupervisorEvent> {
let skip = self.events.len().saturating_sub(limit);
self.events.iter().skip(skip).cloned().collect()
}
pub fn len(&self) -> usize {
self.events.len()
}
pub fn is_empty(&self) -> bool {
self.events.is_empty()
}
}