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<()> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue