Trends: support author threshold (default: 3)

This commit is contained in:
Alex Gleason 2023-07-25 18:04:00 -05:00
parent 969bc1fcba
commit 33f87822d4
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 24 additions and 10 deletions

View File

@ -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,

View File

@ -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);

View File

@ -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<string[]>(
`
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]),