Files
supabase/apps/studio/data/reports/storage-report-query.ts
T
Joshen LimandTzeYiing d025e0f739 chore/fix studio jest tests (#25872)
* Fix tests in tests/unit, tests/components and files under tests, looking into tests/pages

* Fix tests under pages/projects root

* Fix

* Comment out broken tests that im stuck with

* Fix api-report.test

* Fix storage-report-test

* chore: fix some tests

* chore: remove logging

* Fix LogsPreviewer.test.js

* Fix most of logs-query-test

* Skip broken tests instead of false positiving them

---------

Co-authored-by: TzeYiing <ty@tzeyiing.com>
2024-05-11 12:05:25 +02:00

54 lines
1.6 KiB
TypeScript

import { useEffect } from 'react'
import { useParams } from 'common'
import { PRESET_CONFIG } from 'components/interfaces/Reports/Reports.constants'
import { queriesFactory } from 'components/interfaces/Reports/Reports.utils'
import { LogsEndpointParams } from 'components/interfaces/Settings/Logs'
export const useStorageReport = () => {
const { ref: projectRef } = useParams()
const queryHooks = queriesFactory<keyof typeof PRESET_CONFIG.storage.queries>(
PRESET_CONFIG.storage.queries,
projectRef ?? 'default'
)
const cacheHitRate = queryHooks.cacheHitRate()
const topCacheMisses = queryHooks.topCacheMisses()
const activeHooks = [cacheHitRate, topCacheMisses]
const handleRefresh = async () => {
activeHooks.forEach((hook) => hook.runQuery())
}
const handleSetParams = (params: Partial<LogsEndpointParams>) => {
activeHooks.forEach((hook) => {
hook.setParams?.((prev: LogsEndpointParams) => ({ ...prev, ...params }))
})
}
useEffect(() => {
if (cacheHitRate.changeQuery) {
cacheHitRate.changeQuery(PRESET_CONFIG.storage.queries.cacheHitRate.sql([]))
}
if (topCacheMisses.changeQuery) {
topCacheMisses.changeQuery(PRESET_CONFIG.storage.queries.topCacheMisses.sql([]))
}
}, [])
const isLoading = activeHooks.some((hook) => hook.isLoading)
return {
data: {
cacheHitRate: cacheHitRate.logData,
topCacheMisses: topCacheMisses.logData,
},
params: {
cacheHitRate: cacheHitRate.params,
topCacheMisses: topCacheMisses.params,
},
mergeParams: handleSetParams,
isLoading,
refresh: handleRefresh,
}
}