Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heatmap-inline-settings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/app': patch
---

feat: Edit heatmap value and count inline in the chart editor
23 changes: 23 additions & 0 deletions packages/app/src/components/ChartDisplaySettingsDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { shouldFillNullsWithZero } from '@/ChartUtils';
import { MAX_RENDERED_TIME_CHART_SERIES } from '@/defaults';
import { FormatTime } from '@/useFormatTime';

import { HeatmapScaleControl } from './DBHeatmapChart/HeatmapScaleControl';
import { BackgroundChartInput } from './BackgroundChartInput';
import {
attachLocalIds,
Expand All @@ -35,6 +36,7 @@ import {
stripLocalIds,
} from './ColorRulesEditor';
import { ColorSwatchInput } from './ColorSwatchInput';
import type { HeatmapScaleType } from './DBHeatmapChart';
import { CheckBoxControlled, TextInputControlled } from './InputControlled';
import { DEFAULT_NUMBER_FORMAT, NumberFormatForm } from './NumberFormat';

Expand All @@ -61,6 +63,8 @@ export type ChartConfigDisplaySettings = Pick<
// PromQL-only: Handlebars template over each series' Prometheus label set
// that renders the legend/tooltip name.
legendTemplate?: string;
// Heatmaps only; stored on the heatmap's select[0].
heatmapScaleType?: HeatmapScaleType;
};

