From 17662107b07e31b7c23d5a8bca9bb8af5aecc728 Mon Sep 17 00:00:00 2001 From: Marco De Araujo Date: Wed, 21 Jan 2026 12:34:50 -0400 Subject: [PATCH] Refactoring --- src/api/client.rs | 7 ++-- src/ui/app.rs | 81 ++++++++++++++--------------------------------- 2 files changed, 28 insertions(+), 60 deletions(-) diff --git a/src/api/client.rs b/src/api/client.rs index eb3032f..6dc4448 100644 --- a/src/api/client.rs +++ b/src/api/client.rs @@ -1,9 +1,11 @@ +use std::time::Duration; + use anyhow::Result; use reqwest::blocking::Client; use crate::{ api::endpoints::UptimeKumaEndpoints, - data::{self, heartbeat::HeartbeatResponse, status_page::model::StatusPageResponse}, + data::{self, heartbeat::HeartbeatResponse, status_page::model::StatusPageResponse}, i18n::t, }; #[derive(Debug, Clone)] @@ -13,8 +15,9 @@ pub struct UptimeKumaClient { impl UptimeKumaClient { pub fn new() -> Self { + let client = Client::builder().connect_timeout(Duration::from_secs(10)).timeout(Duration::from_secs(30)).build().unwrap_or_else(|_| panic!("{}", t("http-build-error"))); Self { - client: Client::new(), + client, } } diff --git a/src/ui/app.rs b/src/ui/app.rs index a2abab4..0dc9c9c 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -1,12 +1,10 @@ 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, render_header, render_monitor_list}, dashboard::model::DashboardViewState, }; -use anyhow::Result; use crossterm::{ event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind}, execute, @@ -24,8 +22,6 @@ use ratatui::{ use std::{ collections::HashMap, io, - sync::mpsc, - thread, time::{Duration, Instant}, }; @@ -33,11 +29,6 @@ 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), - StatusPage(Result), -} - pub struct App { state: DashboardViewState, terminal: Terminal>, @@ -49,6 +40,26 @@ pub struct App { } impl App { + pub fn new(endpoints: UptimeKumaEndpoints) -> io::Result { + let backend = CrosstermBackend::new(io::stdout()); + let mut terminal = Terminal::new(backend)?; + terminal.hide_cursor()?; + + let state = DashboardViewState::new(); + + let initial_interval = Duration::from_secs(INITIAL_INTERVAL as u64); + + Ok(Self { + state, + terminal, + should_quit: false, + last_update: Instant::now(), + update_interval: initial_interval, + endpoints, + client: UptimeKumaClient::new(), + }) + } + fn setup_terminal(&mut self) -> io::Result<()> { enable_raw_mode()?; execute!(io::stdout(), EnterAlternateScreen, EnableMouseCapture)?; @@ -248,57 +259,11 @@ impl App { Ok(()) } - pub fn new(endpoints: UptimeKumaEndpoints) -> io::Result { - let backend = CrosstermBackend::new(io::stdout()); - let mut terminal = Terminal::new(backend)?; - terminal.hide_cursor()?; - - let state = DashboardViewState::new(); - - let initial_interval = Duration::from_secs(INITIAL_INTERVAL as u64); - - Ok(Self { - state, - terminal, - should_quit: false, - last_update: Instant::now(), - update_interval: initial_interval, - endpoints, - client: UptimeKumaClient::new(), - }) - } - fn fetch_and_update_data(&mut self) -> anyhow::Result<()> { - let (tx, rx) = mpsc::channel(); + let heartbeat_result = self.client.fetch_heartbeat(&self.endpoints)?; + let status_page_result = self.client.fetch_status_page(&self.endpoints)?; - 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); + let unified_data = core::unify_data(&status_page_result, &heartbeat_result); self.state = DashboardViewState::from_unified_data(unified_data); Ok(()) }