Skip to content

Commit

Permalink
Small refactor to recalculate contract metrics for past 2 months
Browse files Browse the repository at this point in the history
  • Loading branch information
IanPhilips committed Jul 30, 2024
1 parent 40a01de commit df1aac9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion backend/scripts/adjust-user-profit-pivot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if (require.main === module) {
for (const userIds of chunks) {
await updateUserMetricsCore(
userIds.map((u) => u[0]),
true
0
)
total += userIds.length
console.log(
Expand Down
6 changes: 3 additions & 3 deletions backend/shared/src/mana-supply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { log } from 'shared/utils'
export const getManaSupply = async (recalculateAllUserPortfolios: boolean) => {
const pg = createSupabaseDirectClient()
if (recalculateAllUserPortfolios) {
const allBetUserIds = await pg.map(
const allUserIdsWithInvestments = await pg.map(
`
select distinct u.id from users u
join user_contract_metrics ucm on u.id = ucm.user_id
Expand All @@ -17,12 +17,12 @@ export const getManaSupply = async (recalculateAllUserPortfolios: boolean) => {
[],
(r) => r.id as string
)
const chunks = chunk(allBetUserIds, 1000)
const chunks = chunk(allUserIdsWithInvestments, 1000)
let processed = 0
for (const userIds of chunks) {
await updateUserMetricsCore(userIds)
processed += userIds.length
log(`Processed ${processed} of ${allBetUserIds.length} users`)
log(`Processed ${processed} of ${allUserIdsWithInvestments.length} users`)
}
}
const userPortfolio = await pg.one(
Expand Down
4 changes: 1 addition & 3 deletions backend/shared/src/test-backend-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
createSupabaseClient,
createSupabaseDirectClient,
} from 'shared/supabase/init'
import * as admin from 'firebase-admin'
import { updateUserMetricsCore } from 'shared/update-user-metrics-core'
import { updateCreatorMetricsCore } from 'shared/update-creator-metrics-core'
import { calculateImportanceScore } from 'shared/importance-score'
Expand All @@ -18,11 +17,10 @@ export async function testBackendFunction() {
try {
const pg = createSupabaseDirectClient()
const db = createSupabaseClient()
const firestore = admin.firestore()
// await backfillUserTopicInterests(pg)
// await calculateImportanceScore(db, pg)
// await updateContractMetricsCore()
await updateUserMetricsCore(['AJwLWoo3xue32XIiAVrL5SyR1WB2'], true)
await updateUserMetricsCore(['AJwLWoo3xue32XIiAVrL5SyR1WB2'], 0)
// await updateCreatorMetricsCore()
} catch (e) {
console.error(e)
Expand Down
16 changes: 8 additions & 8 deletions backend/shared/src/update-user-metrics-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ const userToPortfolioMetrics: {
timeCachedPeriodProfits: number
}
} = {}

const LIMIT = 400
export async function updateUserMetricsCore(
userIds?: string[],
allTime?: boolean
since?: number
) {
const useSince = since !== undefined
const now = Date.now()
const yesterday = now - DAY_MS
const weekAgo = now - DAY_MS * 7
Expand Down Expand Up @@ -73,8 +74,8 @@ export async function updateUserMetricsCore(
and user_contract_metrics.has_shares = true
))
)
order by uph.last_calculated nulls first limit 400`,
[random, BOT_USERNAMES],
order by uph.last_calculated nulls first limit $3`,
[random, BOT_USERNAMES, LIMIT],
(r) => r.id as string
)

Expand Down Expand Up @@ -134,7 +135,7 @@ export async function updateUserMetricsCore(
const metricRelevantBets = await getUnresolvedOrRecentlyResolvedBets(
pg,
activeUserIds,
allTime ? 0 : weekAgo
useSince ? since : weekAgo
)
log(
`Loaded ${sumBy(
Expand Down Expand Up @@ -256,7 +257,7 @@ export async function updateUserMetricsCore(
return !contract.isResolved
})
let resolvedProfitAdjustment = user.resolvedProfitAdjustment ?? 0
if (allTime) {
if (since === 0) {
const resolvedMetrics = freshMetrics.filter((m) => {
const contract = contractsById[m.contractId]
if (contract.mechanism === 'cpmm-multi-1') {
Expand Down Expand Up @@ -343,8 +344,7 @@ export async function updateUserMetricsCore(
const userIdsNotWritten = activeUserIds.filter(
(id) => !portfolioUpdates.some((p) => p.user_id === id)
)
const chunkSize = 50
const userUpdateChunks = chunk(userUpdates, chunkSize)
const userUpdateChunks = chunk(userUpdates, LIMIT / 10)
log('Writing updates and inserts...')
await Promise.all(
buildArray(
Expand Down

0 comments on commit df1aac9

Please sign in to comment.