1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
use rusqlite::{params, OptionalExtension};
use crate::db::{Db, Writer};
use crate::error::Result;
use crate::time;
// ----------------------------------------------------------------- notice --
pub fn put_notice_once(
writer: &Writer,
pkgbase_id: i64,
kind: &str,
severity: &str,
detail: &serde_json::Value,
) -> Result<Option<i64>> {
let (k, s, d) = (kind.to_string(), severity.to_string(), detail.to_string());
let at = time::now();
writer.tx(move |tx| {
let exists: Option<i64> = tx
.query_row(
"SELECT id FROM notice WHERE pkgbase_id=?1 AND kind=?2 AND detail_json=?3
AND acknowledged_at IS NULL ORDER BY id DESC LIMIT 1",
params![pkgbase_id, k, d],
|r| r.get(0),
)
.optional()?;
if exists.is_some() {
return Ok(None);
}
tx.execute(
"INSERT INTO notice (pkgbase_id, kind, severity, detail_json, created_at)
VALUES (?1,?2,?3,?4,?5)",
params![pkgbase_id, k, s, d, at],
)?;
Ok(Some(tx.last_insert_rowid()))
})
}
#[derive(Debug, Clone)]
pub struct NoticeRow {
pub id: i64,
pub pkgbase: String,
pub kind: String,
pub severity: String,
pub detail_json: String,
pub created_at: String,
pub acknowledged_at: Option<String>,
}
pub fn list_notices(db: &Db, include_acknowledged: bool) -> Result<Vec<NoticeRow>> {
db.read(|c| {
let sql = format!(
"SELECT n.id, b.name, n.kind, n.severity, n.detail_json, n.created_at, n.acknowledged_at
FROM notice n JOIN pkgbase b ON b.id = n.pkgbase_id
{} ORDER BY (n.severity='high') DESC, n.created_at DESC LIMIT 500",
if include_acknowledged { "" } else { "WHERE n.acknowledged_at IS NULL" }
);
let mut stmt = c.prepare(&sql)?;
let rows = stmt.query_map([], |r| {
Ok(NoticeRow {
id: r.get(0)?,
pkgbase: r.get(1)?,
kind: r.get(2)?,
severity: r.get(3)?,
detail_json: r.get(4)?,
created_at: r.get(5)?,
acknowledged_at: r.get(6)?,
})
})?;
let mut out = Vec::new();
for r in rows {
out.push(r?);
}
Ok(out)
})
}
pub fn acknowledge_notice(writer: &Writer, id: i64) -> Result<()> {
let at = time::now();
writer.tx(move |tx| {
tx.execute("UPDATE notice SET acknowledged_at=?1 WHERE id=?2", params![at, id])?;
Ok(())
})
}
|