Improve tag history

This commit is contained in:
Alex Gleason 2023-07-25 23:21:42 -05:00
parent d7316c5eeb
commit c88e58344f
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 31 additions and 9 deletions

View File

@ -20,14 +20,27 @@ const trendingTagsController: AppController = (c) => {
limit, limit,
}); });
return c.json(tags.map(({ name }) => ({ return c.json(tags.map(({ name, uses, accounts }) => ({
name, name,
url: Conf.local(`/tags/${name}`), url: Conf.local(`/tags/${name}`),
history: trends.getTagHistory(name, lastWeek, now).map((history) => ({ history: [
{
day: String(Math.floor(now.getTime() / 1000)),
accounts: String(accounts),
uses: String(uses),
},
...trends.getTagHistory({
tag: name,
since: lastWeek,
until: now,
limit: 6,
offset: 1,
}).map((history) => ({
day: String(Math.floor(history.day.getTime() / 1000)), day: String(Math.floor(history.day.getTime() / 1000)),
accounts: String(history.accounts), accounts: String(history.accounts),
uses: String(history.uses), uses: String(history.uses),
})), })),
],
}))); })));
}; };

View File

@ -9,6 +9,14 @@ interface GetTrendingTagsOpts {
threshold?: number; threshold?: number;
} }
interface GetTagHistoryOpts {
tag: string;
since: Date;
until: Date;
limit?: number;
offset?: number;
}
class TrendsDB { class TrendsDB {
#db: Sqlite; #db: Sqlite;
@ -54,7 +62,7 @@ class TrendsDB {
})); }));
} }
getTagHistory(tag: string, since: Date, until: Date, limit = 7) { getTagHistory({ tag, since, until, limit = 7, offset = 0 }: GetTagHistoryOpts) {
return this.#db.query<string[]>( return this.#db.query<string[]>(
` `
SELECT inserted_at, COUNT(DISTINCT pubkey8), COUNT(*) SELECT inserted_at, COUNT(DISTINCT pubkey8), COUNT(*)
@ -62,9 +70,10 @@ class TrendsDB {
WHERE tag = ? AND inserted_at >= ? AND inserted_at < ? WHERE tag = ? AND inserted_at >= ? AND inserted_at < ?
GROUP BY date(inserted_at) GROUP BY date(inserted_at)
ORDER BY date(inserted_at) DESC ORDER BY date(inserted_at) DESC
LIMIT ?; LIMIT ?
OFFSET ?;
`, `,
[tag, since, until, limit], [tag, since, until, limit, offset],
).map((row) => ({ ).map((row) => ({
day: new Date(row[0]), day: new Date(row[0]),
accounts: Number(row[1]), accounts: Number(row[1]),