diff --git a/src/api.rs b/src/api.rs index 5132248..f63dc4f 100644 --- a/src/api.rs +++ b/src/api.rs @@ -60,10 +60,8 @@ async fn api_status( Query(q): Query, ) -> ApiResult>> { let mut state = HashMap::new(); - let five_min_ago = (chrono::Utc::now() - chrono::Duration::minutes(5)).timestamp(); - let since = q.since.unwrap_or(five_min_ago); for (sid, name) in db.services().await? { - if let Ok(up) = db.up(sid, since).await { + if let Ok(up) = db.up(sid, q.since).await { state.insert(name, up); } } diff --git a/src/db.rs b/src/db.rs index 7b4f762..4bc0963 100644 --- a/src/db.rs +++ b/src/db.rs @@ -101,12 +101,18 @@ impl Database { } } - pub async fn up(&self, sid: i64, since: i64) -> rusqlite::Result> { + pub async fn up(&self, sid: i64, since: Option) -> rusqlite::Result> { let db = self.0.lock().await; - let mut stmt = db.prepare("SELECT value FROM events WHERE service = :sid AND time > :time ORDER BY time DESC")?; - stmt.query_row( - named_params! { ":sid": sid, ":time": since }, - |row| row.get::>(0) - ) + let (mut stmt, param) = match since { + Some(t) => ( + db.prepare("SELECT value FROM events WHERE service = :sid AND time > :time ORDER BY time DESC")?, + named_params! { ":sid": sid, ":time": t.clone() }, // TODO what's going on here? why is .clone() needed??? + ), + None => ( + db.prepare("SELECT value FROM events WHERE service = :sid ORDER BY time DESC")?, + named_params! { ":sid": sid } + ), + }; + stmt.query_row(param, |row| row.get::>(0)) } }