The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
move log stats to react-query
Alaister Youngaa327999f0f5
diff --git a/studio/components/interfaces/Home/ProjectUsage.tsx b/studio/components/interfaces/Home/ProjectUsage.tsxindex 3bdd1ed3a8a..d113c2bd5c9 100644--- a/studio/components/interfaces/Home/ProjectUsage.tsx+++ b/studio/components/interfaces/Home/ProjectUsage.tsx@@ -1,4 +1,3 @@-import useSWR from 'swr'import dayjs from 'dayjs'import Link from 'next/link'import { FC, useState } from 'react'@@ -6,9 +5,10 @@ import { useRouter } from 'next/router'import { Button, Dropdown, IconArchive, IconChevronDown, IconDatabase, IconKey, IconZap } from 'ui'import ChartHandler from 'components/to-be-cleaned/Charts/ChartHandler'import Panel from 'components/ui/Panel'-import { get } from 'lib/common/fetch'-import { API_URL, DATE_FORMAT, METRICS } from 'lib/constants'+import { DATE_FORMAT, METRICS } from 'lib/constants'import { ChartIntervals } from 'types'+import { useParams } from 'hooks'+import { useProjectLogStatsQuery } from 'data/logs/project-log-stats-query'const CHART_INTERVALS: ChartIntervals[] = [{@@ -25,14 +25,11 @@ interface Props {}const ProjectUsage: FC<Props> = ({}) => {const router = useRouter()- const { ref } = router.query+ const { ref: projectRef } = useParams()const [interval, setInterval] = useState<string>('hourly')- const { data, error }: any = useSWR(- `${API_URL}/projects/${ref}/log-stats?interval=${interval}`,- get- )+ const { data, error } = useProjectLogStatsQuery({ projectRef, interval })const selectedInterval = CHART_INTERVALS.find((i) => i.key === interval) || CHART_INTERVALS[1]const startDate = dayjs()@@ -51,9 +48,9 @@ const ProjectUsage: FC<Props> = ({}) => {if (timestampDigits < 16) {// pad unix timestamp with additional 0 and then forwardconst paddedTimestamp = String(timestamp) + '0'.repeat(16 - timestampDigits)- router.push(`/project/${ref}/logs/edge-logs?te=${paddedTimestamp}`)+ router.push(`/project/${projectRef}/logs/edge-logs?te=${paddedTimestamp}`)} else {- router.push(`/project/${ref}/logs/edge-logs?te=${timestamp}`)+ router.push(`/project/${projectRef}/logs/edge-logs?te=${timestamp}`)}}@@ -94,7 +91,7 @@ const ProjectUsage: FC<Props> = ({}) => {</div>}title="Database"- href={`/project/${ref}/editor`}+ href={`/project/${projectRef}/editor`}/><ChartHandlerstartDate={startDate}@@ -120,7 +117,7 @@ const ProjectUsage: FC<Props> = ({}) => {</div>}title="Auth"- href={`/project/${ref}/auth/users`}+ href={`/project/${projectRef}/auth/users`}/><ChartHandlerstartDate={startDate}@@ -146,7 +143,7 @@ const ProjectUsage: FC<Props> = ({}) => {</div>}title="Storage"- href={`/project/${ref}/storage/buckets`}+ href={`/project/${projectRef}/storage/buckets`}/><ChartHandlerstartDate={startDate}diff --git a/studio/data/logs/keys.ts b/studio/data/logs/keys.tsnew file mode 100644index 00000000000..c9ba47f22aa--- /dev/null+++ b/studio/data/logs/keys.ts@@ -0,0 +1,4 @@+export const logKeys = {+ logStats: (projectRef: string | undefined, interval: string | undefined) =>+ ['projects', projectRef, 'log-stats', interval] as const,+}diff --git a/studio/data/logs/project-log-stats-query.ts b/studio/data/logs/project-log-stats-query.tsnew file mode 100644index 00000000000..b14039751e9--- /dev/null+++ b/studio/data/logs/project-log-stats-query.ts@@ -0,0 +1,64 @@+import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'+import { get } from 'lib/common/fetch'+import { API_URL } from 'lib/constants'+import { useCallback } from 'react'+import { logKeys } from './keys'++export type ProjectLogStatsVariables = {+ projectRef?: string+ interval?: string+}++export type ProjectLogStatsResponse = any++export async function getProjectLogStats(+ { projectRef, interval }: ProjectLogStatsVariables,+ signal?: AbortSignal+) {+ if (!projectRef) {+ throw new Error('projectRef is required')+ }+ if (!interval) {+ throw new Error('interval is required')+ }++ const response = await get(`${API_URL}/projects/${projectRef}/log-stats?interval=${interval}`, {+ signal,+ })+ if (response.error) {+ throw response.error+ }++ return response as ProjectLogStatsResponse+}++export type ProjectLogStatsData = Awaited<ReturnType<typeof getProjectLogStats>>+export type ProjectLogStatsError = unknown++export const useProjectLogStatsQuery = <TData = ProjectLogStatsData>(+ { projectRef, interval }: ProjectLogStatsVariables,+ {+ enabled = true,+ ...options+ }: UseQueryOptions<ProjectLogStatsData, ProjectLogStatsError, TData> = {}+) =>+ useQuery<ProjectLogStatsData, ProjectLogStatsError, TData>(+ logKeys.logStats(projectRef, interval),+ ({ signal }) => getProjectLogStats({ projectRef, interval }, signal),+ {+ enabled: enabled && typeof projectRef !== 'undefined' && typeof interval !== 'undefined',+ ...options,+ }+ )++export const useProjectLogStatsPrefetch = ({ projectRef, interval }: ProjectLogStatsVariables) => {+ const client = useQueryClient()++ return useCallback(() => {+ if (projectRef && interval) {+ client.prefetchQuery(logKeys.logStats(projectRef, interval), ({ signal }) =>+ getProjectLogStats({ projectRef, interval }, signal)+ )+ }+ }, [projectRef, interval])+}