Skip to main content

edgehdf5_memory/
session.rs

1//! Session tracking cache and data structures.
2
3/// A single session entry.
4#[derive(Debug, Clone)]
5pub struct SessionEntry {
6    pub id: String,
7    pub start_idx: u64,
8    pub end_idx: u64,
9    pub channel: String,
10    pub ts: f64,
11}
12
13/// In-memory cache for the /sessions group.
14#[derive(Debug, Clone)]
15pub struct SessionCache {
16    pub entries: Vec<SessionEntry>,
17    pub summaries: Vec<String>,
18}
19
20impl SessionCache {
21    pub fn new() -> Self {
22        Self {
23            entries: Vec::new(),
24            summaries: Vec::new(),
25        }
26    }
27
28    pub fn len(&self) -> usize {
29        self.entries.len()
30    }
31
32    pub fn is_empty(&self) -> bool {
33        self.entries.is_empty()
34    }
35
36    /// Add a new session with its summary.
37    pub fn add(
38        &mut self,
39        id: &str,
40        start_idx: usize,
41        end_idx: usize,
42        channel: &str,
43        summary: &str,
44    ) {
45        let ts = std::time::SystemTime::now()
46            .duration_since(std::time::UNIX_EPOCH)
47            .unwrap_or_default()
48            .as_secs_f64()
49            * 1_000_000.0; // microseconds
50        self.entries.push(SessionEntry {
51            id: id.to_string(),
52            start_idx: start_idx as u64,
53            end_idx: end_idx as u64,
54            channel: channel.to_string(),
55            ts,
56        });
57        self.summaries.push(summary.to_string());
58    }
59
60    /// Return the ID of the most recently added session, if any.
61    pub fn latest_session_id(&self) -> Option<&str> {
62        self.entries.last().map(|e| e.id.as_str())
63    }
64
65    /// Find the summary for a session by ID.
66    pub fn find_summary(&self, session_id: &str) -> Option<&str> {
67        self.entries
68            .iter()
69            .position(|e| e.id == session_id)
70            .map(|idx| self.summaries[idx].as_str())
71    }
72}
73
74impl Default for SessionCache {
75    fn default() -> Self {
76        Self::new()
77    }
78}