diff --git a/src/core/data.rs b/src/core/data.rs index 9f124e2..8c8e069 100644 --- a/src/core/data.rs +++ b/src/core/data.rs @@ -30,5 +30,6 @@ pub fn unify_data(status_page: &StatusPageResponse, heartbeat: &HeartbeatRespons title: status_page.config.title.clone(), description: status_page.config.description.clone(), monitors, + audo_refresh_interval: status_page.config.auto_refresh_interval, } } diff --git a/src/core/models.rs b/src/core/models.rs index eeb3dc9..e8ca173 100644 --- a/src/core/models.rs +++ b/src/core/models.rs @@ -13,4 +13,5 @@ pub struct UnifiedData { pub title: String, pub description: Option, pub monitors: Vec, + pub audo_refresh_interval: u32, } diff --git a/src/locales/en-US/main.ftl b/src/locales/en-US/main.ftl index 7f587e3..5de5334 100644 --- a/src/locales/en-US/main.ftl +++ b/src/locales/en-US/main.ftl @@ -23,3 +23,5 @@ update-fail = Failed to update data: {error} now = Now uptime = Uptime history = History +auto-update-enabled = Auto-update enabled ({interval} min) +update-failed = Update failed: {error} diff --git a/src/locales/pt-BR/main.ftl b/src/locales/pt-BR/main.ftl index 459b8be..0ecaf91 100644 --- a/src/locales/pt-BR/main.ftl +++ b/src/locales/pt-BR/main.ftl @@ -23,3 +23,5 @@ auto-update-failed = Falha na atualização automática update-fail = Falha ao atualizar dados now = Agora history = Historico +auto-update-enabled = Auto-atualização ativada ({interval} min) +update-failed = Falha na atualização: {error} diff --git a/src/ui/app.rs b/src/ui/app.rs index 144728c..ae1b7f8 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -25,6 +25,8 @@ use std::{ time::{Duration, Instant}, }; +const INITIAL_INTERVAL: u32 = 300; + pub struct App { state: DashboardViewState, terminal: Terminal>, @@ -157,11 +159,15 @@ impl App { self.state.is_loading = true; self.render(); - if let Err(e) = self.fetch_and_update_data() { - let mut error = HashMap::new(); - error.insert("error", e.to_string()); - - self.state.error_message = Some(t_with_args("update-fail", &error)) + match self.fetch_and_update_data() { + Ok(()) => { + self.update_interval = Duration::from_secs(self.state.auto_refresh_interval as u64) + } + Err(e) => { + let mut error = HashMap::new(); + error.insert("error", e.to_string()); + self.state.error_message = Some(t_with_args("update-fail", &error)); + } } self.state.is_loading = false; @@ -175,12 +181,14 @@ impl App { 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: Duration::from_secs(30), + update_interval: initial_interval, endpoints, client: UptimeKumaClient::new(), }) @@ -195,7 +203,9 @@ impl App { } fn load_initial_data(&mut self) -> io::Result<()> { - self.refresh_data() + self.refresh_data()?; + self.update_interval = Duration::from_secs(self.state.auto_refresh_interval as u64); + Ok(()) } pub fn run(&mut self) -> io::Result<()> { diff --git a/src/ui/components/monitor_list.rs b/src/ui/components/monitor_list.rs index 9de4fdb..3b0b7c8 100644 --- a/src/ui/components/monitor_list.rs +++ b/src/ui/components/monitor_list.rs @@ -30,7 +30,7 @@ pub fn render_monitor_list(frame: &mut Frame, area: Rect, state: &DashboardViewS Constraint::Length(MAX_NAME_LENGTH as u16), Constraint::Length(10), Constraint::Length(10), - Constraint::Length(STATUS_LINE_LENGTH as u16 + 16), + Constraint::Length(STATUS_LINE_LENGTH as u16 + 8), ]; let available_height = area.height.saturating_sub(2); @@ -118,26 +118,11 @@ fn create_status_line_spans(status_history: &[MonitorStatus]) -> Line<'_> { .take(STATUS_LINE_LENGTH) .collect(); - spans.push(Span::styled( - "(1h)", - Style::default() - .fg(Color::Gray) - .add_modifier(Modifier::ITALIC), - )); - spans.push(Span::raw(" ")); - for status in recent_status.iter().rev() { let c = get_status_char(status); let color = get_status_color(status); spans.push(Span::styled(c.to_string(), Style::default().fg(color))); } - spans.push(Span::raw(" ")); - spans.push(Span::styled( - t("now"), - Style::default() - .fg(Color::Gray) - .add_modifier(Modifier::ITALIC), - )); Line::from(spans) } diff --git a/src/ui/dashboard/model.rs b/src/ui/dashboard/model.rs index bab3b78..3e1ec7e 100644 --- a/src/ui/dashboard/model.rs +++ b/src/ui/dashboard/model.rs @@ -28,6 +28,7 @@ pub struct DashboardViewState { pub monitors: Vec, pub is_loading: bool, pub error_message: Option, + pub auto_refresh_interval: u32, } impl DashboardViewState { @@ -38,6 +39,7 @@ impl DashboardViewState { monitors: Vec::new(), is_loading: true, error_message: None, + auto_refresh_interval: 300, } } @@ -91,6 +93,7 @@ impl DashboardViewState { monitors, is_loading: false, error_message: None, + auto_refresh_interval: data.audo_refresh_interval, } } }