mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Add a top_authors materialized view
This commit is contained in:
parent
510ad647be
commit
ab7a0e06c7
4 changed files with 31 additions and 5 deletions
18
src/cron.ts
18
src/cron.ts
|
|
@ -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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
13
src/db/migrations/049_author_stats_sorted.ts
Normal file
13
src/db/migrations/049_author_stats_sorted.ts
Normal 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();
|
||||||
|
}
|
||||||
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue