Files
supabase/apps/studio/components/interfaces/QueryInsights/hooks/useQueryInsightsMetrics.ts
T
7ed8ab83a8 feat(studio): query insights improvements (#43109)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

This introduces Query Insights. It's the first edition of possible
future updates. This takes our old prototype and builds upon it for a
more action driven insights view.

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Ali Waseem <waseema393@gmail.com>
2026-03-13 15:09:26 +00:00

24 lines
987 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useMemo } from 'react'
import type { QueryPerformanceRow } from '../../QueryPerformance/QueryPerformance.types'
export const useQueryInsightsMetrics = (data: QueryPerformanceRow[]) => {
const avgP95 = useMemo(() => {
const rows = data.filter((r) => (r.p95_time ?? 0) > 0)
if (rows.length === 0) return 0
return Math.round(rows.reduce((sum, r) => sum + (r.p95_time ?? 0), 0) / rows.length)
}, [data])
const totalCalls = useMemo(() => data.reduce((sum, r) => sum + r.calls, 0), [data])
const totalRowsRead = useMemo(() => data.reduce((sum, r) => sum + r.rows_read, 0), [data])
const cacheHitRate = useMemo(() => {
const hits = data.reduce((sum, r) => sum + (r._total_cache_hits ?? 0), 0)
const misses = data.reduce((sum, r) => sum + (r._total_cache_misses ?? 0), 0)
const total = hits + misses
return total > 0 ? ((hits / total) * 100).toFixed(2) + '%' : '–'
}, [data])
return { avgP95, totalCalls, totalRowsRead, cacheHitRate }
}