Find old contracts to decrement score on
This commit is contained in:
parent
4aface583d
commit
1f0983a145
|
@ -3,6 +3,7 @@ import * as admin from 'firebase-admin'
|
||||||
import { Bet } from 'common/bet'
|
import { Bet } from 'common/bet'
|
||||||
import { uniq } from 'lodash'
|
import { uniq } from 'lodash'
|
||||||
import { Contract } from 'common/contract'
|
import { Contract } from 'common/contract'
|
||||||
|
import { log } from './utils'
|
||||||
|
|
||||||
export const scoreContracts = functions.pubsub
|
export const scoreContracts = functions.pubsub
|
||||||
.schedule('every 1 hours')
|
.schedule('every 1 hours')
|
||||||
|
@ -15,17 +16,29 @@ async function scoreContractsInternal() {
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
const lastHour = now - 3600000
|
const lastHour = now - 3600000
|
||||||
const last3Days = now - 2592000000
|
const last3Days = now - 2592000000
|
||||||
|
const activeContractsSnap = await firestore
|
||||||
const contracts = await firestore
|
|
||||||
.collection('contracts')
|
.collection('contracts')
|
||||||
.where('lastUpdatedTime', '>', lastHour)
|
.where('lastUpdatedTime', '>', lastHour)
|
||||||
.get()
|
.get()
|
||||||
|
const activeContracts = activeContractsSnap.docs.map(
|
||||||
|
(doc) => doc.data() as Contract
|
||||||
|
)
|
||||||
|
// We have to downgrade previously active contracts to allow the new ones to bubble up
|
||||||
|
const previouslyActiveContractsSnap = await firestore
|
||||||
|
.collection('contracts')
|
||||||
|
.where('popularityScore', '>', 0)
|
||||||
|
.get()
|
||||||
|
const activeContractIds = activeContracts.map((c) => c.id)
|
||||||
|
const previouslyActiveContracts = previouslyActiveContractsSnap.docs
|
||||||
|
.map((doc) => doc.data() as Contract)
|
||||||
|
.filter((c) => !activeContractIds.includes(c.id))
|
||||||
|
|
||||||
for (const contractSnap of contracts.docs) {
|
const contracts = activeContracts.concat(previouslyActiveContracts)
|
||||||
const contract = contractSnap.data() as Contract
|
log(`Found ${contracts.length} contracts to score`)
|
||||||
const contractId = contractSnap.id
|
|
||||||
|
for (const contract of contracts) {
|
||||||
const bets = await firestore
|
const bets = await firestore
|
||||||
.collection(`contracts/${contractId}/bets`)
|
.collection(`contracts/${contract.id}/bets`)
|
||||||
.where('createdTime', '>', last3Days)
|
.where('createdTime', '>', last3Days)
|
||||||
.get()
|
.get()
|
||||||
const bettors = bets.docs
|
const bettors = bets.docs
|
||||||
|
@ -35,7 +48,7 @@ async function scoreContractsInternal() {
|
||||||
if (contract.popularityScore !== score)
|
if (contract.popularityScore !== score)
|
||||||
await firestore
|
await firestore
|
||||||
.collection('contracts')
|
.collection('contracts')
|
||||||
.doc(contractId)
|
.doc(contract.id)
|
||||||
.update({ popularityScore: score })
|
.update({ popularityScore: score })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user