Track latency of feed and portfolio page.

This commit is contained in:
James Grugett 2022-04-21 01:00:08 -05:00
parent 8c8a9be6a1
commit 7847a9e781
6 changed files with 56 additions and 1 deletions

View File

@ -10,3 +10,9 @@ export type ClickEvent = {
contractId: string contractId: string
timestamp: number timestamp: number
} }
export type LatencyEvent = {
type: 'feed' | 'portfolio'
latency: number
timestamp: number
}

View File

@ -30,6 +30,10 @@ service cloud.firestore {
allow create: if userId == request.auth.uid; allow create: if userId == request.auth.uid;
} }
match /private-users/{userId}/latency/{loadTimeId} {
allow create: if userId == request.auth.uid;
}
match /contracts/{contractId} { match /contracts/{contractId} {
allow read; allow read;
allow update: if request.resource.data.diff(resource.data).affectedKeys() allow update: if request.resource.data.diff(resource.data).affectedKeys()

View File

@ -37,6 +37,8 @@ import {
resolvedPayout, resolvedPayout,
getContractBetNullMetrics, getContractBetNullMetrics,
} from '../../common/calculate' } from '../../common/calculate'
import { useTimeSinceFirstRender } from '../hooks/use-time-since-first-render'
import { trackLatency } from '../lib/firebase/tracking'
type BetSort = 'newest' | 'profit' | 'closeTime' | 'value' type BetSort = 'newest' | 'profit' | 'closeTime' | 'value'
type BetFilter = 'open' | 'closed' | 'resolved' | 'all' type BetFilter = 'open' | 'closed' | 'resolved' | 'all'
@ -67,6 +69,14 @@ export function BetsList(props: { user: User }) {
} }
}, [bets]) }, [bets])
const getTime = useTimeSinceFirstRender()
useEffect(() => {
if (bets && contracts) {
trackLatency('portfolio', getTime())
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [!!bets, !!contracts])
if (!bets || !contracts) { if (!bets || !contracts) {
return <LoadingIndicator /> return <LoadingIndicator />
} }

View File

@ -14,6 +14,8 @@ import {
getOutcomeProbability, getOutcomeProbability,
getTopAnswer, getTopAnswer,
} from '../../common/calculate' } from '../../common/calculate'
import { useTimeSinceFirstRender } from './use-time-since-first-render'
import { trackLatency } from '../lib/firebase/tracking'
const MAX_FEED_CONTRACTS = 75 const MAX_FEED_CONTRACTS = 75
@ -35,6 +37,8 @@ export const useAlgoFeed = (
const [algoFeed, setAlgoFeed] = useState<Contract[]>([]) const [algoFeed, setAlgoFeed] = useState<Contract[]>([])
const getTime = useTimeSinceFirstRender()
useEffect(() => { useEffect(() => {
if ( if (
initialContracts && initialContracts &&
@ -53,6 +57,7 @@ export const useAlgoFeed = (
seenContracts seenContracts
) )
setAlgoFeed(contracts) setAlgoFeed(contracts)
trackLatency('feed', getTime())
} }
}, [ }, [
initialBets, initialBets,
@ -60,6 +65,7 @@ export const useAlgoFeed = (
initialContracts, initialContracts,
seenContracts, seenContracts,
yourBetContractIds, yourBetContractIds,
getTime,
]) ])
return algoFeed return algoFeed

View File

@ -0,0 +1,13 @@
import { useCallback, useEffect, useRef } from 'react'
export function useTimeSinceFirstRender() {
const startTimeRef = useRef(0)
useEffect(() => {
startTimeRef.current = Date.now()
}, [])
return useCallback(() => {
if (!startTimeRef.current) return 0
return Date.now() - startTimeRef.current
}, [])
}

View File

@ -2,7 +2,7 @@ import { doc, collection, setDoc } from 'firebase/firestore'
import _ from 'lodash' import _ from 'lodash'
import { db } from './init' import { db } from './init'
import { ClickEvent, View } from '../../../common/tracking' import { ClickEvent, LatencyEvent, View } from '../../../common/tracking'
import { listenForLogin, User } from './users' import { listenForLogin, User } from './users'
let user: User | null = null let user: User | null = null
@ -34,3 +34,19 @@ export async function trackClick(contractId: string) {
return await setDoc(ref, clickEvent) return await setDoc(ref, clickEvent)
} }
export async function trackLatency(
type: 'feed' | 'portfolio',
latency: number
) {
if (!user) return
const ref = doc(collection(db, 'private-users', user.id, 'latency'))
const latencyEvent: LatencyEvent = {
type,
latency,
timestamp: Date.now(),
}
return await setDoc(ref, latencyEvent)
}