Rust
snapkitty
compilers
lean4
File size: 1,344 Bytes
81b9e0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// observatory.rs — SnapKitty Runtime Observatory

use serde::{Deserialize, Serialize};

/// Telemetry event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TelemetryEvent {
    pub event_type: String,
    pub declaration_hash: String,
    pub timestamp: String,
    pub data: serde_json::Value,
}

/// SnapKitty Observatory for runtime telemetry.
pub struct Observatory {
    events: Vec<TelemetryEvent>,
}

impl Observatory {
    pub fn new() -> Self {
        Self {
            events: Vec::new(),
        }
    }

    /// Record a telemetry event.
    pub fn record(&mut self, event_type: &str, declaration_hash: &str, data: serde_json::Value) {
        let event = TelemetryEvent {
            event_type: event_type.to_string(),
            declaration_hash: declaration_hash.to_string(),
            timestamp: chrono::Utc::now().to_rfc3339(),
            data,
        };
        self.events.push(event);
    }

    /// Get all events for a declaration.
    pub fn get_events(&self, declaration_hash: &str) -> Vec<&TelemetryEvent> {
        self.events
            .iter()
            .filter(|e| e.declaration_hash == declaration_hash)
            .collect()
    }

    /// Get all events.
    pub fn all_events(&self) -> &[TelemetryEvent] {
        &self.events
    }
}