From 26bbb75ccd8ffa9ffa15cb80eb628bebe6474f41 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Fri, 18 Feb 2022 19:10:02 -0600 Subject: [PATCH] Change to sequential user metrics update instead of doing them all in parallel --- functions/src/update-user-metrics.ts | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/functions/src/update-user-metrics.ts b/functions/src/update-user-metrics.ts index 4c5fbb60..f7d5a34e 100644 --- a/functions/src/update-user-metrics.ts +++ b/functions/src/update-user-metrics.ts @@ -22,24 +22,20 @@ export const updateUserMetrics = functions.pubsub contracts.map((contract) => [contract.id, contract]) ) - await Promise.all( - users.map(async (user) => { - const investmentValue = await computeInvestmentValue( - user, - contractsDict - ) - const totalValue = user.balance + investmentValue + for (const user of users) { + const [investmentValue, creatorVolume] = await Promise.all([ + computeInvestmentValue(user, contractsDict), + computeTotalVolume(user, contractsDict), + ]) - const totalPnL = totalValue - user.totalDeposits + const totalValue = user.balance + investmentValue + const totalPnL = totalValue - user.totalDeposits - const creatorVolume = await computeTotalVolume(user, contractsDict) - - return firestore.collection('users').doc(user.id).update({ - totalPnLCached: totalPnL, - creatorVolumeCached: creatorVolume, - }) + await firestore.collection('users').doc(user.id).update({ + totalPnLCached: totalPnL, + creatorVolumeCached: creatorVolume, }) - ) + } }) const computeInvestmentValue = async (