Cache view script runs on all users

This commit is contained in:
James Grugett 2022-04-29 15:17:40 -04:00
parent 7e8ac0fc8f
commit cd5689a72f

View File

@ -4,20 +4,24 @@ import * as _ from 'lodash'
import { initAdmin } from './script-init' import { initAdmin } from './script-init'
initAdmin() initAdmin()
import { getUserByUsername, getValues } from '../utils' import { getValues } from '../utils'
import { View } from '../../../common/tracking' import { View } from '../../../common/tracking'
import { User } from '../../../common/user'
import { batchedWaitAll } from '../../../common/util/promise'
const firestore = admin.firestore() const firestore = admin.firestore()
async function cacheViews() { async function cacheViews() {
console.log('Caching views') console.log('Caching views')
const user = await getUserByUsername('JamesGrugett') const users = await getValues<User>(firestore.collection('users'))
if (user) { await batchedWaitAll(
console.log('Caching views for', user.username) users.map((user) => () => {
await cacheUserViews(user.id) console.log('Caching views for', user.username)
} return cacheUserViews(user.id)
})
)
} }
async function cacheUserViews(userId: string) { async function cacheUserViews(userId: string) {
@ -39,11 +43,11 @@ async function cacheUserViews(userId: string) {
} }
await firestore await firestore
.doc(`private-users/${userId}/cached/viewCounts`) .doc(`private-users/${userId}/cache/viewCounts`)
.set(viewCounts, { merge: true }) .set(viewCounts, { merge: true })
await firestore await firestore
.doc(`private-users/${userId}/cached/lastViewTime`) .doc(`private-users/${userId}/cache/lastViewTime`)
.set(lastViewTime, { merge: true }) .set(lastViewTime, { merge: true })
console.log(viewCounts, lastViewTime) console.log(viewCounts, lastViewTime)