use std::time::{Duration, Instant};
pub mod logs;
pub mod metrics;
pub mod traces;
pub use logs::LogsState;
pub use metrics::MetricsState;
pub use traces::TracesState;
pub const MAX_ITEMS_IN_MEMORY: usize = 1000;
pub const MIN_REFRESH_INTERVAL: Duration = Duration::from_millis(100);
#[allow(dead_code)]
pub const DATA_RETENTION_DURATION: Duration = Duration::from_secs(3600);
#[allow(dead_code)]
pub trait StateManager {
fn apply_pagination(&mut self);
fn cleanup_old_data(&mut self);
fn item_count(&self) -> usize;
}
#[derive(Debug, Clone)]
pub struct UpdateTracker {
last_update: Instant,
min_interval: Duration,
}
impl UpdateTracker {
pub fn new(min_interval: Duration) -> Self {
Self {
last_update: Instant::now() - min_interval, min_interval,
}
}
pub fn should_update(&self) -> bool {
self.last_update.elapsed() >= self.min_interval
}
pub fn mark_updated(&mut self) {
self.last_update = Instant::now();
}
#[allow(dead_code)]
pub fn time_since_update(&self) -> Duration {
self.last_update.elapsed()
}
}
#[derive(Debug, Clone)]
pub struct ResponseCache<T> {
data: Option<T>,
cached_at: Instant,
ttl: Duration,
}
#[allow(dead_code)]
impl<T: Clone> ResponseCache<T> {
pub fn new(ttl: Duration) -> Self {
Self {
data: None,
cached_at: Instant::now(),
ttl,
}
}
pub fn get(&self) -> Option<&T> {
if self.is_valid() {
self.data.as_ref()
} else {
None
}
}
pub fn set(&mut self, data: T) {
self.data = Some(data);
self.cached_at = Instant::now();
}
pub fn is_valid(&self) -> bool {
self.data.is_some() && self.cached_at.elapsed() < self.ttl
}
pub fn clear(&mut self) {
self.data = None;
}
}
#[derive(Debug, Clone)]
pub struct PaginatedList<T> {
items: Vec<T>,
max_items: usize,
}
#[allow(dead_code)]
impl<T> PaginatedList<T> {
pub fn new(max_items: usize) -> Self {
Self {
items: Vec::with_capacity(max_items),
max_items,
}
}
pub fn extend(&mut self, new_items: Vec<T>) {
self.items.extend(new_items);
self.trim_to_max();
}
pub fn replace(&mut self, new_items: Vec<T>) {
self.items = new_items;
self.trim_to_max();
}
pub fn items(&self) -> &[T] {
&self.items
}
pub fn items_mut(&mut self) -> &mut Vec<T> {
&mut self.items
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub fn clear(&mut self) {
self.items.clear();
}
fn trim_to_max(&mut self) {
if self.items.len() > self.max_items {
let excess = self.items.len() - self.max_items;
self.items.drain(0..excess);
}
}
}
impl<T> Default for PaginatedList<T> {
fn default() -> Self {
Self::new(MAX_ITEMS_IN_MEMORY)
}
}