const db = require('../db'); const { getLastIngestionBySource } = require('../state'); async function statusRoutes(fastify) { fastify.get('/status', async () => { const bySourceRows = db.prepare(` SELECT source, COUNT(*) AS total, SUM(CASE WHEN content IS NOT NULL AND content != '' AND is_index_page = 0 AND EXISTS (SELECT 1 FROM article_embeddings WHERE article_id = articles.id) THEN 1 ELSE 0 END) AS usable FROM articles GROUP BY source ORDER BY source `).all(); const totals = bySourceRows.reduce( (acc, row) => { acc.total += row.total; acc.usable += row.usable; return acc; }, { total: 0, usable: 0 } ); const embeddingModelRows = db.prepare(` SELECT model, COUNT(*) AS article_count FROM article_embedding_store GROUP BY model ORDER BY article_count DESC `).all(); return { total: totals.total, usable: totals.usable, lastIngestionBySource: getLastIngestionBySource(), bySource: Object.fromEntries( bySourceRows.map((row) => [row.source, { total: row.total, usable: row.usable }]) ), embeddingModels: embeddingModelRows.map((row) => ({ model: row.model, articles: row.article_count, })), }; }); } module.exports = statusRoutes;