Files
supabase/apps/studio/components/ui/DataTable/TimelineChart.tsx
Joshen Lim f223b455d6 Unified Logs (Part 1) (#36298)
* init new unified page

* moar logs

* init

* add infinite and live logs example

* Update useLogsPreview.tsx

* add more sources

* wrapped auth logs with edge logs

* add role and user id

* move unified logs

* init

* move demo pages. create a new directory to work in

* extracted beta unified logs into own components

* add example base page and components

* add new files to use actual logging query

* more organization

* change import

* adds new logs page. adds new query

* add data table to UI pacakges

* revert

* table styles

* text size

* add timestamp, table, icons for log types, status code styling

* add host

* add log count to edge functions

* starts to add dynamic filtering

* spiking trace UI

* Update status-code.ts

* add new linik

* now using POST

* fix chart data for default 1 hour view

* update API to accept POST requests

* new filters

* Update level.ts

* fixed up chart to work on level filter. split up the logic into new files

* prep for log type

* prepped query for WHERE

* fix: issue with white space in url param column parsing

* level param now being removed correctly.

* fix issue with chart showing wrong buckets for different time ranges

* remove old query

* refactor the queries into function for each source

* total count fixed

* lots of layout

* start fixing log counts

* comment out min and max for a while

* added trace logging prototype in

* random trace logs added for demo

* added logs and ui to view logs if any

* add Auth user

* fix the live logs issue

* some left over code

* Midway

* First pass refactor + clean up + reorganize files

* Fix TS issues

* Remove unused files

* Clean up

* Final clean up

* more clean up

* More clean up

* Remove unused packages

* Fix

* Lint

* Add feature flag for unified logs

* Refactor

* Remove trace UI

* Snake case log types

* more clean up

* More clean up

* Fix ts

* more clean up

* fixes

* add flag check and redirect if flag is false

* Update middleware.ts

* Nit lint

* Fix

* Last refactors

---------

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
2025-06-20 12:46:17 +08:00

167 lines
5.3 KiB
TypeScript

import { format } from 'date-fns'
import { useMemo, useState } from 'react'
import { Bar, BarChart, CartesianGrid, ReferenceArea, XAxis } from 'recharts'
import type { CategoricalChartFunc } from 'recharts/types/chart/generateCategoricalChart'
import { ChartConfig, ChartContainer, ChartTooltip, ChartTooltipContent, cn } from 'ui'
import { useDataTable } from './providers/DataTableProvider'
export type BaseChartSchema = { timestamp: number; [key: string]: number }
export const description = 'A stacked bar chart'
interface TimelineChartProps<TChart extends BaseChartSchema> {
className?: string
/**
* The table column id to filter by - needs to be a type of `timerange` (e.g. "date").
* TBD: if using keyof TData to be closer to the data table props
*/
columnId: string
/**
* Same data as of the InfiniteQueryMeta.
*/
data: TChart[]
chartConfig: ChartConfig
}
export function TimelineChart<TChart extends BaseChartSchema>({
data,
className,
columnId,
chartConfig,
}: TimelineChartProps<TChart>) {
const { table } = useDataTable()
const [refAreaLeft, setRefAreaLeft] = useState<string | null>(null)
const [refAreaRight, setRefAreaRight] = useState<string | null>(null)
const [isSelecting, setIsSelecting] = useState(false)
// REMINDER: date has to be a string for tooltip label to work - don't ask me why
const chart = useMemo(
() =>
data.map((item) => ({
...item,
[columnId]: new Date(item.timestamp).toString(),
})),
[data]
)
const timerange = useMemo(() => {
if (data.length === 0) return { interval: 0, period: undefined }
const first = data[0].timestamp
const last = data[data.length - 1].timestamp
const interval = Math.abs(first - last) // in ms
return { interval, period: calculatePeriod(interval) }
}, [data])
const handleMouseDown: CategoricalChartFunc = (e) => {
if (e.activeLabel) {
setRefAreaLeft(e.activeLabel)
setIsSelecting(true)
}
}
const handleMouseMove: CategoricalChartFunc = (e) => {
if (isSelecting && e.activeLabel) {
setRefAreaRight(e.activeLabel)
}
}
const handleMouseUp: CategoricalChartFunc = (e) => {
if (refAreaLeft && refAreaRight) {
const [left, right] = [refAreaLeft, refAreaRight].sort(
(a, b) => new Date(a).getTime() - new Date(b).getTime()
)
table.getColumn(columnId)?.setFilterValue([new Date(left), new Date(right)])
}
setRefAreaLeft(null)
setRefAreaRight(null)
setIsSelecting(false)
}
return (
<ChartContainer
config={chartConfig}
className={cn(
'aspect-auto h-[60px] w-full',
'[&_.recharts-rectangle.recharts-tooltip-cursor]:fill-muted/50', // otherwise same color as 200
'select-none', // disable text selection
className
)}
>
<BarChart
accessibilityLayer
data={chart}
margin={{ top: 0, left: 0, right: 0, bottom: 0 }}
onMouseDown={handleMouseDown}
onMouseMove={handleMouseMove}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseUp}
style={{ cursor: 'crosshair' }}
>
<CartesianGrid vertical={false} />
<XAxis
dataKey={columnId}
tickLine={false}
minTickGap={32}
axisLine={false}
// interval="preserveStartEnd"
tickFormatter={(value) => {
const date = new Date(value)
if (isNaN(date.getTime())) return 'N/A'
if (timerange.period === '10m') {
return format(date, 'HH:mm:ss')
} else if (timerange.period === '1d') {
return format(date, 'HH:mm')
} else if (timerange.period === '1w') {
return format(date, 'LLL dd HH:mm')
}
return format(date, 'LLL dd, y')
}}
/>
<ChartTooltip
// defaultIndex={10}
content={
<ChartTooltipContent
labelFormatter={(value) => {
const date = new Date(value)
if (isNaN(date.getTime())) return 'N/A'
if (timerange.period === '10m') {
return format(date, 'LLL dd, HH:mm:ss')
}
return format(date, 'LLL dd, y HH:mm')
}}
/>
}
/>
{/* TODO: we could use the `{timestamp, ...rest} = data[0]` to dynamically create the bars but that would mean the order can be very much random */}
<Bar dataKey="error" stackId="a" fill="var(--color-error)" />
<Bar dataKey="warning" stackId="a" fill="var(--color-warning)" />
<Bar dataKey="success" stackId="a" fill="var(--color-success)" />
{refAreaLeft && refAreaRight && (
<ReferenceArea
x1={refAreaLeft}
x2={refAreaRight}
strokeOpacity={0.3}
fill="hsl(var(--foreground))"
fillOpacity={0.08}
/>
)}
</BarChart>
</ChartContainer>
)
}
// TODO: check what's a good abbreviation for month vs. minutes
function calculatePeriod(interval: number): '10m' | '1d' | '1w' | '1mo' {
if (interval <= 1000 * 60 * 10) {
// less than 10 minutes
return '10m'
} else if (interval <= 1000 * 60 * 60 * 24) {
// less than 1 day
return '1d'
} else if (interval <= 1000 * 60 * 60 * 24 * 7) {
// less than 1 week
return '1w'
}
return '1mo' // defaults to 1 month
}