From 9d51f7a662bb30cb3e95f574c249d68d4c1632a1 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Thu, 11 Aug 2022 22:48:50 -0500 Subject: [PATCH] Update leaderboards to subtract profit of first day --- web/lib/firebase/users.ts | 21 ++++-- web/pages/leaderboards.tsx | 139 ++++++++++++------------------------- 2 files changed, 59 insertions(+), 101 deletions(-) diff --git a/web/lib/firebase/users.ts b/web/lib/firebase/users.ts index a8bb962b..9748d8b3 100644 --- a/web/lib/firebase/users.ts +++ b/web/lib/firebase/users.ts @@ -221,12 +221,7 @@ export async function getTopTraders(period: Period) { ) const topUsers = await getValues(topTraders) - return topUsers - .filter( - (user) => - user.username !== 'SalemCenter' && user.username !== 'RichardHanania' - ) - .slice(0, 20) + return topUsers.slice(0, 20) } export function getTopCreators(period: Period) { @@ -242,6 +237,20 @@ export async function getTopFollowed() { return (await getValues(topFollowedQuery)).slice(0, 20) } +export async function getFirstDayProfit(userId: string) { + const firstDay = new Date('2022-08-08').getTime() + const firstDayPortfolio = query( + collection(users, userId, 'portfolioHistory'), + where('timestamp', '<', firstDay), + orderBy('timestamp', 'desc'), + limit(1) + ) + const values = await getValues(firstDayPortfolio) + if (values.length === 0) return 0 + const portfolioValue = values[0].balance + values[0].investmentValue + return Math.max(0, portfolioValue - 1000) +} + const topFollowedQuery = query( users, orderBy('followerCountCached', 'desc'), diff --git a/web/pages/leaderboards.tsx b/web/pages/leaderboards.tsx index 3e594e40..e7ff3849 100644 --- a/web/pages/leaderboards.tsx +++ b/web/pages/leaderboards.tsx @@ -1,18 +1,12 @@ import { Col } from 'web/components/layout/col' import { Leaderboard } from 'web/components/leaderboard' import { Page } from 'web/components/page' -import { - getTopCreators, - getTopTraders, - getTopFollowed, - Period, - User, -} from 'web/lib/firebase/users' +import { User, getFirstDayProfit, listAllUsers } from 'web/lib/firebase/users' import { useEffect, useState } from 'react' import { Title } from 'web/components/title' -import { Tabs } from 'web/components/layout/tabs' import { useTracking } from 'web/hooks/use-tracking' import { SEO } from 'web/components/SEO' +import { sortBy } from 'lodash' export async function getStaticProps() { const props = await fetchProps() @@ -24,82 +18,43 @@ export async function getStaticProps() { } const fetchProps = async () => { - const [allTime, monthly, weekly, daily] = ( - await Promise.all([ - queryLeaderboardUsers('allTime'), - queryLeaderboardUsers('monthly'), - queryLeaderboardUsers('weekly'), - queryLeaderboardUsers('daily'), - ]) - ).map((leaderboard) => { - // Hide profit for now. - leaderboard.topTraders.forEach((user) => { - user.profitCached.allTime = 0 - }) - return leaderboard + const users = await listAllUsers() + const firstDayProfit = await Promise.all( + users.map((user) => getFirstDayProfit(user.id)) + ) + const userProfit = users.map( + (user, i) => [user, user.profitCached.allTime - firstDayProfit[i]] as const + ) + const topTradersProfit = sortBy(userProfit, ([_, profit]) => profit) + .reverse() + .filter( + ([user]) => + user.username !== 'SalemCenter' && user.username !== 'RichardHanania' + ) + .slice(0, 20) + + console.log(topTradersProfit) + + const topTraders = topTradersProfit.map(([user]) => user) + + // Hide profit for now. + topTraders.forEach((user) => { + user.profitCached.allTime = 0 }) - const topFollowed = await getTopFollowed() - return { - allTime, - monthly, - weekly, - daily, - topFollowed, - } -} - -const queryLeaderboardUsers = async (period: Period) => { - const [topTraders, topCreators] = await Promise.all([ - getTopTraders(period), - getTopCreators(period), - ]) return { topTraders, - topCreators, } } -type leaderboard = { - topTraders: User[] - topCreators: User[] -} +export default function Leaderboards(_props: { topTraders: User[] }) { + const [{ topTraders }, setProps] = + useState[0]>(_props) -export default function Leaderboards(_props: { - allTime: leaderboard - monthly: leaderboard - weekly: leaderboard - daily: leaderboard - topFollowed: User[] -}) { - const [props, setProps] = useState[0]>(_props) useEffect(() => { fetchProps().then((props) => setProps(props)) }, []) - const LeaderboardWithPeriod = (period: Period) => { - const { topTraders } = props[period] - - return ( - <> - - formatMoney(user.profitCached[period]), - // }, - ] - } - /> - - - ) - } useTracking('view leaderboards') return ( @@ -110,29 +65,23 @@ export default function Leaderboards(_props: { url="/leaderboards" /> - <Tabs - currentPageForAnalytics={'leaderboards'} - defaultIndex={0} - tabs={[ - { - title: 'All Time', - content: LeaderboardWithPeriod('allTime'), - }, - // TODO: Enable this near the end of July! - // { - // title: 'Monthly', - // content: LeaderboardWithPeriod('monthly'), - // }, - // { - // title: 'Weekly', - // content: LeaderboardWithPeriod('weekly'), - // }, - // { - // title: 'Daily', - // content: LeaderboardWithPeriod('daily'), - // }, - ]} - /> + + <Col className="mx-4 max-w-sm items-center gap-10 lg:flex-row"> + <Leaderboard + className="my-4" + title="🏅 Top traders" + users={topTraders} + columns={ + [ + // Hide profit for now. + // { + // header: 'Total profit', + // renderCell: (user) => formatMoney(user.profitCached[period]), + // }, + ] + } + /> + </Col> </Page> ) }