Update leaderboards to subtract profit of first day

This commit is contained in:
James Grugett 2022-08-11 22:48:50 -05:00
parent 960b118e90
commit 9d51f7a662
2 changed files with 59 additions and 101 deletions

View File

@ -221,12 +221,7 @@ export async function getTopTraders(period: Period) {
) )
const topUsers = await getValues<User>(topTraders) const topUsers = await getValues<User>(topTraders)
return topUsers return topUsers.slice(0, 20)
.filter(
(user) =>
user.username !== 'SalemCenter' && user.username !== 'RichardHanania'
)
.slice(0, 20)
} }
export function getTopCreators(period: Period) { export function getTopCreators(period: Period) {
@ -242,6 +237,20 @@ export async function getTopFollowed() {
return (await getValues<User>(topFollowedQuery)).slice(0, 20) return (await getValues<User>(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<PortfolioMetrics>(firstDayPortfolio)
if (values.length === 0) return 0
const portfolioValue = values[0].balance + values[0].investmentValue
return Math.max(0, portfolioValue - 1000)
}
const topFollowedQuery = query( const topFollowedQuery = query(
users, users,
orderBy('followerCountCached', 'desc'), orderBy('followerCountCached', 'desc'),

View File

@ -1,18 +1,12 @@
import { Col } from 'web/components/layout/col' import { Col } from 'web/components/layout/col'
import { Leaderboard } from 'web/components/leaderboard' import { Leaderboard } from 'web/components/leaderboard'
import { Page } from 'web/components/page' import { Page } from 'web/components/page'
import { import { User, getFirstDayProfit, listAllUsers } from 'web/lib/firebase/users'
getTopCreators,
getTopTraders,
getTopFollowed,
Period,
User,
} from 'web/lib/firebase/users'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { Title } from 'web/components/title' import { Title } from 'web/components/title'
import { Tabs } from 'web/components/layout/tabs'
import { useTracking } from 'web/hooks/use-tracking' import { useTracking } from 'web/hooks/use-tracking'
import { SEO } from 'web/components/SEO' import { SEO } from 'web/components/SEO'
import { sortBy } from 'lodash'
export async function getStaticProps() { export async function getStaticProps() {
const props = await fetchProps() const props = await fetchProps()
@ -24,66 +18,57 @@ export async function getStaticProps() {
} }
const fetchProps = async () => { const fetchProps = async () => {
const [allTime, monthly, weekly, daily] = ( const users = await listAllUsers()
await Promise.all([ const firstDayProfit = await Promise.all(
queryLeaderboardUsers('allTime'), users.map((user) => getFirstDayProfit(user.id))
queryLeaderboardUsers('monthly'), )
queryLeaderboardUsers('weekly'), const userProfit = users.map(
queryLeaderboardUsers('daily'), (user, i) => [user, user.profitCached.allTime - firstDayProfit[i]] as const
]) )
).map((leaderboard) => { 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. // Hide profit for now.
leaderboard.topTraders.forEach((user) => { topTraders.forEach((user) => {
user.profitCached.allTime = 0 user.profitCached.allTime = 0
}) })
return leaderboard
})
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 { return {
topTraders, topTraders,
topCreators,
} }
} }
type leaderboard = { export default function Leaderboards(_props: { topTraders: User[] }) {
topTraders: User[] const [{ topTraders }, setProps] =
topCreators: User[] useState<Parameters<typeof Leaderboards>[0]>(_props)
}
export default function Leaderboards(_props: {
allTime: leaderboard
monthly: leaderboard
weekly: leaderboard
daily: leaderboard
topFollowed: User[]
}) {
const [props, setProps] = useState<Parameters<typeof Leaderboards>[0]>(_props)
useEffect(() => { useEffect(() => {
fetchProps().then((props) => setProps(props)) fetchProps().then((props) => setProps(props))
}, []) }, [])
const LeaderboardWithPeriod = (period: Period) => { useTracking('view leaderboards')
const { topTraders } = props[period]
return ( return (
<> <Page>
<SEO
title="Leaderboards"
description="Manifold's leaderboards show the top traders and market creators."
url="/leaderboards"
/>
<Title text={'Leaderboards'} className={'hidden md:block'} />
<Col className="mx-4 max-w-sm items-center gap-10 lg:flex-row"> <Col className="mx-4 max-w-sm items-center gap-10 lg:flex-row">
<Leaderboard <Leaderboard
className="my-4"
title="🏅 Top traders" title="🏅 Top traders"
users={topTraders} users={topTraders}
columns={ columns={
@ -97,42 +82,6 @@ export default function Leaderboards(_props: {
} }
/> />
</Col> </Col>
</>
)
}
useTracking('view leaderboards')
return (
<Page>
<SEO
title="Leaderboards"
description="Manifold's leaderboards show the top traders and market creators."
url="/leaderboards"
/>
<Title text={'Leaderboards'} className={'hidden md:block'} />
<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'),
// },
]}
/>
</Page> </Page>
) )
} }