ih_muse_record/
lib.rs

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
51
52
53
54
55
56
57
58
59
60
61
// crates/ih-muse-record/src/lib.rs

mod file_format;
mod file_recorder;
mod file_replayer;
pub mod prelude;

use std::collections::HashMap;

use async_trait::async_trait;
use serde::{Deserialize, Serialize};

use file_format::SerializationFormat;
pub use file_recorder::FileRecorder;
pub use file_replayer::FileReplayer;
use ih_muse_core::{time, Config, MuseResult};
use ih_muse_proto::types::*;

#[async_trait]
pub trait Recorder {
    async fn record(&mut self, event: RecordedEventWithTime) -> MuseResult<()>;
    async fn flush(&mut self) -> MuseResult<()>;
    async fn close(&mut self) -> MuseResult<()>;
}

#[async_trait]
pub trait Replayer {
    async fn next_event(&mut self) -> MuseResult<Option<RecordedEventWithTime>>;
}

#[derive(Debug, Serialize, Deserialize)]
pub enum RecordedEvent {
    MuseConfig(Config),
    ElementRegistration {
        local_elem_id: LocalElementId,
        kind_code: String,
        name: String,
        metadata: HashMap<String, String>,
        parent_id: Option<ElementId>,
    },
    SendMetric {
        local_elem_id: LocalElementId,
        metric_code: String,
        value: MetricValue,
    },
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RecordedEventWithTime {
    pub timestamp: i64,
    pub event: RecordedEvent,
}

impl RecordedEventWithTime {
    pub fn new(event: RecordedEvent) -> Self {
        Self {
            timestamp: time::utc_now_i64(),
            event,
        }
    }
}