Support tag history

This commit is contained in:
Alex Gleason 2023-07-25 20:26:49 -05:00
parent 7c8aa88069
commit 1a860adde7
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 24 additions and 6 deletions

View File

@ -12,6 +12,7 @@ const trendingTagsController: AppController = (c) => {
const now = new Date();
const yesterday = new Date(now.getTime() - Time.days(1));
const lastWeek = new Date(now.getTime() - Time.days(7));
const tags = trends.getTrendingTags({
since: yesterday,
@ -19,14 +20,14 @@ const trendingTagsController: AppController = (c) => {
limit,
});
return c.json(tags.map(({ name, accounts, uses }) => ({
return c.json(tags.map(({ name }) => ({
name,
url: Conf.local(`/tags/${name}`),
history: [{
day: String(Math.floor(yesterday.getTime() / 1000)),
uses: String(uses),
accounts: String(accounts),
}],
history: trends.getTagHistory(name, lastWeek, now).map((history) => ({
day: String(Math.floor(history.day.getTime() / 1000)),
accounts: String(history.accounts),
uses: String(history.uses),
})),
})));
};

View File

@ -54,6 +54,23 @@ class TrendsDB {
}));
}
getTagHistory(tag: string, since: Date, until: Date) {
return this.#db.query<string[]>(
`
SELECT inserted_at, COUNT(DISTINCT pubkey8), COUNT(*)
FROM tag_usages
WHERE tag = ? AND inserted_at >= ? AND inserted_at < ?
GROUP BY date(inserted_at)
ORDER BY date(inserted_at);
`,
[tag, since, until],
).map((row) => ({
day: new Date(row[0]),
accounts: Number(row[1]),
uses: Number(row[2]),
}));
}
addTagUsages(pubkey: string, hashtags: string[], date = new Date()): void {
const pubkey8 = hexIdSchema.parse(pubkey).substring(0, 8);
const tags = hashtagSchema.array().min(1).parse(hashtags);