Skip to content

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

chore: fix failing tests, add test coverage to storage report

TzeYiing0ef21e837d27
diff --git a/studio/components/interfaces/Reports/Reports.constants.ts b/studio/components/interfaces/Reports/Reports.constants.ts
index 354614b26f0..f4c4f69fc39 100644
--- a/studio/components/interfaces/Reports/Reports.constants.ts
+++ b/studio/components/interfaces/Reports/Reports.constants.ts
@@ -235,6 +235,7 @@ export const PRESET_CONFIG: Record<Presets, PresetConfig> = {
queryType: 'logs',
// storage report does not perform any filtering
sql: (_filters) => `
+-- cache-hit-rate
SELECT
timestamp_trunc(timestamp, hour) as timestamp,
countif( h.cf_cache_status in ('HIT', 'STALE', 'REVALIDATED', 'UPDATING') ) as hit_count,
@@ -253,6 +254,7 @@ order by timestamp desc
queryType: 'logs',
// storage report does not perform any filtering
sql: (_filters) => `
+-- top-cache-misses
SELECT
r.path as path,
r.search as search,
diff --git a/studio/components/interfaces/Reports/renderers/StorageRenderers.tsx b/studio/components/interfaces/Reports/renderers/StorageRenderers.tsx
index 5c2d72afc88..cdff467d303 100644
--- a/studio/components/interfaces/Reports/renderers/StorageRenderers.tsx
+++ b/studio/components/interfaces/Reports/renderers/StorageRenderers.tsx
@@ -11,13 +11,6 @@ export const CacheHitRateChartRenderer = (
miss_count: number
}>
) => {
- props.data = [
- {
- timestamp: new Date().toISOString(),
- hit_count: 123,
- miss_count: 123,
- },
- ]
const stackedData = props.data.flatMap((datum) => [
{
timestamp: datum.timestamp,
@@ -51,13 +44,6 @@ export const TopCacheMissesRenderer = (
count: number
}>
) => {
- props.data = [
- {
- path: '123',
- search: '?123',
- count: 123,
- },
- ]
if (props.data.length === 0) return null
const headerClasses = '!text-xs !py-2 p-0 font-bold !bg-scale-400'
const cellClasses = '!text-xs !py-2'
diff --git a/studio/components/ui/Charts/Charts.constants.ts b/studio/components/ui/Charts/Charts.constants.ts
index 1354ac68914..f9161102054 100644
--- a/studio/components/ui/Charts/Charts.constants.ts
+++ b/studio/components/ui/Charts/Charts.constants.ts
@@ -38,7 +38,7 @@ export const genStackColorScales = (colors: ValidStackColor[]) =>
}
})
-export const DEFAULT_STACK_COLORS = ['brand', 'slate', 'blue', 'yellow', 'indigo']
+export const DEFAULT_STACK_COLORS : ValidStackColor[] = ['brand', 'slate', 'blue', 'yellow', 'indigo']
export enum DateTimeFormats {
FULL = 'MMM D, YYYY, hh:mma',
diff --git a/studio/components/ui/Charts/StackedBarChart.tsx b/studio/components/ui/Charts/StackedBarChart.tsx
index f3123578129..3dbf7e214e0 100644
--- a/studio/components/ui/Charts/StackedBarChart.tsx
+++ b/studio/components/ui/Charts/StackedBarChart.tsx
@@ -1,7 +1,7 @@
import { useState } from 'react'
import { BarChart, Bar, XAxis, Tooltip, Legend, Cell, TooltipProps } from 'recharts'
import ChartHeader from './ChartHeader'
-import { CHART_COLORS, STACK_COLORS, DateTimeFormats, DEFAULT_STACK_COLORS, genStackColorScales } from './Charts.constants'
+import { CHART_COLORS, DateTimeFormats, DEFAULT_STACK_COLORS, genStackColorScales, ValidStackColor } from './Charts.constants'
import { CommonChartProps } from './Charts.types'
import { timestampFormatter, useChartSize, useStacked } from './Charts.utils'
import { precisionFormatter } from './Charts.utils'
@@ -16,7 +16,7 @@ interface Props extends CommonChartProps<any> {
displayDateInUtc?: boolean
hideLegend?: boolean
hideHeader?: boolean
- stackColors?: ValidStackColor
+ stackColors?: ValidStackColor[]
}
const StackedBarChart: React.FC<Props> = ({
size,
diff --git a/studio/pages/project/[ref]/reports/storage.tsx b/studio/pages/project/[ref]/reports/storage.tsx
index fe49c1fe7e5..664265e07a0 100644
--- a/studio/pages/project/[ref]/reports/storage.tsx
+++ b/studio/pages/project/[ref]/reports/storage.tsx
@@ -11,7 +11,7 @@ import {
CacheHitRateChartRenderer,
TopCacheMissesRenderer,
} from 'components/interfaces/Reports/renderers/StorageRenderers'
-import { useMemo } from 'react'
+import { useEffect, useMemo } from 'react'
import ReportHeader from 'components/interfaces/Reports/ReportHeader'
import { DatePickerToFrom, LogsEndpointParams } from 'components/interfaces/Settings/Logs'
import { useParams } from 'common'
@@ -94,6 +94,17 @@ const useStorageReport = () => {
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: {
diff --git a/studio/tests/pages/projects/reports/storage-report.test.js b/studio/tests/pages/projects/reports/storage-report.test.js
new file mode 100644
index 00000000000..6d24e13ca85
--- /dev/null
+++ b/studio/tests/pages/projects/reports/storage-report.test.js
@@ -0,0 +1,42 @@
+import { get } from 'lib/common/fetch'
+import { render } from '../../../helpers'
+import { waitFor, screen } from '@testing-library/react'
+import { StorageReport } from 'pages/project/[ref]/reports/storage'
+import userEvent from '@testing-library/user-event'
+
+beforeEach(() => {
+ // reset mocks between tests
+ get.mockReset()
+ get.mockImplementation(async (_url) => [{ result: [] }])
+})
+
+test(`static elements`, async () => {
+ render(<StorageReport />)
+ await screen.findByText('Request Caching')
+ await screen.findByText(/Last 24 hours/)
+ await screen.findByText(/Custom/)
+})
+
+test('refresh button', async () => {
+ render(<StorageReport />)
+ await waitFor(() => expect(get).toBeCalled())
+ get.mockReset()
+ userEvent.click(await screen.findByText(/Refresh/))
+ await waitFor(() => expect(get).toBeCalled())
+})
+
+test('append - top cache misses', async () => {
+ get.mockImplementation(async (url) => {
+ if (decodeURIComponent(url).includes('misses')) {
+ return {
+ result: [{ path: 'mypath', search: 'some-query', count: 22 }],
+ }
+ }
+ return { result: [{ timestamp: new Date().toISOString(), miss_count: 123, hit_count: 123 }] }
+ })
+ render(<StorageReport />)
+ await waitFor(() => expect(get).toBeCalled())
+ await screen.findAllByText(/mypath/)
+ await screen.findAllByText(/some\-query/)
+ await screen.findAllByText(/22/)
+})