1use crate::domain::entities::Event;
2use crate::error::Result;
3use chrono::{DateTime, Utc};
4use parking_lot::RwLock;
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7use std::sync::Arc;
8use uuid::Uuid;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct Snapshot {
13 pub id: Uuid,
15
16 pub entity_id: String,
18
19 pub state: serde_json::Value,
21
22 pub created_at: DateTime<Utc>,
24
25 pub as_of: DateTime<Utc>,
27
28 pub event_count: usize,
30
31 pub metadata: SnapshotMetadata,
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct SnapshotMetadata {
37 pub snapshot_type: SnapshotType,
39
40 pub size_bytes: usize,
42
43 pub version: u32,
45}
46
47#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
48#[serde(rename_all = "lowercase")]
49pub enum SnapshotType {
50 Manual,
51 Automatic,
52 OnDemand,
53}
54
55impl Snapshot {
56 pub fn new(
58 entity_id: String,
59 state: serde_json::Value,
60 as_of: DateTime<Utc>,
61 event_count: usize,
62 snapshot_type: SnapshotType,
63 ) -> Self {
64 let state_json = serde_json::to_string(&state).unwrap_or_default();
65 let size_bytes = state_json.len();
66
67 Self {
68 id: Uuid::new_v4(),
69 entity_id,
70 state,
71 created_at: Utc::now(),
72 as_of,
73 event_count,
74 metadata: SnapshotMetadata {
75 snapshot_type,
76 size_bytes,
77 version: 1,
78 },
79 }
80 }
81
82 pub fn merge_with_events(&self, events: &[Event]) -> serde_json::Value {
84 let mut merged = self.state.clone();
85
86 for event in events {
87 if event.timestamp > self.as_of {
89 if let serde_json::Value::Object(ref mut state_map) = merged {
90 if let serde_json::Value::Object(ref payload_map) = event.payload {
91 for (key, value) in payload_map {
92 state_map.insert(key.clone(), value.clone());
93 }
94 }
95 }
96 }
97 }
98
99 merged
100 }
101}
102
103#[derive(Debug, Clone)]
105pub struct SnapshotConfig {
106 pub event_threshold: usize,
108
109 pub time_threshold_seconds: i64,
111
112 pub max_snapshots_per_entity: usize,
114
115 pub auto_snapshot: bool,
117}
118
119impl Default for SnapshotConfig {
120 fn default() -> Self {
121 Self {
122 event_threshold: 100,
123 time_threshold_seconds: 3600, max_snapshots_per_entity: 10,
125 auto_snapshot: true,
126 }
127 }
128}
129
130pub struct SnapshotManager {
132 snapshots: Arc<RwLock<HashMap<String, Vec<Snapshot>>>>,
134
135 config: SnapshotConfig,
137
138 stats: Arc<RwLock<SnapshotStats>>,
140}
141
142#[derive(Debug, Clone, Default, Serialize)]
143pub struct SnapshotStats {
144 pub total_snapshots: usize,
145 pub total_entities: usize,
146 pub total_size_bytes: usize,
147 pub snapshots_created: u64,
148 pub snapshots_pruned: u64,
149}
150
151impl SnapshotManager {
152 pub fn new(config: SnapshotConfig) -> Self {
154 Self {
155 snapshots: Arc::new(RwLock::new(HashMap::new())),
156 config,
157 stats: Arc::new(RwLock::new(SnapshotStats::default())),
158 }
159 }
160
161 pub fn create_snapshot(
163 &self,
164 entity_id: String,
165 state: serde_json::Value,
166 as_of: DateTime<Utc>,
167 event_count: usize,
168 snapshot_type: SnapshotType,
169 ) -> Result<Snapshot> {
170 let snapshot = Snapshot::new(entity_id.clone(), state, as_of, event_count, snapshot_type);
171
172 let mut snapshots = self.snapshots.write();
173 let entity_snapshots = snapshots.entry(entity_id.clone()).or_insert_with(Vec::new);
174
175 entity_snapshots.push(snapshot.clone());
177
178 entity_snapshots.sort_by(|a, b| b.as_of.cmp(&a.as_of));
180
181 let mut pruned = 0;
183 if entity_snapshots.len() > self.config.max_snapshots_per_entity {
184 let to_remove = entity_snapshots.len() - self.config.max_snapshots_per_entity;
185 entity_snapshots.truncate(self.config.max_snapshots_per_entity);
186 pruned = to_remove;
187 }
188
189 let mut stats = self.stats.write();
191 stats.snapshots_created += 1;
192 stats.snapshots_pruned += pruned as u64;
193 stats.total_snapshots = snapshots.values().map(|v| v.len()).sum();
194 stats.total_entities = snapshots.len();
195 stats.total_size_bytes = snapshots
196 .values()
197 .flatten()
198 .map(|s| s.metadata.size_bytes)
199 .sum();
200
201 tracing::info!(
202 "๐ธ Created {} snapshot for entity: {} (events: {}, size: {} bytes)",
203 match snapshot_type {
204 SnapshotType::Manual => "manual",
205 SnapshotType::Automatic => "automatic",
206 SnapshotType::OnDemand => "on-demand",
207 },
208 entity_id,
209 event_count,
210 snapshot.metadata.size_bytes
211 );
212
213 Ok(snapshot)
214 }
215
216 pub fn get_latest_snapshot(&self, entity_id: &str) -> Option<Snapshot> {
218 let snapshots = self.snapshots.read();
219 snapshots
220 .get(entity_id)
221 .and_then(|entity_snapshots| entity_snapshots.first().cloned())
222 }
223
224 pub fn get_snapshot_as_of(&self, entity_id: &str, as_of: DateTime<Utc>) -> Option<Snapshot> {
226 let snapshots = self.snapshots.read();
227 snapshots.get(entity_id).and_then(|entity_snapshots| {
228 entity_snapshots
229 .iter()
230 .filter(|s| s.as_of <= as_of)
231 .max_by_key(|s| s.as_of)
232 .cloned()
233 })
234 }
235
236 pub fn get_all_snapshots(&self, entity_id: &str) -> Vec<Snapshot> {
238 let snapshots = self.snapshots.read();
239 snapshots
240 .get(entity_id)
241 .map(|v| v.clone())
242 .unwrap_or_default()
243 }
244
245 pub fn should_create_snapshot(
247 &self,
248 entity_id: &str,
249 current_event_count: usize,
250 last_event_time: DateTime<Utc>,
251 ) -> bool {
252 if !self.config.auto_snapshot {
253 return false;
254 }
255
256 let snapshots = self.snapshots.read();
257 let entity_snapshots = snapshots.get(entity_id);
258
259 match entity_snapshots {
260 None => {
261 current_event_count >= self.config.event_threshold
263 }
264 Some(snaps) => {
265 if let Some(latest) = snaps.first() {
266 let events_since_snapshot = current_event_count - latest.event_count;
268 if events_since_snapshot >= self.config.event_threshold {
269 return true;
270 }
271
272 let time_since_snapshot = (last_event_time - latest.as_of).num_seconds();
274 if time_since_snapshot >= self.config.time_threshold_seconds {
275 return true;
276 }
277 }
278 false
279 }
280 }
281 }
282
283 pub fn delete_snapshots(&self, entity_id: &str) -> Result<usize> {
285 let mut snapshots = self.snapshots.write();
286 let removed = snapshots.remove(entity_id).map(|v| v.len()).unwrap_or(0);
287
288 let mut stats = self.stats.write();
290 stats.total_snapshots = stats.total_snapshots.saturating_sub(removed);
291 stats.total_entities = snapshots.len();
292
293 tracing::info!("๐๏ธ Deleted {} snapshots for entity: {}", removed, entity_id);
294
295 Ok(removed)
296 }
297
298 pub fn delete_snapshot(&self, entity_id: &str, snapshot_id: Uuid) -> Result<bool> {
300 let mut snapshots = self.snapshots.write();
301
302 if let Some(entity_snapshots) = snapshots.get_mut(entity_id) {
303 let initial_len = entity_snapshots.len();
304 entity_snapshots.retain(|s| s.id != snapshot_id);
305 let removed = initial_len != entity_snapshots.len();
306
307 if removed {
308 let mut stats = self.stats.write();
310 stats.total_snapshots = stats.total_snapshots.saturating_sub(1);
311 tracing::debug!("Deleted snapshot {} for entity {}", snapshot_id, entity_id);
312 }
313
314 return Ok(removed);
315 }
316
317 Ok(false)
318 }
319
320 pub fn stats(&self) -> SnapshotStats {
322 (*self.stats.read()).clone()
323 }
324
325 pub fn clear_all(&self) {
327 let mut snapshots = self.snapshots.write();
328 snapshots.clear();
329
330 let mut stats = self.stats.write();
331 *stats = SnapshotStats::default();
332
333 tracing::info!("๐งน Cleared all snapshots");
334 }
335
336 pub fn config(&self) -> &SnapshotConfig {
338 &self.config
339 }
340
341 pub fn list_entities(&self) -> Vec<String> {
343 let snapshots = self.snapshots.read();
344 snapshots.keys().cloned().collect()
345 }
346}
347
348#[derive(Debug, Deserialize)]
350pub struct CreateSnapshotRequest {
351 pub entity_id: String,
352}
353
354#[derive(Debug, Serialize)]
356pub struct CreateSnapshotResponse {
357 pub snapshot_id: Uuid,
358 pub entity_id: String,
359 pub created_at: DateTime<Utc>,
360 pub event_count: usize,
361 pub size_bytes: usize,
362}
363
364#[derive(Debug, Deserialize)]
366pub struct ListSnapshotsRequest {
367 pub entity_id: Option<String>,
368}
369
370#[derive(Debug, Serialize)]
372pub struct ListSnapshotsResponse {
373 pub snapshots: Vec<SnapshotInfo>,
374 pub total: usize,
375}
376
377#[derive(Debug, Serialize)]
378pub struct SnapshotInfo {
379 pub id: Uuid,
380 pub entity_id: String,
381 pub created_at: DateTime<Utc>,
382 pub as_of: DateTime<Utc>,
383 pub event_count: usize,
384 pub size_bytes: usize,
385 pub snapshot_type: SnapshotType,
386}
387
388impl From<Snapshot> for SnapshotInfo {
389 fn from(snapshot: Snapshot) -> Self {
390 Self {
391 id: snapshot.id,
392 entity_id: snapshot.entity_id,
393 created_at: snapshot.created_at,
394 as_of: snapshot.as_of,
395 event_count: snapshot.event_count,
396 size_bytes: snapshot.metadata.size_bytes,
397 snapshot_type: snapshot.metadata.snapshot_type,
398 }
399 }
400}
401
402#[cfg(test)]
403mod tests {
404 use super::*;
405 use serde_json::json;
406
407 fn create_test_snapshot(entity_id: &str, event_count: usize) -> Snapshot {
408 Snapshot::new(
409 entity_id.to_string(),
410 json!({"count": event_count}),
411 Utc::now(),
412 event_count,
413 SnapshotType::Automatic,
414 )
415 }
416
417 #[test]
418 fn test_snapshot_creation() {
419 let snapshot = create_test_snapshot("entity-1", 100);
420 assert_eq!(snapshot.entity_id, "entity-1");
421 assert_eq!(snapshot.event_count, 100);
422 assert_eq!(snapshot.metadata.snapshot_type, SnapshotType::Automatic);
423 }
424
425 #[test]
426 fn test_snapshot_manager() {
427 let manager = SnapshotManager::new(SnapshotConfig::default());
428
429 let result = manager.create_snapshot(
430 "entity-1".to_string(),
431 json!({"value": 42}),
432 Utc::now(),
433 100,
434 SnapshotType::Manual,
435 );
436
437 assert!(result.is_ok());
438
439 let latest = manager.get_latest_snapshot("entity-1");
440 assert!(latest.is_some());
441 assert_eq!(latest.unwrap().event_count, 100);
442 }
443
444 #[test]
445 fn test_snapshot_pruning() {
446 let config = SnapshotConfig {
447 max_snapshots_per_entity: 3,
448 ..Default::default()
449 };
450 let manager = SnapshotManager::new(config);
451
452 for i in 0..5 {
454 manager
455 .create_snapshot(
456 "entity-1".to_string(),
457 json!({"count": i}),
458 Utc::now(),
459 i,
460 SnapshotType::Automatic,
461 )
462 .unwrap();
463 }
464
465 let snapshots = manager.get_all_snapshots("entity-1");
467 assert_eq!(snapshots.len(), 3);
468 }
469
470 #[test]
471 fn test_should_create_snapshot() {
472 let config = SnapshotConfig {
473 event_threshold: 100,
474 time_threshold_seconds: 3600,
475 auto_snapshot: true,
476 ..Default::default()
477 };
478 let manager = SnapshotManager::new(config);
479
480 assert!(!manager.should_create_snapshot("entity-1", 50, Utc::now()));
482
483 assert!(manager.should_create_snapshot("entity-1", 100, Utc::now()));
485
486 manager
488 .create_snapshot(
489 "entity-1".to_string(),
490 json!({"value": 1}),
491 Utc::now(),
492 100,
493 SnapshotType::Automatic,
494 )
495 .unwrap();
496
497 assert!(!manager.should_create_snapshot("entity-1", 150, Utc::now()));
499
500 assert!(manager.should_create_snapshot("entity-1", 200, Utc::now()));
502 }
503
504 #[test]
505 fn test_merge_with_events() {
506 let snapshot = Snapshot::new(
507 "entity-1".to_string(),
508 json!({"name": "Alice", "score": 10}),
509 Utc::now(),
510 5,
511 SnapshotType::Automatic,
512 );
513
514 let event = Event::reconstruct_from_strings(
515 Uuid::new_v4(),
516 "score.updated".to_string(),
517 "entity-1".to_string(),
518 "default".to_string(),
519 json!({"score": 20}),
520 Utc::now(),
521 None,
522 1,
523 );
524
525 let merged = snapshot.merge_with_events(&[event]);
526 assert_eq!(merged["name"], "Alice");
527 assert_eq!(merged["score"], 20);
528 }
529}