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.

chore: y-axis unit selector changes in alerts and dashboards (#9936)

Amlan Kumar Nandyd98851dcf209
diff --git a/frontend/src/components/YAxisUnitSelector/YAxisUnitSelector.tsx b/frontend/src/components/YAxisUnitSelector/YAxisUnitSelector.tsx
index 338a782e3d..5f1e8b7ab1 100644
--- a/frontend/src/components/YAxisUnitSelector/YAxisUnitSelector.tsx
+++ b/frontend/src/components/YAxisUnitSelector/YAxisUnitSelector.tsx
@@ -34,7 +34,7 @@ function YAxisUnitSelector({
initialValue,
);
const currentUniversalUnitName = getUniversalNameFromMetricUnit(value);
- return `Unit mismatch. Saved unit is ${initialUniversalUnitName}, but ${currentUniversalUnitName} is selected.`;
+ return `Unit mismatch. The metric was sent with unit ${initialUniversalUnitName}, but ${currentUniversalUnitName} is selected.`;
}
return '';
}, [initialValue, value, loading]);
@@ -82,6 +82,7 @@ function YAxisUnitSelector({
'warning-state': incompatibleUnitMessage,
})}
data-testid={dataTestId}
+ allowClear
>
{categories.map((category) => (
<Select.OptGroup key={category.name} label={category.name}>
diff --git a/frontend/src/components/YAxisUnitSelector/__tests__/YAxisUnitSelector.test.tsx b/frontend/src/components/YAxisUnitSelector/__tests__/YAxisUnitSelector.test.tsx
index e130c9d497..4ac1c615b4 100644
--- a/frontend/src/components/YAxisUnitSelector/__tests__/YAxisUnitSelector.test.tsx
+++ b/frontend/src/components/YAxisUnitSelector/__tests__/YAxisUnitSelector.test.tsx
@@ -106,7 +106,7 @@ describe('YAxisUnitSelector', () => {
fireEvent.mouseOver(warningIcon);
return screen
.findByText(
- 'Unit mismatch. Saved unit is Seconds (s), but Bytes (B) is selected.',
+ 'Unit mismatch. The metric was sent with unit Seconds (s), but Bytes (B) is selected.',
)
.then((el) => expect(el).toBeInTheDocument());
});
diff --git a/frontend/src/components/YAxisUnitSelector/styles.scss b/frontend/src/components/YAxisUnitSelector/styles.scss
index 4ac57178b9..c15fc06444 100644
--- a/frontend/src/components/YAxisUnitSelector/styles.scss
+++ b/frontend/src/components/YAxisUnitSelector/styles.scss
@@ -12,4 +12,8 @@
.anticon {
color: var(--bg-amber-400) !important;
}
+
+ .ant-select-clear {
+ right: 28px;
+ }
}
diff --git a/frontend/src/container/CreateAlertV2/QuerySection/ChartPreview/ChartPreview.tsx b/frontend/src/container/CreateAlertV2/QuerySection/ChartPreview/ChartPreview.tsx
index 464d084128..65144e8468 100644
--- a/frontend/src/container/CreateAlertV2/QuerySection/ChartPreview/ChartPreview.tsx
+++ b/frontend/src/container/CreateAlertV2/QuerySection/ChartPreview/ChartPreview.tsx
@@ -35,24 +35,21 @@ function ChartPreview({ alertDef }: ChartPreviewProps): JSX.Element {
const yAxisUnit = alertState.yAxisUnit || '';
- const fetchYAxisUnit =
+ const shouldUpdateYAxisUnit =
!isEditMode && alertType === AlertTypes.METRICS_BASED_ALERT;
const selectedQueryName = thresholdState.selectedQuery;
const { yAxisUnit: initialYAxisUnit, isLoading } = useGetYAxisUnit(
selectedQueryName,
- {
- enabled: fetchYAxisUnit,
- },
);
// Every time a new metric is selected, set the y-axis unit to its unit value if present
- // Only for metrics-based alerts
+ // Only for metrics-based alerts in create mode
useEffect(() => {
- if (fetchYAxisUnit) {
+ if (shouldUpdateYAxisUnit) {
setAlertState({ type: 'SET_Y_AXIS_UNIT', payload: initialYAxisUnit });
}
- }, [initialYAxisUnit, setAlertState, fetchYAxisUnit]);
+ }, [initialYAxisUnit, setAlertState, shouldUpdateYAxisUnit]);
const headline = (
<div className="chart-preview-headline">
diff --git a/frontend/src/container/FormAlertRules/index.tsx b/frontend/src/container/FormAlertRules/index.tsx
index 0098156212..97629b9868 100644
--- a/frontend/src/container/FormAlertRules/index.tsx
+++ b/frontend/src/container/FormAlertRules/index.tsx
@@ -6,6 +6,8 @@ import saveAlertApi from 'api/alerts/save';
import testAlertApi from 'api/alerts/testAlert';
import logEvent from 'api/common/logEvent';
import { getInvolvedQueriesInTraceOperator } from 'components/QueryBuilderV2/QueryV2/TraceOperator/utils/utils';
+import YAxisUnitSelector from 'components/YAxisUnitSelector';
+import { YAxisSource } from 'components/YAxisUnitSelector/types';
import { ALERTS_DATA_SOURCE_MAP } from 'constants/alerts';
import { FeatureKeys } from 'constants/features';
import { QueryParams } from 'constants/query';
@@ -14,9 +16,9 @@ import { REACT_QUERY_KEY } from 'constants/reactQueryKeys';
import ROUTES from 'constants/routes';
import QueryTypeTag from 'container/NewWidget/LeftContainer/QueryTypeTag';
import PlotTag from 'container/NewWidget/LeftContainer/WidgetGraph/PlotTag';
-import { BuilderUnitsFilter } from 'container/QueryBuilder/filters';
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
import { useShareBuilderUrl } from 'hooks/queryBuilder/useShareBuilderUrl';
+import useGetYAxisUnit from 'hooks/useGetYAxisUnit';
import { useNotifications } from 'hooks/useNotifications';
import { useSafeNavigate } from 'hooks/useSafeNavigate';
import useUrlQuery from 'hooks/useUrlQuery';
@@ -483,7 +485,7 @@ function FormAlertRules({
chQueries: mapQueryDataToApi(currentQuery.clickhouse_sql, 'name').data,
queryType: currentQuery.queryType,
panelType: panelType || initQuery.panelType,
- unit: currentQuery.unit,
+ unit: yAxisUnit,
}),
},
};
@@ -502,6 +504,7 @@ function FormAlertRules({
alertType,
initQuery,
panelType,
+ yAxisUnit,
]);
const saveRule = useCallback(async () => {
@@ -527,8 +530,7 @@ function FormAlertRules({
if (response.statusCode === 200) {
logData = {
status: 'success',
- statusMessage:
- !ruleId || isEmpty(ruleId) ? t('rule_created') : t('rule_edited'),
+ statusMessage: isNewRule ? t('rule_created') : t('rule_edited'),
};
notifications.success({
@@ -580,7 +582,7 @@ function FormAlertRules({
dataSource: ALERTS_DATA_SOURCE_MAP[postableAlert?.alertType as AlertTypes],
channelNames: postableAlert?.preferredChannels,
broadcastToAll: postableAlert?.broadcastToAll,
- isNewRule: !ruleId || isEmpty(ruleId),
+ isNewRule,
ruleId,
queryType: currentQuery.queryType,
alertId: postableAlert?.id,
@@ -665,7 +667,7 @@ function FormAlertRules({
dataSource: ALERTS_DATA_SOURCE_MAP[alertDef?.alertType as AlertTypes],
channelNames: postableAlert?.preferredChannels,
broadcastToAll: postableAlert?.broadcastToAll,
- isNewRule: !ruleId || isEmpty(ruleId),
+ isNewRule,
ruleId,
queryType: currentQuery.queryType,
status: statusResponse.status,
@@ -737,8 +739,6 @@ function FormAlertRules({
alertDef?.broadcastToAll ||
(alertDef.preferredChannels && alertDef.preferredChannels.length > 0);
- const isRuleCreated = !ruleId || isEmpty(ruleId);
-
function handleRedirection(option: AlertTypes): void {
let url;
if (
@@ -753,7 +753,7 @@ function FormAlertRules({
if (url) {
logEvent('Alert: Check example alert clicked', {
dataSource: ALERTS_DATA_SOURCE_MAP[alertDef?.alertType as AlertTypes],
- isNewRule: !ruleId || isEmpty(ruleId),
+ isNewRule,
ruleId,
queryType: currentQuery.queryType,
link: url,
@@ -763,7 +763,7 @@ function FormAlertRules({
}
useEffect(() => {
- if (!isRuleCreated) {
+ if (!isNewRule) {
logEvent('Alert: Edit page visited', {
ruleId,
dataSource: ALERTS_DATA_SOURCE_MAP[alertType as AlertTypes],
@@ -788,6 +788,24 @@ function FormAlertRules({
featureFlags?.find((flag) => flag.name === FeatureKeys.ANOMALY_DETECTION)
?.active || false;
+ // Only update automatically when creating a new metrics-based alert rule
+ const shouldUpdateYAxisUnit = useMemo(
+ () => isNewRule && alertType === AlertTypes.METRICS_BASED_ALERT,
+ [isNewRule, alertType],
+ );
+
+ const { yAxisUnit: initialYAxisUnit, isLoading } = useGetYAxisUnit(
+ alertDef.condition.selectedQueryName,
+ );
+
+ // Every time a new metric is selected, set the y-axis unit to its unit
+ // Only for metrics-based alerts in create mode
+ useEffect(() => {
+ if (shouldUpdateYAxisUnit) {
+ setYAxisUnit(initialYAxisUnit || '');
+ }
+ }, [initialYAxisUnit, shouldUpdateYAxisUnit]);
+
return (
<>
{Element}
@@ -795,7 +813,7 @@ function FormAlertRules({
<div
id="top"
className={`form-alert-rules-container ${
- isRuleCreated ? 'create-mode' : 'edit-mode'
+ isNewRule ? 'create-mode' : 'edit-mode'
}`}
>
<div className="overview-header">
@@ -843,9 +861,12 @@ function FormAlertRules({
</div>
<StepContainer>
- <BuilderUnitsFilter
+ <YAxisUnitSelector
+ value={yAxisUnit}
+ initialValue={initialYAxisUnit}
onChange={onUnitChangeHandler}
- yAxisUnit={yAxisUnit}
+ source={YAxisSource.ALERTS}
+ loading={isLoading}
/>
</StepContainer>
@@ -923,7 +944,7 @@ function FormAlertRules({
type="default"
onClick={onCancelHandler}
>
- {(!ruleId || isEmpty(ruleId)) && t('button_cancelchanges')}
+ {isNewRule && t('button_cancelchanges')}
{ruleId && !isEmpty(ruleId) && t('button_discard')}
</ActionButton>
</ButtonContainer>
diff --git a/frontend/src/container/NewWidget/RightContainer/ColumnUnitSelector/ColumnUnitSelector.styles.scss b/frontend/src/container/NewWidget/RightContainer/ColumnUnitSelector/ColumnUnitSelector.styles.scss
index bd08709322..41866397cb 100644
--- a/frontend/src/container/NewWidget/RightContainer/ColumnUnitSelector/ColumnUnitSelector.styles.scss
+++ b/frontend/src/container/NewWidget/RightContainer/ColumnUnitSelector/ColumnUnitSelector.styles.scss
@@ -20,6 +20,16 @@
width: 32px;
}
}
+
+ .y-axis-unit-selector-v2 {
+ flex-direction: row !important;
+ align-items: center;
+ gap: 24px !important;
+
+ .y-axis-unit-selector-component {
+ width: 100%;
+ }
+ }
}
.lightMode {
diff --git a/frontend/src/container/NewWidget/RightContainer/ColumnUnitSelector/ColumnUnitSelector.tsx b/frontend/src/container/NewWidget/RightContainer/ColumnUnitSelector/ColumnUnitSelector.tsx
index 5742815de0..95318f91aa 100644
--- a/frontend/src/container/NewWidget/RightContainer/ColumnUnitSelector/ColumnUnitSelector.tsx
+++ b/frontend/src/container/NewWidget/RightContainer/ColumnUnitSelector/ColumnUnitSelector.tsx
@@ -7,18 +7,19 @@ import { isEmpty } from 'lodash-es';
import { Dispatch, SetStateAction, useCallback, useEffect } from 'react';
import { ColumnUnit } from 'types/api/dashboard/getAll';
-import YAxisUnitSelector from '../YAxisUnitSelector';
+import YAxisUnitSelectorV2 from '../DashboardYAxisUnitSelectorWrapper';
interface ColumnUnitSelectorProps {
columnUnits: ColumnUnit;
setColumnUnits: Dispatch<SetStateAction<ColumnUnit>>;
+ isNewDashboard: boolean;
}
export function ColumnUnitSelector(
props: ColumnUnitSelectorProps,
): JSX.Element {
const { currentQuery } = useQueryBuilder();
- const { columnUnits, setColumnUnits } = props;
+ const { columnUnits, setColumnUnits, isNewDashboard } = props;
const aggregationQueries = useGetQueryLabels(currentQuery);
@@ -71,19 +72,22 @@ export function ColumnUnitSelector(
return (
<section className="column-unit-selector">
<Typography.Text className="heading">Column Units</Typography.Text>
- {aggregationQueries.map(({ value, label }) => (
- <YAxisUnitSelector
- value={columnUnits[value] || ''}
- onSelect={(unitValue: string): void =>
- handleColumnUnitSelect(value, unitValue)
- }
- fieldLabel={label}
- key={value}
- handleClear={(): void => {
- handleColumnUnitSelect(value, '');
- }}
- />
- ))}
+ {aggregationQueries.map(({ value, label }) => {
+ const baseQueryName = value.split('.')[0];
+ return (
+ <YAxisUnitSelectorV2
+ value={columnUnits[value] || ''}
+ onSelect={(unitValue: string): void =>
+ handleColumnUnitSelect(value, unitValue)
+ }
+ fieldLabel={label}
+ key={value}
+ selectedQueryName={baseQueryName}
+ // Update the column unit value automatically only in create mode
+ shouldUpdateYAxisUnit={isNewDashboard}
+ />
+ );
+ })}
</section>
);
}
diff --git a/frontend/src/container/NewWidget/RightContainer/ColumnUnitSelector/__tests__/ColumnSelector.test.tsx b/frontend/src/container/NewWidget/RightContainer/ColumnUnitSelector/__tests__/ColumnSelector.test.tsx
index a25c79c313..fc556e1e21 100644
--- a/frontend/src/container/NewWidget/RightContainer/ColumnUnitSelector/__tests__/ColumnSelector.test.tsx
+++ b/frontend/src/container/NewWidget/RightContainer/ColumnUnitSelector/__tests__/ColumnSelector.test.tsx
@@ -99,7 +99,12 @@ describe('Column unit selector panel unit test', () => {
(useLocation as jest.Mock).mockReturnValue(mockLocation);
const { getByText } = render(
<QueryBuilderProvider>
- <ColumnUnitSelector columnUnits={{}} setColumnUnits={(): void => {}} />,
+ <ColumnUnitSelector
+ columnUnits={{}}
+ setColumnUnits={(): void => {}}
+ isNewDashboard={false}
+ />
+ ,
</QueryBuilderProvider>,
);
diff --git a/frontend/src/container/NewWidget/RightContainer/DashboardYAxisUnitSelectorWrapper.tsx b/frontend/src/container/NewWidget/RightContainer/DashboardYAxisUnitSelectorWrapper.tsx
new file mode 100644
index 0000000000..08ca81194a
--- /dev/null
+++ b/frontend/src/container/NewWidget/RightContainer/DashboardYAxisUnitSelectorWrapper.tsx
@@ -0,0 +1,54 @@
+import { Typography } from 'antd';
+import YAxisUnitSelectorComponent from 'components/YAxisUnitSelector';
+import { YAxisSource } from 'components/YAxisUnitSelector/types';
+import useGetYAxisUnit from 'hooks/useGetYAxisUnit';
+import { Dispatch, SetStateAction, useEffect } from 'react';
+
+type OnSelectType = Dispatch<SetStateAction<string>> | ((val: string) => void);
+
+/**
+ * Wrapper component for the y-axis unit selector for dashboards.
+ */
+function DashboardYAxisUnitSelectorWrapper({
+ value,
+ onSelect,
+ fieldLabel,
+ shouldUpdateYAxisUnit,
+ selectedQueryName,
+}: {
+ value: string;
+ onSelect: OnSelectType;
+ fieldLabel: string;
+ shouldUpdateYAxisUnit: boolean;
+ selectedQueryName?: string;
+}): JSX.Element {
+ const { yAxisUnit: initialYAxisUnit, isLoading } = useGetYAxisUnit(
+ selectedQueryName,
+ );
+
+ useEffect(() => {
+ if (shouldUpdateYAxisUnit) {
+ onSelect(initialYAxisUnit || '');
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [initialYAxisUnit, shouldUpdateYAxisUnit]);
+
+ return (
+ <div className="y-axis-unit-selector-v2">
+ <Typography.Text className="heading">{fieldLabel}</Typography.Text>
+ <YAxisUnitSelectorComponent
+ value={value}
+ onChange={onSelect}
+ initialValue={initialYAxisUnit}
+ source={YAxisSource.DASHBOARDS}
+ loading={isLoading}
+ />
+ </div>
+ );
+}
+
+DashboardYAxisUnitSelectorWrapper.defaultProps = {
+ selectedQueryName: undefined,
+};
+
+export default DashboardYAxisUnitSelectorWrapper;
diff --git a/frontend/src/container/NewWidget/RightContainer/RightContainer.styles.scss b/frontend/src/container/NewWidget/RightContainer/RightContainer.styles.scss
index b1da70c6cb..ee8654725e 100644
--- a/frontend/src/container/NewWidget/RightContainer/RightContainer.styles.scss
+++ b/frontend/src/container/NewWidget/RightContainer/RightContainer.styles.scss
@@ -191,7 +191,8 @@
text-transform: uppercase;
}
- .y-axis-unit-selector {
+ .y-axis-unit-selector,
+ .y-axis-unit-selector-v2 {
margin-top: 16px;
display: flex;
flex-direction: column;
@@ -224,6 +225,21 @@
}
}
}
+
+ .y-axis-unit-selector-v2 {
+ .y-axis-unit-selector-component {
+ .ant-select {
+ width: 100%;
+ border-radius: 2px;
+
+ .ant-select-selector {
+ border: 1px solid var(--bg-slate-400);
+ background: var(--bg-ink-300);
+ }
+ }
+ }
+ }
+
.soft-min-max {
display: flex;
align-items: center;
@@ -444,6 +460,17 @@
}
}
+ .y-axis-unit-selector-v2 {
+ .y-axis-unit-selector-component {
+ .ant-select {
+ .ant-select-selector {
+ border: 1px solid var(--bg-vanilla-300);
+ background: var(--bg-vanilla-300);
+ }
+ }
+ }
+ }
+
.fill-gaps {
border: 1px solid var(--bg-vanilla-300);
background: var(--bg-vanilla-300);
@@ -472,7 +499,8 @@
color: var(--bg-ink-400);
}
- .y-axis-unit-selector {
+ .y-axis-unit-selector,
+ .y-axis-unit-selector-v2 {
.heading {
color: var(--bg-ink-400);
}
diff --git a/frontend/src/container/NewWidget/RightContainer/YAxisUnitSelector.tsx b/frontend/src/container/NewWidget/RightContainer/YAxisUnitSelector.tsx
index 2641690971..1b71212085 100644
--- a/frontend/src/container/NewWidget/RightContainer/YAxisUnitSelector.tsx
+++ b/frontend/src/container/NewWidget/RightContainer/YAxisUnitSelector.tsx
@@ -15,6 +15,9 @@ const findCategoryByName = (
type OnSelectType = Dispatch<SetStateAction<string>> | ((val: string) => void);
+/**
+ * @deprecated Use DashboardYAxisUnitSelectorWrapper instead.
+ */
function YAxisUnitSelector({
value,
onSelect,
diff --git a/frontend/src/container/NewWidget/RightContainer/__tests__/DashboardYAxisUnitSelectorWrapper.test.tsx b/frontend/src/container/NewWidget/RightContainer/__tests__/DashboardYAxisUnitSelectorWrapper.test.tsx
new file mode 100644
index 0000000000..2010e6b4cd
--- /dev/null
+++ b/frontend/src/container/NewWidget/RightContainer/__tests__/DashboardYAxisUnitSelectorWrapper.test.tsx
@@ -0,0 +1,79 @@
+import { render, screen } from '@testing-library/react';
+import { UniversalYAxisUnit } from 'components/YAxisUnitSelector/types';
+import * as getYAxisUnitHooks from 'hooks/useGetYAxisUnit';
+
+import DashboardYAxisUnitSelectorWrapper from '../DashboardYAxisUnitSelectorWrapper';
+
+describe('YAxisUnitSelectorV2', () => {
+ const mockUseGetYAxisUnit = jest.spyOn(getYAxisUnitHooks, 'default');
+ const onSelect = jest.fn();
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ mockUseGetYAxisUnit.mockReturnValue({
+ yAxisUnit: UniversalYAxisUnit.BYTES,
+ isLoading: false,
+ isError: false,
+ });
+ });
+
+ it('should render the selector with correct label and value', () => {
+ render(
+ <DashboardYAxisUnitSelectorWrapper
+ value={UniversalYAxisUnit.BYTES}
+ onSelect={onSelect}
+ fieldLabel="Bytes label"
+ shouldUpdateYAxisUnit={false}
+ />,
+ );
+
+ expect(screen.getByText('Bytes label')).toBeInTheDocument();
+ expect(screen.getByText('Bytes (B)')).toBeInTheDocument();
+ });
+
+ it('should call onSelect when showWarning is true and useGetYAxisUnit is called and provides the correct value', () => {
+ mockUseGetYAxisUnit.mockReturnValueOnce({
+ yAxisUnit: UniversalYAxisUnit.SECONDS,
+ isLoading: false,
+ isError: false,
+ });
+ render(
+ <DashboardYAxisUnitSelectorWrapper
+ value={UniversalYAxisUnit.BYTES}
+ onSelect={onSelect}
+ fieldLabel="Bytes label"
+ shouldUpdateYAxisUnit
+ />,
+ );
+ expect(onSelect).toHaveBeenCalledWith(UniversalYAxisUnit.SECONDS);
+ });
+
+ it('should not call onSelect when showWarning is false', () => {
+ render(
+ <DashboardYAxisUnitSelectorWrapper
+ value={UniversalYAxisUnit.BYTES}
+ onSelect={onSelect}
+ fieldLabel="Bytes label"
+ shouldUpdateYAxisUnit={false}
+ />,
+ );
+ expect(onSelect).not.toHaveBeenCalled();
+ });
+
+ it('should call onSelect when yAxisUnit is undefined even if showWarning is true', () => {
+ mockUseGetYAxisUnit.mockReturnValueOnce({
+ yAxisUnit: undefined,
+ isLoading: false,
+ isError: false,
+ });
+ render(
+ <DashboardYAxisUnitSelectorWrapper
+ value={UniversalYAxisUnit.BYTES}
+ onSelect={onSelect}
+ fieldLabel="Bytes label"
+ shouldUpdateYAxisUnit
+ />,
+ );
+ expect(onSelect).toHaveBeenCalledWith('');
+ });
+});
diff --git a/frontend/src/container/NewWidget/RightContainer/__tests__/RightContainer.test.tsx b/frontend/src/container/NewWidget/RightContainer/__tests__/RightContainer.test.tsx
index 3d3fcc2d6a..629f896e1b 100644
--- a/frontend/src/container/NewWidget/RightContainer/__tests__/RightContainer.test.tsx
+++ b/frontend/src/container/NewWidget/RightContainer/__tests__/RightContainer.test.tsx
@@ -168,6 +168,7 @@ describe('RightContainer - Alerts Section', () => {
contextLinks: { linksData: [] },
setContextLinks: jest.fn(),
enableDrillDown: false,
+ isNewDashboard: false,
};
beforeEach(() => {
diff --git a/frontend/src/container/NewWidget/RightContainer/index.tsx b/frontend/src/container/NewWidget/RightContainer/index.tsx
index ea0e2a8027..b0e5a4aae5 100644
--- a/frontend/src/container/NewWidget/RightContainer/index.tsx
+++ b/frontend/src/container/NewWidget/RightContainer/index.tsx
@@ -67,11 +67,11 @@ import {
panelTypeVsYAxisUnit,
} from './constants';
import ContextLinks from './ContextLinks';
+import DashboardYAxisUnitSelectorWrapper from './DashboardYAxisUnitSelectorWrapper';
import LegendColors from './LegendColors/LegendColors';
import ThresholdSelector from './Threshold/ThresholdSelector';
import { ThresholdProps } from './Threshold/types';
import { timePreferance } from './timeItems';
-import YAxisUnitSelector from './YAxisUnitSelector';
const { TextArea } = Input;
const { Option } = Select;
@@ -129,6 +129,7 @@ function RightContainer({
contextLinks,
setContextLinks,
enableDrillDown = false,
+ isNewDashboard,
}: RightContainerProps): JSX.Element {
const { selectedDashboard } = useDashboard();
const [inputValue, setInputValue] = useState(title);
@@ -348,11 +349,12 @@ function RightContainer({
<ColumnUnitSelector
columnUnits={columnUnits}
setColumnUnits={setColumnUnits}
+ isNewDashboard={isNewDashboard}
/>
)}
{allowYAxisUnit && (
- <YAxisUnitSelector
+ <DashboardYAxisUnitSelectorWrapper
onSelect={setYAxisUnit}
value={yAxisUnit || ''}
fieldLabel={
@@ -361,6 +363,8 @@ function RightContainer({
? 'Unit'
: 'Y Axis Unit'
}
+ // Only update the y-axis unit value automatically in create mode
+ shouldUpdateYAxisUnit={isNewDashboard}
/>
)}
@@ -612,6 +616,7 @@ export interface RightContainerProps {
contextLinks: ContextLinksData;
setContextLinks: Dispatch<SetStateAction<ContextLinksData>>;
enableDrillDown?: boolean;
+ isNewDashboard: boolean;
}
RightContainer.defaultProps = {
diff --git a/frontend/src/container/NewWidget/index.tsx b/frontend/src/container/NewWidget/index.tsx
index fe519fd31c..3a6ea69da2 100644
--- a/frontend/src/container/NewWidget/index.tsx
+++ b/frontend/src/container/NewWidget/index.tsx
@@ -860,6 +860,7 @@ function NewWidget({
contextLinks={contextLinks}
setContextLinks={setContextLinks}
enableDrillDown={enableDrillDown}
+ isNewDashboard={isNewDashboard}
/>
</OverlayScrollbar>
</RightContainerWrapper>
diff --git a/frontend/src/hooks/useGetYAxisUnit.ts b/frontend/src/hooks/useGetYAxisUnit.ts
index 44c863dfe1..bc498dc100 100644
--- a/frontend/src/hooks/useGetYAxisUnit.ts
+++ b/frontend/src/hooks/useGetYAxisUnit.ts
@@ -3,6 +3,8 @@ import {
useGetMetrics,
} from 'container/MetricsExplorer/Explorer/utils';
import { useEffect, useMemo, useState } from 'react';
+import { IBuilderQuery } from 'types/api/queryBuilder/queryBuilderData';
+import { MetricAggregation } from 'types/api/v5/queryRange';
import { EQueryType } from 'types/common/dashboard';
import { DataSource } from 'types/common/queryBuilder';
@@ -14,6 +16,19 @@ interface UseGetYAxisUnitResult {
isError: boolean;
}
+function getMetricNameFromQueryData(queryData: IBuilderQuery): string | null {
+ if (queryData.dataSource !== DataSource.METRICS) {
+ return null;
+ }
+ if (queryData.aggregateAttribute?.key) {
+ return queryData.aggregateAttribute?.key;
+ }
+ if (queryData.aggregations?.length && queryData.aggregations.length > 0) {
+ return (queryData.aggregations?.[0] as MetricAggregation)?.metricName;
+ }
+ return null;
+}
+
/**
* Hook to get the y-axis unit for a given metrics-based query.
* @param selectedQueryName - The name of the query to get the y-axis unit for.
@@ -45,19 +60,18 @@ function useGetYAxisUnit(
// If a selected query name is provided, return the metric name for that query only
if (selectedQueryName) {
stagedQuery?.builder?.queryData?.forEach((query) => {
- if (
- query.queryName === selectedQueryName &&
- query.aggregateAttribute?.key
- ) {
- currentMetricNames.push(query.aggregateAttribute?.key);
+ const metricName = getMetricNameFromQueryData(query);
+ if (query.queryName === selectedQueryName && metricName) {
+ currentMetricNames.push(metricName);
}
});
return currentMetricNames.length ? currentMetricNames : null;
}
// Else, return all metric names
stagedQuery?.builder?.queryData?.forEach((query) => {
- if (query.aggregateAttribute?.key) {
- currentMetricNames.push(query.aggregateAttribute?.key);
+ const metricName = getMetricNameFromQueryData(query);
+ if (metricName) {
+ currentMetricNames.push(metricName);
}
});
return currentMetricNames.length ? currentMetricNames : null;