summaryrefslogtreecommitdiff
path: root/ofborg/tickborg/src/stats.rs
blob: 16705e6a6834dbe9cc1841407cd26028da74d85e (plain)
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
use lapin::options::BasicPublishOptions;

include!(concat!(env!("OUT_DIR"), "/events.rs"));

#[macro_use]
mod macros {
    #[macro_export]
    macro_rules! my_macro(() => (FooBar));
}

pub trait SysEvents: Send {
    fn notify(&mut self, event: Event) -> impl std::future::Future<Output = ()>;
}

#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct EventMessage {
    pub sender: String,
    pub events: Vec<Event>,
}

pub struct RabbitMq<C> {
    identity: String,
    channel: C,
}

impl RabbitMq<lapin::Channel> {
    pub fn from_lapin(identity: &str, channel: lapin::Channel) -> Self {
        RabbitMq {
            identity: identity.to_owned(),
            channel,
        }
    }
}

impl SysEvents for RabbitMq<lapin::Channel> {
    async fn notify(&mut self, event: Event) {
        let props = lapin::BasicProperties::default().with_content_type("application/json".into());
        let _confirmaton = self
            .channel
            .basic_publish(
                "stats".into(),
                "".into(),
                BasicPublishOptions::default(),
                &serde_json::to_string(&EventMessage {
                    sender: self.identity.clone(),
                    events: vec![event],
                })
                .unwrap()
                .into_bytes(),
                props,
            )
            .await
            .unwrap()
            .await
            .unwrap();
    }
}