Support tag history
This commit is contained in:
parent
7c8aa88069
commit
1a860adde7
|
@ -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),
|
||||
})),
|
||||
})));
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue