70 lines
2.2 KiB
Rust
70 lines
2.2 KiB
Rust
use std::borrow::Cow;
|
|
use std::collections::HashMap;
|
|
|
|
use crate::core::models::{UnifiedData, UnifiedGroupData, UnifiedMonitorData};
|
|
use crate::data::{
|
|
heartbeat::model::{HeartbeatEntry, HeartbeatResponse, UptimeData},
|
|
status_page::model::StatusPageResponse,
|
|
};
|
|
|
|
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 mut heartbeat_map: HashMap<u64, &'a [HeartbeatEntry]> =
|
|
HashMap::with_capacity(heartbeat.monitors.len());
|
|
heartbeat_map.extend(
|
|
heartbeat
|
|
.monitors
|
|
.iter()
|
|
.map(|m| (m.monitor_id, &m.heartbeats[..])),
|
|
);
|
|
|
|
let mut uptime_map: HashMap<(u64, u32), &'a UptimeData> =
|
|
HashMap::with_capacity(heartbeat.uptime_data.len());
|
|
uptime_map.extend(
|
|
heartbeat
|
|
.uptime_data
|
|
.iter()
|
|
.map(|u| ((u.monitor_id, u.period_hours), u)),
|
|
);
|
|
|
|
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)).copied();
|
|
let heartbeats = heartbeat_map.get(&monitor_info.id).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,
|
|
heartbeats,
|
|
uptime_data,
|
|
});
|
|
}
|
|
|
|
monitors.sort_by_key(|m| m.id);
|
|
|
|
groups.push(UnifiedGroupData {
|
|
group_info: group,
|
|
monitors,
|
|
});
|
|
}
|
|
|
|
groups.sort_by_key(|g| g.group_info.weight);
|
|
|
|
UnifiedData {
|
|
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,
|
|
}
|
|
}
|