From 2a5172cb4886fdb6a8f0d5b7801a2275d1c11f8f Mon Sep 17 00:00:00 2001 From: James Grugett Date: Fri, 18 Feb 2022 12:43:13 -0600 Subject: [PATCH 1/4] Switch to sending only recent bets and comments from static props --- web/pages/home.tsx | 61 ++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/web/pages/home.tsx b/web/pages/home.tsx index 802493d5..6ddba66c 100644 --- a/web/pages/home.tsx +++ b/web/pages/home.tsx @@ -5,8 +5,8 @@ import _ from 'lodash' import { Contract, listAllContracts } from '../lib/firebase/contracts' import { Page } from '../components/page' import { ActivityFeed, findActiveContracts } from './activity' -import { Comment, listAllComments } from '../lib/firebase/comments' -import { Bet, listAllBets } from '../lib/firebase/bets' +import { Comment, getRecentComments } from '../lib/firebase/comments' +import { Bet, getRecentBets } from '../lib/firebase/bets' import FeedCreate from '../components/feed-create' import { Spacer } from '../components/layout/spacer' import { Col } from '../components/layout/col' @@ -30,16 +30,16 @@ export async function getStaticProps() { listAllFolds().catch(() => []), ]) - const [contractBets, contractComments] = await Promise.all([ - Promise.all(contracts.map((contract) => listAllBets(contract.id))), - Promise.all(contracts.map((contract) => listAllComments(contract.id))), + const [recentBets, recentComments] = await Promise.all([ + getRecentBets(), + getRecentComments(), ]) return { props: { contracts, - contractBets, - contractComments, + recentBets, + recentComments, folds, }, @@ -49,18 +49,15 @@ export async function getStaticProps() { const Home = (props: { contracts: Contract[] - contractBets: Bet[][] - contractComments: Comment[][] folds: Fold[] + recentBets: Bet[] + recentComments: Comment[] }) => { - const { contractBets, contractComments, folds } = props + const { folds, recentBets, recentComments } = props const user = useUser() const contracts = useUpdatedContracts(props.contracts) - const contractIdToIndex = _.fromPairs( - contracts.map((contract, index) => [contract.id, index]) - ) const followedFoldIds = useFollowedFolds(user) const followedFolds = filterDefined( @@ -88,29 +85,25 @@ const Home = (props: { ) } - const oneDayMS = 24 * 60 * 60 * 1000 - const recentBets = (feedContracts ?? []) - .map((c) => contractBets[contractIdToIndex[c.id]]) - .flat() - .filter((bet) => bet.createdTime > Date.now() - oneDayMS) - const feedComments = (feedContracts ?? []) - .map((c) => contractComments[contractIdToIndex[c.id]]) - .flat() + const activeContracts = findActiveContracts( + feedContracts, + recentComments, + recentBets, + 365 + ) - const activeContracts = - feedContracts && - findActiveContracts(feedContracts, feedComments, recentBets, 365) + const betsByContract = _.groupBy(recentBets, (bet) => bet.contractId) + const activeBets = activeContracts.map( + (contract) => betsByContract[contract.id] ?? [] + ) - const activeBets = activeContracts - ? activeContracts.map( - (contract) => contractBets[contractIdToIndex[contract.id]] - ) - : [] - const activeComments = activeContracts - ? activeContracts.map( - (contract) => contractComments[contractIdToIndex[contract.id]] - ) - : [] + const commentsByContract = _.groupBy( + recentComments, + (comment) => comment.contractId + ) + const activeComments = activeContracts.map( + (contract) => commentsByContract[contract.id] ?? [] + ) if (user === null) { Router.replace('/') From 26bbb75ccd8ffa9ffa15cb80eb628bebe6474f41 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Fri, 18 Feb 2022 19:10:02 -0600 Subject: [PATCH 2/4] 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 ( From 0641db1f2d394fb82ac12adf6b958e0f82c54026 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Sat, 19 Feb 2022 11:42:27 -0600 Subject: [PATCH 3/4] Show sign in button instead of submit answer if logged out. --- web/components/answers-panel.tsx | 34 +++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/web/components/answers-panel.tsx b/web/components/answers-panel.tsx index 483190ef..4309d8a2 100644 --- a/web/components/answers-panel.tsx +++ b/web/components/answers-panel.tsx @@ -350,6 +350,7 @@ function AnswerBetPanel(props: { function CreateAnswerInput(props: { contract: Contract }) { const { contract } = props + const user = useUser() const [text, setText] = useState('') const [betAmount, setBetAmount] = useState(10) const [amountError, setAmountError] = useState() @@ -451,17 +452,28 @@ function CreateAnswerInput(props: { contract: Contract }) { )} - + {user ? ( + + ) : ( + text && ( + + ) + )} From 693652935d97959db2e29181db9608fe785761a4 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Sat, 19 Feb 2022 12:14:45 -0600 Subject: [PATCH 4/4] Switch to pool size instead of volume for creator leaderboard. --- functions/src/update-user-metrics.ts | 32 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/functions/src/update-user-metrics.ts b/functions/src/update-user-metrics.ts index f7d5a34e..f59d4a34 100644 --- a/functions/src/update-user-metrics.ts +++ b/functions/src/update-user-metrics.ts @@ -22,20 +22,22 @@ export const updateUserMetrics = functions.pubsub contracts.map((contract) => [contract.id, contract]) ) - for (const user of users) { - const [investmentValue, creatorVolume] = await Promise.all([ - computeInvestmentValue(user, contractsDict), - computeTotalVolume(user, contractsDict), - ]) + await Promise.all( + users.map(async (user) => { + const [investmentValue, creatorVolume] = await Promise.all([ + computeInvestmentValue(user, contractsDict), + computeTotalPool(user, contractsDict), + ]) - const totalValue = user.balance + investmentValue - const totalPnL = totalValue - user.totalDeposits + const totalValue = user.balance + investmentValue + const totalPnL = totalValue - user.totalDeposits - await 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 ( @@ -54,15 +56,17 @@ const computeInvestmentValue = async ( }) } -const computeTotalVolume = async ( +const computeTotalPool = async ( user: User, contractsDict: _.Dictionary ) => { const creatorContracts = Object.values(contractsDict).filter( (contract) => contract.creatorId === user.id ) - const volumes = await Promise.all(creatorContracts.map(computeVolume)) - return _.sum(volumes) + const pools = creatorContracts.map((contract) => + _.sum(Object.values(contract.pool)) + ) + return _.sum(pools) } const computeVolume = async (contract: Contract) => {