feat: since in uptime check is optional

This commit is contained in:
əlemi 2024-12-10 22:14:27 +01:00
parent 12ba835223
commit 5841ba7da4
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 13 additions and 9 deletions

View file

@ -60,10 +60,8 @@ async fn api_status(
Query(q): Query<StatusQuery>,
) -> ApiResult<HashMap<String, Option<i64>>> {
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);
}
}

View file

@ -101,12 +101,18 @@ impl Database {
}
}
pub async fn up(&self, sid: i64, since: i64) -> rusqlite::Result<Option<i64>> {
pub async fn up(&self, sid: i64, since: Option<i64>) -> rusqlite::Result<Option<i64>> {
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::<usize, Option<i64>>(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::<usize, Option<i64>>(0))
}
}