From df1aac93cee13335956c418ed022eb8bd05deaeb Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Tue, 30 Jul 2024 13:54:30 -0700 Subject: [PATCH] Small refactor to recalculate contract metrics for past 2 months --- backend/scripts/adjust-user-profit-pivot.ts | 2 +- backend/shared/src/mana-supply.ts | 6 +++--- backend/shared/src/test-backend-function.ts | 4 +--- backend/shared/src/update-user-metrics-core.ts | 16 ++++++++-------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/backend/scripts/adjust-user-profit-pivot.ts b/backend/scripts/adjust-user-profit-pivot.ts index faa542c41d..f7d9aa508d 100644 --- a/backend/scripts/adjust-user-profit-pivot.ts +++ b/backend/scripts/adjust-user-profit-pivot.ts @@ -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( diff --git a/backend/shared/src/mana-supply.ts b/backend/shared/src/mana-supply.ts index 048dd8c81e..cde2c6d8c6 100644 --- a/backend/shared/src/mana-supply.ts +++ b/backend/shared/src/mana-supply.ts @@ -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 @@ -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( diff --git a/backend/shared/src/test-backend-function.ts b/backend/shared/src/test-backend-function.ts index cfebce6edf..e5e783ea11 100644 --- a/backend/shared/src/test-backend-function.ts +++ b/backend/shared/src/test-backend-function.ts @@ -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' @@ -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) diff --git a/backend/shared/src/update-user-metrics-core.ts b/backend/shared/src/update-user-metrics-core.ts index 59465c75ee..e7748d97bd 100644 --- a/backend/shared/src/update-user-metrics-core.ts +++ b/backend/shared/src/update-user-metrics-core.ts @@ -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 @@ -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 ) @@ -134,7 +135,7 @@ export async function updateUserMetricsCore( const metricRelevantBets = await getUnresolvedOrRecentlyResolvedBets( pg, activeUserIds, - allTime ? 0 : weekAgo + useSince ? since : weekAgo ) log( `Loaded ${sumBy( @@ -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') { @@ -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(