stacker.news/worker/views.js
Keyan 0b0e36e3cb
Monthly rewards (#890)
* show placeholder for hidden stackers in top

* top rewardability views

* make territory revenue idependent job

* monthly rewards and leaderboard on rewards pages

* fix earn reschedule

* add query for rewards leaderboard

* reduce likelihood of rewards racing with views

* fix earn and refine values views
2024-03-01 10:28:55 -06:00

42 lines
1.4 KiB
JavaScript

import { PrismaClient } from '@prisma/client'
const viewPrefixes = ['reg_growth', 'spender_growth', 'item_growth', 'spending_growth',
'stackers_growth', 'stacking_growth', 'user_stats', 'sub_stats']
// this is intended to be run everyday after midnight CT
export async function views ({ data: { period } = { period: 'days' } }) {
// grab a greedy connection
const models = new PrismaClient()
try {
// these views are bespoke so we can't use the loop
if (period === 'days') {
await models.$queryRawUnsafe('REFRESH MATERIALIZED VIEW CONCURRENTLY user_values_days')
await models.$queryRawUnsafe('REFRESH MATERIALIZED VIEW CONCURRENTLY rewards_days')
}
if (period === 'hours') {
await models.$queryRawUnsafe('REFRESH MATERIALIZED VIEW CONCURRENTLY user_values_today')
await models.$queryRawUnsafe('REFRESH MATERIALIZED VIEW CONCURRENTLY rewards_today')
}
for (const view of viewPrefixes) {
await models.$queryRawUnsafe(`REFRESH MATERIALIZED VIEW CONCURRENTLY ${view}_${period}`)
}
} finally {
await models.$disconnect()
}
}
// this should be run regularly ... like, every 5 minutes
export async function rankViews () {
// grab a greedy connection
const models = new PrismaClient()
try {
for (const view of ['zap_rank_personal_view']) {
await models.$queryRawUnsafe(`REFRESH MATERIALIZED VIEW CONCURRENTLY ${view}`)
}
} finally {
await models.$disconnect()
}
}