From 436646cc47f886ee6adf0658d3378a678732afa7 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Sat, 17 Sep 2022 19:18:46 -0500 Subject: [PATCH] Use algolia to fetch daily movers so it's faster. --- web/hooks/use-prefetch.ts | 2 -- web/hooks/use-prob-changes.tsx | 30 +++++++++++++++++++++++++++++- web/pages/daily-movers.tsx | 4 ++-- web/pages/home/index.tsx | 4 ++-- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/web/hooks/use-prefetch.ts b/web/hooks/use-prefetch.ts index 5d95baf4..46d78b3c 100644 --- a/web/hooks/use-prefetch.ts +++ b/web/hooks/use-prefetch.ts @@ -1,6 +1,5 @@ import { usePrefetchUserBetContracts } from './use-contracts' import { usePrefetchPortfolioHistory } from './use-portfolio-history' -import { usePrefetchProbChanges } from './use-prob-changes' import { usePrefetchUserBets } from './use-user-bets' export function usePrefetch(userId: string | undefined) { @@ -9,6 +8,5 @@ export function usePrefetch(userId: string | undefined) { usePrefetchUserBets(maybeUserId), usePrefetchUserBetContracts(maybeUserId), usePrefetchPortfolioHistory(maybeUserId, 'weekly'), - usePrefetchProbChanges(userId), ]) } diff --git a/web/hooks/use-prob-changes.tsx b/web/hooks/use-prob-changes.tsx index 699b67ee..698111b8 100644 --- a/web/hooks/use-prob-changes.tsx +++ b/web/hooks/use-prob-changes.tsx @@ -1,11 +1,39 @@ import { useFirestoreQueryData } from '@react-query-firebase/firestore' +import { CPMMContract } from 'common/contract' import { MINUTE_MS } from 'common/util/time' -import { useQueryClient } from 'react-query' +import { useQuery, useQueryClient } from 'react-query' import { getProbChangesNegative, getProbChangesPositive, } from 'web/lib/firebase/contracts' import { getValues } from 'web/lib/firebase/utils' +import { getIndexName, searchClient } from 'web/lib/service/algolia' + +export const useProbChangesAlgolia = (userId: string) => { + const { data: positiveData } = useQuery(['prob-change-day', userId], () => + searchClient + .initIndex(getIndexName('prob-change-day')) + .search('', { facetFilters: ['uniqueBettorIds:' + userId] }) + ) + const { data: negativeData } = useQuery( + ['prob-change-day-ascending', userId], + () => + searchClient + .initIndex(getIndexName('prob-change-day-ascending')) + .search('', { + facetFilters: ['uniqueBettorIds:' + userId], + }) + ) + + if (!positiveData || !negativeData) { + return undefined + } + + return { + positiveChanges: positiveData.hits, + negativeChanges: negativeData.hits, + } +} export const useProbChanges = (userId: string) => { const { data: positiveChanges } = useFirestoreQueryData( diff --git a/web/pages/daily-movers.tsx b/web/pages/daily-movers.tsx index 1e5b4c48..a925a425 100644 --- a/web/pages/daily-movers.tsx +++ b/web/pages/daily-movers.tsx @@ -2,13 +2,13 @@ import { ProbChangeTable } from 'web/components/contract/prob-change-table' import { Col } from 'web/components/layout/col' import { Page } from 'web/components/page' import { Title } from 'web/components/title' -import { useProbChanges } from 'web/hooks/use-prob-changes' +import { useProbChangesAlgolia } from 'web/hooks/use-prob-changes' import { useUser } from 'web/hooks/use-user' export default function DailyMovers() { const user = useUser() - const changes = useProbChanges(user?.id ?? '') + const changes = useProbChangesAlgolia(user?.id ?? '') return ( diff --git a/web/pages/home/index.tsx b/web/pages/home/index.tsx index 17d55d56..69914e53 100644 --- a/web/pages/home/index.tsx +++ b/web/pages/home/index.tsx @@ -31,7 +31,7 @@ import { ProbChangeTable } from 'web/components/contract/prob-change-table' import { groupPath, joinGroup, leaveGroup } from 'web/lib/firebase/groups' import { usePortfolioHistory } from 'web/hooks/use-portfolio-history' import { formatMoney } from 'common/util/format' -import { useProbChanges } from 'web/hooks/use-prob-changes' +import { useProbChangesAlgolia } from 'web/hooks/use-prob-changes' import { ProfitBadge } from 'web/components/bets-list' import { calculatePortfolioProfit } from 'common/calculate-metrics' import { hasCompletedStreakToday } from 'web/components/profile/betting-streak-modal' @@ -243,7 +243,7 @@ function GroupSection(props: { function DailyMoversSection(props: { userId: string | null | undefined }) { const { userId } = props - const changes = useProbChanges(userId ?? '') + const changes = useProbChangesAlgolia(userId ?? '') return (