Reorganazing files

This commit is contained in:
Marco De Araujo 2025-12-23 05:14:56 -04:00
parent 4ff103454e
commit 7270249ef6
10 changed files with 8 additions and 7 deletions

View file

@ -0,0 +1,4 @@
pub mod model;
pub mod parser;
pub use model::HeartbeatResponse;
pub use parser::parse_response;

102
src/data/heartbeat/model.rs Normal file
View file

@ -0,0 +1,102 @@
use std::collections::HashMap;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use crate::i18n::{t, t_with_args};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct HeartbeatEntry {
pub status: u8,
pub time: String,
pub msg: String,
#[serde(default)]
pub ping: Option<u64>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct MonitorHeartbeats {
pub monitor_id: u64,
pub heartbeats: Vec<HeartbeatEntry>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct UptimeData {
pub monitor_id: u64,
pub period_hours: u32,
pub uptime_percentage: f64,
}
impl UptimeData {
pub fn get_perc_formated(&self) -> String {
format!("{:.2}%", self.uptime_percentage * 100.00)
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct HeartbeatResponse {
#[serde(rename = "heartbeatList")]
pub heartbeat_list_raw: HashMap<String, Vec<HeartbeatEntry>>,
#[serde(rename = "uptimeList")]
pub uptime_list_raw: HashMap<String, f64>,
#[serde(skip)]
pub monitors: Vec<MonitorHeartbeats>,
#[serde(skip)]
pub uptime_data: Vec<UptimeData>,
}
impl HeartbeatResponse {
pub fn process(&mut self) -> Result<()> {
for (monitor_id_str, heartbeats) in &self.heartbeat_list_raw {
let monitor_id = monitor_id_str
.parse::<u64>()
.with_context(|| format!("{}", t("invalid-monitor-id")))?;
self.monitors.push(MonitorHeartbeats {
monitor_id,
heartbeats: heartbeats.clone(),
});
}
for (key, percentage) in &self.uptime_list_raw {
let parts: Vec<&str> = key.split('_').collect();
if parts.len() != 2 {
let t_args: &mut HashMap<&str, std::string::String> =
&mut HashMap::<&str, String>::new();
t_args.insert("key", key.to_string());
return Err(anyhow::anyhow!(t_with_args(
"invalid-uptime-key-format",
t_args
)));
}
let monitor_id = parts[0]
.parse::<u64>()
.with_context(|| format!("{}", t("invalid-monitor-id")))?;
let period_hours = parts[1]
.parse::<u32>()
.with_context(|| format!("{}", t("invalid-period-hours")))?;
self.uptime_data.push(UptimeData {
monitor_id,
period_hours,
uptime_percentage: *percentage,
});
}
self.monitors.sort_by_key(|m| m.monitor_id);
self.uptime_data
.sort_by_key(|u| (u.monitor_id, u.period_hours));
Ok(())
}
pub fn get_uptime(&self, monitor_id: u64, period_hours: u32) -> Option<&UptimeData> {
self.uptime_data
.iter()
.find(|u| u.monitor_id == monitor_id && u.period_hours == period_hours)
}
}

View file

@ -0,0 +1,11 @@
use super::HeartbeatResponse;
use crate::i18n::t;
use anyhow::{Context, Ok, Result};
pub fn parse_response(json_text: &str) -> Result<HeartbeatResponse> {
let mut response: HeartbeatResponse =
serde_json::from_str(json_text).with_context(|| t("invalid-json-heartbeat"))?;
response.process()?;
Ok(response)
}

View file

@ -1,2 +1,4 @@
pub mod unified;
pub use unified::unify_data;
pub use unified::model::unify_data;
pub mod heartbeat;
pub mod status_page;

View file

@ -0,0 +1,3 @@
pub mod model;
pub mod parser;
pub use parser::parse_response;

View file

@ -0,0 +1,56 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct StatusPageConfig {
pub slug: String,
pub title: String,
#[serde(default)]
pub description: Option<String>,
pub icon: String,
#[serde(rename = "autoRefreshInterval")]
pub auto_refresh_interval: u32,
pub theme: String,
pub published: bool,
#[serde(rename = "showTags")]
pub show_tags: bool,
#[serde(rename = "customCSS")]
pub custon_css: String,
#[serde(rename = "footerText", default)]
pub footer_text: Option<String>,
#[serde(rename = "showPoweredBy")]
pub show_powered_by: bool,
#[serde(rename = "googleAnalyticsId", default)]
pub google_analytics_id: Option<String>,
#[serde(rename = "showCertificateExpiry")]
pub show_certificate_expiry: bool,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct MonitorInfo {
pub id: u64,
pub name: String,
#[serde(rename = "sendUrl")]
pub send_url: u8,
#[serde(rename = "type")]
pub monitor_type: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct StatusPageGroup {
pub id: u64,
pub name: String,
pub weight: u32,
#[serde(rename = "monitorList")]
pub monitor_list: Vec<MonitorInfo>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct StatusPageResponse {
pub config: StatusPageConfig,
#[serde(skip, default)]
pub incident: Option<String>,
#[serde(rename = "publicGroupList")]
pub public_group_list: Vec<StatusPageGroup>,
#[serde(rename = "maintenanceList", skip)]
pub maintenance_list: Vec<String>,
}

View file

@ -0,0 +1,9 @@
use super::model::StatusPageResponse;
use crate::i18n::t;
use anyhow::{Context, Ok, Result};
pub fn parse_response(json_text: &str) -> Result<StatusPageResponse> {
let response: StatusPageResponse =
serde_json::from_str(json_text).with_context(|| t("invalid-json-status-page"))?;
Ok(response)
}

1
src/data/unified/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod model;

View file

@ -1,5 +1,5 @@
use crate::heartbeat::model::{HeartbeatResponse, UptimeData};
use crate::status_page::model::{MonitorInfo, StatusPageResponse};
use crate::data::heartbeat::model::{HeartbeatResponse, UptimeData};
use crate::data::status_page::model::{MonitorInfo, StatusPageResponse};
#[derive(Debug, Clone)]
pub struct UnifiedMonitorData {