Implementing auto refresh
This commit is contained in:
parent
4f9ced3e29
commit
2819df185e
7 changed files with 27 additions and 23 deletions
|
|
@ -25,6 +25,8 @@ use std::{
|
|||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
const INITIAL_INTERVAL: u32 = 300;
|
||||
|
||||
pub struct App {
|
||||
state: DashboardViewState,
|
||||
terminal: Terminal<CrosstermBackend<io::Stdout>>,
|
||||
|
|
@ -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<()> {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ pub struct DashboardViewState {
|
|||
pub monitors: Vec<MonitorViewState>,
|
||||
pub is_loading: bool,
|
||||
pub error_message: Option<String>,
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue