Moved to update-metrics
This commit is contained in:
parent
82772cb733
commit
b6b2be3587
|
@ -12,7 +12,22 @@ export type Group = {
|
||||||
aboutPostId?: string
|
aboutPostId?: string
|
||||||
chatDisabled?: boolean
|
chatDisabled?: boolean
|
||||||
mostRecentContractAddedTime?: number
|
mostRecentContractAddedTime?: number
|
||||||
|
/** @deprecated - members and contracts now stored as subcollections*/
|
||||||
|
memberIds?: string[] // Deprecated
|
||||||
|
/** @deprecated - members and contracts now stored as subcollections*/
|
||||||
|
contractIds?: string[] // Deprecated
|
||||||
|
cachedLeaderboard?: {
|
||||||
|
topTraders: {
|
||||||
|
userId: string
|
||||||
|
score: number
|
||||||
|
}[]
|
||||||
|
topCreators: {
|
||||||
|
userId: string
|
||||||
|
score: number
|
||||||
|
}[]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const MAX_GROUP_NAME_LENGTH = 75
|
export const MAX_GROUP_NAME_LENGTH = 75
|
||||||
export const MAX_ABOUT_LENGTH = 140
|
export const MAX_ABOUT_LENGTH = 140
|
||||||
export const MAX_ID_LENGTH = 60
|
export const MAX_ID_LENGTH = 60
|
||||||
|
|
|
@ -4,9 +4,11 @@ import { groupBy, isEmpty, keyBy, last, sortBy } from 'lodash'
|
||||||
import { getValues, log, logMemory, writeAsync } from './utils'
|
import { getValues, log, logMemory, writeAsync } from './utils'
|
||||||
import { Bet } from '../../common/bet'
|
import { Bet } from '../../common/bet'
|
||||||
import { Contract, CPMM } from '../../common/contract'
|
import { Contract, CPMM } from '../../common/contract'
|
||||||
|
|
||||||
import { PortfolioMetrics, User } from '../../common/user'
|
import { PortfolioMetrics, User } from '../../common/user'
|
||||||
import { DAY_MS } from '../../common/util/time'
|
import { DAY_MS } from '../../common/util/time'
|
||||||
import { getLoanUpdates } from '../../common/loans'
|
import { getLoanUpdates } from '../../common/loans'
|
||||||
|
import { scoreTraders, scoreCreators } from '../../common/scoring'
|
||||||
import {
|
import {
|
||||||
calculateCreatorVolume,
|
calculateCreatorVolume,
|
||||||
calculateNewPortfolioMetrics,
|
calculateNewPortfolioMetrics,
|
||||||
|
@ -15,6 +17,7 @@ import {
|
||||||
computeVolume,
|
computeVolume,
|
||||||
} from '../../common/calculate-metrics'
|
} from '../../common/calculate-metrics'
|
||||||
import { getProbability } from '../../common/calculate'
|
import { getProbability } from '../../common/calculate'
|
||||||
|
import { Group } from 'common/group'
|
||||||
|
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
|
@ -24,16 +27,29 @@ export const updateMetrics = functions
|
||||||
.onRun(updateMetricsCore)
|
.onRun(updateMetricsCore)
|
||||||
|
|
||||||
export async function updateMetricsCore() {
|
export async function updateMetricsCore() {
|
||||||
const [users, contracts, bets, allPortfolioHistories] = await Promise.all([
|
const [users, contracts, bets, allPortfolioHistories, groups] =
|
||||||
getValues<User>(firestore.collection('users')),
|
await Promise.all([
|
||||||
getValues<Contract>(firestore.collection('contracts')),
|
getValues<User>(firestore.collection('users')),
|
||||||
getValues<Bet>(firestore.collectionGroup('bets')),
|
getValues<Contract>(firestore.collection('contracts')),
|
||||||
getValues<PortfolioMetrics>(
|
getValues<Bet>(firestore.collectionGroup('bets')),
|
||||||
firestore
|
getValues<PortfolioMetrics>(
|
||||||
.collectionGroup('portfolioHistory')
|
firestore
|
||||||
.where('timestamp', '>', Date.now() - 31 * DAY_MS) // so it includes just over a month ago
|
.collectionGroup('portfolioHistory')
|
||||||
),
|
.where('timestamp', '>', Date.now() - 31 * DAY_MS) // so it includes just over a month ago
|
||||||
])
|
),
|
||||||
|
getValues<Group>(firestore.collection('groups')),
|
||||||
|
])
|
||||||
|
|
||||||
|
const contractsByGroup = await Promise.all(
|
||||||
|
groups.map((group) => {
|
||||||
|
return getValues(
|
||||||
|
firestore
|
||||||
|
.collection('groups')
|
||||||
|
.doc(group.id)
|
||||||
|
.collection('groupContracts')
|
||||||
|
)
|
||||||
|
})
|
||||||
|
)
|
||||||
log(
|
log(
|
||||||
`Loaded ${users.length} users, ${contracts.length} contracts, and ${bets.length} bets.`
|
`Loaded ${users.length} users, ${contracts.length} contracts, and ${bets.length} bets.`
|
||||||
)
|
)
|
||||||
|
@ -162,4 +178,40 @@ export async function updateMetricsCore() {
|
||||||
'set'
|
'set'
|
||||||
)
|
)
|
||||||
log(`Updated metrics for ${users.length} users.`)
|
log(`Updated metrics for ${users.length} users.`)
|
||||||
|
|
||||||
|
const groupUpdates = groups.map((group, index) => {
|
||||||
|
const groupContractIds = contractsByGroup[index] as GroupContractDoc[]
|
||||||
|
const groupContracts = groupContractIds.map(
|
||||||
|
(e) => contractsById[e.contractId]
|
||||||
|
)
|
||||||
|
const bets = groupContracts.map((e) => {
|
||||||
|
return betsByContract[e.id] ?? []
|
||||||
|
})
|
||||||
|
|
||||||
|
const creatorScores = scoreCreators(groupContracts)
|
||||||
|
const traderScores = scoreTraders(groupContracts, bets)
|
||||||
|
|
||||||
|
const topTraderScores = topUserScores(traderScores)
|
||||||
|
const topCreatorScores = topUserScores(creatorScores)
|
||||||
|
|
||||||
|
return {
|
||||||
|
doc: firestore.collection('groups').doc(group.id),
|
||||||
|
fields: {
|
||||||
|
cachedLeaderboard: {
|
||||||
|
topTraders: topTraderScores,
|
||||||
|
topCreators: topCreatorScores,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
await writeAsync(firestore, groupUpdates)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const topUserScores = (scores: { [userId: string]: number }) => {
|
||||||
|
const top50 = Object.entries(scores)
|
||||||
|
.sort(([, scoreA], [, scoreB]) => scoreB - scoreA)
|
||||||
|
.slice(0, 50)
|
||||||
|
return top50.map(([userId, score]) => ({ userId, score }))
|
||||||
|
}
|
||||||
|
|
||||||
|
type GroupContractDoc = { contractId: string; createdTime: number }
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
import React, { useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import { sortBy, take } from 'lodash'
|
|
||||||
import { toast } from 'react-hot-toast'
|
import { toast } from 'react-hot-toast'
|
||||||
|
|
||||||
import { Group, GROUP_CHAT_SLUG } from 'common/group'
|
import { Group, GROUP_CHAT_SLUG } from 'common/group'
|
||||||
import { Page } from 'web/components/page'
|
import { Page } from 'web/components/page'
|
||||||
import { listAllBets } from 'web/lib/firebase/bets'
|
|
||||||
import { Contract, listContractsByGroupSlug } from 'web/lib/firebase/contracts'
|
import { Contract, listContractsByGroupSlug } from 'web/lib/firebase/contracts'
|
||||||
import {
|
import {
|
||||||
addContractToGroup,
|
addContractToGroup,
|
||||||
|
@ -25,7 +23,6 @@ import {
|
||||||
useGroupContractIds,
|
useGroupContractIds,
|
||||||
useMemberIds,
|
useMemberIds,
|
||||||
} from 'web/hooks/use-group'
|
} from 'web/hooks/use-group'
|
||||||
import { scoreCreators, scoreTraders } from 'common/scoring'
|
|
||||||
import { Leaderboard } from 'web/components/leaderboard'
|
import { Leaderboard } from 'web/components/leaderboard'
|
||||||
import { formatMoney } from 'common/util/format'
|
import { formatMoney } from 'common/util/format'
|
||||||
import { EditGroupButton } from 'web/components/groups/edit-group-button'
|
import { EditGroupButton } from 'web/components/groups/edit-group-button'
|
||||||
|
@ -72,19 +69,15 @@ export async function getStaticPropz(props: { params: { slugs: string[] } }) {
|
||||||
: 'open'
|
: 'open'
|
||||||
const aboutPost =
|
const aboutPost =
|
||||||
group && group.aboutPostId != null && (await getPost(group.aboutPostId))
|
group && group.aboutPostId != null && (await getPost(group.aboutPostId))
|
||||||
const bets = await Promise.all(
|
|
||||||
contracts.map((contract: Contract) => listAllBets(contract.id))
|
|
||||||
)
|
|
||||||
const messages = group && (await listAllCommentsOnGroup(group.id))
|
const messages = group && (await listAllCommentsOnGroup(group.id))
|
||||||
|
|
||||||
const creatorScores = scoreCreators(contracts)
|
const cachedTopTraderIds =
|
||||||
const traderScores = scoreTraders(contracts, bets)
|
(group && group.cachedLeaderboard?.topTraders) ?? []
|
||||||
const [topCreators, topTraders] =
|
const cachedTopCreatorIds =
|
||||||
(memberIds && [
|
(group && group.cachedLeaderboard?.topCreators) ?? []
|
||||||
await toTopUsers(creatorScores, memberIds),
|
const topTraders = await toTopUsers(cachedTopTraderIds)
|
||||||
await toTopUsers(traderScores, memberIds),
|
|
||||||
]) ??
|
const topCreators = await toTopUsers(cachedTopCreatorIds)
|
||||||
[]
|
|
||||||
|
|
||||||
const creator = await creatorPromise
|
const creator = await creatorPromise
|
||||||
// Only count unresolved markets
|
// Only count unresolved markets
|
||||||
|
@ -96,9 +89,7 @@ export async function getStaticPropz(props: { params: { slugs: string[] } }) {
|
||||||
group,
|
group,
|
||||||
memberIds,
|
memberIds,
|
||||||
creator,
|
creator,
|
||||||
traderScores,
|
|
||||||
topTraders,
|
topTraders,
|
||||||
creatorScores,
|
|
||||||
topCreators,
|
topCreators,
|
||||||
messages,
|
messages,
|
||||||
aboutPost,
|
aboutPost,
|
||||||
|
@ -108,22 +99,6 @@ export async function getStaticPropz(props: { params: { slugs: string[] } }) {
|
||||||
revalidate: 60, // regenerate after a minute
|
revalidate: 60, // regenerate after a minute
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function toTopUsers(
|
|
||||||
userScores: { [userId: string]: number },
|
|
||||||
userIds: string[]
|
|
||||||
) {
|
|
||||||
const topUserPairs = take(
|
|
||||||
sortBy(Object.entries(userScores), ([_, score]) => -1 * score),
|
|
||||||
10
|
|
||||||
).filter(([_, score]) => score >= 0.5)
|
|
||||||
|
|
||||||
const topUserIds = topUserPairs
|
|
||||||
.map(([userId]) => userIds.filter((uid) => uid === userId)[0])
|
|
||||||
.filter((userId) => userId != null) as string[]
|
|
||||||
|
|
||||||
return Promise.all(topUserIds.map(getUser))
|
|
||||||
}
|
|
||||||
export async function getStaticPaths() {
|
export async function getStaticPaths() {
|
||||||
return { paths: [], fallback: 'blocking' }
|
return { paths: [], fallback: 'blocking' }
|
||||||
}
|
}
|
||||||
|
@ -140,10 +115,8 @@ export default function GroupPage(props: {
|
||||||
group: Group | null
|
group: Group | null
|
||||||
memberIds: string[]
|
memberIds: string[]
|
||||||
creator: User
|
creator: User
|
||||||
traderScores: { [userId: string]: number }
|
topTraders: { user: User; score: number }[]
|
||||||
topTraders: User[]
|
topCreators: { user: User; score: number }[]
|
||||||
creatorScores: { [userId: string]: number }
|
|
||||||
topCreators: User[]
|
|
||||||
messages: GroupComment[]
|
messages: GroupComment[]
|
||||||
aboutPost: Post
|
aboutPost: Post
|
||||||
suggestedFilter: 'open' | 'all'
|
suggestedFilter: 'open' | 'all'
|
||||||
|
@ -153,22 +126,13 @@ export default function GroupPage(props: {
|
||||||
group: null,
|
group: null,
|
||||||
memberIds: [],
|
memberIds: [],
|
||||||
creator: null,
|
creator: null,
|
||||||
traderScores: {},
|
|
||||||
topTraders: [],
|
topTraders: [],
|
||||||
creatorScores: {},
|
|
||||||
topCreators: [],
|
topCreators: [],
|
||||||
messages: [],
|
messages: [],
|
||||||
suggestedFilter: 'open',
|
suggestedFilter: 'open',
|
||||||
}
|
}
|
||||||
const {
|
const { contractsCount, creator, topTraders, topCreators, suggestedFilter } =
|
||||||
contractsCount,
|
props
|
||||||
creator,
|
|
||||||
traderScores,
|
|
||||||
topTraders,
|
|
||||||
creatorScores,
|
|
||||||
topCreators,
|
|
||||||
suggestedFilter,
|
|
||||||
} = props
|
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { slugs } = router.query as { slugs: string[] }
|
const { slugs } = router.query as { slugs: string[] }
|
||||||
|
@ -191,17 +155,24 @@ export default function GroupPage(props: {
|
||||||
}
|
}
|
||||||
const isCreator = user && group && user.id === group.creatorId
|
const isCreator = user && group && user.id === group.creatorId
|
||||||
const isMember = user && memberIds.includes(user.id)
|
const isMember = user && memberIds.includes(user.id)
|
||||||
|
const maxLeaderboardSize = 50
|
||||||
|
|
||||||
const leaderboard = (
|
const leaderboard = (
|
||||||
<Col>
|
<Col>
|
||||||
<GroupLeaderboards
|
<div className="mt-4 flex flex-col gap-8 px-4 md:flex-row">
|
||||||
traderScores={traderScores}
|
<GroupLeaderboard
|
||||||
creatorScores={creatorScores}
|
topUsers={topTraders}
|
||||||
topTraders={topTraders}
|
title="🏅 Top traders"
|
||||||
topCreators={topCreators}
|
header="Profit"
|
||||||
memberIds={memberIds}
|
maxToShow={maxLeaderboardSize}
|
||||||
user={user}
|
/>
|
||||||
/>
|
<GroupLeaderboard
|
||||||
|
topUsers={topCreators}
|
||||||
|
title="🏅 Top creators"
|
||||||
|
header="Market volume"
|
||||||
|
maxToShow={maxLeaderboardSize}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</Col>
|
</Col>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -408,96 +379,32 @@ function GroupOverview(props: {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function SortedLeaderboard(props: {
|
function GroupLeaderboard(props: {
|
||||||
users: User[]
|
topUsers: { user: User; score: number }[]
|
||||||
scoreFunction: (user: User) => number
|
|
||||||
title: string
|
title: string
|
||||||
|
maxToShow: number
|
||||||
header: string
|
header: string
|
||||||
maxToShow?: number
|
|
||||||
}) {
|
}) {
|
||||||
const { users, scoreFunction, title, header, maxToShow } = props
|
const { topUsers, title, maxToShow, header } = props
|
||||||
|
|
||||||
|
const scoresByUser = topUsers.reduce((acc, { user, score }) => {
|
||||||
|
acc[user.id] = score
|
||||||
|
return acc
|
||||||
|
}, {} as { [key: string]: number })
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Leaderboard
|
<Leaderboard
|
||||||
className="max-w-xl"
|
className="max-w-xl"
|
||||||
users={users}
|
users={topUsers.map((t) => t.user)}
|
||||||
title={title}
|
title={title}
|
||||||
columns={[
|
columns={[
|
||||||
{ header, renderCell: (user) => formatMoney(scoreFunction(user)) },
|
{ header, renderCell: (user) => formatMoney(scoresByUser[user.id]) },
|
||||||
]}
|
]}
|
||||||
maxToShow={maxToShow}
|
maxToShow={maxToShow}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function GroupLeaderboards(props: {
|
|
||||||
traderScores: { [userId: string]: number }
|
|
||||||
creatorScores: { [userId: string]: number }
|
|
||||||
topTraders: User[]
|
|
||||||
topCreators: User[]
|
|
||||||
memberIds: string[]
|
|
||||||
user: User | null | undefined
|
|
||||||
}) {
|
|
||||||
const { traderScores, creatorScores, memberIds, topTraders, topCreators } =
|
|
||||||
props
|
|
||||||
const maxToShow = 50
|
|
||||||
// Consider hiding M$0
|
|
||||||
// If it's just one member (curator), show all bettors, otherwise just show members
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Col>
|
|
||||||
<div className="mt-4 flex flex-col gap-8 px-4 md:flex-row">
|
|
||||||
{memberIds.length > 1 ? (
|
|
||||||
<>
|
|
||||||
<SortedLeaderboard
|
|
||||||
users={topTraders}
|
|
||||||
scoreFunction={(user) => traderScores[user.id] ?? 0}
|
|
||||||
title="🏅 Top traders"
|
|
||||||
header="Profit"
|
|
||||||
maxToShow={maxToShow}
|
|
||||||
/>
|
|
||||||
<SortedLeaderboard
|
|
||||||
users={topCreators}
|
|
||||||
scoreFunction={(user) => creatorScores[user.id] ?? 0}
|
|
||||||
title="🏅 Top creators"
|
|
||||||
header="Market volume"
|
|
||||||
maxToShow={maxToShow}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
<>
|
|
||||||
<Leaderboard
|
|
||||||
className="max-w-xl"
|
|
||||||
title="🏅 Top traders"
|
|
||||||
users={topTraders}
|
|
||||||
columns={[
|
|
||||||
{
|
|
||||||
header: 'Profit',
|
|
||||||
renderCell: (user) => formatMoney(traderScores[user.id] ?? 0),
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
maxToShow={maxToShow}
|
|
||||||
/>
|
|
||||||
<Leaderboard
|
|
||||||
className="max-w-xl"
|
|
||||||
title="🏅 Top creators"
|
|
||||||
users={topCreators}
|
|
||||||
columns={[
|
|
||||||
{
|
|
||||||
header: 'Market volume',
|
|
||||||
renderCell: (user) =>
|
|
||||||
formatMoney(creatorScores[user.id] ?? 0),
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
maxToShow={maxToShow}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</Col>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function AddContractButton(props: { group: Group; user: User }) {
|
function AddContractButton(props: { group: Group; user: User }) {
|
||||||
const { group, user } = props
|
const { group, user } = props
|
||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
|
@ -634,3 +541,15 @@ function JoinGroupButton(props: {
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const toTopUsers = async (
|
||||||
|
cachedUserIds: { userId: string; score: number }[]
|
||||||
|
): Promise<{ user: User; score: number }[]> =>
|
||||||
|
(
|
||||||
|
await Promise.all(
|
||||||
|
cachedUserIds.map(async (e) => {
|
||||||
|
const user = await getUser(e.userId)
|
||||||
|
return { user, score: e.score ?? 0 }
|
||||||
|
})
|
||||||
|
)
|
||||||
|
).filter((e) => e.user != null)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user