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

View File

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

View File

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

View File

@ -78,6 +78,7 @@ export const alertsCategory = [
name: CategoryNames.Miscellaneous,
formats: [
{ 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({
onChange,
yAxisUnit,
}: IBuilderUnitsFilterProps): JSX.Element {
const { currentQuery, handleOnUnitsChange } = useQueryBuilder();
const selectedValue = currentQuery?.unit;
const selectedValue = yAxisUnit || currentQuery?.unit;
const allOptions = categoryToSupport.map((category) => ({
label: category,

View File

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

View File

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