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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
use rusqlite::{params, OptionalExtension, Row};
use crate::db::*;
use crate::error::Result;
use crate::time;
// ------------------------------------------------------------- subscriptions --
#[derive(Debug, Clone)]
// Subscription stays `pub`: what `get_subscription` and `list_subscriptions` hand back to `watch`, `usecase`, the binary and tests/detect.rs.
pub struct Subscription {
pub id: i64,
pub pkgbase: String,
pub aur_url: String,
pub maintainer: Option<String>,
pub state: SubscriptionState,
pub next_check_at: Option<String>,
pub last_checked_at: Option<String>,
pub last_error: Option<String>,
pub poll_interval_secs: Option<i64>,
}
fn row_to_sub(r: &Row<'_>) -> rusqlite::Result<Subscription> {
Ok(Subscription {
id: r.get(0)?,
pkgbase: r.get(1)?,
aur_url: r.get(2)?,
maintainer: r.get(3)?,
state: crate::db::state_col::<SubscriptionState>(4, &r.get::<_, String>(4)?)?,
next_check_at: r.get(5)?,
last_checked_at: r.get(6)?,
last_error: r.get(7)?,
poll_interval_secs: r.get(8)?,
})
}
const SUB_COLS: &str = "id, pkgbase, aur_url, maintainer, state, next_check_at, last_checked_at, \
last_error, poll_interval_secs";
pub fn list_subscriptions(db: &Db) -> Result<Vec<Subscription>> {
db.read(|c| {
let mut stmt = c.prepare(&format!("SELECT {SUB_COLS} FROM subscription ORDER BY pkgbase"))?;
let rows = stmt.query_map([], row_to_sub)?;
let mut out = Vec::new();
for r in rows {
out.push(r?);
}
Ok(out)
})
}
pub fn get_subscription(db: &Db, pkgbase: &str) -> Result<Option<Subscription>> {
db.read(|c| {
c.query_row(
&format!("SELECT {SUB_COLS} FROM subscription WHERE pkgbase = ?1"),
params![pkgbase],
row_to_sub,
)
.optional()
.map_err(Into::into)
})
}
pub fn subscribe(writer: &Writer, repo_id: i64, pkgbase: &str, aur_url: &str) -> Result<i64> {
let (p, u) = (pkgbase.to_string(), aur_url.to_string());
let at = time::now();
writer.tx(move |tx| {
tx.execute(
"INSERT INTO pkgbase (name, aur_url, created_at) VALUES (?1,?2,?3)
ON CONFLICT(name) DO UPDATE SET aur_url = excluded.aur_url",
params![p, u, at],
)?;
tx.execute(
"INSERT INTO subscription (repo_id, pkgbase, aur_url, state, next_check_at, created_at)
VALUES (?1,?2,?3,'active',?4,?5)
ON CONFLICT(repo_id, pkgbase) DO UPDATE SET state='active', aur_url=excluded.aur_url",
params![repo_id, p, u, at.clone(), at],
)?;
Ok(1)
})
}
pub fn set_subscription_state(writer: &Writer, pkgbase: &str, state: SubscriptionState) -> Result<()> {
let (p, s) = (pkgbase.to_string(), state.to_string());
writer.tx(move |tx| {
tx.execute(
"UPDATE subscription SET state=?1 WHERE pkgbase=?2",
params![s, p],
)?;
Ok(())
})
}
pub fn note_check(writer: &Writer, pkgbase: &str, next_check_at: &str, err: Option<&str>) -> Result<()> {
let (p, n) = (pkgbase.to_string(), next_check_at.to_string());
let e = err.map(|s| s.to_string());
let at = time::now();
writer.tx(move |tx| {
tx.execute(
"UPDATE subscription SET last_checked_at=?1, next_check_at=?2, last_error=?3
WHERE pkgbase=?4",
params![at, n, e, p],
)?;
Ok(())
})
}
pub fn due_subscriptions(db: &Db, now: &str) -> Result<Vec<Subscription>> {
db.read(|c| {
let mut stmt = c.prepare(&format!(
"SELECT {SUB_COLS} FROM subscription
WHERE state IN ('active','orphaned') AND (next_check_at IS NULL OR next_check_at <= ?1)
ORDER BY next_check_at IS NULL DESC, next_check_at LIMIT 200"
))?;
let rows = stmt.query_map(params![now], row_to_sub)?;
let mut out = Vec::new();
for r in rows {
out.push(r?);
}
Ok(out)
})
}
|