Skip to content

Commit 00cc6bf

Browse files
fakemonsterbenjaminleonarddavid-crespo
authored
OxQL metrics explorer (#3312)
This commit adds a (very big!) OxQL metrics explorer. In general it interacts with a new endpoint, so it mostly leaves alone the good deal of other OxQL-ish content in this repository, which has some subtle differences). It introduces example queries that are completely arbitrary, but help showcase the different display modes of the chart set. It also makes some general adjustments to how charting works: different axis ranges, different tick logic, etc. But generally it expects that the existing charts in the application haven't changed too much. --------- Co-authored-by: benjaminleonard <[email protected]> Co-authored-by: David Crespo <[email protected]>
1 parent d922915 commit 00cc6bf

49 files changed

Lines changed: 8393 additions & 1470 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎app/api/__generated__/timeseries-metadata.ts‎

Lines changed: 2841 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/api/__tests__/safety.spec.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import path from 'path'
1212
import { expect, it } from 'vitest'
1313

1414
import viteConfigFn from '../../../vite.config'
15+
import { omicronVersion as timeseriesMetadataVersion } from '../__generated__/timeseries-metadata'
1516

1617
it('Generated API client version matches API version specified for deployment', () => {
1718
const generatedVersion = fs
@@ -26,6 +27,8 @@ it('Generated API client version matches API version specified for deployment',
2627
// if this test fails, most likely you have updated the API_VERSION in
2728
// console/OMICRON_VERSION without re-running `npm run gen-api`
2829
expect(generatedVersion).toEqual(pinnedVersion)
30+
// same deal: re-run `npm run gen-api`
31+
expect(timeseriesMetadataVersion).toEqual(pinnedVersion)
2932
})
3033

3134
// omicron releng reads API_VERSION at our pinned commit to check the console

‎app/api/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export * from './client'
1414
export * from './roles'
1515
export * from './util'
1616
export * from './__generated__/Api'
17-
export { snakeify } from './__generated__/util'
17+
export { camelToSnake, snakeify } from './__generated__/util'
1818
// export * as ZVal from './__generated__/validate'
1919

2020
export type { ApiTypes }

‎app/components/ChartTooltip.tsx‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright Oxide Computer Company
7+
*/
8+
import { format } from 'date-fns'
9+
import type { ReactNode } from 'react'
10+
import { match } from 'ts-pattern'
11+
12+
const longDateTime = (ts: number) => format(new Date(ts), 'MMM d, yyyy HH:mm:ss zz')
13+
14+
type ChartTooltipProps = {
15+
timestamp: number
16+
left: number
17+
top: number
18+
offset: [LeftRight, TopBottom]
19+
/** What's being hovered, e.g. a series name or bin range */
20+
label: ReactNode
21+
/** The hovered value */
22+
children: ReactNode
23+
}
24+
25+
/** Offset the box into the quadrant away from the point so it never overflows an edge */
26+
export type LeftRight = 'left' | 'right'
27+
export type TopBottom = 'top' | 'bottom'
28+
29+
const TOOLTIP_GAP = 12
30+
function tooltipTransform(leftRight: LeftRight, topBottom: TopBottom): string {
31+
const tx = match(leftRight)
32+
.with('left', () => `calc(-100% - ${TOOLTIP_GAP}px)`)
33+
.with('right', () => `${TOOLTIP_GAP}px`)
34+
.exhaustive()
35+
const ty = match(topBottom)
36+
.with('top', () => `calc(-100% - ${TOOLTIP_GAP}px)`)
37+
.with('bottom', () => `${TOOLTIP_GAP}px`)
38+
.exhaustive()
39+
return `translate(${tx}, ${ty})`
40+
}
41+
42+
export function ChartTooltip({
43+
timestamp,
44+
left,
45+
top,
46+
offset,
47+
label,
48+
children,
49+
}: ChartTooltipProps) {
50+
return (
51+
<div
52+
className="pointer-events-none absolute z-10 w-max max-w-80"
53+
style={{
54+
left: left,
55+
top: top,
56+
transform: tooltipTransform(...offset),
57+
}}
58+
>
59+
<div
60+
role="tooltip"
61+
className="text-sans-md text-secondary bg-raise shadow-tooltip rounded-md outline-0"
62+
>
63+
<div className="border-secondary border-b px-3 py-2 pr-6">
64+
{longDateTime(timestamp)}
65+
</div>
66+
<div className="border-secondary border-b px-3 py-2">{label}</div>
67+
<div className="text-raise px-3 py-2">{children}</div>
68+
</div>
69+
</div>
70+
)
71+
}

‎app/components/FramedChart.tsx‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright Oxide Computer Company
7+
*/
8+
import { useMemo, type ReactNode, type RefObject } from 'react'
9+
import type uPlot from 'uplot'
10+
import UplotReact from 'uplot-react'
11+
12+
import { useElementSize } from '~/hooks/use-element-size'
13+
14+
// The intended left padding (px-5) is taken from the container and given to
15+
// uPlot instead, so the plot sits flush left while x-tick labels can bleed into
16+
// the gutter without clipping.
17+
const CHART_LEFT_PAD = 20
18+
19+
export type UPlotOptions = Omit<uPlot.Options, 'width' | 'height' | 'padding'>
20+
21+
type Props = {
22+
title: string
23+
height: number
24+
chartOptions: UPlotOptions
25+
data: uPlot.AlignedData
26+
uRef: RefObject<uPlot | null>
27+
onCreate?: (u: uPlot) => void
28+
children?: ReactNode
29+
legend?: ReactNode
30+
}
31+
32+
export function FramedChart({
33+
title,
34+
height,
35+
chartOptions,
36+
data,
37+
uRef,
38+
onCreate,
39+
children,
40+
legend,
41+
}: Props) {
42+
const [size, sizeRef] = useElementSize()
43+
44+
// Width/height changes cause a cheaper "update" path for uplot, instead of
45+
// "create", so it gets its own layer of memoization
46+
const options = useMemo(
47+
() =>
48+
({
49+
...chartOptions,
50+
padding: [null, null, null, CHART_LEFT_PAD],
51+
width: size?.width ?? 0,
52+
height,
53+
}) satisfies uPlot.Options,
54+
[chartOptions, size?.width, height]
55+
)
56+
57+
return (
58+
<figure
59+
aria-label={title}
60+
className="m-0 pt-8 pb-5 pl-0"
61+
style={{ paddingRight: CHART_LEFT_PAD }}
62+
>
63+
{/* The actual chart is absolutely positioned so its fixed pixel width
64+
doesn't influence layout and block future resizing. That in turn makes
65+
its container need an explicit height */}
66+
<div ref={sizeRef} className="relative" style={{ height }}>
67+
{size && (
68+
<UplotReact
69+
className="absolute top-0 left-0"
70+
options={options}
71+
data={data}
72+
onCreate={(u) => {
73+
uRef.current = u
74+
onCreate?.(u)
75+
}}
76+
/>
77+
)}
78+
{children}
79+
</div>
80+
{legend}
81+
</figure>
82+
)
83+
}

0 commit comments

Comments
 (0)