diff --git a/src/controllers/api/trends.ts b/src/controllers/api/trends.ts index a8d74bc..99c2d7b 100644 --- a/src/controllers/api/trends.ts +++ b/src/controllers/api/trends.ts @@ -13,7 +13,11 @@ const trendingTagsController: AppController = (c) => { const now = new Date(); const yesterday = new Date(now.getTime() - Time.days(1)); - const tags = trends.getTrendingTags(yesterday, now, limit); + const tags = trends.getTrendingTags({ + since: yesterday, + until: now, + limit, + }); return c.json(tags.map(({ name, accounts, uses }) => ({ name, diff --git a/src/trends.test.ts b/src/trends.test.ts index 3f7eaeb..dbc6d7c 100644 --- a/src/trends.test.ts +++ b/src/trends.test.ts @@ -10,18 +10,20 @@ const p8 = (pubkey8: string) => `${pubkey8}0000000000000000000000000000000000000 Deno.test('getTrendingTags', () => { trends.addTagUsages(p8('00000000'), ['ditto', 'hello', 'yolo']); + trends.addTagUsages(p8('00000000'), ['hello']); trends.addTagUsages(p8('00000001'), ['Ditto', 'hello']); trends.addTagUsages(p8('00000010'), ['DITTO']); - const result = trends.getTrendingTags( - new Date('1999-01-01T00:00:00'), - new Date('2999-01-01T00:00:00'), - ); + const result = trends.getTrendingTags({ + since: new Date('1999-01-01T00:00:00'), + until: new Date('2999-01-01T00:00:00'), + threshold: 1, + }); const expected = [ - { name: 'ditto', accounts: 3 }, - { name: 'hello', accounts: 2 }, - { name: 'yolo', accounts: 1 }, + { name: 'ditto', accounts: 3, uses: 3 }, + { name: 'hello', accounts: 2, uses: 3 }, + { name: 'yolo', accounts: 1, uses: 1 }, ]; assertEquals(result, expected); diff --git a/src/trends.ts b/src/trends.ts index 9a04fa1..4a471ce 100644 --- a/src/trends.ts +++ b/src/trends.ts @@ -2,6 +2,13 @@ import { Sqlite } from '@/deps.ts'; import { hashtagSchema, hexIdSchema } from '@/schema.ts'; import { Time } from './utils.ts'; +interface GetTrendingTagsOpts { + since: Date; + until: Date; + limit?: number; + threshold?: number; +} + class TrendsDB { #db: Sqlite; @@ -28,17 +35,18 @@ class TrendsDB { cleanup(); } - getTrendingTags(since: Date, until: Date, limit = 10) { + getTrendingTags({ since, until, limit = 10, threshold = 3 }: GetTrendingTagsOpts) { return this.#db.query( ` SELECT tag, COUNT(DISTINCT pubkey8), COUNT(*) FROM tag_usages WHERE inserted_at >= ? AND inserted_at < ? GROUP BY tag + HAVING COUNT(DISTINCT pubkey8) >= ? ORDER BY COUNT(DISTINCT pubkey8) DESC LIMIT ?; `, - [since, until, limit], + [since, until, threshold, limit], ).map((row) => ({ name: row[0], accounts: Number(row[1]),