Skip to content

Commit

Permalink
Cache view script runs on all users
Browse files Browse the repository at this point in the history
  • Loading branch information
jahooma committed Apr 29, 2022
1 parent 7e8ac0f commit cd5689a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions functions/src/scripts/cache-views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin()

import { getUserByUsername, getValues } from '../utils'
import { getValues } from '../utils'
import { View } from '../../../common/tracking'
import { User } from '../../../common/user'
import { batchedWaitAll } from '../../../common/util/promise'

const firestore = admin.firestore()

async function cacheViews() {
console.log('Caching views')

const user = await getUserByUsername('JamesGrugett')
const users = await getValues<User>(firestore.collection('users'))

if (user) {
console.log('Caching views for', user.username)
await cacheUserViews(user.id)
}
await batchedWaitAll(
users.map((user) => () => {
console.log('Caching views for', user.username)
return cacheUserViews(user.id)
})
)
}

async function cacheUserViews(userId: string) {
Expand All @@ -39,11 +43,11 @@ async function cacheUserViews(userId: string) {
}

await firestore
.doc(`private-users/${userId}/cached/viewCounts`)
.doc(`private-users/${userId}/cache/viewCounts`)
.set(viewCounts, { merge: true })

await firestore
.doc(`private-users/${userId}/cached/lastViewTime`)
.doc(`private-users/${userId}/cache/lastViewTime`)
.set(lastViewTime, { merge: true })

console.log(viewCounts, lastViewTime)
Expand Down

0 comments on commit cd5689a

Please sign in to comment.