[Refactor]: added percent 0 - 100 in yaxis for alerts (#4173)

This commit is contained in:
Rajat Dabade 2023-12-11 18:34:24 +05:30 committed by GitHub
parent 9826ab04b3
commit 6170b2c5dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 7 deletions

View File

@ -32,6 +32,7 @@ export interface ChartPreviewProps {
alertDef?: AlertDef; alertDef?: AlertDef;
userQueryKey?: string; userQueryKey?: string;
allowSelectedIntervalForStepGen?: boolean; allowSelectedIntervalForStepGen?: boolean;
yAxisUnit: string;
} }
function ChartPreview({ function ChartPreview({
@ -44,6 +45,7 @@ function ChartPreview({
userQueryKey, userQueryKey,
allowSelectedIntervalForStepGen = false, allowSelectedIntervalForStepGen = false,
alertDef, alertDef,
yAxisUnit,
}: ChartPreviewProps): JSX.Element | null { }: ChartPreviewProps): JSX.Element | null {
const { t } = useTranslation('alerts'); const { t } = useTranslation('alerts');
const threshold = alertDef?.condition.target || 0; const threshold = alertDef?.condition.target || 0;
@ -112,7 +114,7 @@ function ChartPreview({
() => () =>
getUPlotChartOptions({ getUPlotChartOptions({
id: 'alert_legend_widget', id: 'alert_legend_widget',
yAxisUnit: query?.unit, yAxisUnit,
apiResponse: queryResponse?.data?.payload, apiResponse: queryResponse?.data?.payload,
dimensions: containerDimensions, dimensions: containerDimensions,
isDarkMode, isDarkMode,
@ -129,14 +131,14 @@ function ChartPreview({
optionName, optionName,
threshold, threshold,
alertDef?.condition.targetUnit, alertDef?.condition.targetUnit,
query?.unit, yAxisUnit,
)})`, )})`,
thresholdUnit: alertDef?.condition.targetUnit, thresholdUnit: alertDef?.condition.targetUnit,
}, },
], ],
}), }),
[ [
query?.unit, yAxisUnit,
queryResponse?.data?.payload, queryResponse?.data?.payload,
containerDimensions, containerDimensions,
isDarkMode, isDarkMode,
@ -168,7 +170,7 @@ function ChartPreview({
name={name || 'Chart Preview'} name={name || 'Chart Preview'}
panelData={queryResponse.data?.payload.data.newResult.data.result || []} panelData={queryResponse.data?.payload.data.newResult.data.result || []}
query={query || initialQueriesMap.metrics} query={query || initialQueriesMap.metrics}
yAxisUnit={query?.unit} yAxisUnit={yAxisUnit}
/> />
</div> </div>
)} )}

View File

@ -61,8 +61,20 @@ export const getThresholdLabel = (
unit === MiscellaneousFormats.PercentUnit || unit === MiscellaneousFormats.PercentUnit ||
yAxisUnit === MiscellaneousFormats.PercentUnit yAxisUnit === MiscellaneousFormats.PercentUnit
) { ) {
if (unit === MiscellaneousFormats.Percent) {
return `${value}%`;
}
return `${value * 100}%`; return `${value * 100}%`;
} }
if (
unit === MiscellaneousFormats.Percent ||
yAxisUnit === MiscellaneousFormats.Percent
) {
if (unit === MiscellaneousFormats.PercentUnit) {
return `${value * 100}%`;
}
return `${value}%`;
}
return `${value} ${optionName}`; return `${value} ${optionName}`;
}; };

View File

@ -82,6 +82,7 @@ function FormAlertRules({
// alertDef holds the form values to be posted // alertDef holds the form values to be posted
const [alertDef, setAlertDef] = useState<AlertDef>(initialValue); const [alertDef, setAlertDef] = useState<AlertDef>(initialValue);
const [yAxisUnit, setYAxisUnit] = useState<string>(currentQuery.unit || '');
// initQuery contains initial query when component was mounted // initQuery contains initial query when component was mounted
const initQuery = useMemo(() => initialValue.condition.compositeQuery, [ const initQuery = useMemo(() => initialValue.condition.compositeQuery, [
@ -400,6 +401,7 @@ function FormAlertRules({
query={stagedQuery} query={stagedQuery}
selectedInterval={globalSelectedInterval} selectedInterval={globalSelectedInterval}
alertDef={alertDef} alertDef={alertDef}
yAxisUnit={yAxisUnit || ''}
/> />
); );
@ -415,6 +417,7 @@ function FormAlertRules({
query={stagedQuery} query={stagedQuery}
alertDef={alertDef} alertDef={alertDef}
selectedInterval={globalSelectedInterval} selectedInterval={globalSelectedInterval}
yAxisUnit={yAxisUnit || ''}
/> />
); );
@ -427,7 +430,8 @@ function FormAlertRules({
currentQuery.queryType === EQueryType.QUERY_BUILDER && currentQuery.queryType === EQueryType.QUERY_BUILDER &&
alertType !== AlertTypes.METRICS_BASED_ALERT; alertType !== AlertTypes.METRICS_BASED_ALERT;
const onUnitChangeHandler = (): void => { const onUnitChangeHandler = (value: string): void => {
setYAxisUnit(value);
// reset target unit // reset target unit
setAlertDef((def) => ({ setAlertDef((def) => ({
...def, ...def,
@ -457,7 +461,10 @@ function FormAlertRules({
renderPromAndChQueryChartPreview()} renderPromAndChQueryChartPreview()}
<StepContainer> <StepContainer>
<BuilderUnitsFilter onChange={onUnitChangeHandler} /> <BuilderUnitsFilter
onChange={onUnitChangeHandler}
yAxisUnit={yAxisUnit}
/>
</StepContainer> </StepContainer>
<QuerySection <QuerySection

View File

@ -78,6 +78,7 @@ export const alertsCategory = [
name: CategoryNames.Miscellaneous, name: CategoryNames.Miscellaneous,
formats: [ formats: [
{ name: 'Percent (0.0-1.0)', id: MiscellaneousFormats.PercentUnit }, { name: 'Percent (0.0-1.0)', id: MiscellaneousFormats.PercentUnit },
{ name: 'Percent (0 - 100)', id: MiscellaneousFormats.Percent },
], ],
}, },
{ {

View File

@ -10,10 +10,11 @@ import { filterOption } from './utils';
function BuilderUnitsFilter({ function BuilderUnitsFilter({
onChange, onChange,
yAxisUnit,
}: IBuilderUnitsFilterProps): JSX.Element { }: IBuilderUnitsFilterProps): JSX.Element {
const { currentQuery, handleOnUnitsChange } = useQueryBuilder(); const { currentQuery, handleOnUnitsChange } = useQueryBuilder();
const selectedValue = currentQuery?.unit; const selectedValue = yAxisUnit || currentQuery?.unit;
const allOptions = categoryToSupport.map((category) => ({ const allOptions = categoryToSupport.map((category) => ({
label: category, label: category,

View File

@ -1,3 +1,4 @@
export interface IBuilderUnitsFilterProps { export interface IBuilderUnitsFilterProps {
onChange?: (value: string) => void; onChange?: (value: string) => void;
yAxisUnit?: string;
} }

View File

@ -232,6 +232,11 @@ const unitsMapping = [
{ {
label: 'Percent (0.0-1.0)', label: 'Percent (0.0-1.0)',
value: 'percentunit', value: 'percentunit',
factor: 100,
},
{
label: 'Percent (0 - 100)',
value: 'percent',
factor: 1, factor: 1,
}, },
], ],