Add preliminary TrendsDB module
This commit is contained in:
parent
1923b65099
commit
9ebf83af5d
|
@ -39,3 +39,4 @@ export { default as sanitizeHtml } from 'npm:sanitize-html@^2.10.0';
|
|||
export { default as ISO6391 } from 'npm:iso-639-1@2.1.15';
|
||||
export { Dongoose } from 'https://raw.githubusercontent.com/alexgleason/dongoose/68b7ad9dd7b6ec0615e246a9f1603123c1709793/mod.ts';
|
||||
export { createPentagon } from 'https://deno.land/x/pentagon@v0.1.1/mod.ts';
|
||||
export { DB as Sqlite } from 'https://deno.land/x/sqlite@v3.7.0/mod.ts';
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
import { Sqlite } from '@/deps.ts';
|
||||
|
||||
class TrendsDB {
|
||||
#db: Sqlite;
|
||||
|
||||
constructor(db: Sqlite) {
|
||||
this.#db = db;
|
||||
|
||||
this.#db.execute(`
|
||||
CREATE TABLE IF NOT EXISTS tag_usages (
|
||||
tag TEXT NOT NULL,
|
||||
pubkey8 TEXT NOT NULL,
|
||||
inserted_at DATETIME NOT NULL,
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_time_tag ON tag_usages(inserted_at, tag);
|
||||
`);
|
||||
}
|
||||
|
||||
getTrendingTags(): string[] {
|
||||
return this.#db.query<string[]>(`
|
||||
SELECT tag, COUNT(DISTINCT pubkey8)
|
||||
FROM tag_usages
|
||||
WHERE inserted_at >= $1 AND inserted_at < $2
|
||||
GROUP BY tag
|
||||
ORDER BY COUNT(DISTINCT pubkey8)
|
||||
DESC LIMIT 10;
|
||||
`).map((row) => row[0]);
|
||||
}
|
||||
|
||||
addTagUsage(tag: string, pubkey8: string): void {
|
||||
this.#db.query(
|
||||
'INSERT INTO tag_usages (tag, pubkey8, inserted_at) VALUES (?, ?, ?)',
|
||||
[tag, pubkey8, new Date()],
|
||||
);
|
||||
}
|
||||
|
||||
cleanupTagUsages(): void {
|
||||
this.#db.query(
|
||||
'DELETE FROM tag_usages WHERE inserted_at < ?',
|
||||
[new Date(Date.now() - 1000 * 60 * 60 * 24 * 7)],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export { TrendsDB };
|
Loading…
Reference in New Issue