-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #367 from duyet/feat/overview
feat: add charts to failed-query page
- Loading branch information
Showing
5 changed files
with
255 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { type ChartProps } from '@/components/charts/chart-props' | ||
import { BarChart } from '@/components/generic-charts/bar' | ||
import { ChartCard } from '@/components/generic-charts/chart-card' | ||
import { fetchData } from '@/lib/clickhouse' | ||
import { applyInterval } from '@/lib/clickhouse-query' | ||
|
||
export async function ChartFailedQueryCountByType({ | ||
title, | ||
interval = 'toStartOfDay', | ||
lastHours = 24 * 14, | ||
className, | ||
chartClassName, | ||
...props | ||
}: ChartProps) { | ||
const query = ` | ||
SELECT ${applyInterval(interval, 'event_time')}, | ||
user, | ||
countDistinct(query_id) AS count | ||
FROM merge(system, '^query_log') | ||
WHERE | ||
type IN ['ExceptionBeforeStart', 'ExceptionWhileProcessing'] | ||
AND event_time >= (now() - INTERVAL ${lastHours} HOUR) | ||
GROUP BY 1, 2 | ||
ORDER BY | ||
1 ASC, | ||
3 DESC | ||
` | ||
const { data: raw } = await fetchData< | ||
{ | ||
event_time: string | ||
user: string | ||
count: number | ||
}[] | ||
>({ query }) | ||
|
||
const data = raw.reduce( | ||
(acc, cur) => { | ||
const { event_time, user, count } = cur | ||
if (acc[event_time] === undefined) { | ||
acc[event_time] = {} | ||
} | ||
acc[event_time][user] = count | ||
return acc | ||
}, | ||
{} as Record<string, Record<string, number>> | ||
) | ||
|
||
const barData = Object.entries(data).map(([event_time, obj]) => { | ||
return { event_time, ...obj } | ||
}) | ||
|
||
const users = Object.values(data).reduce((acc, cur) => { | ||
return Array.from(new Set([...acc, ...Object.keys(cur)])) | ||
}, [] as string[]) | ||
|
||
return ( | ||
<ChartCard title={title} className={className} sql={query} data={barData}> | ||
<BarChart | ||
className={chartClassName} | ||
data={barData} | ||
index="event_time" | ||
categories={users} | ||
showLegend | ||
stack | ||
{...props} | ||
/> | ||
</ChartCard> | ||
) | ||
} | ||
|
||
export default ChartFailedQueryCountByType |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { type ChartProps } from '@/components/charts/chart-props' | ||
import { AreaChart } from '@/components/generic-charts/area' | ||
import { ChartCard } from '@/components/generic-charts/chart-card' | ||
import { fetchData } from '@/lib/clickhouse' | ||
import { cn } from '@/lib/utils' | ||
|
||
export async function ChartFailedQueryCount({ | ||
title, | ||
interval = 'toStartOfMinute', | ||
className, | ||
chartClassName, | ||
chartCardContentClassName, | ||
lastHours = 24, | ||
showXAxis = true, | ||
showLegend = false, | ||
showCartesianGrid = true, | ||
breakdown = 'breakdown', | ||
...props | ||
}: ChartProps) { | ||
const query = ` | ||
WITH event_count AS ( | ||
SELECT ${interval}(event_time) AS event_time, | ||
COUNT() AS query_count | ||
FROM merge(system, '^query_log') | ||
WHERE | ||
type IN ['ExceptionBeforeStart', 'ExceptionWhileProcessing'] | ||
AND event_time >= (now() - INTERVAL ${lastHours} HOUR) | ||
GROUP BY 1 | ||
ORDER BY 1 | ||
), | ||
query_type AS ( | ||
SELECT ${interval}(event_time) AS event_time, | ||
type AS query_type, | ||
COUNT() AS count | ||
FROM merge(system, '^query_log') | ||
WHERE | ||
type IN ['ExceptionBeforeStart', 'ExceptionWhileProcessing'] | ||
AND event_time >= (now() - INTERVAL ${lastHours} HOUR) | ||
GROUP BY 1, 2 | ||
ORDER BY 3 DESC | ||
), | ||
breakdown AS ( | ||
SELECT event_time, | ||
groupArray((query_type, count)) AS breakdown | ||
FROM query_type | ||
GROUP BY 1 | ||
) | ||
SELECT event_time, | ||
query_count, | ||
breakdown.breakdown AS breakdown | ||
FROM event_count | ||
LEFT JOIN breakdown USING event_time | ||
ORDER BY 1 | ||
` | ||
const { data } = await fetchData< | ||
{ | ||
event_time: string | ||
query_count: number | ||
breakdown: Array<[string, number] | Record<string, string>> | ||
}[] | ||
>({ query }) | ||
|
||
return ( | ||
<ChartCard | ||
title={title} | ||
className={className} | ||
contentClassName={chartCardContentClassName} | ||
sql={query} | ||
data={data} | ||
> | ||
<AreaChart | ||
className={cn('h-52', chartClassName)} | ||
data={data} | ||
index="event_time" | ||
categories={['query_count']} | ||
readable="quantity" | ||
stack | ||
showLegend={showLegend} | ||
showXAxis={showXAxis} | ||
showCartesianGrid={showCartesianGrid} | ||
colors={['--chart-1']} | ||
breakdown={breakdown} | ||
breakdownLabel="query_type" | ||
breakdownValue="count" | ||
{...props} | ||
/> | ||
</ChartCard> | ||
) | ||
} | ||
|
||
export default ChartFailedQueryCount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4be031a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
clickhouse-monitoring – ./
clickhouse-monitoring.vercel.app
clickhouse-dashboard.vercel.app
clickhouse-monitoring-duyet-team.vercel.app
clickhouse-monitoring-git-main-duyet-team.vercel.app
clickhouse.duyet.net
clickhouse-monitor.vercel.app