perf: compute archive stats off the API thread

This commit is contained in:
ImBenji
2026-08-04 21:45:17 +01:00
parent 002b7b320c
commit f5f092cfb0
2 changed files with 62 additions and 27 deletions
+33
View File
@@ -0,0 +1,33 @@
const { parentPort, workerData } = require('node:worker_threads');
const Database = require('better-sqlite3');
try {
const db = new Database(workerData.databasePath, { readonly: true, fileMustExist: true });
const counts = db.prepare(`
SELECT
(SELECT COUNT(*) FROM articles) AS total,
(SELECT COUNT(*) FROM article_embedding_meta) AS withContent,
(SELECT COUNT(*) FROM article_embedding_meta) AS withEmbedding,
(SELECT COUNT(*) FROM events) AS eventCount,
(SELECT COUNT(*) FROM articles WHERE ingested_at >= datetime('now', '-1 hour')) AS ingestedPerHour,
(SELECT COUNT(*) FROM article_embedding_meta WHERE embedded_at >= datetime('now', '-1 hour')) AS contentPerHour
`).get();
const bySource = db.prepare(`
SELECT source, COUNT(*) AS n FROM articles GROUP BY source ORDER BY n DESC
`).all();
const byStatus = db.prepare(`
SELECT COALESCE(content_status, 'null') AS status, COUNT(*) AS n
FROM articles GROUP BY content_status ORDER BY n DESC
`).all();
let embeddingsPerHour = 0;
try {
embeddingsPerHour = db.prepare(`
SELECT COUNT(*) AS n FROM article_embedding_meta
WHERE embedded_at >= datetime('now', '-1 hour')
`).get().n;
} catch (_) {}
db.close();
parentPort.postMessage({ value: { ...counts, bySource, byStatus, embeddingsPerHour } });
} catch (error) {
parentPort.postMessage({ error: error.message });
}