Files
supabase/apps/studio/data/usage/org-usage-query.ts
Kevin Grüneberg fc5d389a9f feat: rework usage overview (#19434)
- Added a quick indicator about over-usage on non-usage-based plans (free or pro with spend cap) at the top of the subscription page
- Removed the usage summary from upcoming invoice (replacement on the usage page)
- Added a new usage summary on the organization usage page
- Per-project breakdown for usage
- Displays costs for over-usage on usage-based plans
- Improved scrolling behaviour for anchors on usage page
- Metrics with higher usage/costs will be sorted to the top
- Insights into compute usage in summary
- Removed duplicate typing and rely on API types for usage DTO
- Usage can now be retrieved for a custom period and not just the current billing cycle
- Usage can be filtered by project
2023-12-05 16:50:02 +01:00

48 lines
1.5 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get } from 'data/fetchers'
import { usageKeys } from './keys'
import { ResponseError } from 'types'
import { components } from 'data/api'
export type OrgUsageVariables = {
orgSlug?: string
projectRef?: string
start?: Date
end?: Date
}
export type OrgUsageResponse = components['schemas']['OrgUsageResponse']
export type OrgMetricsUsage = components['schemas']['OrgMetricUsage']
export async function getOrgUsage(
{ orgSlug, projectRef, start, end }: OrgUsageVariables,
signal?: AbortSignal
): Promise<OrgUsageResponse> {
if (!orgSlug) throw new Error('orgSlug is required')
const { data, error } = await get(`/platform/organizations/{slug}/usage`, {
params: {
path: { slug: orgSlug },
query: { project_ref: projectRef, start: start?.toISOString(), end: end?.toISOString() },
},
signal,
})
if (error) throw error
return data
}
export type OrgUsageData = Awaited<ReturnType<typeof getOrgUsage>>
export type OrgUsageError = ResponseError
export const useOrgUsageQuery = <TData = OrgUsageData>(
{ orgSlug, projectRef, start, end }: OrgUsageVariables,
{ enabled = true, ...options }: UseQueryOptions<OrgUsageData, OrgUsageError, TData> = {}
) =>
useQuery<OrgUsageData, OrgUsageError, TData>(
usageKeys.orgUsage(orgSlug, projectRef, start?.toISOString(), end?.toISOString()),
({ signal }) => getOrgUsage({ orgSlug, projectRef, start, end }, signal),
{
enabled: enabled && typeof orgSlug !== 'undefined',
...options,
}
)