Add a top_authors materialized view

This commit is contained in:
Alex Gleason 2025-02-12 15:21:09 -06:00
parent 510ad647be
commit ab7a0e06c7
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
4 changed files with 31 additions and 5 deletions

View file

@ -1,6 +1,13 @@
import { updateTrendingLinks } from '@/trends.ts'; import { sql } from 'kysely';
import { updateTrendingHashtags } from '@/trends.ts';
import { updateTrendingEvents, updateTrendingPubkeys, updateTrendingZappedEvents } from '@/trends.ts'; import { Storages } from '@/storages.ts';
import {
updateTrendingEvents,
updateTrendingHashtags,
updateTrendingLinks,
updateTrendingPubkeys,
updateTrendingZappedEvents,
} from '@/trends.ts';
/** Start cron jobs for the application. */ /** Start cron jobs for the application. */
export function cron() { export function cron() {
@ -9,4 +16,9 @@ export function cron() {
Deno.cron('update trending events', '15 * * * *', updateTrendingEvents); Deno.cron('update trending events', '15 * * * *', updateTrendingEvents);
Deno.cron('update trending hashtags', '30 * * * *', updateTrendingHashtags); Deno.cron('update trending hashtags', '30 * * * *', updateTrendingHashtags);
Deno.cron('update trending links', '45 * * * *', updateTrendingLinks); Deno.cron('update trending links', '45 * * * *', updateTrendingLinks);
Deno.cron('refresh top authors', '20 * * * *', async () => {
const kysely = await Storages.kysely();
await sql`refresh materialized view top_authors`.execute(kysely);
});
} }

View file

@ -9,6 +9,8 @@ export interface DittoTables extends NPostgresSchema {
event_stats: EventStatsRow; event_stats: EventStatsRow;
event_zaps: EventZapRow; event_zaps: EventZapRow;
push_subscriptions: PushSubscriptionRow; push_subscriptions: PushSubscriptionRow;
/** This is a materialized view of `author_stats` pre-sorted by followers_count. */
top_authors: Pick<AuthorStatsRow, 'pubkey' | 'followers_count' | 'search'>;
} }
interface AuthorStatsRow { interface AuthorStatsRow {

View file

@ -0,0 +1,13 @@
import { Kysely } from 'kysely';
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createView('top_authors')
.materialized()
.as(db.selectFrom('author_stats').select(['pubkey', 'followers_count', 'search']).orderBy('followers_count desc'))
.execute();
}
export async function down(db: Kysely<unknown>): Promise<void> {
await db.schema.dropView('top_authors').execute();
}

View file

@ -12,10 +12,9 @@ export async function getPubkeysBySearch(
const pubkeys = new Set<string>(); const pubkeys = new Set<string>();
const query = kysely const query = kysely
.selectFrom('author_stats') .selectFrom('top_authors')
.select('pubkey') .select('pubkey')
.where('search', sql`%>`, q) .where('search', sql`%>`, q)
.orderBy('followers_count desc')
.limit(limit) .limit(limit)
.offset(offset); .offset(offset);