Update leaderboards to subtract profit of first day
This commit is contained in:
		
							parent
							
								
									960b118e90
								
							
						
					
					
						commit
						9d51f7a662
					
				|  | @ -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'), | ||||
|  |  | |||
|  | @ -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<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] | ||||
| 
 | ||||
|     return ( | ||||
|       <> | ||||
|         <Col className="mx-4 max-w-sm items-center gap-10 lg:flex-row"> | ||||
|           <Leaderboard | ||||
|             title="🏅 Top traders" | ||||
|             users={topTraders} | ||||
|             columns={ | ||||
|               [ | ||||
|                 // Hide profit for now.
 | ||||
|                 // {
 | ||||
|                 //   header: 'Total profit',
 | ||||
|                 //   renderCell: (user) => formatMoney(user.profitCached[period]),
 | ||||
|                 // },
 | ||||
|               ] | ||||
|             } | ||||
|           /> | ||||
|         </Col> | ||||
|       </> | ||||
|     ) | ||||
|   } | ||||
|   useTracking('view leaderboards') | ||||
| 
 | ||||
|   return ( | ||||
|  | @ -110,29 +65,23 @@ export default function Leaderboards(_props: { | |||
|         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'),
 | ||||
|           // },
 | ||||
|         ]} | ||||
|       /> | ||||
| 
 | ||||
|       <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> | ||||
|   ) | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user