Adding paralel request
This commit is contained in:
parent
21584f4edf
commit
0de1f9e083
2 changed files with 40 additions and 3 deletions
|
|
@ -1,11 +1,13 @@
|
|||
use crate::api::{UptimeKumaClient, UptimeKumaEndpoints};
|
||||
use crate::core;
|
||||
use crate::data::{heartbeat::HeartbeatResponse, status_page::model::StatusPageResponse};
|
||||
use crate::i18n::{t, t_with_args};
|
||||
use crate::ui::components::render_footer;
|
||||
use crate::ui::{
|
||||
components::{render_header, render_monitor_list},
|
||||
dashboard::model::DashboardViewState,
|
||||
};
|
||||
use anyhow::Result;
|
||||
use crossterm::{
|
||||
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
|
||||
execute,
|
||||
|
|
@ -20,14 +22,21 @@ use ratatui::{
|
|||
widgets::{Block, Borders, Padding, Paragraph},
|
||||
};
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::{
|
||||
io,
|
||||
thread,
|
||||
sync::mpsc,
|
||||
collections::HashMap,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
const INITIAL_INTERVAL: u32 = 300;
|
||||
|
||||
enum FetchResult {
|
||||
Heartbeat(Result<HeartbeatResponse>),
|
||||
StatusPage(Result<StatusPageResponse>),
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
state: DashboardViewState,
|
||||
terminal: Terminal<CrosstermBackend<io::Stdout>>,
|
||||
|
|
@ -201,8 +210,35 @@ impl App {
|
|||
}
|
||||
|
||||
fn fetch_and_update_data(&mut self) -> anyhow::Result<()> {
|
||||
let heartbeat_data = self.client.fetch_heartbeat(&self.endpoints)?;
|
||||
let status_page_data = self.client.fetch_status_page(&self.endpoints)?;
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
let heartbeat_client = self.client.clone();
|
||||
let heartbeat_endpoints = self.endpoints.clone();
|
||||
let tx_clone = tx.clone();
|
||||
thread::spawn(move || {
|
||||
let result = heartbeat_client.fetch_heartbeat(&heartbeat_endpoints);
|
||||
tx.send(FetchResult::Heartbeat(result)).unwrap();
|
||||
});
|
||||
|
||||
let status_page_client = self.client.clone();
|
||||
let status_page_endpoints = self.endpoints.clone();
|
||||
|
||||
thread::spawn(move || {
|
||||
let result = status_page_client.fetch_status_page(&status_page_endpoints);
|
||||
tx_clone.send(FetchResult::StatusPage(result)).unwrap();
|
||||
});
|
||||
|
||||
let mut heartbeat_result = None;
|
||||
let mut status_page_result = None;
|
||||
|
||||
for _ in 0..2 {
|
||||
match rx.recv()? {
|
||||
FetchResult::Heartbeat(result) => heartbeat_result = Some(result?),
|
||||
FetchResult::StatusPage(result) => status_page_result = Some(result?),
|
||||
}
|
||||
}
|
||||
let heartbeat_data = heartbeat_result.unwrap();
|
||||
let status_page_data = status_page_result.unwrap();
|
||||
let unified_data = core::unify_data(&status_page_data, &heartbeat_data);
|
||||
self.state = DashboardViewState::from_unified_data(unified_data);
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue