mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem A custom date range in a Storage report URL could show one window in the header and another in the charts. Refresh also left Database Size and several Auth charts showing cached results. ## Fix Use the selected window for all Storage report requests, run the Database Size query on refresh, and refresh Auth Usage, Errors and Latency alongside the shared API charts. ## How to test - Open Storage with a custom start and end time in the URL. Inspect the chart requests on load and after Refresh. Both sets should use those exact timestamps. - Refresh the Database report and check that the large objects query runs again. - Open Auth with a fixed range and press Shift+R. The Usage, Errors, Latency and API charts should request fresh data for the same range. Fixes [FE-5122](https://linear.app/supabase/issue/FE-5122).
210 lines
6.9 KiB
TypeScript
210 lines
6.9 KiB
TypeScript
import { FeatureFlagContext, useFlag, useParams } from 'common'
|
|
import isEqual from 'lodash/isEqual'
|
|
import { useContext, useEffect, useState } from 'react'
|
|
|
|
import { PRESET_CONFIG } from '@/components/interfaces/Reports/Reports.constants'
|
|
import { ReportFilterItem } from '@/components/interfaces/Reports/Reports.types'
|
|
import { getLogsSql, queriesFactory } from '@/components/interfaces/Reports/Reports.utils'
|
|
import type { LogsEndpointParams } from '@/components/interfaces/Settings/Logs/Logs.types'
|
|
import type { ReportDateRange } from '@/hooks/misc/useReportDateRange'
|
|
import { IS_PLATFORM } from '@/lib/constants'
|
|
import { useDatabaseSelectorStateSnapshot } from '@/state/database-selector'
|
|
|
|
export const useStorageReport = (dateRange: ReportDateRange) => {
|
|
const { ref: projectRef } = useParams()
|
|
const [filters, setFilters] = useState<ReportFilterItem[]>([])
|
|
const state = useDatabaseSelectorStateSnapshot()
|
|
const desiredUseOtel = useFlag('otelReports')
|
|
const { hasLoaded: hasLoadedFlags } = useContext(FeatureFlagContext)
|
|
const [appliedUseOtel, setAppliedUseOtel] = useState<boolean | null>(null)
|
|
|
|
const identifier = state.selectedDatabaseId
|
|
const hasAppliedQueryMode = appliedUseOtel !== null
|
|
const useOtel = appliedUseOtel ?? false
|
|
const resolvedProjectRef = IS_PLATFORM ? projectRef : (projectRef ?? 'default')
|
|
const hasResolvedProjectRef = !IS_PLATFORM || projectRef !== undefined
|
|
const areQueriesReady = hasAppliedQueryMode && hasResolvedProjectRef
|
|
|
|
const dateRangeParams = {
|
|
iso_timestamp_start: dateRange.period_start.date,
|
|
iso_timestamp_end: dateRange.period_end.date,
|
|
}
|
|
|
|
const queryHooks = queriesFactory<keyof typeof PRESET_CONFIG.api.queries>(
|
|
PRESET_CONFIG.api.queries,
|
|
resolvedProjectRef,
|
|
useOtel,
|
|
areQueriesReady,
|
|
dateRangeParams
|
|
)
|
|
const storageQueryHooks = queriesFactory<keyof typeof PRESET_CONFIG.storage.queries>(
|
|
PRESET_CONFIG.storage.queries,
|
|
resolvedProjectRef,
|
|
useOtel,
|
|
areQueriesReady,
|
|
dateRangeParams
|
|
)
|
|
const totalRequests = queryHooks.totalRequests()
|
|
const topRoutes = queryHooks.topRoutes()
|
|
const errorCounts = queryHooks.errorCounts()
|
|
const topErrorRoutes = queryHooks.topErrorRoutes()
|
|
const responseSpeed = queryHooks.responseSpeed()
|
|
const topSlowRoutes = queryHooks.topSlowRoutes()
|
|
const networkTraffic = queryHooks.networkTraffic()
|
|
const cacheHitRate = storageQueryHooks.cacheHitRate()
|
|
const topCacheMisses = storageQueryHooks.topCacheMisses()
|
|
const activeHooks = [
|
|
totalRequests,
|
|
topRoutes,
|
|
errorCounts,
|
|
topErrorRoutes,
|
|
responseSpeed,
|
|
topSlowRoutes,
|
|
networkTraffic,
|
|
cacheHitRate,
|
|
topCacheMisses,
|
|
]
|
|
|
|
const handleRefresh = async () => {
|
|
if (!areQueriesReady) return
|
|
activeHooks.forEach((hook) => hook.runQuery())
|
|
}
|
|
const handleSetParams = (params: Partial<LogsEndpointParams>) => {
|
|
activeHooks.forEach((hook) => {
|
|
hook.setParams?.((prev: LogsEndpointParams) => ({ ...prev, ...params }))
|
|
})
|
|
}
|
|
|
|
const addFilter = (filter: ReportFilterItem) => {
|
|
// use a deep equal when comparing objects.
|
|
if (filters.some((f) => isEqual(f, filter))) return
|
|
setFilters((prev) =>
|
|
[...prev, filter].sort((a, b) => {
|
|
const keyA = a.key.toLowerCase()
|
|
const keyB = b.key.toLowerCase()
|
|
if (keyA < keyB) {
|
|
return -1
|
|
}
|
|
if (keyA > keyB) {
|
|
return 1
|
|
}
|
|
return 0
|
|
})
|
|
)
|
|
}
|
|
const removeFilter = (filter: ReportFilterItem) => removeFilters([filter])
|
|
const removeFilters = (toRemove: ReportFilterItem[]) => {
|
|
setFilters((prev) => {
|
|
return prev.filter((f) => !toRemove.find((r) => isEqual(f, r)))
|
|
})
|
|
}
|
|
|
|
const formattedFilters: ReportFilterItem[] = [
|
|
...filters,
|
|
...(identifier !== undefined
|
|
? [{ key: 'identifier', value: identifier, compare: 'is' } as ReportFilterItem]
|
|
: []),
|
|
]
|
|
|
|
useEffect(() => {
|
|
if (IS_PLATFORM && !hasLoadedFlags) return
|
|
|
|
const nextUseOtel = IS_PLATFORM ? desiredUseOtel : false
|
|
|
|
if (totalRequests.changeQuery) {
|
|
totalRequests.changeQuery(
|
|
getLogsSql(PRESET_CONFIG.api.queries.totalRequests, formattedFilters, nextUseOtel)
|
|
)
|
|
}
|
|
if (topRoutes.changeQuery) {
|
|
topRoutes.changeQuery(
|
|
getLogsSql(PRESET_CONFIG.api.queries.topRoutes, formattedFilters, nextUseOtel)
|
|
)
|
|
}
|
|
if (errorCounts.changeQuery) {
|
|
errorCounts.changeQuery(
|
|
getLogsSql(PRESET_CONFIG.api.queries.errorCounts, formattedFilters, nextUseOtel)
|
|
)
|
|
}
|
|
|
|
if (topErrorRoutes.changeQuery) {
|
|
topErrorRoutes.changeQuery(
|
|
getLogsSql(PRESET_CONFIG.api.queries.topErrorRoutes, formattedFilters, nextUseOtel)
|
|
)
|
|
}
|
|
if (responseSpeed.changeQuery) {
|
|
responseSpeed.changeQuery(
|
|
getLogsSql(PRESET_CONFIG.api.queries.responseSpeed, formattedFilters, nextUseOtel)
|
|
)
|
|
}
|
|
|
|
if (topSlowRoutes.changeQuery) {
|
|
topSlowRoutes.changeQuery(
|
|
getLogsSql(PRESET_CONFIG.api.queries.topSlowRoutes, formattedFilters, nextUseOtel)
|
|
)
|
|
}
|
|
|
|
if (networkTraffic.changeQuery) {
|
|
networkTraffic.changeQuery(
|
|
getLogsSql(PRESET_CONFIG.api.queries.networkTraffic, formattedFilters, nextUseOtel)
|
|
)
|
|
}
|
|
|
|
if (cacheHitRate.changeQuery) {
|
|
cacheHitRate.changeQuery(
|
|
getLogsSql(PRESET_CONFIG.storage.queries.cacheHitRate, [], nextUseOtel)
|
|
)
|
|
}
|
|
|
|
if (topCacheMisses.changeQuery) {
|
|
topCacheMisses.changeQuery(
|
|
getLogsSql(PRESET_CONFIG.storage.queries.topCacheMisses, [], nextUseOtel)
|
|
)
|
|
}
|
|
setAppliedUseOtel(nextUseOtel)
|
|
}, [JSON.stringify(formattedFilters), desiredUseOtel, hasLoadedFlags])
|
|
|
|
const isLoading = !areQueriesReady || activeHooks.some((hook) => hook.isLoading)
|
|
|
|
return {
|
|
data: {
|
|
totalRequests: totalRequests.logData,
|
|
errorCounts: errorCounts.logData,
|
|
responseSpeed: responseSpeed.logData,
|
|
topRoutes: topRoutes.logData,
|
|
topErrorRoutes: topErrorRoutes.logData,
|
|
topSlowRoutes: topSlowRoutes.logData,
|
|
networkTraffic: networkTraffic.logData,
|
|
cacheHitRate: cacheHitRate.logData,
|
|
topCacheMisses: topCacheMisses.logData,
|
|
},
|
|
params: {
|
|
totalRequests: totalRequests.params,
|
|
errorCounts: errorCounts.params,
|
|
responseSpeed: responseSpeed.params,
|
|
topRoutes: topRoutes.params,
|
|
topErrorRoutes: topErrorRoutes.params,
|
|
topSlowRoutes: topSlowRoutes.params,
|
|
networkTraffic: networkTraffic.params,
|
|
cacheHitRate: cacheHitRate.params,
|
|
topCacheMisses: topCacheMisses.params,
|
|
},
|
|
error: {
|
|
totalRequest: totalRequests.error,
|
|
errorCounts: errorCounts.error,
|
|
responseSpeed: responseSpeed.error,
|
|
topRoutes: topRoutes.error,
|
|
topErrorRoute: topErrorRoutes.error,
|
|
topSlowRoutes: topSlowRoutes.error,
|
|
networkTraffic: networkTraffic.error,
|
|
},
|
|
mergeParams: handleSetParams,
|
|
filters,
|
|
addFilter,
|
|
removeFilter,
|
|
removeFilters,
|
|
isLoading,
|
|
refresh: handleRefresh,
|
|
}
|
|
}
|