Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: convert logs.all endpoints to new fetchers (#33097)

Alexandre Villeneuveb122914b1276
diff --git a/apps/studio/components/interfaces/Settings/Logs/Logs.constants.ts b/apps/studio/components/interfaces/Settings/Logs/Logs.constants.ts
index 4a79934d899..606d3c59950 100644
--- a/apps/studio/components/interfaces/Settings/Logs/Logs.constants.ts
+++ b/apps/studio/components/interfaces/Settings/Logs/Logs.constants.ts
@@ -397,17 +397,6 @@ export const LOGS_SOURCE_DESCRIPTION = {
[LogsTableName.PG_CRON]: 'Postgres logs from pg_cron cron jobs',
}
-export const genQueryParams = (params: { [k: string]: string }) => {
- // remove keys which are empty strings, null, or undefined
- for (const k in params) {
- const v = params[k]
- if (v === null || v === '' || v === undefined) {
- delete params[k]
- }
- }
- const qs = new URLSearchParams(params).toString()
- return qs
-}
export const FILTER_OPTIONS: FilterTableSet = {
// Postgres logs
postgres_logs: {
diff --git a/apps/studio/hooks/analytics/useLogsPreview.tsx b/apps/studio/hooks/analytics/useLogsPreview.tsx
index babc968e005..97d97f94aca 100644
--- a/apps/studio/hooks/analytics/useLogsPreview.tsx
+++ b/apps/studio/hooks/analytics/useLogsPreview.tsx
@@ -5,7 +5,6 @@ import { Dispatch, SetStateAction, useEffect, useMemo, useRef, useState } from '
import {
LogsTableName,
PREVIEWER_DATEPICKER_HELPERS,
- genQueryParams,
getDefaultHelper,
} from 'components/interfaces/Settings/Logs/Logs.constants'
import type {
@@ -22,11 +21,10 @@ import {
genCountQuery,
genDefaultQuery,
} from 'components/interfaces/Settings/Logs/Logs.utils'
-import { get, isResponseOk } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
+import { get } from 'data/fetchers'
+import { parseAsString, useQueryStates } from 'nuqs'
import { useFillTimeseriesSorted } from './useFillTimeseriesSorted'
import useTimeseriesUnixToIso from './useTimeseriesUnixToIso'
-import { parseAsString, useQueryStates } from 'nuqs'
interface LogsPreviewHook {
logData: LogData[]
@@ -82,18 +80,28 @@ function useLogsPreview({
refetch,
} = useInfiniteQuery(
['projects', projectRef, 'logs', params],
- ({ signal, pageParam }) => {
- const uri = `${API_URL}/projects/${projectRef}/analytics/endpoints/logs.all?${genQueryParams({
- ...params,
- // don't overwrite unless user has already clicked on load older
- iso_timestamp_end: pageParam || params.iso_timestamp_end,
- } as any)}`
- return get<Logs>(uri, { signal })
+ async ({ signal, pageParam }) => {
+ const { data, error } = await get(`/platform/projects/{ref}/analytics/endpoints/logs.all`, {
+ params: {
+ path: { ref: projectRef },
+ query: {
+ ...params,
+ // don't overwrite unless user has already clicked on load older
+ iso_timestamp_end: pageParam || params.iso_timestamp_end,
+ },
+ },
+ signal,
+ })
+ if (error) {
+ throw error
+ }
+
+ return data as unknown as Logs
},
{
refetchOnWindowFocus: false,
getNextPageParam(lastPage) {
- if (!isResponseOk(lastPage) || (lastPage.result?.length ?? 0) === 0) {
+ if ((lastPage.result?.length ?? 0) === 0) {
return undefined
}
const len = lastPage.result.length
@@ -120,7 +128,7 @@ function useLogsPreview({
let error: null | string | object = rqError ? (rqError as any).message : null
data?.pages.forEach((response) => {
- if (isResponseOk(response) && response.result) {
+ if (response.result) {
logData = [...logData, ...response.result]
}
if (!error && response && response.error) {
@@ -133,14 +141,6 @@ function useLogsPreview({
return { logData, error, oldestTimestamp }
}, [data?.pages])
- const countUrl = () => {
- return `${API_URL}/projects/${projectRef}/analytics/endpoints/logs.all?${genQueryParams({
- ...params,
- sql: genCountQuery(table, filters),
- iso_timestamp_start: latestRefresh,
- } as any)}`
- }
-
const { data: countData } = useQuery(
[
'projects',
@@ -148,7 +148,24 @@ function useLogsPreview({
'logs-count',
{ ...params, sql: genCountQuery(table, filters), iso_timestamp_start: latestRefresh },
],
- ({ signal }) => get<Count>(countUrl(), { signal }),
+ async ({ signal }) => {
+ const { data, error } = await get(`/platform/projects/{ref}/analytics/endpoints/logs.all`, {
+ params: {
+ path: { ref: projectRef },
+ query: {
+ ...params,
+ sql: genCountQuery(table, filters),
+ iso_timestamp_start: latestRefresh,
+ },
+ },
+ signal,
+ })
+ if (error) {
+ throw error
+ }
+
+ return data as unknown as Count
+ },
{
refetchOnWindowFocus: false,
// refresh each minute only
@@ -158,23 +175,13 @@ function useLogsPreview({
}
)
- const newCount = isResponseOk(countData) ? countData.result?.[0]?.count ?? 0 : 0
+ const newCount = countData?.result?.[0]?.count ?? 0
// chart data
const chartQuery = useMemo(
() => genChartQuery(table, params, filters),
[table, params.iso_timestamp_end, params.project, filters]
)
-
- const chartUrl = useMemo(() => {
- return `${API_URL}/projects/${projectRef}/analytics/endpoints/logs.all?${genQueryParams({
- iso_timestamp_start: params.iso_timestamp_start ?? '',
- iso_timestamp_end: params.iso_timestamp_end ?? '',
- project: params.project ?? '',
- sql: chartQuery,
- })}`
- }, [projectRef, params.iso_timestamp_start, params.iso_timestamp_end, params.project, chartQuery])
-
const { data: eventChartResponse, refetch: refreshEventChart } = useQuery(
[
'projects',
@@ -182,7 +189,25 @@ function useLogsPreview({
'logs-chart',
{ iso_timestamp_end: params.iso_timestamp_end, project: params.project, sql: chartQuery },
],
- ({ signal }) => get<EventChart>(chartUrl, { signal }),
+ async ({ signal }) => {
+ const { data, error } = await get(`/platform/projects/{ref}/analytics/endpoints/logs.all`, {
+ params: {
+ path: { ref: projectRef },
+ query: {
+ iso_timestamp_start: params.iso_timestamp_start ?? '',
+ iso_timestamp_end: params.iso_timestamp_end ?? '',
+ project: params.project ?? '',
+ sql: chartQuery,
+ },
+ },
+ signal,
+ })
+ if (error) {
+ throw error
+ }
+
+ return data as unknown as EventChart
+ },
{ refetchOnWindowFocus: false }
)
@@ -207,7 +232,7 @@ function useLogsPreview({
}
const normalizedEventChartData = useTimeseriesUnixToIso(
- (isResponseOk(eventChartResponse) && eventChartResponse.result) || [],
+ eventChartResponse?.result ?? [],
'timestamp'
)
diff --git a/apps/studio/hooks/analytics/useLogsQuery.tsx b/apps/studio/hooks/analytics/useLogsQuery.tsx
index 0452722bb38..29ac83f2e99 100644
--- a/apps/studio/hooks/analytics/useLogsQuery.tsx
+++ b/apps/studio/hooks/analytics/useLogsQuery.tsx
@@ -1,9 +1,9 @@
import { useQuery } from '@tanstack/react-query'
import { Dispatch, SetStateAction, useState } from 'react'
+import { IS_PLATFORM } from 'common'
import {
EXPLORER_DATEPICKER_HELPERS,
- genQueryParams,
getDefaultHelper,
} from 'components/interfaces/Settings/Logs/Logs.constants'
import type {
@@ -11,14 +11,11 @@ import type {
Logs,
LogsEndpointParams,
} from 'components/interfaces/Settings/Logs/Logs.types'
-import { get, isResponseOk } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
import {
checkForILIKEClause,
- checkForWildcard,
checkForWithClause,
} from 'components/interfaces/Settings/Logs/Logs.utils'
-import { IS_PLATFORM } from 'common'
+import { get } from 'data/fetchers'
export interface LogsQueryHook {
params: LogsEndpointParams
@@ -51,8 +48,6 @@ const useLogsQuery = (
const _enabled = enabled && typeof projectRef !== 'undefined' && Boolean(params.sql)
- const queryParams = genQueryParams(params as any)
-
const usesWith = checkForWithClause(params.sql || '')
const usesILIKE = checkForILIKEClause(params.sql || '')
@@ -63,11 +58,21 @@ const useLogsQuery = (
isRefetching,
refetch,
} = useQuery(
- ['projects', projectRef, 'logs', queryParams],
- ({ signal }) =>
- get<Logs>(`${API_URL}/projects/${projectRef}/analytics/endpoints/logs.all?${queryParams}`, {
+ ['projects', projectRef, 'logs', params],
+ async ({ signal }) => {
+ const { data, error } = await get(`/platform/projects/{ref}/analytics/endpoints/logs.all`, {
+ params: {
+ path: { ref: projectRef },
+ query: params,
+ },
signal,
- }),
+ })
+ if (error) {
+ throw error
+ }
+
+ return data as unknown as Logs
+ },
{
enabled: _enabled,
refetchOnWindowFocus: false,
@@ -101,7 +106,7 @@ const useLogsQuery = (
return {
params,
isLoading: (_enabled && isLoading) || isRefetching,
- logData: isResponseOk(data) && data.result ? data.result : [],
+ logData: data?.result ?? [],
error,
changeQuery,
runQuery: () => refetch(),
diff --git a/apps/studio/hooks/analytics/useSingleLog.tsx b/apps/studio/hooks/analytics/useSingleLog.tsx
index e4e293a93c7..291c17ecb47 100644
--- a/apps/studio/hooks/analytics/useSingleLog.tsx
+++ b/apps/studio/hooks/analytics/useSingleLog.tsx
@@ -1,5 +1,5 @@
import { useQuery } from '@tanstack/react-query'
-import { LOGS_TABLES, genQueryParams } from 'components/interfaces/Settings/Logs/Logs.constants'
+import { LOGS_TABLES } from 'components/interfaces/Settings/Logs/Logs.constants'
import type {
LogData,
Logs,
@@ -7,8 +7,7 @@ import type {
QueryType,
} from 'components/interfaces/Settings/Logs/Logs.types'
import { genSingleLogQuery } from 'components/interfaces/Settings/Logs/Logs.utils'
-import { get } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
+import { get } from 'data/fetchers'
interface SingleLogHook {
data: LogData | undefined
@@ -34,10 +33,6 @@ function useSingleLog({
const params: LogsEndpointParams = { ...paramsToMerge, project: projectRef, sql }
- const endpointUrl = `${API_URL}/projects/${projectRef}/analytics/endpoints/logs.all?${genQueryParams(
- params as any
- )}`
-
const isWarehouseQuery = queryType === 'warehouse'
// Warehouse queries are handled differently
const enabled = Boolean(id && table && !isWarehouseQuery)
@@ -50,7 +45,20 @@ function useSingleLog({
refetch,
} = useQuery(
['projects', projectRef, 'single-log', id, queryType],
- ({ signal }) => get(endpointUrl, { signal }) as Promise<Logs>,
+ async ({ signal }) => {
+ const { data, error } = await get(`/platform/projects/{ref}/analytics/endpoints/logs.all`, {
+ params: {
+ path: { ref: projectRef },
+ query: params,
+ },
+ signal,
+ })
+ if (error) {
+ throw error
+ }
+
+ return data as unknown as Logs
+ },
{
enabled,
refetchOnWindowFocus: false,