Skip to main content

unifly_api/legacy/
events.rs

1// Legacy API event and alarm endpoints
2//
3// Events (stat/event) and alarms (stat/alarm) with archive support
4// via cmd/evtmgr.
5
6use serde_json::json;
7use tracing::debug;
8
9use crate::error::Error;
10use crate::legacy::client::LegacyClient;
11use crate::legacy::models::{LegacyAlarm, LegacyEvent};
12
13impl LegacyClient {
14    /// List recent events.
15    ///
16    /// `GET /api/s/{site}/stat/event`
17    ///
18    /// If `count` is provided, limits the number of events returned
19    /// by appending `?_limit={count}` to the request.
20    pub async fn list_events(&self, count: Option<u32>) -> Result<Vec<LegacyEvent>, Error> {
21        let path = match count {
22            Some(n) => format!("stat/event?_limit={n}"),
23            None => "stat/event".to_string(),
24        };
25        let url = self.site_url(&path);
26        debug!(?count, "listing events");
27        self.get(url).await
28    }
29
30    /// List active alarms.
31    ///
32    /// `GET /api/s/{site}/stat/alarm`
33    pub async fn list_alarms(&self) -> Result<Vec<LegacyAlarm>, Error> {
34        let url = self.site_url("stat/alarm");
35        debug!("listing alarms");
36        self.get(url).await
37    }
38
39    /// Archive (acknowledge) a specific alarm by its ID.
40    ///
41    /// `POST /api/s/{site}/cmd/evtmgr` with `{"cmd": "archive-alarm", "_id": "..."}`
42    pub async fn archive_alarm(&self, id: &str) -> Result<(), Error> {
43        let url = self.site_url("cmd/evtmgr");
44        debug!(id, "archiving alarm");
45        let _: Vec<serde_json::Value> = self
46            .post(
47                url,
48                &json!({
49                    "cmd": "archive-alarm",
50                    "_id": id,
51                }),
52            )
53            .await?;
54        Ok(())
55    }
56
57    /// Archive all alarms for the active site.
58    ///
59    /// `POST /api/s/{site}/cmd/evtmgr` with `{"cmd": "archive-all-alarms"}`
60    pub async fn archive_all_alarms(&self) -> Result<(), Error> {
61        let url = self.site_url("cmd/evtmgr");
62        debug!("archiving all alarms");
63        let _: Vec<serde_json::Value> = self
64            .post(
65                url,
66                &json!({
67                    "cmd": "archive-all-alarms",
68                }),
69            )
70            .await?;
71        Ok(())
72    }
73}