Refactor
This commit is contained in:
parent
70a70af859
commit
00cb8f7a9b
4 changed files with 41 additions and 27 deletions
|
|
@ -1,3 +1,4 @@
|
|||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::core::models::{UnifiedData, UnifiedGroupData, UnifiedMonitorData};
|
||||
|
|
@ -6,32 +7,38 @@ use crate::data::{
|
|||
status_page::model::StatusPageResponse,
|
||||
};
|
||||
|
||||
pub fn unify_data(status_page: &StatusPageResponse, heartbeat: &HeartbeatResponse) -> UnifiedData {
|
||||
pub fn unify_data<'a>(status_page: &'a StatusPageResponse, heartbeat: &'a HeartbeatResponse) -> UnifiedData<'a> {
|
||||
let mut groups = Vec::with_capacity(status_page.public_group_list.len());
|
||||
let heartbeat_map: HashMap<u64, Vec<HeartbeatEntry>> = heartbeat
|
||||
let heartbeat_map: HashMap<u64, &'a [HeartbeatEntry]> = heartbeat
|
||||
.monitors
|
||||
.iter()
|
||||
.map(|m| (m.monitor_id, m.heartbeats.clone()))
|
||||
.map(|m| (m.monitor_id, &m.heartbeats[..]))
|
||||
.collect();
|
||||
|
||||
let uptime_map: HashMap<(u64, u32), UptimeData> = heartbeat
|
||||
let uptime_map: HashMap<(u64, u32), &'a UptimeData> = heartbeat
|
||||
.uptime_data
|
||||
.iter()
|
||||
.map(|u| ((u.monitor_id, u.period_hours), u.clone()))
|
||||
.map(|u| ((u.monitor_id, u.period_hours), u))
|
||||
.collect();
|
||||
|
||||
for group in &status_page.public_group_list {
|
||||
let mut monitors = Vec::with_capacity(group.monitor_list.len());
|
||||
for monitor_info in &group.monitor_list {
|
||||
let uptime_data = uptime_map.get(&(monitor_info.id, 24)).cloned();
|
||||
let uptime_data = uptime_map.get(&(monitor_info.id, 24)).copied();
|
||||
let heartbeats = heartbeat_map
|
||||
.get(&monitor_info.id)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
.copied()
|
||||
.unwrap_or(&[]);
|
||||
|
||||
let name: Cow<'a, str> = if monitor_info.name.len() > 100 {
|
||||
Cow::Owned(monitor_info.name.clone())
|
||||
} else {
|
||||
Cow::Borrowed(monitor_info.name.as_str())
|
||||
};
|
||||
|
||||
monitors.push(UnifiedMonitorData {
|
||||
id: monitor_info.id,
|
||||
name: monitor_info.name.clone(),
|
||||
name,
|
||||
heartbeats,
|
||||
uptime_data,
|
||||
});
|
||||
|
|
@ -40,7 +47,7 @@ pub fn unify_data(status_page: &StatusPageResponse, heartbeat: &HeartbeatRespons
|
|||
monitors.sort_by_key(|m| m.id);
|
||||
|
||||
groups.push(UnifiedGroupData {
|
||||
group_info: group.clone(),
|
||||
group_info: group,
|
||||
monitors,
|
||||
});
|
||||
}
|
||||
|
|
@ -48,8 +55,8 @@ pub fn unify_data(status_page: &StatusPageResponse, heartbeat: &HeartbeatRespons
|
|||
groups.sort_by_key(|g| g.group_info.weight);
|
||||
|
||||
UnifiedData {
|
||||
title: status_page.config.title.clone(),
|
||||
description: status_page.config.description.clone(),
|
||||
title: Cow::Borrowed(&status_page.config.title),
|
||||
description: status_page.config.description.as_deref().map(Cow::Borrowed),
|
||||
groups,
|
||||
auto_refresh_interval: status_page.config.auto_refresh_interval,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,26 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use crate::data::heartbeat::model::{HeartbeatEntry, UptimeData};
|
||||
use crate::data::status_page::model::{StatusPageGroup};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UnifiedMonitorData {
|
||||
pub struct UnifiedMonitorData<'a> {
|
||||
pub id: u64,
|
||||
pub name: String,
|
||||
pub heartbeats: Vec<HeartbeatEntry>,
|
||||
pub uptime_data: Option<UptimeData>,
|
||||
pub name: Cow<'a, str>,
|
||||
pub heartbeats: &'a [HeartbeatEntry],
|
||||
pub uptime_data: Option<&'a UptimeData>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UnifiedData {
|
||||
pub title: String,
|
||||
pub description: Option<String>,
|
||||
pub struct UnifiedData<'a> {
|
||||
pub title: Cow<'a, str>,
|
||||
pub description: Option<Cow<'a, str>>,
|
||||
pub auto_refresh_interval: u32,
|
||||
pub groups: Vec<UnifiedGroupData>,
|
||||
pub groups: Vec<UnifiedGroupData<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UnifiedGroupData {
|
||||
pub group_info: StatusPageGroup,
|
||||
pub monitors: Vec<UnifiedMonitorData>,
|
||||
pub struct UnifiedGroupData<'a> {
|
||||
pub group_info: &'a StatusPageGroup,
|
||||
pub monitors: Vec<UnifiedMonitorData<'a>>,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue