Files
supabase/apps/studio/data/analytics/project-log-stats-query.ts
T
9d534c9f5a fix: response error codes (#30581)
* fix: response error codes

* upgrade docs

* remove request url modification middleware

* move api routes for self-hosted to platform folder

* remove some lib/common/fetch usage

* docs: use middleware for openapi-fetch (#30600)

Get rid of the unauthedAllowedPost function (I don't think there's any harm in letting any requests that require authentication to just 403, they should be disabled at the React Query level and if not they will fail gracefully enough...)

* fix local count query

* add default values for clone mutation

* fix ts and codegen

* add missing lodash dep to playwright tests

* Fix the playwright tests to match the new folder structure for selfhosted variant.

* remove unused import

* Remove unused state

* remove unused sql debug mutation

* remove unused export

* fix notifications query

* fix jwt updating status

* fix typescript

* save sql snippet after renaming

* update codegen & fix ts error

* override array querySerializer

---------

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2025-01-20 11:27:38 +08:00

78 lines
2.1 KiB
TypeScript

import { QueryClient, useQuery, UseQueryOptions } from '@tanstack/react-query'
import { operations } from 'api-types'
import { get, handleError } from 'data/fetchers'
import { analyticsKeys } from './keys'
export type ProjectLogStatsVariables = {
projectRef?: string
interval?: 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
}
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
}: UseQueryOptions<ProjectLogStatsData, ProjectLogStatsError, TData> = {}
) =>
useQuery<ProjectLogStatsData, ProjectLogStatsError, TData>(
analyticsKeys.usageApiCounts(projectRef, interval),
({ 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(analyticsKeys.usageApiCounts(projectRef, interval), ({ signal }) =>
getProjectLogStats({ projectRef, interval }, signal)
)
}