optimizations
This commit is contained in:
parent
64f5115d54
commit
99d4b94e5c
6 changed files with 54 additions and 48 deletions
|
|
@ -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
|
|
||||||
.monitors
|
|
||||||
.iter()
|
|
||||||
.map(|m| (m.monitor_id, &m.heartbeats[..]))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let uptime_map: HashMap<(u64, u32), &'a UptimeData> = heartbeat
|
let mut heartbeat_map: HashMap<u64, &'a [HeartbeatEntry]> =
|
||||||
.uptime_data
|
HashMap::with_capacity(heartbeat.monitors.len());
|
||||||
.iter()
|
heartbeat_map.extend(
|
||||||
.map(|u| ((u.monitor_id, u.period_hours), u))
|
heartbeat
|
||||||
.collect();
|
.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 {
|
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())
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
.iter()
|
spans.extend(
|
||||||
.rev()
|
status_history
|
||||||
.take(STATUS_LINE_LENGTH)
|
.iter()
|
||||||
.map(|status| get_status_span(status).clone())
|
.rev()
|
||||||
.collect();
|
.take(STATUS_LINE_LENGTH)
|
||||||
|
.map(|status| get_status_span(status).clone()),
|
||||||
|
);
|
||||||
|
|
||||||
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);
|
||||||
|
|
|
||||||
|
|
@ -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()
|
0 => MonitorStatus::Down,
|
||||||
.take(100)
|
1 => MonitorStatus::Up,
|
||||||
.map(|h| match h.status {
|
_ => MonitorStatus::Unknown,
|
||||||
0 => MonitorStatus::Down,
|
}));
|
||||||
1 => MonitorStatus::Up,
|
|
||||||
_ => 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 {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue