Only set daily score on contracts that are at least day old

This commit is contained in:
James Grugett 2022-09-26 17:43:27 -04:00
parent 90eaf83775
commit 2ef025a151

View File

@ -5,6 +5,7 @@ import { Bet } from '../../common/bet'
import { Contract } from '../../common/contract' import { Contract } from '../../common/contract'
import { log } from './utils' import { log } from './utils'
import { removeUndefinedProps } from '../../common/util/object' import { removeUndefinedProps } from '../../common/util/object'
import { DAY_MS, HOUR_MS } from '../../common/util/time'
export const scoreContracts = functions export const scoreContracts = functions
.runWith({ memory: '4GB', timeoutSeconds: 540 }) .runWith({ memory: '4GB', timeoutSeconds: 540 })
@ -16,11 +17,12 @@ const firestore = admin.firestore()
async function scoreContractsInternal() { async function scoreContractsInternal() {
const now = Date.now() const now = Date.now()
const lastHour = now - 60 * 60 * 1000 const hourAgo = now - HOUR_MS
const last3Days = now - 1000 * 60 * 60 * 24 * 3 const dayAgo = now - DAY_MS
const threeDaysAgo = now - DAY_MS * 3
const activeContractsSnap = await firestore const activeContractsSnap = await firestore
.collection('contracts') .collection('contracts')
.where('lastUpdatedTime', '>', lastHour) .where('lastUpdatedTime', '>', hourAgo)
.get() .get()
const activeContracts = activeContractsSnap.docs.map( const activeContracts = activeContractsSnap.docs.map(
(doc) => doc.data() as Contract (doc) => doc.data() as Contract
@ -41,15 +43,21 @@ async function scoreContractsInternal() {
for (const contract of contracts) { for (const contract of contracts) {
const bets = await firestore const bets = await firestore
.collection(`contracts/${contract.id}/bets`) .collection(`contracts/${contract.id}/bets`)
.where('createdTime', '>', last3Days) .where('createdTime', '>', threeDaysAgo)
.get() .get()
const bettors = bets.docs const bettors = bets.docs
.map((doc) => doc.data() as Bet) .map((doc) => doc.data() as Bet)
.map((bet) => bet.userId) .map((bet) => bet.userId)
const popularityScore = uniq(bettors).length const popularityScore = uniq(bettors).length
const wasCreatedToday = contract.createdTime > dayAgo
let dailyScore: number | undefined let dailyScore: number | undefined
if (contract.outcomeType === 'BINARY' && contract.mechanism === 'cpmm-1') { if (
contract.outcomeType === 'BINARY' &&
contract.mechanism === 'cpmm-1' &&
!wasCreatedToday
) {
const percentChange = Math.abs(contract.probChanges.day) const percentChange = Math.abs(contract.probChanges.day)
dailyScore = popularityScore * percentChange dailyScore = popularityScore * percentChange
} }