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)
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<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(
users,
orderBy('followerCountCached', 'desc'),

View File

@ -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,66 +18,57 @@ 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) => {
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.
leaderboard.topTraders.forEach((user) => {
topTraders.forEach((user) => {
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 {
topTraders,
topCreators,
}
}
type leaderboard = {
topTraders: User[]
topCreators: User[]
}
export default function Leaderboards(_props: { topTraders: User[] }) {
const [{ topTraders }, setProps] =
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(() => {
fetchProps().then((props) => setProps(props))
}, [])
const LeaderboardWithPeriod = (period: Period) => {
const { topTraders } = props[period]
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'} />
<Col className="mx-4 max-w-sm items-center gap-10 lg:flex-row">
<Leaderboard
className="my-4"
title="🏅 Top traders"
users={topTraders}
columns={
@ -97,42 +82,6 @@ export default function Leaderboards(_props: {
}
/>
</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>
)
}