Skip to main content

fetchium_core/monitor/
snapshot.rs

1//! Content snapshot store for change detection (Phase 5, PRD §10, Mode G).
2//!
3//! Stores SHA-256 content hashes + full content snapshots in SQLite.
4//! Change detection is O(1) via hash comparison.
5
6use crate::error::FetchiumError;
7use rusqlite::{Connection, OptionalExtension};
8use sha2::{Digest, Sha256};
9use std::path::PathBuf;
10use std::sync::Mutex;
11use tracing::debug;
12
13/// A saved content snapshot.
14#[derive(Debug, Clone)]
15pub struct Snapshot {
16    pub id: i64,
17    pub url: String,
18    pub content_hash: String,
19    pub content: String,
20    pub fetched_at: String,
21}
22
23/// A registered monitor entry.
24#[derive(Debug, Clone)]
25pub struct MonitorEntry {
26    pub url: String,
27    pub interval_secs: u64,
28    pub last_checked: Option<String>,
29    pub notify_method: Option<String>,
30}
31
32/// SQLite-backed snapshot store.
33pub struct SnapshotStore {
34    conn: Mutex<Connection>,
35}
36
37impl SnapshotStore {
38    /// Open or create the snapshot database.
39    pub fn new() -> Result<Self, FetchiumError> {
40        let path = default_db_path();
41        if let Some(p) = path.parent() {
42            std::fs::create_dir_all(p)?;
43        }
44        let conn = Connection::open(&path)?;
45        conn.execute_batch(
46            "PRAGMA journal_mode=WAL;
47             CREATE TABLE IF NOT EXISTS snapshots (
48                 id           INTEGER PRIMARY KEY AUTOINCREMENT,
49                 url          TEXT    NOT NULL,
50                 content_hash TEXT    NOT NULL,
51                 content      TEXT    NOT NULL,
52                 fetched_at   TEXT    DEFAULT (datetime('now')),
53                 UNIQUE(url, content_hash)
54             );
55             CREATE TABLE IF NOT EXISTS monitors (
56                 url             TEXT PRIMARY KEY,
57                 interval_secs   INTEGER NOT NULL DEFAULT 3600,
58                 last_checked    TEXT,
59                 notify_method   TEXT
60             );",
61        )?;
62        Ok(Self {
63            conn: Mutex::new(conn),
64        })
65    }
66
67    /// Open the snapshot database at a custom path (useful for tests).
68    pub fn new_at(path: &std::path::Path) -> Result<Self, FetchiumError> {
69        if let Some(p) = path.parent() {
70            std::fs::create_dir_all(p)?;
71        }
72        let conn = Connection::open(path)?;
73        conn.execute_batch(
74            "PRAGMA journal_mode=WAL;
75             CREATE TABLE IF NOT EXISTS snapshots (
76                 id           INTEGER PRIMARY KEY AUTOINCREMENT,
77                 url          TEXT    NOT NULL,
78                 content_hash TEXT    NOT NULL,
79                 content      TEXT    NOT NULL,
80                 fetched_at   TEXT    DEFAULT (datetime('now')),
81                 UNIQUE(url, content_hash)
82             );
83             CREATE TABLE IF NOT EXISTS monitors (
84                 url             TEXT PRIMARY KEY,
85                 interval_secs   INTEGER NOT NULL DEFAULT 3600,
86                 last_checked    TEXT,
87                 notify_method   TEXT
88             );",
89        )?;
90        Ok(Self {
91            conn: Mutex::new(conn),
92        })
93    }
94
95    /// Save a new snapshot. Returns `true` if the content changed since last snapshot.
96    pub fn save_snapshot(&self, url: &str, content: &str) -> Result<bool, FetchiumError> {
97        let hash = sha256_hex(content);
98        let conn = self.conn.lock().unwrap();
99
100        // Check last hash for this URL
101        let last_hash: Option<String> = conn
102            .query_row(
103                "SELECT content_hash FROM snapshots WHERE url = ?1 ORDER BY id DESC LIMIT 1",
104                [url],
105                |r| r.get(0),
106            )
107            .optional()?;
108
109        let changed = last_hash.as_deref() != Some(hash.as_str());
110
111        if changed {
112            conn.execute(
113                "INSERT OR IGNORE INTO snapshots (url, content_hash, content) VALUES (?1, ?2, ?3)",
114                rusqlite::params![url, hash, content],
115            )?;
116            debug!("Snapshot saved for {url} (hash={:.8})", hash);
117        } else {
118            debug!("No change detected for {url}");
119        }
120
121        Ok(changed)
122    }
123
124    /// Get the most recent snapshot for a URL.
125    pub fn get_latest(&self, url: &str) -> Result<Option<Snapshot>, FetchiumError> {
126        let conn = self.conn.lock().unwrap();
127        let result = conn
128            .query_row(
129                "SELECT id, url, content_hash, content, fetched_at
130                 FROM snapshots WHERE url = ?1 ORDER BY id DESC LIMIT 1",
131                [url],
132                |r| {
133                    Ok(Snapshot {
134                        id: r.get(0)?,
135                        url: r.get(1)?,
136                        content_hash: r.get(2)?,
137                        content: r.get(3)?,
138                        fetched_at: r.get(4)?,
139                    })
140                },
141            )
142            .optional()?;
143        Ok(result)
144    }
145
146    /// Get the previous snapshot (second-latest) for a URL.
147    pub fn get_previous(&self, url: &str) -> Result<Option<Snapshot>, FetchiumError> {
148        let conn = self.conn.lock().unwrap();
149        let result = conn
150            .query_row(
151                "SELECT id, url, content_hash, content, fetched_at
152                 FROM snapshots WHERE url = ?1 ORDER BY id DESC LIMIT 1 OFFSET 1",
153                [url],
154                |r| {
155                    Ok(Snapshot {
156                        id: r.get(0)?,
157                        url: r.get(1)?,
158                        content_hash: r.get(2)?,
159                        content: r.get(3)?,
160                        fetched_at: r.get(4)?,
161                    })
162                },
163            )
164            .optional()?;
165        Ok(result)
166    }
167
168    /// Register a URL for monitoring.
169    pub fn register(
170        &self,
171        url: &str,
172        interval_secs: u64,
173        notify_method: Option<&str>,
174    ) -> Result<(), FetchiumError> {
175        let conn = self.conn.lock().unwrap();
176        conn.execute(
177            "INSERT INTO monitors (url, interval_secs, notify_method) VALUES (?1, ?2, ?3)
178             ON CONFLICT(url) DO UPDATE SET interval_secs = excluded.interval_secs,
179                                            notify_method = excluded.notify_method",
180            rusqlite::params![url, interval_secs, notify_method],
181        )?;
182        Ok(())
183    }
184
185    /// Unregister a URL from monitoring.
186    pub fn unregister(&self, url: &str) -> Result<bool, FetchiumError> {
187        let conn = self.conn.lock().unwrap();
188        let rows = conn.execute("DELETE FROM monitors WHERE url = ?1", [url])?;
189        Ok(rows > 0)
190    }
191
192    /// List all monitored URLs.
193    pub fn list_monitors(&self) -> Result<Vec<MonitorEntry>, FetchiumError> {
194        let conn = self.conn.lock().unwrap();
195        let mut stmt =
196            conn.prepare("SELECT url, interval_secs, last_checked, notify_method FROM monitors")?;
197        let entries = stmt
198            .query_map([], |r| {
199                Ok(MonitorEntry {
200                    url: r.get(0)?,
201                    interval_secs: r.get(1)?,
202                    last_checked: r.get(2)?,
203                    notify_method: r.get(3)?,
204                })
205            })?
206            .collect::<Result<Vec<_>, _>>()?;
207        Ok(entries)
208    }
209
210    /// Prune old snapshots, keeping at most `keep` per URL.
211    pub fn prune_old_snapshots(&self, keep: usize) -> Result<usize, FetchiumError> {
212        let conn = self.conn.lock().unwrap();
213        let deleted = conn.execute(
214            &format!(
215                "DELETE FROM snapshots WHERE id NOT IN (
216                     SELECT id FROM snapshots s2
217                     WHERE s2.url = snapshots.url
218                     ORDER BY id DESC LIMIT {keep}
219                 )"
220            ),
221            [],
222        )?;
223        Ok(deleted)
224    }
225}
226
227fn sha256_hex(content: &str) -> String {
228    format!("{:x}", Sha256::digest(content.as_bytes()))
229}
230
231fn default_db_path() -> PathBuf {
232    dirs::data_dir()
233        .unwrap_or_else(|| PathBuf::from("."))
234        .join("fetchium")
235        .join("monitor.db")
236}
237
238#[cfg(test)]
239mod tests {
240    use super::*;
241    use tempfile::NamedTempFile;
242
243    fn make_store() -> (SnapshotStore, NamedTempFile) {
244        let tmp = NamedTempFile::new().unwrap();
245        let store = SnapshotStore::new_at(tmp.path()).unwrap();
246        (store, tmp)
247    }
248
249    #[test]
250    fn first_snapshot_is_changed() {
251        let (store, _tmp) = make_store();
252        let changed = store
253            .save_snapshot("https://example.com", "content v1")
254            .unwrap();
255        assert!(changed, "first snapshot should report changed");
256    }
257
258    #[test]
259    fn same_content_not_changed() {
260        let (store, _tmp) = make_store();
261        store
262            .save_snapshot("https://example.com", "content")
263            .unwrap();
264        let changed = store
265            .save_snapshot("https://example.com", "content")
266            .unwrap();
267        assert!(!changed, "identical content should not report changed");
268    }
269
270    #[test]
271    fn different_content_is_changed() {
272        let (store, _tmp) = make_store();
273        store
274            .save_snapshot("https://example.com", "version 1")
275            .unwrap();
276        let changed = store
277            .save_snapshot("https://example.com", "version 2")
278            .unwrap();
279        assert!(changed, "different content should report changed");
280    }
281
282    #[test]
283    fn get_latest_returns_most_recent() {
284        let (store, _tmp) = make_store();
285        store.save_snapshot("https://example.com", "v1").unwrap();
286        store.save_snapshot("https://example.com", "v2").unwrap();
287        let snap = store.get_latest("https://example.com").unwrap().unwrap();
288        assert_eq!(snap.content, "v2");
289    }
290
291    #[test]
292    fn register_and_list_monitors() {
293        let (store, _tmp) = make_store();
294        store.register("https://a.com", 3600, None).unwrap();
295        store
296            .register("https://b.com", 1800, Some("webhook"))
297            .unwrap();
298        let monitors = store.list_monitors().unwrap();
299        assert_eq!(monitors.len(), 2);
300    }
301
302    #[test]
303    fn unregister_removes_entry() {
304        let (store, _tmp) = make_store();
305        store.register("https://x.com", 3600, None).unwrap();
306        let removed = store.unregister("https://x.com").unwrap();
307        assert!(removed);
308        let monitors = store.list_monitors().unwrap();
309        assert!(monitors.is_empty());
310    }
311}