From 5decdf3afa88d1ba4f3c4d3c1b8728edb97e9777 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 6 Jul 2023 00:37:16 +0000 Subject: [PATCH] Better tags query. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4339 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- apps/ssb/tf-app.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/apps/ssb/tf-app.js b/apps/ssb/tf-app.js index 941eca22..f7ace23c 100644 --- a/apps/ssb/tf-app.js +++ b/apps/ssb/tf-app.js @@ -256,13 +256,22 @@ class TfElement extends LitElement { } async load_recent_tags() { + let start = new Date(); this.tags = await tfrpc.rpc.query(` - WITH recent AS (SELECT '#' || json_extract(content, '$.channel') AS tag - FROM messages - WHERE json_extract(content, '$.channel') IS NOT NULL - ORDER BY timestamp DESC LIMIT 100) - SELECT tag, COUNT(*) AS count FROM recent GROUP BY tag ORDER BY count DESC LIMIT 10 - `, []); + WITH + recent AS (SELECT content FROM messages + WHERE messages.timestamp > ? AND json_extract(content, '$.type') = 'post' + ORDER BY timestamp DESC LIMIT 1024), + recent_channels AS (SELECT '#' || json_extract(content, '$.channel') AS tag + FROM recent + WHERE json_extract(content, '$.channel') IS NOT NULL), + recent_mentions AS (SELECT json_extract(mention.value, '$.link') AS tag + FROM recent, json_each(recent.content, '$.mentions') AS mention + WHERE json_valid(mention.value) AND tag LIKE '#%'), + combined AS (SELECT tag FROM recent_channels UNION ALL SELECT tag FROM recent_mentions) + SELECT tag, COUNT(*) AS count FROM combined GROUP BY tag ORDER BY count DESC LIMIT 10 + `, [new Date() - 7 * 24 * 60 * 60 * 1000]); + console.log('tags took', (new Date() - start) / 1000.0, 'seconds'); } async load() {