Alingment
This commit is contained in:
parent
2819df185e
commit
3a87c7a011
9 changed files with 325 additions and 78 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use crate::core::models::UnifiedData;
|
||||
use crate::core::models::{UnifiedData, UnifiedGroupData};
|
||||
use crate::data::heartbeat::model::HeartbeatEntry;
|
||||
use crate::i18n::t;
|
||||
|
||||
|
|
@ -21,11 +21,17 @@ pub struct MonitorViewState {
|
|||
pub status_history: Vec<MonitorStatus>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct GroupViewState {
|
||||
pub name: String,
|
||||
pub monitors: Vec<MonitorViewState>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DashboardViewState {
|
||||
pub title: String,
|
||||
pub descriptions: Option<String>,
|
||||
pub monitors: Vec<MonitorViewState>,
|
||||
pub groups: Vec<GroupViewState>,
|
||||
pub is_loading: bool,
|
||||
pub error_message: Option<String>,
|
||||
pub auto_refresh_interval: u32,
|
||||
|
|
@ -36,7 +42,7 @@ impl DashboardViewState {
|
|||
Self {
|
||||
title: t("loading").to_string(),
|
||||
descriptions: None,
|
||||
monitors: Vec::new(),
|
||||
groups: Vec::new(),
|
||||
is_loading: true,
|
||||
error_message: None,
|
||||
auto_refresh_interval: 300,
|
||||
|
|
@ -44,58 +50,28 @@ impl DashboardViewState {
|
|||
}
|
||||
|
||||
pub fn from_unified_data(data: UnifiedData) -> Self {
|
||||
let mut monitors = Vec::new();
|
||||
let mut groups = Vec::new();
|
||||
|
||||
for monitor in data.monitors {
|
||||
let status_history = get_status_history(&monitor.heartbeats);
|
||||
|
||||
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,
|
||||
status_history,
|
||||
for group in data.groups {
|
||||
groups.push(GroupViewState {
|
||||
name: group.group_info.name.clone(),
|
||||
monitors: add_monitor_view_state(group),
|
||||
});
|
||||
}
|
||||
|
||||
monitors.sort_by_key(|m| m.name.clone());
|
||||
|
||||
Self {
|
||||
title: data.title,
|
||||
descriptions: data.description,
|
||||
monitors,
|
||||
groups,
|
||||
is_loading: false,
|
||||
error_message: None,
|
||||
auto_refresh_interval: data.audo_refresh_interval,
|
||||
auto_refresh_interval: data.auto_refresh_interval.max(30),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_all_monitors(&self) -> Vec<&MonitorViewState> {
|
||||
self.groups.iter().flat_map(|g| g.monitors.iter()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
fn get_status_history(heartbeats: &[HeartbeatEntry]) -> Vec<MonitorStatus> {
|
||||
|
|
@ -117,3 +93,50 @@ fn get_status_history(heartbeats: &[HeartbeatEntry]) -> Vec<MonitorStatus> {
|
|||
history.reverse();
|
||||
history
|
||||
}
|
||||
|
||||
fn add_monitor_view_state(group: UnifiedGroupData) -> Vec<MonitorViewState> {
|
||||
let mut monitors = Vec::new();
|
||||
|
||||
for monitor in group.monitors {
|
||||
let status_history = get_status_history(&monitor.heartbeats);
|
||||
|
||||
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: group.group_info.name.clone(),
|
||||
status,
|
||||
response_time,
|
||||
uptime_24h,
|
||||
last_check,
|
||||
status_history,
|
||||
});
|
||||
}
|
||||
|
||||
monitors.sort_by_key(|m| m.name.clone());
|
||||
|
||||
monitors
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue