Files
supabase/apps/studio/hooks/analytics/useLogsQuery.tsx
T
1afaae6447 Chore/reports read replicas filter (#23711)
* Scaffold for API reports

* Add support for filtering database reports by replicas

* midway adding read replica support for custom reports

* Refactor reports page to use react query and deprecate project content mobxstore

* Reports add chart labels if metric is read replica specific

* Give an easier way to remove charts from reports

* make reports layout non-blocking

* Update apps/studio/components/interfaces/Reports/Reports.tsx

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>

---------

Co-authored-by: Alaister Young <a@alaisteryoung.com>
Co-authored-by: Alaister Young <alaister@users.noreply.github.com>
2024-05-07 16:44:23 +08:00

91 lines
2.5 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { Dispatch, SetStateAction, useState } from 'react'
import {
EXPLORER_DATEPICKER_HELPERS,
genQueryParams,
getDefaultHelper,
} from 'components/interfaces/Settings/Logs'
import type {
LogData,
Logs,
LogsEndpointParams,
} from 'components/interfaces/Settings/Logs/Logs.types'
import { useReadReplicasQuery } from 'data/read-replicas/replicas-query'
import { get, isResponseOk } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { useDatabaseSelectorStateSnapshot } from 'state/database-selector'
export interface LogsQueryHook {
params: LogsEndpointParams
isLoading: boolean
logData: LogData[]
data?: never
error: string | Object | null
changeQuery: (newQuery?: string) => void
runQuery: () => void
setParams: Dispatch<SetStateAction<LogsEndpointParams>>
}
const useLogsQuery = (
projectRef: string,
initialParams: Partial<LogsEndpointParams> = {}
): LogsQueryHook => {
// [Joshen] TODO once API is unblocked
const state = useDatabaseSelectorStateSnapshot()
const { data: databases } = useReadReplicasQuery({ projectRef })
const defaultHelper = getDefaultHelper(EXPLORER_DATEPICKER_HELPERS)
const [params, setParams] = useState<LogsEndpointParams>({
sql: initialParams?.sql || '',
project: projectRef,
iso_timestamp_start: initialParams.iso_timestamp_start
? initialParams.iso_timestamp_start
: defaultHelper.calcFrom(),
iso_timestamp_end: initialParams.iso_timestamp_end
? initialParams.iso_timestamp_end
: defaultHelper.calcTo(),
})
const enabled = typeof projectRef !== 'undefined' && Boolean(params.sql)
const queryParams = genQueryParams(params as any)
const {
data,
error: rqError,
isLoading,
isRefetching,
refetch,
} = useQuery(
['projects', projectRef, 'logs', queryParams],
({ signal }) =>
get<Logs>(`${API_URL}/projects/${projectRef}/analytics/endpoints/logs.all?${queryParams}`, {
signal,
}),
{
enabled,
refetchOnWindowFocus: false,
}
)
let error: null | string | object = rqError ? (rqError as any).message : null
if (!error && data?.error) {
error = data?.error
}
const changeQuery = (newQuery = '') => {
setParams((prev) => ({ ...prev, sql: newQuery }))
}
return {
params,
isLoading: (enabled && isLoading) || isRefetching,
logData: isResponseOk(data) && data.result ? data.result : [],
error,
changeQuery,
runQuery: () => refetch(),
setParams,
}
}
export default useLogsQuery