/**
Expand Down Expand Up @@ -113,6 +117,7 @@ function applyDefaultSettings(
? attachLocalIds(settings.colorRules)
: undefined,
backgroundChart: settings.backgroundChart,
heatmapScaleType: settings.heatmapScaleType ?? 'log',
};
}

Expand Down Expand Up @@ -243,6 +248,8 @@ export default function ChartDisplaySettingsDrawer({
? 'Available on PromQL range queries.'
: 'Available on query-builder number tiles.';

const showHeatmapScale = displayType === DisplayType.Heatmap;

return (
<Drawer
title="Display Settings"
Expand Down Expand Up @@ -322,6 +329,22 @@ export default function ChartDisplaySettingsDrawer({
</>
)}

{showHeatmapScale && (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — The Log/Linear scale control now exists in two drawers

This adds a second copy of the Log/Linear SegmentedControl. The original is still in HeatmapSettingsDrawer.tsx (lines 75-92), which Search/DBSearchHeatmapChart.tsx still uses. The two can now drift in label ("Scale" vs "Y axis scale") and in options. Extract a shared HeatmapScaleControl component and use it in both drawers.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

<>
<Controller
control={control}
name="heatmapScaleType"
render={({ field: { onChange, value } }) => (
<HeatmapScaleControl
value={value ?? 'log'}
onChange={onChange}
/>
)}
/>
<Divider />
</>
)}

{showLegendTemplate && (
<>
<Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ type ChartEditorControlsProps = {
chartConfigForExplanations?: ChartConfigWithOptTimestamp;
onSubmit: (suppressErrorNotification?: boolean) => void;
openDisplaySettings: () => void;
openHeatmapSettings: () => void;
};

export function ChartEditorControls({
Expand Down Expand Up @@ -117,7 +116,6 @@ export function ChartEditorControls({
chartConfigForExplanations,
onSubmit,
openDisplaySettings,
openHeatmapSettings,
}: ChartEditorControlsProps) {
// Formulas (HDX-5080): derived series computed from the chart's series via
// letter-ref arithmetic expressions. Metric and event (log/trace) sources,
Expand Down Expand Up @@ -224,12 +222,15 @@ export function ChartEditorControls({
</Flex>
{displayType === DisplayType.Heatmap && Array.isArray(select) ? (
<HeatmapSeriesEditor
key={fields[0]?.id}
control={control}
setValue={setValue}
errors={errors}
tableSource={tableSource}
dateRange={dateRange}
parentRef={parentRef}
onSubmit={onSubmit}
onOpenDisplaySettings={openHeatmapSettings}
onOpenDisplaySettings={openDisplaySettings}
/>
) : displayType === DisplayType.EventPatterns ? (
<Flex gap="xs" direction="column">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,8 @@ import {
isStringSelectDisplayType,
validateChartForm,
} from '@/components/ChartEditor/utils';
import {
HEATMAP_DURATION_NUMBER_FORMAT,
type HeatmapScaleType,
} from '@/components/DBHeatmapChart';
import { HEATMAP_DURATION_NUMBER_FORMAT } from '@/components/DBHeatmapChart';
import { ErrorBoundary } from '@/components/Error/ErrorBoundary';
import HeatmapSettingsDrawer, {
HeatmapSettingsValues,
} from '@/components/HeatmapSettingsDrawer';
import { InputControlled } from '@/components/InputControlled';
import SaveToDashboardModal from '@/components/SaveToDashboardModal';
import { getStoredLanguage } from '@/components/SearchInput/SearchWhereInput';
Expand Down Expand Up @@ -349,6 +343,7 @@ export default function EditTimeChartForm({
colorRules,
backgroundChart,
legendTemplate,
heatmapScaleType,
] = useWatch({
control,
name: [
Expand All @@ -364,6 +359,7 @@ export default function EditTimeChartForm({
'colorRules',
'backgroundChart',
'legendTemplate',
'series.0.heatmapScaleType',
],
});

Expand Down Expand Up @@ -395,6 +391,7 @@ export default function EditTimeChartForm({
colorRules,
backgroundChart,
legendTemplate,
heatmapScaleType,
}),
[
alignDateRangeToGranularity,
Expand All @@ -409,6 +406,7 @@ export default function EditTimeChartForm({
colorRules,
backgroundChart,
legendTemplate,
heatmapScaleType,
],
);

Expand All @@ -417,11 +415,6 @@ export default function EditTimeChartForm({
{ open: openDisplaySettings, close: closeDisplaySettings },
] = useDisclosure(false);

const [
heatmapSettingsOpened,
{ open: openHeatmapSettings, close: closeHeatmapSettings },
] = useDisclosure(false);

// Only update this on submit, otherwise we'll have issues
// with using the source value from the last submit
// (ex. ignoring local custom source updates)
Expand Down Expand Up @@ -890,6 +883,7 @@ export default function EditTimeChartForm({
colorRules,
backgroundChart,
legendTemplate,
heatmapScaleType,
}: ChartConfigDisplaySettings,
isDirty: boolean,
) => {
Expand All @@ -916,6 +910,9 @@ export default function EditTimeChartForm({
if (configType === 'promql') {
setValue('legendTemplate', legendTemplate ?? '');
}
if (displayType === DisplayType.Heatmap) {
setValue('series.0.heatmapScaleType', heatmapScaleType);
}
// Display settings live in a separate drawer form, so RHF can't track
// them. Latch dirty state only when the drawer reports actual changes.
if (isDirty) {
Expand All @@ -924,44 +921,7 @@ export default function EditTimeChartForm({
}
onSubmit();
},
[setValue, onDirtyChange, onSubmit, configType],
);

const handleUpdateHeatmapSettings = useCallback(
(data: HeatmapSettingsValues) => {
setValue('series.0.valueExpression', data.value);
setValue('series.0.countExpression', data.count || 'count()');
setValue('series.0.heatmapScaleType', data.scaleType);
// Heatmap settings are applied outside RHF's change tracking.
subFormDirtyRef.current = true;
onDirtyChange?.(true);
onSubmit();
closeHeatmapSettings();
},
[setValue, onDirtyChange, onSubmit, closeHeatmapSettings],
);

const heatmapValueExpression = useWatch({
control,
name: 'series.0.valueExpression',
});
const heatmapCountExpression = useWatch({
control,
name: 'series.0.countExpression',
});
const heatmapScaleType: HeatmapScaleType =
useWatch({
control,
name: 'series.0.heatmapScaleType',
}) ?? 'log';

const heatmapSettingsDefaults = useMemo(
() => ({
value: heatmapValueExpression || '',
count: heatmapCountExpression || 'count()',
scaleType: heatmapScaleType,
}),
[heatmapValueExpression, heatmapCountExpression, heatmapScaleType],
[setValue, onDirtyChange, onSubmit, configType, displayType],
);

const tableConnection = useMemo(
Expand Down Expand Up @@ -1152,7 +1112,6 @@ export default function EditTimeChartForm({
chartConfigForExplanations={chartConfigForExplanations}
onSubmit={onSubmit}
openDisplaySettings={openDisplaySettings}
openHeatmapSettings={openHeatmapSettings}
/>
)}
<ChartActionBar
Expand Down Expand Up @@ -1221,17 +1180,10 @@ export default function EditTimeChartForm({
})}
onChange={handleUpdateDisplaySettings}
onClose={closeDisplaySettings}
isPerSeriesNumberFormatAllowed={configType !== 'sql'}
/>
<HeatmapSettingsDrawer
opened={heatmapSettingsOpened}
onClose={closeHeatmapSettings}
connection={tableConnection}
sourceId={tableSource?.id}
dateRange={dateRange}
parentRef={parentRef}
defaultValues={heatmapSettingsDefaults}
onSubmit={handleUpdateHeatmapSettings}
// Heatmaps format with the chart-level number format only.
isPerSeriesNumberFormatAllowed={
configType !== 'sql' && displayType !== DisplayType.Heatmap
}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,122 @@
import { useMemo } from 'react';
import { Control, UseFormSetValue } from 'react-hook-form';
import { Control, FieldErrors, UseFormSetValue } from 'react-hook-form';
import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata';
import { TSource } from '@hyperdx/common-utils/dist/types';
import { Button, Divider, Flex } from '@mantine/core';
import { Button, Divider, Flex, Stack, Text, Tooltip } from '@mantine/core';
import { IconHelpCircle } from '@tabler/icons-react';

import { ChartEditorFormState } from '@/components/ChartEditor/types';
import { EDITOR_INPUT_HEIGHTS } from '@/components/editorInputHeights';
import SearchWhereInput from '@/components/SearchInput/SearchWhereInput';
import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor';

type HeatmapSeriesEditorProps = {
control: Control<ChartEditorFormState>;
setValue: UseFormSetValue<ChartEditorFormState>;
errors: FieldErrors<ChartEditorFormState>;
tableSource?: TSource;
dateRange?: [Date, Date];
parentRef: HTMLElement | null;
onSubmit: () => void;
onOpenDisplaySettings: () => void;
};

function InlineLabel({ label, tooltip }: { label: string; tooltip: string }) {
return (
<Flex h={`${EDITOR_INPUT_HEIGHTS.sm}px`} align="center" gap={4}>
<Text size="sm" style={{ whiteSpace: 'nowrap' }}>
{label}
</Text>
<Tooltip label={tooltip}>
<IconHelpCircle size={16} opacity={0.5} />
</Tooltip>
</Flex>
);
}

export function HeatmapSeriesEditor({
control,
setValue,
errors,
tableSource,
dateRange,
parentRef,
onSubmit,
onOpenDisplaySettings,
}: HeatmapSeriesEditorProps) {
const connection = useMemo(() => tcFromSource(tableSource), [tableSource]);

return (
<Flex direction="column" gap="sm">
<SearchWhereInput
tableConnection={connection}
sourceId={tableSource?.id}
dateRange={dateRange}
control={control}
name="where"
onSubmit={onSubmit}
onLanguageChange={(lang: 'sql' | 'lucene') =>
setValue('whereLanguage', lang)
}
enableVariables
/>
<Divider />
<Flex direction="column">
<Stack gap="sm">
<div
className="gap-2"
style={{
display: 'grid',
gridTemplateColumns: 'auto 1fr auto 1fr',
alignItems: 'start',
}}
>
<InlineLabel
label="Value"
tooltip="The y axis: the value each event is bucketed by."
/>
<div data-testid="heatmap-value-input">
<SQLInlineEditorControlled
parentRef={parentRef}
tableConnection={connection}
sourceId={tableSource?.id}
dateRange={dateRange}
control={control}
name="series.0.valueExpression"
placeholder="SQL expression, e.g. Duration"
onSubmit={onSubmit}
error={
Array.isArray(errors.series)
? errors.series[0]?.valueExpression?.message
: undefined
}
enableVariables
/>
</div>
<InlineLabel
label="Count"
tooltip="The color intensity: how much each bucket holds."
/>
<div data-testid="heatmap-count-input">
<SQLInlineEditorControlled
parentRef={parentRef}
tableConnection={connection}
sourceId={tableSource?.id}
dateRange={dateRange}
control={control}
name="series.0.countExpression"
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
placeholder="SQL expression, e.g. count()"
onSubmit={onSubmit}
enableVariables
/>
</div>
</div>
<SearchWhereInput
tableConnection={connection}
sourceId={tableSource?.id}
dateRange={dateRange}
control={control}
name="where"
onSubmit={onSubmit}
onLanguageChange={(lang: 'sql' | 'lucene') =>
setValue('whereLanguage', lang)
}
enableVariables
/>
</Stack>
<Divider my="sm" />
<Flex justify="flex-end">
<Button
onClick={onOpenDisplaySettings}
size="compact-sm"
variant="secondary"
data-testid="display-settings-button"
>
Display Settings
</Button>
Expand Down
Loading
Loading