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,
|
||||
};
|
||||
|
||||
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 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
|
||||
.iter()
|
||||
.map(|m| (m.monitor_id, &m.heartbeats[..]))
|
||||
.collect();
|
||||
.map(|m| (m.monitor_id, &m.heartbeats[..])),
|
||||
);
|
||||
|
||||
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
|
||||
.iter()
|
||||
.map(|u| ((u.monitor_id, u.period_hours), u))
|
||||
.collect();
|
||||
.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 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())
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ use crate::i18n::t;
|
|||
use anyhow::{Context, Ok, Result};
|
||||
|
||||
pub fn parse_response(json_text: &str) -> Result<HeartbeatResponse> {
|
||||
let mut response: HeartbeatResponse =
|
||||
serde_json::from_str(json_text).with_context(|| t("invalid-json-heartbeat"))?;
|
||||
let mut response: HeartbeatResponse = serde_json::from_slice(json_text.as_bytes())
|
||||
.with_context(|| t("invalid-json-heartbeat"))?;
|
||||
|
||||
response.process()?;
|
||||
Ok(response)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use crate::i18n::t;
|
|||
use anyhow::{Context, Ok, Result};
|
||||
|
||||
pub fn parse_response(json_text: &str) -> Result<StatusPageResponse> {
|
||||
let response: StatusPageResponse =
|
||||
serde_json::from_str(json_text).with_context(|| t("invalid-json-status-page"))?;
|
||||
let response: StatusPageResponse = serde_json::from_slice(json_text.as_bytes())
|
||||
.with_context(|| t("invalid-json-status-page"))?;
|
||||
Ok(response)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,8 @@ use std::{
|
|||
};
|
||||
|
||||
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 {
|
||||
Heartbeat(Result<HeartbeatResponse>),
|
||||
|
|
@ -134,12 +136,11 @@ impl App {
|
|||
|
||||
render_header(frame, chunks[0], &self.state);
|
||||
|
||||
let mut main_constraint = Vec::with_capacity(2);
|
||||
main_constraint.push(Constraint::Min(1));
|
||||
|
||||
if self.state.show_vertical_scrollbar(chunks[1].height) {
|
||||
main_constraint.push(Constraint::Length(1));
|
||||
}
|
||||
let main_constraint = if self.state.show_vertical_scrollbar(chunks[1].height) {
|
||||
&MAIN_LAYOUT_WITH_SCROLLBAR[..]
|
||||
} else {
|
||||
&MAIN_LAYOUT_WITHOUT_SCROLLBAR[..]
|
||||
};
|
||||
|
||||
let main_chunks = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
|
|
|
|||
|
|
@ -137,11 +137,10 @@ fn render_monitor_table(
|
|||
|
||||
let header = Row::new(header_cells).style(title_style()).height(1);
|
||||
|
||||
let rows: Vec<Row> = monitors
|
||||
.iter()
|
||||
.take(items_to_show)
|
||||
.map(|monitor| create_monitor_item(monitor))
|
||||
.collect();
|
||||
let mut rows: Vec<Row> = Vec::with_capacity(items_to_show);
|
||||
for monitor in monitors.iter().take(items_to_show) {
|
||||
rows.push(create_monitor_item(monitor));
|
||||
}
|
||||
|
||||
let widths = vec![
|
||||
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()
|
||||
.rev()
|
||||
.take(STATUS_LINE_LENGTH)
|
||||
.map(|status| get_status_span(status).clone())
|
||||
.collect();
|
||||
.map(|status| get_status_span(status).clone()),
|
||||
);
|
||||
|
||||
let new_line = Line::from(spans);
|
||||
let mut write = cache.write().unwrap();
|
||||
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 {
|
||||
write.remove(&key);
|
||||
|
|
|
|||
|
|
@ -95,16 +95,12 @@ impl DashboardViewState {
|
|||
}
|
||||
|
||||
fn get_status_history(heartbeats: &[HeartbeatEntry]) -> Vec<MonitorStatus> {
|
||||
let mut history = heartbeats
|
||||
.iter()
|
||||
.rev()
|
||||
.take(100)
|
||||
.map(|h| match h.status {
|
||||
let mut history: Vec<_> = Vec::with_capacity(heartbeats.len());
|
||||
history.extend(heartbeats.iter().rev().take(100).map(|h| match h.status {
|
||||
0 => MonitorStatus::Down,
|
||||
1 => MonitorStatus::Up,
|
||||
_ => MonitorStatus::Unknown,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
}));
|
||||
|
||||
while history.len() < 100 {
|
||||
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 {
|
||||
Cow::Borrowed(borrowed) => Cow::Owned(borrowed.to_string()),
|
||||
Cow::Owned(owned) => Cow::Owned(owned)
|
||||
Cow::Owned(owned) => Cow::Owned(owned),
|
||||
};
|
||||
|
||||
MonitorViewState {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue