Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix: fix threshold validation mismatch (#9196)

SagarRajput-7e05ede397828
diff --git a/frontend/src/container/NewWidget/RightContainer/Threshold/Threshold.tsx b/frontend/src/container/NewWidget/RightContainer/Threshold/Threshold.tsx
index bc96438c65..158091e21c 100644
--- a/frontend/src/container/NewWidget/RightContainer/Threshold/Threshold.tsx
+++ b/frontend/src/container/NewWidget/RightContainer/Threshold/Threshold.tsx
@@ -43,6 +43,7 @@ function Threshold({
tableOptions,
thresholdTableOptions = '',
columnUnits,
+ yAxisUnit,
}: ThresholdProps): JSX.Element {
const [isEditMode, setIsEditMode] = useState<boolean>(isEditEnabled);
const [operator, setOperator] = useState<string | number>(
@@ -195,16 +196,13 @@ function Threshold({
const allowDragAndDrop = panelTypeVsDragAndDrop[selectedGraph];
- const isInvalidUnitComparison = useMemo(
- () =>
- unit !== 'none' &&
- convertUnit(
- value,
- unit,
- getColumnUnit(tableSelectedOption, columnUnits || {}),
- ) === null,
- [unit, value, columnUnits, tableSelectedOption],
- );
+ const isInvalidUnitComparison = useMemo(() => {
+ const toUnitId =
+ selectedGraph === PANEL_TYPES.TABLE
+ ? getColumnUnit(tableSelectedOption, columnUnits || {})
+ : yAxisUnit;
+ return unit !== 'none' && convertUnit(value, unit, toUnitId) === null;
+ }, [selectedGraph, yAxisUnit, tableSelectedOption, columnUnits, unit, value]);
return (
<div
@@ -318,7 +316,9 @@ function Threshold({
<Select
defaultValue={unit}
options={unitOptions(
- getColumnUnit(tableSelectedOption, columnUnits || {}) || '',
+ selectedGraph === PANEL_TYPES.TABLE
+ ? getColumnUnit(tableSelectedOption, columnUnits || {}) || ''
+ : yAxisUnit || '',
)}
onChange={handleUnitChange}
showSearch
@@ -357,8 +357,12 @@ function Threshold({
</div>
{isInvalidUnitComparison && (
<Typography.Text className="invalid-unit">
- Threshold unit ({unit}) is not valid in comparison with the column unit (
- {getColumnUnit(tableSelectedOption, columnUnits || {}) || 'none'})
+ Threshold unit ({unit}) is not valid in comparison with the{' '}
+ {selectedGraph === PANEL_TYPES.TABLE ? 'column' : 'y-axis'} unit (
+ {selectedGraph === PANEL_TYPES.TABLE
+ ? getColumnUnit(tableSelectedOption, columnUnits || {}) || 'none'
+ : yAxisUnit || 'none'}
+ )
</Typography.Text>
)}
{isEditMode && (
diff --git a/frontend/src/container/NewWidget/RightContainer/Threshold/ThresholdSelector.tsx b/frontend/src/container/NewWidget/RightContainer/Threshold/ThresholdSelector.tsx
index 77b602684a..f1d9786c97 100644
--- a/frontend/src/container/NewWidget/RightContainer/Threshold/ThresholdSelector.tsx
+++ b/frontend/src/container/NewWidget/RightContainer/Threshold/ThresholdSelector.tsx
@@ -95,6 +95,7 @@ function ThresholdSelector({
tableOptions={aggregationQueries}
thresholdTableOptions={threshold.thresholdTableOptions}
columnUnits={columnUnits}
+ yAxisUnit={yAxisUnit}
/>
))}
</div>
diff --git a/frontend/src/container/NewWidget/RightContainer/Threshold/__tests__/Threshold.test.tsx b/frontend/src/container/NewWidget/RightContainer/Threshold/__tests__/Threshold.test.tsx
new file mode 100644
index 0000000000..99e1c42b17
--- /dev/null
+++ b/frontend/src/container/NewWidget/RightContainer/Threshold/__tests__/Threshold.test.tsx
@@ -0,0 +1,158 @@
+/* eslint-disable react/jsx-props-no-spreading */
+import { PANEL_TYPES } from 'constants/queryBuilder';
+import { DndProvider } from 'react-dnd';
+import { HTML5Backend } from 'react-dnd-html5-backend';
+import { render, screen } from 'tests/test-utils';
+
+import Threshold from '../Threshold';
+
+// Mock the getColumnUnit function
+jest.mock('lib/query/createTableColumnsFromQuery', () => ({
+ getColumnUnit: jest.fn(
+ (option: string, columnUnits: Record<string, string>) =>
+ columnUnits[option] || 'percent',
+ ),
+}));
+
+// Mock the unitOptions function
+jest.mock('container/NewWidget/utils', () => ({
+ unitOptions: jest.fn(() => [
+ { value: 'none', label: 'None' },
+ { value: 'percent', label: 'Percent' },
+ { value: 'ms', label: 'Milliseconds' },
+ ]),
+}));
+
+const defaultProps = {
+ index: 'test-threshold-1',
+ keyIndex: 0,
+ thresholdOperator: '>' as const,
+ thresholdValue: 50,
+ thresholdUnit: 'none',
+ thresholdColor: 'Red',
+ thresholdFormat: 'Text' as const,
+ isEditEnabled: true,
+ selectedGraph: PANEL_TYPES.TABLE,
+ tableOptions: [
+ { value: 'cpu_usage', label: 'CPU Usage' },
+ { value: 'memory_usage', label: 'Memory Usage' },
+ ],
+ thresholdTableOptions: 'cpu_usage',
+ columnUnits: { cpu_usage: 'percent', memory_usage: 'bytes' },
+ yAxisUnit: 'percent',
+ moveThreshold: jest.fn(),
+};
+
+const renderThreshold = (props = {}): void => {
+ render(
+ <DndProvider backend={HTML5Backend}>
+ <Threshold {...{ ...defaultProps, ...props }} />
+ </DndProvider>,
+ );
+};
+
+describe('Threshold Component Unit Validation', () => {
+ it('should not show validation error when threshold unit is "none" regardless of column unit', () => {
+ // Act - Render component with "none" threshold unit
+ renderThreshold({
+ thresholdUnit: 'none',
+ thresholdValue: 50,
+ });
+
+ // Assert - No validation error should be displayed
+ expect(
+ screen.queryByText(/Threshold unit.*is not valid in comparison/i),
+ ).not.toBeInTheDocument();
+ });
+
+ it('should show validation error when threshold unit is not "none" and units are incompatible', () => {
+ // Act - Render component with incompatible units (ms vs percent)
+ renderThreshold({
+ thresholdUnit: 'ms',
+ thresholdValue: 50,
+ });
+
+ // Assert - Validation error should be displayed
+ expect(
+ screen.getByText(
+ /Threshold unit \(ms\) is not valid in comparison with the column unit \(percent\)/i,
+ ),
+ ).toBeInTheDocument();
+ });
+
+ it('should not show validation error when threshold unit matches column unit', () => {
+ // Act - Render component with matching units
+ renderThreshold({
+ thresholdUnit: 'percent',
+ thresholdValue: 50,
+ });
+
+ // Assert - No validation error should be displayed
+ expect(
+ screen.queryByText(/Threshold unit.*is not valid in comparison/i),
+ ).not.toBeInTheDocument();
+ });
+
+ it('should show validation error for time series graph when units are incompatible', () => {
+ // Act - Render component for time series with incompatible units
+ renderThreshold({
+ selectedGraph: PANEL_TYPES.TIME_SERIES,
+ thresholdUnit: 'ms',
+ thresholdValue: 100,
+ yAxisUnit: 'percent',
+ });
+
+ // Assert - Validation error should be displayed
+ expect(
+ screen.getByText(
+ /Threshold unit \(ms\) is not valid in comparison with the y-axis unit \(percent\)/i,
+ ),
+ ).toBeInTheDocument();
+ });
+
+ it('should not show validation error for time series graph when threshold unit is "none"', () => {
+ // Act - Render component for time series with "none" threshold unit
+ renderThreshold({
+ selectedGraph: PANEL_TYPES.TIME_SERIES,
+ thresholdUnit: 'none',
+ thresholdValue: 100,
+ yAxisUnit: 'percent',
+ });
+
+ // Assert - No validation error should be displayed
+ expect(
+ screen.queryByText(/Threshold unit.*is not valid in comparison/i),
+ ).not.toBeInTheDocument();
+ });
+
+ it('should not show validation error when threshold unit is compatible with column unit', () => {
+ // Act - Render component with compatible units (both in same category - Time)
+ renderThreshold({
+ thresholdUnit: 's',
+ thresholdValue: 100,
+ columnUnits: { cpu_usage: 'ms' },
+ thresholdTableOptions: 'cpu_usage',
+ });
+
+ // Assert - No validation error should be displayed
+ expect(
+ screen.queryByText(/Threshold unit.*is not valid in comparison/i),
+ ).not.toBeInTheDocument();
+ });
+
+ it('should show validation error when threshold unit is in different category than column unit', () => {
+ // Act - Render component with units from different categories
+ renderThreshold({
+ thresholdUnit: 'bytes',
+ thresholdValue: 100,
+ yAxisUnit: 'percent',
+ });
+
+ // Assert - Validation error should be displayed
+ expect(
+ screen.getByText(
+ /Threshold unit \(bytes\) is not valid in comparison with the column unit \(percent\)/i,
+ ),
+ ).toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/container/NewWidget/RightContainer/Threshold/types.ts b/frontend/src/container/NewWidget/RightContainer/Threshold/types.ts
index 6f09f2136e..b230d47a7c 100644
--- a/frontend/src/container/NewWidget/RightContainer/Threshold/types.ts
+++ b/frontend/src/container/NewWidget/RightContainer/Threshold/types.ts
@@ -21,6 +21,7 @@ export type ThresholdProps = {
selectedGraph: PANEL_TYPES;
tableOptions?: Array<{ value: string; label: string }>;
columnUnits?: ColumnUnit;
+ yAxisUnit?: string;
};
export type ShowCaseValueProps = {