-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
75 lines (65 loc) · 2.63 KB
/
Copy pathstorage.py
File metadata and controls
75 lines (65 loc) · 2.63 KB
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
"""SQLite-backed store for tracking which events have already been notified."""
import logging
import sqlite3
from contextlib import contextmanager
from pathlib import Path
log = logging.getLogger(__name__)
class NotificationStore:
def __init__(self, db_path: str = "alertle.db"):
self.path = Path(db_path)
self._init_db()
@contextmanager
def _conn(self):
conn = sqlite3.connect(self.path)
try:
yield conn
conn.commit()
finally:
conn.close()
def _init_db(self):
with self._conn() as conn:
conn.execute(
"""
CREATE TABLE IF NOT EXISTS sent (
uid TEXT NOT NULL,
subscription_label TEXT NOT NULL,
notified_at TEXT NOT NULL,
description_hash TEXT,
PRIMARY KEY (uid, subscription_label)
)
"""
)
# Migration for existing installs
try:
conn.execute("ALTER TABLE sent ADD COLUMN description_hash TEXT")
except sqlite3.OperationalError:
pass # column already exists
except Exception as exc:
log.warning("DB migration failed: %s", exc)
def already_sent(self, uid: str, subscription_label: str) -> bool:
with self._conn() as conn:
row = conn.execute(
"SELECT 1 FROM sent WHERE uid = ? AND subscription_label = ?",
(uid, subscription_label),
).fetchone()
return row is not None
def description_already_sent(self, description_hash: str) -> bool:
"""True if any previously sent record has this exact description hash."""
with self._conn() as conn:
row = conn.execute(
"SELECT 1 FROM sent WHERE description_hash = ? LIMIT 1",
(description_hash,),
).fetchone()
return row is not None
def mark_sent(self, uid: str, subscription_label: str, notified_at: str,
description_hash: str | None = None):
with self._conn() as conn:
conn.execute(
"INSERT OR IGNORE INTO sent (uid, subscription_label, notified_at, description_hash) "
"VALUES (?, ?, ?, ?)",
(uid, subscription_label, notified_at, description_hash),
)
def prune_old(self, before_iso: str):
"""Remove entries for events that started before *before_iso* (ISO timestamp)."""
with self._conn() as conn:
conn.execute("DELETE FROM sent WHERE notified_at < ?", (before_iso,))