perf: remove blocking archive counts from navigation

This commit is contained in:
ImBenji
2026-08-04 21:42:23 +01:00
parent 2020a0fde2
commit 002b7b320c
12 changed files with 87 additions and 82 deletions
+19 -20
View File
@@ -251,18 +251,20 @@ async function adminRoutes(fastify) {
const where = conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
const total = db.prepare(`SELECT COUNT(*) as n FROM articles ${where}`).get(...params).n;
const total = conditions.length
? null
: (db.prepare("SELECT seq FROM sqlite_sequence WHERE name='articles'").get()?.seq || 0);
params.push(limit, offset);
const rows = db.prepare(`
params.push(limit + 1, offset);
const fetchedRows = db.prepare(`
SELECT id, title, url, source, pub_date, ingested_at, content_status, is_index_page, has_embedding, language
FROM articles
${where}
ORDER BY ingested_at DESC, id DESC
ORDER BY id DESC
LIMIT ? OFFSET ?
`).all(...params);
return { total, rows };
return { total, rows: fetchedRows.slice(0, limit), hasMore: fetchedRows.length > limit };
});
fastify.get('/admin/api/articles/:id', async (request, reply) => {
@@ -366,7 +368,7 @@ async function adminRoutes(fastify) {
// whitelist sort columns + direction so user input cant break the query
const sortMap = {
created_desc: 'e.created_at DESC',
created_desc: 'e.id DESC',
created_asc: 'e.created_at ASC',
articles_desc: 'article_count DESC',
articles_asc: 'article_count ASC',
@@ -385,15 +387,16 @@ async function adminRoutes(fastify) {
FROM events e ${whereClause}
`;
const filteredEvents = `SELECT * FROM (${eventProjection}) ${countWhereClause}`;
const totalRow = db.prepare(`SELECT COUNT(*) AS n FROM (${filteredEvents})`)
.get(...whereParams, ...havingParams);
const rows = db.prepare(`
const total = where.length || having.length
? null
: (db.prepare("SELECT seq FROM sqlite_sequence WHERE name='events'").get()?.seq || 0);
const fetchedRows = db.prepare(`
${filteredEvents}
ORDER BY ${orderBy.replaceAll('e.', '')}
LIMIT ? OFFSET ?
`).all(...whereParams, ...havingParams, limit, offset);
`).all(...whereParams, ...havingParams, limit + 1, offset);
return { total: totalRow.n, rows };
return { total, rows: fetchedRows.slice(0, limit), hasMore: fetchedRows.length > limit };
});
fastify.delete('/admin/api/events/:id', async (request, reply) => {
@@ -844,10 +847,8 @@ async function adminRoutes(fastify) {
const counts = db.prepare(`
SELECT
COALESCE((SELECT seq FROM sqlite_sequence WHERE name='articles'), 0) AS total,
(SELECT COUNT(*) FROM articles
WHERE content IS NOT NULL AND content != ''
AND is_index_page = 0 AND has_embedding = 1) AS withContent,
(SELECT COUNT(*) FROM articles WHERE has_embedding = 1) AS withEmbedding,
(SELECT COUNT(*) FROM article_embedding_meta) AS withContent,
(SELECT COUNT(*) FROM article_embedding_meta) AS withEmbedding,
COALESCE((SELECT seq FROM sqlite_sequence WHERE name='events'), 0) AS eventCount
`).get();
statsSummaryCache = { at: Date.now(), value: counts };
@@ -865,13 +866,11 @@ async function adminRoutes(fastify) {
const counts = db.prepare(`
SELECT
(SELECT COUNT(*) FROM articles) as total,
(SELECT COUNT(*) FROM articles
WHERE content IS NOT NULL AND content != ''
AND is_index_page = 0 AND has_embedding = 1) as withContent,
(SELECT COUNT(*) FROM articles WHERE has_embedding = 1) as withEmbedding,
(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 articles WHERE content_attempted_at >= datetime('now', '-1 hour')) as contentPerHour
(SELECT COUNT(*) FROM article_embedding_meta WHERE embedded_at >= datetime('now', '-1 hour')) as contentPerHour
`).get();
const bySource = db.prepare(`