98 lines
2.6 KiB
Rust
98 lines
2.6 KiB
Rust
use crate::core::models::{UnifiedData, UnifiedMonitorData};
|
|
use crate::i18n::t;
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub enum MonitorStatus {
|
|
Up,
|
|
Down,
|
|
Unknown,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct MonitorViewState {
|
|
pub id: u64,
|
|
pub name: String,
|
|
pub group_name: String,
|
|
pub status: MonitorStatus,
|
|
pub response_time: String,
|
|
pub uptime_24h: String,
|
|
pub last_check: String,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct DashboardViewState {
|
|
pub title: String,
|
|
pub descriptions: Option<String>,
|
|
pub monitors: Vec<MonitorViewState>,
|
|
pub selected_index: usize,
|
|
pub is_loading: bool,
|
|
pub error_message: Option<String>,
|
|
}
|
|
|
|
impl DashboardViewState {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
title: t("loading").to_string(),
|
|
descriptions: None,
|
|
monitors: Vec::new(),
|
|
selected_index: 0,
|
|
is_loading: true,
|
|
error_message: None,
|
|
}
|
|
}
|
|
|
|
pub fn from_unified_data(data: UnifiedData) -> Self {
|
|
let mut monitors = Vec::new();
|
|
|
|
for monitor in data.monitors {
|
|
let status = match monitor.heartbeats.last().map(|h| h.status) {
|
|
Some(1) => MonitorStatus::Up,
|
|
Some(0) => MonitorStatus::Down,
|
|
_ => MonitorStatus::Unknown,
|
|
};
|
|
|
|
let response_time = monitor
|
|
.heartbeats
|
|
.last()
|
|
.and_then(|h| h.ping)
|
|
.map(|ms| format!("{}", ms))
|
|
.unwrap_or_else(|| t("unknown").to_string());
|
|
|
|
let uptime_24h = monitor
|
|
.uptime_data
|
|
.map(|u| u.get_perc_formated())
|
|
.unwrap_or_else(|| t("unknown").to_string());
|
|
|
|
let last_check = monitor
|
|
.heartbeats
|
|
.last()
|
|
.map(|h| h.time.clone())
|
|
.unwrap_or_else(|| t("never").to_string());
|
|
|
|
monitors.push(MonitorViewState {
|
|
id: monitor.monitor_info.id,
|
|
name: monitor.monitor_info.name,
|
|
group_name: "Services".to_string(),
|
|
status,
|
|
response_time,
|
|
uptime_24h,
|
|
last_check,
|
|
});
|
|
}
|
|
|
|
monitors.sort_by_key(|m| m.name.clone());
|
|
|
|
Self {
|
|
title: data.title,
|
|
descriptions: data.description,
|
|
monitors,
|
|
selected_index: 0,
|
|
is_loading: false,
|
|
error_message: None,
|
|
}
|
|
}
|
|
|
|
pub fn get_selected_monitor(&self) -> Option<&MonitorViewState> {
|
|
self.monitors.get(self.selected_index)
|
|
}
|
|
}
|