Files
supabase/apps/studio/data/analytics/project-log-stats-query.ts
T
Jordi Enric db0a2ab752 refactor useFillTimeSeriesSorted hook (#42255)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Safer error rendering across analytics and reporting with a fallback
"Unknown error".

* **Tests**
* Added unit tests covering timeseries sorting and timestamp validation.

* **Refactor**
* Standardized timeseries hook and all callers to accept a single
options object and improved nullish handling.

* **New Features**
* Exposed timeseries utilities and explicit options/result types;
exported chart data type.

* **Chores**
  * Relaxed index signatures to allow dynamic metric keys.

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-01-29 21:05:09 +01:00

82 lines
2.3 KiB
TypeScript

import { QueryClient, useQuery } from '@tanstack/react-query'
import { operations } from 'api-types'
import { get, handleError } from 'data/fetchers'
import { UseCustomQueryOptions } from 'types'
import { analyticsKeys } from './keys'
export type ProjectLogStatsVariables = {
projectRef?: string
interval?: NonNullable<
operations['UsageApiController_getApiCounts']['parameters']['query']
>['interval']
}
export type ProjectLogStatsResponse = {
result: UsageApiCounts[]
}
export interface UsageApiCounts {
total_auth_requests: number
total_storage_requests: number
total_rest_requests: number
total_realtime_requests: number
timestamp: string
[key: string]: string | number
}
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 { data, error } = await get(
'/platform/projects/{ref}/analytics/endpoints/usage.api-counts',
{
params: {
path: { ref: projectRef },
query: {
interval,
},
},
signal,
}
)
if (error) handleError(error)
return data as unknown as ProjectLogStatsResponse
}
export type ProjectLogStatsData = Awaited<ReturnType<typeof getProjectLogStats>>
export type ProjectLogStatsError = unknown
export const useProjectLogStatsQuery = <TData = ProjectLogStatsData>(
{ projectRef, interval }: ProjectLogStatsVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<ProjectLogStatsData, ProjectLogStatsError, TData> = {}
) =>
useQuery<ProjectLogStatsData, ProjectLogStatsError, TData>({
queryKey: analyticsKeys.usageApiCounts(projectRef, interval),
queryFn: ({ signal }) => getProjectLogStats({ projectRef, interval }, signal),
enabled: enabled && typeof projectRef !== 'undefined' && typeof interval !== 'undefined',
...options,
})
export function prefetchProjectLogStats(
client: QueryClient,
{ projectRef, interval }: ProjectLogStatsVariables
) {
return client.fetchQuery({
queryKey: analyticsKeys.usageApiCounts(projectRef, interval),
queryFn: ({ signal }) => getProjectLogStats({ projectRef, interval }, signal),
})
}