-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add /query-cache #288
Changes from 3 commits
fc67372
531248c
508ae8d
58bbdad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ColumnFormat } from '@/lib/types/column-format' | ||
import { type QueryConfig } from '@/lib/types/query-config' | ||
|
||
export const queryCacheConfig: QueryConfig = { | ||
name: 'query-cache', | ||
sql: ` | ||
SELECT | ||
query, | ||
result_size, | ||
formatReadableSize(result_size) AS readable_result_size, | ||
round(100 * result_size / max(result_size) OVER ()) AS pct_result_size, | ||
stale, | ||
shared, | ||
compressed, | ||
expires_at, | ||
(now() - expires_at) AS expires_in, | ||
key_hash | ||
FROM system.query_cache | ||
ORDER BY expires_at DESC | ||
LIMIT 1000 | ||
`, | ||
columns: [ | ||
'query', | ||
'readable_result_size', | ||
'stale', | ||
'shared', | ||
'compressed', | ||
'expires_at', | ||
'expires_in', | ||
'key_hash', | ||
], | ||
columnFormats: { | ||
query: ColumnFormat.CodeToggle, | ||
readable_result_size: ColumnFormat.BackgroundBar, | ||
stale: ColumnFormat.Boolean, | ||
shared: ColumnFormat.Boolean, | ||
compressed: ColumnFormat.Boolean, | ||
expires_in: ColumnFormat.Duration, | ||
}, | ||
|
||
relatedCharts: [['query-cache', {}]], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Clarify the purpose of the empty object in If the empty object is intended for future use, consider adding a placeholder comment or a TODO to clarify its purpose. |
||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,43 @@ | ||||||||||||||
import { ChartCard } from '@/components/chart-card' | ||||||||||||||
import { type ChartProps } from '@/components/charts/chart-props' | ||||||||||||||
import { CardMetric } from '@/components/tremor/card-metric' | ||||||||||||||
import { fetchData } from '@/lib/clickhouse' | ||||||||||||||
|
||||||||||||||
export async function ChartQueryCache({ | ||||||||||||||
title = 'Query Cache', | ||||||||||||||
className, | ||||||||||||||
}: ChartProps & { name?: string }) { | ||||||||||||||
const query = ` | ||||||||||||||
SELECT | ||||||||||||||
sumIf(result_size, stale = 0) AS total_result_size, | ||||||||||||||
sumIf(result_size, stale = 1) AS total_staled_result_size, | ||||||||||||||
formatReadableSize(total_result_size) AS readable_total_result_size, | ||||||||||||||
formatReadableSize(total_staled_result_size) AS readable_total_staled_result_size | ||||||||||||||
FROM system.query_cache | ||||||||||||||
` | ||||||||||||||
const { data } = await fetchData< | ||||||||||||||
{ | ||||||||||||||
total_result_size: number | ||||||||||||||
total_staled_result_size: number | ||||||||||||||
readable_total_result_size: string | ||||||||||||||
readable_total_staled_result_size: string | ||||||||||||||
}[] | ||||||||||||||
>({ query }) | ||||||||||||||
Comment on lines
+18
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): Handle potential errors from Consider adding error handling for the |
||||||||||||||
const first = data?.[0] | ||||||||||||||
|
||||||||||||||
if (!data || !first) return null | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Provide user feedback when data is not available. Instead of returning
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): Use block braces for ifs, whiles, etc. (
Suggested change
ExplanationIt is recommended to always use braces and create explicit statement blocks.Using the allowed syntax to just write a single statement can lead to very confusing |
||||||||||||||
|
||||||||||||||
return ( | ||||||||||||||
<ChartCard title={title} className={className} sql={query}> | ||||||||||||||
<CardMetric | ||||||||||||||
current={first.total_result_size} | ||||||||||||||
currentReadable={first.readable_total_result_size + ' cached'} | ||||||||||||||
duyet marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
target={first.total_staled_result_size} | ||||||||||||||
targetReadable={first.readable_total_staled_result_size + ' staled'} | ||||||||||||||
className="p-2" | ||||||||||||||
/> | ||||||||||||||
</ChartCard> | ||||||||||||||
) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
export default ChartQueryCache |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -23,6 +23,7 @@ import { QUERY_COMMENT } from '@/lib/clickhouse' | |||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||
CircleDollarSignIcon, | ||||||||||||||||||||||||||||
CombineIcon, | ||||||||||||||||||||||||||||
DatabaseZapIcon, | ||||||||||||||||||||||||||||
FilePlus2Icon, | ||||||||||||||||||||||||||||
HardDriveIcon, | ||||||||||||||||||||||||||||
RollerCoasterIcon, | ||||||||||||||||||||||||||||
|
@@ -147,6 +148,12 @@ export const menuItemsConfig: MenuItem[] = [ | |||||||||||||||||||||||||||
description: 'Shows the execution plan of a statement', | ||||||||||||||||||||||||||||
icon: InfoCircledIcon, | ||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
title: 'Query Cache', | ||||||||||||||||||||||||||||
href: '/query-cache', | ||||||||||||||||||||||||||||
description: 'Query cache usage', | ||||||||||||||||||||||||||||
icon: DatabaseZapIcon, | ||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||
Comment on lines
+151
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Consider adding a unique identifier for the new menu item. Adding a unique identifier (e.g.,
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one can respect the type when suggesting adding an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for pointing that out, @duyet. You're correct that the suggestion should respect the existing type definitions. If the
Would you like me to update the type definition in the PR, or do you prefer to handle it? |
||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
|
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.
issue (bug_risk): Check for division by zero.
Ensure that
max(result_size)
is never zero to avoid potential division by zero errors.