Files
supabase/apps/studio/data/analytics/org-daily-stats-query.ts
Gildas GarciaandAlaister Young 737b8595f2 Update API types (#50234)
## Problem

platform, v1 and v2 have been already completely migrated and introduced
some changes.

Some types have been renamed, some outputs and inputs updated.

## Solution

- Update the API types
- Fix the TS errors

## Update

Taking this over to unblock #50134, which needs the new scoped token
permission ids from the regenerated types.

- Merged `master`.
- Regenerated `api-v2.d.ts` from the production spec. The previous files
came from a local API that exposed a webhook events endpoint production
doesn't have yet. Production has since added standardized 400 error
responses on the v2 organization endpoints. `api-v1.d.ts` and
`platform.d.ts` already matched production.
- Fixed `verify-production-types`. It formatted the regenerated files in
a temp directory outside the repository, so Prettier fell back to its
defaults and the comparison could never match the committed files. It
now passes the repository config explicitly. `pnpm api:verify-types`
passes on this branch.
- Verified locally: `pnpm typecheck`, `pnpm api:verify-types`, Studio
unit tests.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Preserved descriptions when saving, sharing, moving, or unsharing
notebooks, reports, SQL snippets, and saved queries.
* Improved handling of empty or null values across notebook
descriptions, billing usage, pooler settings, and infrastructure fields.
* Improved read-replica connection handling, including read-only
connection strings.
* Updated storage configuration and capability handling to match current
settings.

* **API and Compatibility**
* Updated organization, project, storage, OAuth, billing, and
infrastructure data handling to match current API responses.
  * OAuth app creation and updates now require scopes.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-09-11 12:17:49 +08:00

175 lines
5.8 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import type { components } from 'api-types'
import { analyticsKeys } from './keys'
import { get, handleError } from '@/data/fetchers'
import type { ResponseError, UseCustomQueryOptions } from '@/types'
export enum EgressType {
REST = 'egress_rest',
AUTH = 'egress_auth',
STORAGE = 'egress_storage',
REALTIME = 'egress_realtime',
FUNCTIONS = 'egress_function',
SUPAVISOR = 'egress_supavisor',
LOGDRAIN = 'egress_logdrain',
}
// [Joshen] Get this from common package instead of API and dashboard having one copy each
export enum PricingMetric {
EGRESS = 'EGRESS',
CACHED_EGRESS = 'CACHED_EGRESS',
DATABASE_SIZE = 'DATABASE_SIZE',
STORAGE_SIZE = 'STORAGE_SIZE',
DISK_SIZE_GB_HOURS_GP3 = 'DISK_SIZE_GB_HOURS_GP3',
DISK_SIZE_GB_HOURS_IO2 = 'DISK_SIZE_GB_HOURS_IO2',
MONTHLY_ACTIVE_USERS = 'MONTHLY_ACTIVE_USERS',
MONTHLY_ACTIVE_SSO_USERS = 'MONTHLY_ACTIVE_SSO_USERS',
MONTHLY_ACTIVE_THIRD_PARTY_USERS = 'MONTHLY_ACTIVE_THIRD_PARTY_USERS',
FUNCTION_INVOCATIONS = 'FUNCTION_INVOCATIONS',
STORAGE_IMAGES_TRANSFORMED = 'STORAGE_IMAGES_TRANSFORMED',
REALTIME_MESSAGE_COUNT = 'REALTIME_MESSAGE_COUNT',
REALTIME_PEAK_CONNECTIONS = 'REALTIME_PEAK_CONNECTIONS',
CUSTOM_DOMAIN = 'CUSTOM_DOMAIN',
IPV4 = 'IPV4',
PITR_7 = 'PITR_7',
PITR_14 = 'PITR_14',
PITR_28 = 'PITR_28',
DISK_IOPS_GP3 = 'DISK_IOPS_GP3',
DISK_IOPS_IO2 = 'DISK_IOPS_IO2',
DISK_THROUGHPUT_GP3 = 'DISK_THROUGHPUT_GP3',
LOG_DRAIN = 'LOG_DRAIN',
LOG_DRAIN_EVENTS = 'LOG_DRAIN_EVENTS',
AUTH_MFA_PHONE = 'AUTH_MFA_PHONE',
AUTH_MFA_WEB_AUTHN = 'AUTH_MFA_WEB_AUTHN',
ACTIVE_COMPUTE_HOURS = 'ACTIVE_COMPUTE_HOURS',
LOG_INGESTION = 'LOG_INGESTION',
LOG_QUERYING = 'LOG_QUERYING',
LOG_STORAGE = 'LOG_STORAGE',
ETL_PIPELINE = 'ETL_PIPELINE',
ETL_REPLICATED_DATA = 'ETL_REPLICATED_DATA',
ETL_COPY_BACKFILL_DATA = 'ETL_COPY_BACKFILL_DATA',
}
export enum ComputeUsageMetric {
COMPUTE_HOURS_BRANCH = 'COMPUTE_HOURS_BRANCH',
COMPUTE_HOURS_XS = 'COMPUTE_HOURS_XS',
COMPUTE_HOURS_SM = 'COMPUTE_HOURS_SM',
COMPUTE_HOURS_MD = 'COMPUTE_HOURS_MD',
COMPUTE_HOURS_L = 'COMPUTE_HOURS_L',
COMPUTE_HOURS_XL = 'COMPUTE_HOURS_XL',
COMPUTE_HOURS_2XL = 'COMPUTE_HOURS_2XL',
COMPUTE_HOURS_4XL = 'COMPUTE_HOURS_4XL',
COMPUTE_HOURS_8XL = 'COMPUTE_HOURS_8XL',
COMPUTE_HOURS_12XL = 'COMPUTE_HOURS_12XL',
COMPUTE_HOURS_16XL = 'COMPUTE_HOURS_16XL',
COMPUTE_HOURS_24XL = 'COMPUTE_HOURS_24XL',
COMPUTE_HOURS_24XL_OPTIMIZED_CPU = 'COMPUTE_HOURS_24XL_OPTIMIZED_CPU',
COMPUTE_HOURS_24XL_OPTIMIZED_MEMORY = 'COMPUTE_HOURS_24XL_OPTIMIZED_MEMORY',
COMPUTE_HOURS_24XL_HIGH_MEMORY = 'COMPUTE_HOURS_24XL_HIGH_MEMORY',
COMPUTE_HOURS_48XL = 'COMPUTE_HOURS_48XL',
COMPUTE_HOURS_48XL_OPTIMIZED_CPU = 'COMPUTE_HOURS_48XL_OPTIMIZED_CPU',
COMPUTE_HOURS_48XL_OPTIMIZED_MEMORY = 'COMPUTE_HOURS_48XL_OPTIMIZED_MEMORY',
COMPUTE_HOURS_48XL_HIGH_MEMORY = 'COMPUTE_HOURS_48XL_HIGH_MEMORY',
}
export const computeUsageMetricLabel = (computeUsageMetric: ComputeUsageMetric) => {
switch (computeUsageMetric) {
case 'COMPUTE_HOURS_BRANCH':
return 'Branching'
case 'COMPUTE_HOURS_XS':
return 'Micro'
case 'COMPUTE_HOURS_SM':
return 'Small'
case 'COMPUTE_HOURS_MD':
return 'Medium'
case 'COMPUTE_HOURS_L':
return 'Large'
case 'COMPUTE_HOURS_XL':
return 'XL'
case 'COMPUTE_HOURS_2XL':
return '2XL'
case 'COMPUTE_HOURS_4XL':
return '4XL'
case 'COMPUTE_HOURS_8XL':
return '8XL'
case 'COMPUTE_HOURS_12XL':
return '12XL'
case 'COMPUTE_HOURS_16XL':
return '16XL'
case 'COMPUTE_HOURS_24XL':
return '24XL'
case 'COMPUTE_HOURS_24XL_OPTIMIZED_CPU':
return '24XL - Optimized CPU'
case 'COMPUTE_HOURS_24XL_OPTIMIZED_MEMORY':
return '24XL - Optimized Memory'
case 'COMPUTE_HOURS_24XL_HIGH_MEMORY':
return '24XL - High Memory'
case 'COMPUTE_HOURS_48XL':
return '48XL'
case 'COMPUTE_HOURS_48XL_OPTIMIZED_CPU':
return '48XL - Optimized CPU'
case 'COMPUTE_HOURS_48XL_OPTIMIZED_MEMORY':
return '48XL - Optimized Memory'
case 'COMPUTE_HOURS_48XL_HIGH_MEMORY':
return '48XL - High Memory'
}
}
export type OrgDailyStatsVariables = {
// API parameters
orgSlug?: string
startDate?: string
endDate?: string
projectRef?: string
}
export type OrgDailyUsageResponse = components['schemas']['OrgDailyUsageResponse_Output']
export async function getOrgDailyStats(
{ orgSlug, startDate, endDate, projectRef }: OrgDailyStatsVariables,
signal?: AbortSignal
) {
if (!orgSlug) throw new Error('Org slug is required')
if (!startDate) throw new Error('Start date is required')
if (!endDate) throw new Error('Start date is required')
const { data, error } = await get('/platform/organizations/{slug}/usage/daily', {
params: {
path: { slug: orgSlug },
query: {
start: startDate,
end: endDate,
project_ref: projectRef,
},
},
signal,
})
if (error) handleError(error)
return data
}
export type OrgDailyStatsData = Awaited<ReturnType<typeof getOrgDailyStats>>
export type OrgDailyStatsError = ResponseError
export const useOrgDailyStatsQuery = <TData = OrgDailyStatsData>(
{ orgSlug, startDate, endDate, projectRef }: OrgDailyStatsVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<OrgDailyStatsData, OrgDailyStatsError, TData> = {}
) =>
useQuery<OrgDailyStatsData, OrgDailyStatsError, TData>({
queryKey: analyticsKeys.orgDailyStats(orgSlug, { startDate, endDate, projectRef }),
queryFn: ({ signal }) => getOrgDailyStats({ orgSlug, startDate, endDate, projectRef }, signal),
enabled:
enabled &&
typeof orgSlug !== 'undefined' &&
typeof startDate !== 'undefined' &&
typeof endDate !== 'undefined',
staleTime: 1000 * 60 * 60,
...options,
})