Trends: support author threshold (default: 3)
This commit is contained in:
parent
969bc1fcba
commit
33f87822d4
|
@ -13,7 +13,11 @@ const trendingTagsController: AppController = (c) => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const yesterday = new Date(now.getTime() - Time.days(1));
|
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 }) => ({
|
return c.json(tags.map(({ name, accounts, uses }) => ({
|
||||||
name,
|
name,
|
||||||
|
|
|
@ -10,18 +10,20 @@ const p8 = (pubkey8: string) => `${pubkey8}0000000000000000000000000000000000000
|
||||||
|
|
||||||
Deno.test('getTrendingTags', () => {
|
Deno.test('getTrendingTags', () => {
|
||||||
trends.addTagUsages(p8('00000000'), ['ditto', 'hello', 'yolo']);
|
trends.addTagUsages(p8('00000000'), ['ditto', 'hello', 'yolo']);
|
||||||
|
trends.addTagUsages(p8('00000000'), ['hello']);
|
||||||
trends.addTagUsages(p8('00000001'), ['Ditto', 'hello']);
|
trends.addTagUsages(p8('00000001'), ['Ditto', 'hello']);
|
||||||
trends.addTagUsages(p8('00000010'), ['DITTO']);
|
trends.addTagUsages(p8('00000010'), ['DITTO']);
|
||||||
|
|
||||||
const result = trends.getTrendingTags(
|
const result = trends.getTrendingTags({
|
||||||
new Date('1999-01-01T00:00:00'),
|
since: new Date('1999-01-01T00:00:00'),
|
||||||
new Date('2999-01-01T00:00:00'),
|
until: new Date('2999-01-01T00:00:00'),
|
||||||
);
|
threshold: 1,
|
||||||
|
});
|
||||||
|
|
||||||
const expected = [
|
const expected = [
|
||||||
{ name: 'ditto', accounts: 3 },
|
{ name: 'ditto', accounts: 3, uses: 3 },
|
||||||
{ name: 'hello', accounts: 2 },
|
{ name: 'hello', accounts: 2, uses: 3 },
|
||||||
{ name: 'yolo', accounts: 1 },
|
{ name: 'yolo', accounts: 1, uses: 1 },
|
||||||
];
|
];
|
||||||
|
|
||||||
assertEquals(result, expected);
|
assertEquals(result, expected);
|
||||||
|
|
|
@ -2,6 +2,13 @@ import { Sqlite } from '@/deps.ts';
|
||||||
import { hashtagSchema, hexIdSchema } from '@/schema.ts';
|
import { hashtagSchema, hexIdSchema } from '@/schema.ts';
|
||||||
import { Time } from './utils.ts';
|
import { Time } from './utils.ts';
|
||||||
|
|
||||||
|
interface GetTrendingTagsOpts {
|
||||||
|
since: Date;
|
||||||
|
until: Date;
|
||||||
|
limit?: number;
|
||||||
|
threshold?: number;
|
||||||
|
}
|
||||||
|
|
||||||
class TrendsDB {
|
class TrendsDB {
|
||||||
#db: Sqlite;
|
#db: Sqlite;
|
||||||
|
|
||||||
|
@ -28,17 +35,18 @@ class TrendsDB {
|
||||||
cleanup();
|
cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
getTrendingTags(since: Date, until: Date, limit = 10) {
|
getTrendingTags({ since, until, limit = 10, threshold = 3 }: GetTrendingTagsOpts) {
|
||||||
return this.#db.query<string[]>(
|
return this.#db.query<string[]>(
|
||||||
`
|
`
|
||||||
SELECT tag, COUNT(DISTINCT pubkey8), COUNT(*)
|
SELECT tag, COUNT(DISTINCT pubkey8), COUNT(*)
|
||||||
FROM tag_usages
|
FROM tag_usages
|
||||||
WHERE inserted_at >= ? AND inserted_at < ?
|
WHERE inserted_at >= ? AND inserted_at < ?
|
||||||
GROUP BY tag
|
GROUP BY tag
|
||||||
|
HAVING COUNT(DISTINCT pubkey8) >= ?
|
||||||
ORDER BY COUNT(DISTINCT pubkey8)
|
ORDER BY COUNT(DISTINCT pubkey8)
|
||||||
DESC LIMIT ?;
|
DESC LIMIT ?;
|
||||||
`,
|
`,
|
||||||
[since, until, limit],
|
[since, until, threshold, limit],
|
||||||
).map((row) => ({
|
).map((row) => ({
|
||||||
name: row[0],
|
name: row[0],
|
||||||
accounts: Number(row[1]),
|
accounts: Number(row[1]),
|
||||||
|
|
Loading…
Reference in New Issue