feat: since in uptime check is optional
This commit is contained in:
parent
12ba835223
commit
5841ba7da4
2 changed files with 13 additions and 9 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
18
src/db.rs
18
src/db.rs
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue