use serde_json::json;
use tracing::debug;
use crate::error::Error;
use crate::legacy::client::LegacyClient;
fn attrs_or_default(attrs: Option<&[String]>, default: &[&str]) -> serde_json::Value {
attrs.map_or_else(|| json!(default), |custom| json!(custom))
}
impl LegacyClient {
pub async fn get_site_stats(
&self,
interval: &str,
start: Option<i64>,
end: Option<i64>,
attrs: Option<&[String]>,
) -> Result<Vec<serde_json::Value>, Error> {
let path = format!("stat/report/{interval}.site");
let url = self.site_url(&path);
debug!(interval, ?start, ?end, "fetching site stats");
let mut body = json!({
"attrs": attrs_or_default(
attrs,
&["bytes", "num_sta", "time", "wlan-num_sta", "lan-num_sta"],
),
});
if let Some(s) = start {
body["start"] = json!(s);
}
if let Some(e) = end {
body["end"] = json!(e);
}
self.post(url, &body).await
}
pub async fn get_device_stats(
&self,
interval: &str,
macs: Option<&[String]>,
attrs: Option<&[String]>,
) -> Result<Vec<serde_json::Value>, Error> {
let path = format!("stat/report/{interval}.device");
let url = self.site_url(&path);
debug!(interval, "fetching device stats");
let mut body = json!({
"attrs": attrs_or_default(attrs, &["bytes", "num_sta", "time", "rx_bytes", "tx_bytes"]),
});
if let Some(m) = macs {
body["macs"] = json!(m);
}
self.post(url, &body).await
}
pub async fn get_client_stats(
&self,
interval: &str,
macs: Option<&[String]>,
attrs: Option<&[String]>,
) -> Result<Vec<serde_json::Value>, Error> {
let path = format!("stat/report/{interval}.user");
let url = self.site_url(&path);
debug!(interval, "fetching client stats");
let mut body = json!({
"attrs": attrs_or_default(attrs, &["bytes", "time", "rx_bytes", "tx_bytes"]),
});
if let Some(m) = macs {
body["macs"] = json!(m);
}
self.post(url, &body).await
}
pub async fn get_gateway_stats(
&self,
interval: &str,
start: Option<i64>,
end: Option<i64>,
attrs: Option<&[String]>,
) -> Result<Vec<serde_json::Value>, Error> {
let path = format!("stat/report/{interval}.gw");
let url = self.site_url(&path);
debug!(interval, ?start, ?end, "fetching gateway stats");
let mut body = json!({
"attrs": attrs_or_default(
attrs,
&[
"bytes",
"time",
"wan-tx_bytes",
"wan-rx_bytes",
"lan-rx_bytes",
"lan-tx_bytes",
],
),
});
if let Some(s) = start {
body["start"] = json!(s);
}
if let Some(e) = end {
body["end"] = json!(e);
}
self.post(url, &body).await
}
pub async fn get_dpi_stats(
&self,
group_by: &str,
macs: Option<&[String]>,
) -> Result<Vec<serde_json::Value>, Error> {
let url = self.site_url("stat/sitedpi");
debug!(group_by, "fetching site DPI stats");
let mut body = json!({"type": group_by});
if let Some(m) = macs {
body["macs"] = json!(m);
}
self.post(url, &body).await
}
}