|
| 1 | +import type { Id } from '../_generated/dataModel' |
| 2 | +import type { MutationCtx, QueryCtx } from '../_generated/server' |
| 3 | + |
| 4 | +const DAY_MS = 24 * 60 * 60 * 1000 |
| 5 | +export const TRENDING_DAYS = 7 |
| 6 | + |
| 7 | +type LeaderboardEntry = { |
| 8 | + skillId: Id<'skills'> |
| 9 | + score: number |
| 10 | + installs: number |
| 11 | + downloads: number |
| 12 | +} |
| 13 | + |
| 14 | +export function toDayKey(timestamp: number) { |
| 15 | + return Math.floor(timestamp / DAY_MS) |
| 16 | +} |
| 17 | + |
| 18 | +export function getTrendingRange(now: number) { |
| 19 | + const endDay = toDayKey(now) |
| 20 | + const startDay = endDay - (TRENDING_DAYS - 1) |
| 21 | + return { startDay, endDay } |
| 22 | +} |
| 23 | + |
| 24 | +export async function buildTrendingLeaderboard( |
| 25 | + ctx: QueryCtx | MutationCtx, |
| 26 | + params: { limit: number; now?: number }, |
| 27 | +) { |
| 28 | + const now = params.now ?? Date.now() |
| 29 | + const { startDay, endDay } = getTrendingRange(now) |
| 30 | + const rows = await ctx.db |
| 31 | + .query('skillDailyStats') |
| 32 | + .withIndex('by_day', (q) => q.gte('day', startDay).lte('day', endDay)) |
| 33 | + .collect() |
| 34 | + |
| 35 | + const totals = new Map<Id<'skills'>, { installs: number; downloads: number }>() |
| 36 | + for (const row of rows) { |
| 37 | + const current = totals.get(row.skillId) ?? { installs: 0, downloads: 0 } |
| 38 | + current.installs += row.installs |
| 39 | + current.downloads += row.downloads |
| 40 | + totals.set(row.skillId, current) |
| 41 | + } |
| 42 | + |
| 43 | + const entries = Array.from(totals, ([skillId, totalsEntry]) => ({ |
| 44 | + skillId, |
| 45 | + installs: totalsEntry.installs, |
| 46 | + downloads: totalsEntry.downloads, |
| 47 | + score: totalsEntry.installs, |
| 48 | + })) |
| 49 | + |
| 50 | + const items = topN(entries, params.limit, compareTrendingEntries).sort((a, b) => |
| 51 | + compareTrendingEntries(b, a), |
| 52 | + ) |
| 53 | + |
| 54 | + return { startDay, endDay, items } |
| 55 | +} |
| 56 | + |
| 57 | +function compareTrendingEntries(a: LeaderboardEntry, b: LeaderboardEntry) { |
| 58 | + if (a.score !== b.score) return a.score - b.score |
| 59 | + if (a.downloads !== b.downloads) return a.downloads - b.downloads |
| 60 | + return 0 |
| 61 | +} |
| 62 | + |
| 63 | +function topN<T>(entries: T[], limit: number, compare: (a: T, b: T) => number) { |
| 64 | + if (entries.length <= limit) return entries.slice() |
| 65 | + |
| 66 | + const heap: T[] = [] |
| 67 | + for (const entry of entries) { |
| 68 | + if (heap.length < limit) { |
| 69 | + heap.push(entry) |
| 70 | + siftUp(heap, heap.length - 1, compare) |
| 71 | + continue |
| 72 | + } |
| 73 | + if (compare(entry, heap[0]) <= 0) continue |
| 74 | + heap[0] = entry |
| 75 | + siftDown(heap, 0, compare) |
| 76 | + } |
| 77 | + return heap |
| 78 | +} |
| 79 | + |
| 80 | +function siftUp<T>(heap: T[], index: number, compare: (a: T, b: T) => number) { |
| 81 | + let current = index |
| 82 | + while (current > 0) { |
| 83 | + const parent = Math.floor((current - 1) / 2) |
| 84 | + if (compare(heap[current], heap[parent]) >= 0) break |
| 85 | + ;[heap[current], heap[parent]] = [heap[parent], heap[current]] |
| 86 | + current = parent |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function siftDown<T>(heap: T[], index: number, compare: (a: T, b: T) => number) { |
| 91 | + let current = index |
| 92 | + const length = heap.length |
| 93 | + while (true) { |
| 94 | + const left = current * 2 + 1 |
| 95 | + const right = current * 2 + 2 |
| 96 | + let smallest = current |
| 97 | + if (left < length && compare(heap[left], heap[smallest]) < 0) smallest = left |
| 98 | + if (right < length && compare(heap[right], heap[smallest]) < 0) smallest = right |
| 99 | + if (smallest === current) break |
| 100 | + ;[heap[current], heap[smallest]] = [heap[smallest], heap[current]] |
| 101 | + current = smallest |
| 102 | + } |
| 103 | +} |
0 commit comments