:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(charts): render the CH-queries debug chart with quill (#87919)
Sam Pennington937ada70ebb8
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 5425f64fda60..e610f41e0f2d 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -400,6 +400,10 @@ snapshots:hash: v1.k794b7964.d81ec763d541634271916482f860bfdfc2d1db7e14e6daf8f80dee2ff7d9ee1a.KPijkSly1tKiFDaoh2IZuJuyNlskNylPt3Pa0ybdRZwcomponents-date-picker--with-value--light:hash: v1.k794b7964.9637f1dcc335c1a3caf269b72e5d299d2b5a42b0f06fcdd2ea410044f62e11f9.-dDqqmYYCzqZKtVOp1FJw6mHETpKlG5oS6qb6zVpEhE+ components-debugchqueries--query-volume-chart--dark:+ hash: v1.k794b7964.8f0c289c4d1e36599a1db5d7862066adfdbf7409107b41fb06142ed4e587597c.2PVYQO83gs9m6EhjBSEmOxAtPi3IWwwvqhXgUtUFoYM+ components-debugchqueries--query-volume-chart--light:+ hash: v1.k794b7964.c90f0e08f0a6151006d52ffb3da5fb770f61245d1e5d0c62ea4d234d9b62309a.hblu9RsN5dANuKmMUcGZ1XhofXyWhLAnzNU1FqLBVaIcomponents-definition-icons--event-definition-icons--dark:hash: v1.k794b7964.16cde7300e72420837eb1542a3eb25e0a76a44937ad9b8273a77dfac5c6d8b99.2FLdNg_cl_qw4ZfqAY1JYvuE5tcLyISHnFP7jWGNtpkcomponents-definition-icons--event-definition-icons--light:diff --git a/frontend/src/lib/components/Shortcuts/utils/DebugCHQueries.tsx b/frontend/src/lib/components/Shortcuts/utils/DebugCHQueries.tsxindex 6b3c3154ba50..6e30de8a28e0 100644--- a/frontend/src/lib/components/Shortcuts/utils/DebugCHQueries.tsx+++ b/frontend/src/lib/components/Shortcuts/utils/DebugCHQueries.tsx@@ -10,7 +10,7 @@ const LazyDebugCHQueries = lazyWithRetry(() =>import('./DebugCHQueriesImpl').then((m) => ({ default: m.DebugCHQueries })))-/** Lazy facade so the debug panel's chart.js dependencies stay out of the eager menu/shortcut chunks. */+/** Lazy facade so the debug panel's charting dependencies stay out of the eager menu/shortcut chunks. */export function DebugCHQueries(props: DebugCHQueriesProps): JSX.Element {return (<Suspense fallback={<Spinner />}>diff --git a/frontend/src/lib/components/Shortcuts/utils/DebugCHQueriesImpl.stories.tsx b/frontend/src/lib/components/Shortcuts/utils/DebugCHQueriesImpl.stories.tsxnew file mode 100644index 000000000000..3ec7b0b81fd1--- /dev/null+++ b/frontend/src/lib/components/Shortcuts/utils/DebugCHQueriesImpl.stories.tsx@@ -0,0 +1,41 @@+import type { Meta, StoryObj } from '@storybook/react'++import { dayjs } from 'lib/dayjs'++import { BarChartWithLine, type DataPoint } from './DebugCHQueriesImpl'++// Recent hours so the bars and line sit against the right edge of the fixed 14-day hourly axis.+// Built in render so it shares the mocked clock with the component's own label generation, which+// keeps the data aligned to the axis and the snapshot deterministic.+function recentHours(count: number): DataPoint[] {+ const now = dayjs().startOf('hour')+ return Array.from({ length: count }, (_, i) => ({+ hour: now.subtract(count - 1 - i, 'hour').format('YYYY-MM-DDTHH:00:00'),+ successful_queries: 40 + ((i * 7) % 60),+ exceptions: (i * 3) % 11,+ avg_response_time_ms: 120 + ((i * 13) % 240),+ }))+}++const meta: Meta<typeof BarChartWithLine> = {+ title: 'Components/DebugCHQueries',+ component: BarChartWithLine,+ parameters: {+ layout: 'centered',+ mockDate: '2023-01-15 12:00:00',+ testOptions: { snapshotBrowsers: ['chromium'] },+ },+}+export default meta++type Story = StoryObj<typeof BarChartWithLine>++// BarChartWithLine fills its flex parent, so give it a column container with a definite size —+// otherwise the chart resolves to size 0 and quill paints a 0-size canvas.+export const QueryVolumeChart: Story = {+ render: () => (+ <div className="h-[340px] w-[760px] flex flex-col">+ <BarChartWithLine data={recentHours(96)} />+ </div>+ ),+}diff --git a/frontend/src/lib/components/Shortcuts/utils/DebugCHQueriesImpl.tsx b/frontend/src/lib/components/Shortcuts/utils/DebugCHQueriesImpl.tsxindex 20ccb1b41b90..acf98bd7ad93 100644--- a/frontend/src/lib/components/Shortcuts/utils/DebugCHQueriesImpl.tsx+++ b/frontend/src/lib/components/Shortcuts/utils/DebugCHQueriesImpl.tsx@@ -1,16 +1,16 @@-import ChartDataLabels from 'chartjs-plugin-datalabels'-import ChartjsPluginStacked100 from 'chartjs-plugin-stacked100'import { MakeLogicType, actions, afterMount, kea, path, reducers, selectors, useActions, useValues } from 'kea'import { loaders } from 'kea-loaders'import { useMemo, useState } from 'react'import { IconCodeInsert, IconCopy, IconRefresh } from '@posthog/icons'+import { TimeSeriesComboChart } from '@posthog/quill-charts'+import type { Series } from '@posthog/quill-charts'import api from 'lib/api'-import { Chart, ChartConfiguration, ChartDataset } from 'lib/Chart'+import { useChartTheme } from 'lib/charts/hooks'+import { getColorVar } from 'lib/colors'import { LinkMetabaseQuery } from 'lib/components/MetabaseQueryLink'import { dayjs } from 'lib/dayjs'-import { useChart } from 'lib/hooks/useChart'import { LemonBanner } from 'lib/lemon-ui/LemonBanner'import { LemonButton } from 'lib/lemon-ui/LemonButton'import { LemonTable } from 'lib/lemon-ui/LemonTable'@@ -32,7 +32,7 @@ export interface Stats {exception_percentage: number | null}-interface DataPoint {+export interface DataPoint {hour: stringsuccessful_queries: numberexceptions: number@@ -176,89 +176,52 @@ const generateHourlyLabels = (days: number): string[] => {return labels.reverse()}-const BarChartWithLine: React.FC<{ data: DataPoint[] }> = ({ data }) => {- const labels = generateHourlyLabels(14)+export const BarChartWithLine = ({ data }: { data: DataPoint[] }): JSX.Element => {+ const theme = useChartTheme()+ const labels = useMemo(() => generateHourlyLabels(14), [])- Chart.register(ChartjsPluginStacked100, ChartDataLabels)-- const { canvasRef } = useChart({- getConfig: () => {- const dataMap = new Map(data.map((d) => [d.hour, d]))-- const successfulQueries = labels.map((label) => dataMap.get(label)?.successful_queries || 0)- const exceptions = labels.map((label) => dataMap.get(label)?.exceptions || 0)- const avgResponseTime = labels.map((label) => dataMap.get(label)?.avg_response_time_ms || 0)-- const datasets: ChartDataset[] = [- {- label: 'Successful Queries',- data: successfulQueries,- backgroundColor: 'rgba(75, 192, 192, 0.2)',- borderColor: 'rgba(75, 192, 192, 1)',- borderWidth: 1,- stack: 'Stack 0',- },- {- label: 'Exceptions',- data: exceptions,- backgroundColor: 'rgba(255, 99, 132, 0.2)',- borderColor: 'rgba(255, 99, 132, 1)',- borderWidth: 1,- stack: 'Stack 0',- },- {- label: 'Avg Response Time (ms)',- data: avgResponseTime,- type: 'line',- fill: false,- borderColor: 'rgba(153, 102, 255, 0.5)',- yAxisID: 'y-axis-2',- },- ]-- const maxQueryCount = Math.max(...successfulQueries, ...exceptions)- const maxResponseTime = Math.max(...avgResponseTime)- const options: ChartConfiguration['options'] = {- responsive: true,- maintainAspectRatio: false,- scales: {- x: {- display: false,- },- y: {- type: 'linear',- display: true,- position: 'left',- max: maxQueryCount * 1.1,- },- 'y-axis-2': {- type: 'linear',- display: true,- position: 'right',- grid: {- drawOnChartArea: false,- },- max: maxResponseTime * 2,- },- },- plugins: {- // @ts-expect-error Types of library are out of date- crosshair: false,- datalabels: { display: false },- },- }-- return {- type: 'bar' as const,- data: { labels, datasets },- options,- plugins: [ChartDataLabels],- }- },- deps: [data, labels],- })+ const series = useMemo<Series[]>(() => {+ const dataMap = new Map(data.map((d) => [d.hour, d]))+ return [+ {+ key: 'successful_queries',+ label: 'Successful queries',+ type: 'bar',+ color: getColorVar('success'),+ data: labels.map((label) => dataMap.get(label)?.successful_queries || 0),+ },+ {+ key: 'exceptions',+ label: 'Exceptions',+ type: 'bar',+ color: getColorVar('danger'),+ data: labels.map((label) => dataMap.get(label)?.exceptions || 0),+ },+ {+ key: 'avg_response_time',+ label: 'Avg response time (ms)',+ type: 'line',+ yAxisId: 'right',+ color: getColorVar('data-color-1'),+ data: labels.map((label) => dataMap.get(label)?.avg_response_time_ms || 0),+ },+ ]+ }, [data, labels, theme])- return <canvas ref={canvasRef} className="h-[300px] w-full" />+ return (+ <div className="h-[300px] w-full flex flex-col">+ <TimeSeriesComboChart+ series={series}+ labels={labels}+ theme={theme}+ config={{+ barLayout: 'stacked',+ xAxis: { hide: true },+ yAxis: [{ id: 'left' }, { id: 'right', position: 'right' }],+ }}+ />+ </div>+ )}export interface DebugCHQueriesProps {