Skip to content

Commit

Permalink
Graceful shutdown: flush tag page stats
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwarden committed Dec 5, 2023
1 parent 1574560 commit 5f4b5f2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
6 changes: 1 addition & 5 deletions app/attention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ export function logTagVote(tag: string) {
stats.votes += 1
}


export function logTagPageView(userId: string, tag: string, posts: number[]) {
export function logTagPageView(userId: string, tag: string) {

let stats = getOrCreateStatsForTag(tag)
let filter = stats.filter
Expand All @@ -62,9 +61,6 @@ export function logTagPageView(userId: string, tag: string, posts: number[]) {
if (!filter.has(key)) {
filter.set(key, true)
stats.views += 1
// console.log("Incrementing stats views", stats.views)
} else {
// console.log("Deduping", userId)
}
}

Expand Down
13 changes: 12 additions & 1 deletion app/ranking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { db } from "#app/db.ts";
import { type Post } from '#app/db/types.ts'; // this is the Database interface we defined earlier
import { sql } from 'kysely';
// import { cumulativeAttention } from './attention.ts';
import { saveTagPageStats } from './attention.ts';
import { logTagPageView, saveTagPageStats } from './attention.ts';

Check failure on line 5 in app/ranking.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

'logTagPageView' is defined but never used
import { findTopNoteId, GLOBAL_PRIOR_VOTE_RATE } from './probabilities.ts';
import { getOrInsertTagId } from './tag.ts';

Expand Down Expand Up @@ -45,10 +45,20 @@ let rankingsCache = new LRUCache<string, RankedPost[]>({
// when we dispose of the page from the cache, call saveTagPageStats to update attention
// stats in the DB for each post on the tag page.
dispose: (posts, tag, _reason) => {
console.log("Saving stats for", tag)
saveTagPageStats(tag, posts.map(p => p.id))
},
})

export async function saveAllTagPageStats() {
console.log("Closing gracefully")
let keys = rankingsCache.keys()
for (let tag of keys) {
console.log("Deleting tag", tag)
rankingsCache.delete(tag)
}
}

export async function getRankedPosts(tag: string, maxResults: number): Promise<RankedPost[]> {

let tagId = await getOrInsertTagId(tag)
Expand All @@ -58,6 +68,7 @@ export async function getRankedPosts(tag: string, maxResults: number): Promise<R
result = await getRankedPostsInternal(tagId, maxResults)
rankingsCache.set(tag, result)
}
// logTagPageView(userId, tag)

return result
}
Expand Down
3 changes: 1 addition & 2 deletions app/routes/tags+/$tag_.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export async function loader({ params, request }: DataFunctionArgs) {
invariant(tag, 'Missing tag param')

const posts = await getRankedPosts(tag, maxPosts)

logTagPageView(userId, tag, posts.map((p) => p.id))
logTagPageView(userId, tag)

return json({ posts, userId, positions, tag })
}
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'dotenv/config'
import 'source-map-support/register.js'
import { saveAllTagPageStats } from '#app/ranking.ts'
import { installGlobals } from '@remix-run/node'
import chalk from 'chalk'
import closeWithGrace from 'close-with-grace'
import 'dotenv/config'
import 'source-map-support/register.js'

installGlobals()

closeWithGrace(async ({ err }) => {
console.log("Received shutdown signal.")
await saveAllTagPageStats()
if (err) {
console.error(chalk.red(err))
console.error(chalk.red(err.stack))
Expand Down
2 changes: 1 addition & 1 deletion simulate-attention-share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ async function simulateAttentionShare() {
// assume all users views the tag page
for (let i = 0; i < nUsers; i++) {
let userId = (1000 + i).toString()
logTagPageView(userId, tag, tagPage)
logTagPageView(userId, tag)
}

// for each rank
Expand Down

0 comments on commit 5f4b5f2

Please sign in to comment.