Skip to content

Commit ccdb881

Browse files
authored
fix: Allow aliases in alert chart preview (#816)
![Screenshot 2025-05-14 at 11 34 08 PM](https://github.com/user-attachments/assets/7d64592f-23df-490e-b816-201ceb7d4dd4)
1 parent e935bb6 commit ccdb881

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

packages/app/src/DBSearchPageAlertModal.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const AlertForm = ({
6363
sourceId,
6464
where,
6565
whereLanguage,
66+
select,
6667
defaultValues,
6768
loading,
6869
deleteLoading,
@@ -74,6 +75,7 @@ const AlertForm = ({
7475
sourceId?: string | null;
7576
where?: SearchCondition | null;
7677
whereLanguage?: SearchConditionLanguage | null;
78+
select?: string | null;
7779
defaultValues?: null | Alert;
7880
loading?: boolean;
7981
deleteLoading?: boolean;
@@ -167,15 +169,18 @@ const AlertForm = ({
167169
<Text size="sm">Threshold chart</Text>
168170
</Accordion.Control>
169171
<Accordion.Panel>
170-
<AlertPreviewChart
171-
sourceId={sourceId}
172-
where={where}
173-
whereLanguage={whereLanguage}
174-
interval={watch('interval')}
175-
groupBy={watch('groupBy')}
176-
threshold={watch('threshold')}
177-
thresholdType={watch('thresholdType')}
178-
/>
172+
{source && (
173+
<AlertPreviewChart
174+
source={source}
175+
where={where}
176+
whereLanguage={whereLanguage}
177+
select={select}
178+
interval={watch('interval')}
179+
groupBy={watch('groupBy')}
180+
threshold={watch('threshold')}
181+
thresholdType={watch('thresholdType')}
182+
/>
183+
)}
179184
</Accordion.Panel>
180185
</Accordion.Item>
181186
</Accordion>
@@ -397,6 +402,7 @@ export const DBSearchPageAlertModal = ({
397402
sourceId={searchedConfig?.source}
398403
where={searchedConfig?.where}
399404
whereLanguage={searchedConfig?.whereLanguage}
405+
select={searchedConfig?.select}
400406
defaultValues={
401407
activeIndex === 'stage'
402408
? null

packages/app/src/components/AlertPreviewChart.tsx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,56 @@ import {
44
SearchCondition,
55
SearchConditionLanguage,
66
} from '@hyperdx/common-utils/dist/types';
7+
import { TSource } from '@hyperdx/common-utils/dist/types';
78
import { Paper } from '@mantine/core';
89

910
import { DBTimeChart } from '@/components/DBTimeChart';
10-
import { useSource } from '@/source';
11+
import { useAliasMapFromChartConfig } from '@/hooks/useChartConfig';
1112
import { intervalToDateRange, intervalToGranularity } from '@/utils/alerts';
1213

1314
import { getAlertReferenceLines } from './Alerts';
1415

1516
export type AlertPreviewChartProps = {
16-
sourceId?: string | null;
17+
source: TSource;
1718
where?: SearchCondition | null;
1819
whereLanguage?: SearchConditionLanguage | null;
1920
interval: AlertInterval;
2021
groupBy?: string;
2122
thresholdType: 'above' | 'below';
2223
threshold: number;
24+
select?: string | null;
2325
};
2426

2527
export const AlertPreviewChart = ({
26-
sourceId,
28+
source,
2729
where,
2830
whereLanguage,
2931
interval,
3032
groupBy,
3133
threshold,
3234
thresholdType,
35+
select,
3336
}: AlertPreviewChartProps) => {
34-
const { data: source } = useSource({ id: sourceId });
37+
const { data: aliasMap } = useAliasMapFromChartConfig({
38+
select: select || '',
39+
where: where || '',
40+
connection: source.connection,
41+
from: source.from,
42+
});
3543

36-
if (!sourceId || !source) {
37-
return null;
38-
}
44+
const aliasWith = Object.entries(aliasMap ?? {}).map(([key, value]) => ({
45+
name: key,
46+
sql: {
47+
sql: value,
48+
params: {},
49+
},
50+
isSubquery: false,
51+
}));
3952

4053
return (
4154
<Paper w="100%" h={200}>
4255
<DBTimeChart
43-
sourceId={sourceId}
56+
sourceId={source.id}
4457
showDisplaySwitcher={false}
4558
referenceLines={getAlertReferenceLines({ threshold, thresholdType })}
4659
config={{
@@ -50,6 +63,7 @@ export const AlertPreviewChart = ({
5063
granularity: intervalToGranularity(interval),
5164
implicitColumnExpression: source.implicitColumnExpression,
5265
groupBy,
66+
with: aliasWith,
5367
select: [
5468
{
5569
aggFn: 'count' as const,

0 commit comments

Comments
 (0)