Trends: also track total tag usages

This commit is contained in:
Alex Gleason 2023-07-25 17:19:31 -05:00
parent 1d67181e52
commit e8df411834
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 5 additions and 4 deletions

View File

@ -4,17 +4,17 @@ import { trends } from '@/trends.ts';
import { Time } from '@/utils.ts'; import { Time } from '@/utils.ts';
const trendingTagsController: AppController = (c) => { const trendingTagsController: AppController = (c) => {
const yesterday = new Date(new Date().getTime() - Time.days(1));
const now = new Date(); const now = new Date();
const yesterday = new Date(now.getTime() - Time.days(1));
const tags = trends.getTrendingTags(yesterday, now); const tags = trends.getTrendingTags(yesterday, now);
return c.json(tags.map(({ name, accounts }) => ({ return c.json(tags.map(({ name, accounts, uses }) => ({
name, name,
url: Conf.local(`/tags/${name}`), url: Conf.local(`/tags/${name}`),
history: [{ history: [{
day: String(Math.floor(yesterday.getTime() / 1000)), day: String(Math.floor(yesterday.getTime() / 1000)),
uses: String(accounts), // Not actually true - we don't collect this uses: String(uses),
accounts: String(accounts), accounts: String(accounts),
}], }],
}))); })));

View File

@ -21,7 +21,7 @@ class TrendsDB {
getTrendingTags(since: Date, until: Date) { getTrendingTags(since: Date, until: Date) {
return this.#db.query<string[]>( return this.#db.query<string[]>(
` `
SELECT tag, COUNT(DISTINCT pubkey8) 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
@ -32,6 +32,7 @@ class TrendsDB {
).map((row) => ({ ).map((row) => ({
name: row[0], name: row[0],
accounts: Number(row[1]), accounts: Number(row[1]),
uses: Number(row[2]),
})); }));
} }