optimizations

This commit is contained in:
Marco De Araujo 2026-01-20 16:18:54 -04:00
parent 64f5115d54
commit 99d4b94e5c
6 changed files with 54 additions and 48 deletions

View file

@ -7,28 +7,35 @@ use crate::data::{
status_page::model::StatusPageResponse, status_page::model::StatusPageResponse,
}; };
pub fn unify_data<'a>(status_page: &'a StatusPageResponse, heartbeat: &'a HeartbeatResponse) -> UnifiedData<'a> { 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 groups = Vec::with_capacity(status_page.public_group_list.len());
let heartbeat_map: HashMap<u64, &'a [HeartbeatEntry]> = heartbeat
let mut heartbeat_map: HashMap<u64, &'a [HeartbeatEntry]> =
HashMap::with_capacity(heartbeat.monitors.len());
heartbeat_map.extend(
heartbeat
.monitors .monitors
.iter() .iter()
.map(|m| (m.monitor_id, &m.heartbeats[..])) .map(|m| (m.monitor_id, &m.heartbeats[..])),
.collect(); );
let uptime_map: HashMap<(u64, u32), &'a UptimeData> = heartbeat let mut uptime_map: HashMap<(u64, u32), &'a UptimeData> =
HashMap::with_capacity(heartbeat.uptime_data.len());
uptime_map.extend(
heartbeat
.uptime_data .uptime_data
.iter() .iter()
.map(|u| ((u.monitor_id, u.period_hours), u)) .map(|u| ((u.monitor_id, u.period_hours), u)),
.collect(); );
for group in &status_page.public_group_list { for group in &status_page.public_group_list {
let mut monitors = Vec::with_capacity(group.monitor_list.len()); let mut monitors = Vec::with_capacity(group.monitor_list.len());
for monitor_info in &group.monitor_list { for monitor_info in &group.monitor_list {
let uptime_data = uptime_map.get(&(monitor_info.id, 24)).copied(); let uptime_data = uptime_map.get(&(monitor_info.id, 24)).copied();
let heartbeats = heartbeat_map let heartbeats = heartbeat_map.get(&monitor_info.id).copied().unwrap_or(&[]);
.get(&monitor_info.id)
.copied()
.unwrap_or(&[]);
let name: Cow<'a, str> = if monitor_info.name.len() > 100 { let name: Cow<'a, str> = if monitor_info.name.len() > 100 {
Cow::Owned(monitor_info.name.clone()) Cow::Owned(monitor_info.name.clone())

View file

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

View file

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

View file

@ -30,6 +30,8 @@ use std::{
}; };
const INITIAL_INTERVAL: u32 = 300; const INITIAL_INTERVAL: u32 = 300;
const MAIN_LAYOUT_WITH_SCROLLBAR: [Constraint; 2] = [Constraint::Min(1), Constraint::Length(1)];
const MAIN_LAYOUT_WITHOUT_SCROLLBAR: [Constraint; 1] = [Constraint::Min(1)];
enum FetchResult { enum FetchResult {
Heartbeat(Result<HeartbeatResponse>), Heartbeat(Result<HeartbeatResponse>),
@ -134,12 +136,11 @@ impl App {
render_header(frame, chunks[0], &self.state); render_header(frame, chunks[0], &self.state);
let mut main_constraint = Vec::with_capacity(2); let main_constraint = if self.state.show_vertical_scrollbar(chunks[1].height) {
main_constraint.push(Constraint::Min(1)); &MAIN_LAYOUT_WITH_SCROLLBAR[..]
} else {
if self.state.show_vertical_scrollbar(chunks[1].height) { &MAIN_LAYOUT_WITHOUT_SCROLLBAR[..]
main_constraint.push(Constraint::Length(1)); };
}
let main_chunks = Layout::default() let main_chunks = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)

View file

@ -137,11 +137,10 @@ fn render_monitor_table(
let header = Row::new(header_cells).style(title_style()).height(1); let header = Row::new(header_cells).style(title_style()).height(1);
let rows: Vec<Row> = monitors let mut rows: Vec<Row> = Vec::with_capacity(items_to_show);
.iter() for monitor in monitors.iter().take(items_to_show) {
.take(items_to_show) rows.push(create_monitor_item(monitor));
.map(|monitor| create_monitor_item(monitor)) }
.collect();
let widths = vec![ let widths = vec![
Constraint::Length(3), Constraint::Length(3),
@ -256,17 +255,20 @@ fn get_cached_status_line(status_history: &[MonitorStatus]) -> Line<'static> {
} }
} }
let spans: Vec<Span<'static>> = status_history let mut spans: Vec<Span<'static>> = Vec::with_capacity(STATUS_LINE_LENGTH);
spans.extend(
status_history
.iter() .iter()
.rev() .rev()
.take(STATUS_LINE_LENGTH) .take(STATUS_LINE_LENGTH)
.map(|status| get_status_span(status).clone()) .map(|status| get_status_span(status).clone()),
.collect(); );
let new_line = Line::from(spans); let new_line = Line::from(spans);
let mut write = cache.write().unwrap(); let mut write = cache.write().unwrap();
if write.len() > 1000 { if write.len() > 1000 {
let keys_to_remove: Vec<_> = write.keys().take(250).copied().collect(); let mut keys_to_remove: Vec<u64> = Vec::with_capacity(250);
keys_to_remove.extend(write.keys().take(250).copied());
for key in keys_to_remove { for key in keys_to_remove {
write.remove(&key); write.remove(&key);

View file

@ -95,16 +95,12 @@ impl DashboardViewState {
} }
fn get_status_history(heartbeats: &[HeartbeatEntry]) -> Vec<MonitorStatus> { fn get_status_history(heartbeats: &[HeartbeatEntry]) -> Vec<MonitorStatus> {
let mut history = heartbeats let mut history: Vec<_> = Vec::with_capacity(heartbeats.len());
.iter() history.extend(heartbeats.iter().rev().take(100).map(|h| match h.status {
.rev()
.take(100)
.map(|h| match h.status {
0 => MonitorStatus::Down, 0 => MonitorStatus::Down,
1 => MonitorStatus::Up, 1 => MonitorStatus::Up,
_ => MonitorStatus::Unknown, _ => MonitorStatus::Unknown,
}) }));
.collect::<Vec<_>>();
while history.len() < 100 { while history.len() < 100 {
history.push(MonitorStatus::Unknown); history.push(MonitorStatus::Unknown);
@ -142,7 +138,7 @@ fn add_monitor_view_state(group: UnifiedGroupData) -> Vec<MonitorViewState> {
let name: Cow<'static, str> = match monitor.name { let name: Cow<'static, str> = match monitor.name {
Cow::Borrowed(borrowed) => Cow::Owned(borrowed.to_string()), Cow::Borrowed(borrowed) => Cow::Owned(borrowed.to_string()),
Cow::Owned(owned) => Cow::Owned(owned) Cow::Owned(owned) => Cow::Owned(owned),
}; };
MonitorViewState { MonitorViewState {