Update feed script
This commit is contained in:
parent
72768d6d0a
commit
542ec218f9
38
functions/src/scripts/update-feed.ts
Normal file
38
functions/src/scripts/update-feed.ts
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import * as admin from 'firebase-admin'
|
||||||
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
|
import { initAdmin } from './script-init'
|
||||||
|
initAdmin()
|
||||||
|
|
||||||
|
import { getValues } from '../utils'
|
||||||
|
import { User } from '../../../common/user'
|
||||||
|
import { batchedWaitAll } from '../../../common/util/promise'
|
||||||
|
import { Contract } from '../../../common/contract'
|
||||||
|
import { updateUserRecommendations } from '../update-recommendations'
|
||||||
|
import {
|
||||||
|
getFeedContracts,
|
||||||
|
updateFeed as updateUserFeed,
|
||||||
|
} from '../update-user-feed'
|
||||||
|
|
||||||
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
|
async function updateFeed() {
|
||||||
|
console.log('Updating feed')
|
||||||
|
|
||||||
|
const contracts = await getValues<Contract>(firestore.collection('contracts'))
|
||||||
|
const feedContracts = await getFeedContracts()
|
||||||
|
const users = await getValues<User>(firestore.collection('users'))
|
||||||
|
|
||||||
|
await batchedWaitAll(
|
||||||
|
users.map((user) => async () => {
|
||||||
|
console.log('Updating recs for', user.username)
|
||||||
|
await updateUserRecommendations(user, contracts)
|
||||||
|
console.log('Updating feed for', user.username)
|
||||||
|
await updateUserFeed(user, feedContracts)
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (require.main === module) {
|
||||||
|
updateFeed().then(() => process.exit())
|
||||||
|
}
|
|
@ -23,6 +23,13 @@ const MAX_FEED_CONTRACTS = 60
|
||||||
export const updateUserFeed = functions.pubsub
|
export const updateUserFeed = functions.pubsub
|
||||||
.schedule('every 60 minutes')
|
.schedule('every 60 minutes')
|
||||||
.onRun(async () => {
|
.onRun(async () => {
|
||||||
|
const contracts = await getFeedContracts()
|
||||||
|
const users = await getValues<User>(firestore.collection('users'))
|
||||||
|
|
||||||
|
await batchedWaitAll(users.map((user) => () => updateFeed(user, contracts)))
|
||||||
|
})
|
||||||
|
|
||||||
|
export async function getFeedContracts() {
|
||||||
// Get contracts bet on or created in last week.
|
// Get contracts bet on or created in last week.
|
||||||
const contracts = await Promise.all([
|
const contracts = await Promise.all([
|
||||||
getValues<Contract>(
|
getValues<Contract>(
|
||||||
|
@ -45,12 +52,10 @@ export const updateUserFeed = functions.pubsub
|
||||||
return combined.filter((c) => (c.closeTime ?? Infinity) > Date.now())
|
return combined.filter((c) => (c.closeTime ?? Infinity) > Date.now())
|
||||||
})
|
})
|
||||||
|
|
||||||
const users = await getValues<User>(firestore.collection('users'))
|
return contracts
|
||||||
|
}
|
||||||
|
|
||||||
await batchedWaitAll(users.map((user) => () => updateFeed(user, contracts)))
|
export const updateFeed = async (user: User, contracts: Contract[]) => {
|
||||||
})
|
|
||||||
|
|
||||||
const updateFeed = async (user: User, contracts: Contract[]) => {
|
|
||||||
const userCacheCollection = firestore.collection(
|
const userCacheCollection = firestore.collection(
|
||||||
`private-users/${user.id}/cache`
|
`private-users/${user.id}/cache`
|
||||||
)
|
)
|
||||||
|
@ -95,7 +100,7 @@ const updateFeed = async (user: User, contracts: Contract[]) => {
|
||||||
feedContracts.map((contract) => getRecentBetsAndComments(contract))
|
feedContracts.map((contract) => getRecentBetsAndComments(contract))
|
||||||
)
|
)
|
||||||
|
|
||||||
await userCacheCollection.doc('feed').set(feed)
|
await userCacheCollection.doc('feed').set({ feed })
|
||||||
}
|
}
|
||||||
|
|
||||||
function scoreContract(
|
function scoreContract(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user