trends: maybe make postgres trends work?

This commit is contained in:
Alex Gleason 2024-08-11 20:20:31 -05:00
parent 8db5489350
commit 8dc3b5d5a7
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -1,5 +1,6 @@
import { NostrFilter } from '@nostrify/nostrify'; import { NostrFilter } from '@nostrify/nostrify';
import { Stickynotes } from '@soapbox/stickynotes'; import { Stickynotes } from '@soapbox/stickynotes';
import { sql } from 'kysely';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { DittoDatabase, DittoDB } from '@/db/DittoDB.ts'; import { DittoDatabase, DittoDB } from '@/db/DittoDB.ts';
@ -18,7 +19,8 @@ export async function getTrendingTagValues(
/** Filter of eligible events. */ /** Filter of eligible events. */
filter: NostrFilter, filter: NostrFilter,
): Promise<{ value: string; authors: number; uses: number }[]> { ): Promise<{ value: string; authors: number; uses: number }[]> {
/* if (dialect === 'postgres') {
const { rows } = await sql<{ value: string; authors: number; uses: number }>`
SELECT SELECT
LOWER(element.value) AS value, LOWER(element.value) AS value,
COUNT(DISTINCT nostr_events.pubkey) AS authors, COUNT(DISTINCT nostr_events.pubkey) AS authors,
@ -28,16 +30,23 @@ export async function getTrendingTagValues(
jsonb_each_text(nostr_events.tags_index) kv, jsonb_each_text(nostr_events.tags_index) kv,
jsonb_array_elements_text(kv.value::jsonb) element jsonb_array_elements_text(kv.value::jsonb) element
WHERE WHERE
kv.key = 't' kv.key = ANY(${tagNames})
AND nostr_events.kind = 1 ${filter.kinds ? sql`AND nostr_events.kind = ANY(${filter.kinds})` : sql``}
AND nostr_events.created_at >= 1723325796 ${typeof filter.since === 'number' ? sql`AND nostr_events.created_at >= ${filter.since}` : sql``}
AND nostr_events.created_at <= 1723412196 ${typeof filter.until === 'number' ? sql`AND nostr_events.created_at <= ${filter.until}` : sql``}
GROUP BY GROUP BY
LOWER(element.value) LOWER(element.value)
ORDER BY ORDER BY
COUNT(DISTINCT nostr_events.pubkey) DESC COUNT(DISTINCT nostr_events.pubkey) DESC
LIMIT 20; ${typeof filter.limit === 'number' ? sql`LIMIT ${filter.limit}` : sql``};`
*/ .execute(kysely);
return rows.map((row) => ({
value: row.value,
authors: Number(row.authors),
uses: Number(row.uses),
}));
}
if (dialect === 'sqlite') { if (dialect === 'sqlite') {
let query = kysely let query = kysely