Commit 98406d6c by Hugo Häggmark Committed by GitHub

Alerting: Hides threshold handle for percentual thresholds (#30431)

parent e1243e07
...@@ -390,6 +390,7 @@ export class AlertTabCtrl { ...@@ -390,6 +390,7 @@ export class AlertTabCtrl {
case 'action': { case 'action': {
conditionModel.source.reducer.type = evt.action.value; conditionModel.source.reducer.type = evt.action.value;
conditionModel.reducerPart = alertDef.createReducerPart(conditionModel.source.reducer); conditionModel.reducerPart = alertDef.createReducerPart(conditionModel.source.reducer);
this.evaluatorParamsChanged();
break; break;
} }
case 'get-part-actions': { case 'get-part-actions': {
......
import { describe, it, expect } from 'test/lib/common'; import { hiddenReducerTypes, ThresholdMapper } from './ThresholdMapper';
import alertDef from './alertDef';
import { ThresholdMapper } from './ThresholdMapper'; const visibleReducerTypes = alertDef.reducerTypes
.filter(({ value }) => hiddenReducerTypes.indexOf(value) === -1)
.map(({ value }) => value);
describe('ThresholdMapper', () => { describe('ThresholdMapper', () => {
describe('with greater than evaluator', () => { describe('with greater than evaluator', () => {
...@@ -74,4 +77,62 @@ describe('ThresholdMapper', () => { ...@@ -74,4 +77,62 @@ describe('ThresholdMapper', () => {
expect(panel.thresholds[1].value).toBe(200); expect(panel.thresholds[1].value).toBe(200);
}); });
}); });
visibleReducerTypes.forEach((type) => {
describe(`with {${type}} reducer`, () => {
it('visible should be true', () => {
const panel = getPanel({ reducerType: type });
const updated = ThresholdMapper.alertToGraphThresholds(panel);
expect(updated).toBe(true);
expect(panel.thresholds[0]).toEqual({
value: 100,
op: 'gt',
fill: true,
line: true,
colorMode: 'critical',
visible: true,
});
});
});
});
hiddenReducerTypes.forEach((type) => {
describe(`with {${type}} reducer`, () => {
it('visible should be false', () => {
const panel = getPanel({ reducerType: type });
const updated = ThresholdMapper.alertToGraphThresholds(panel);
expect(updated).toBe(true);
expect(panel.thresholds[0]).toEqual({
value: 100,
op: 'gt',
fill: true,
line: true,
colorMode: 'critical',
visible: false,
});
});
});
});
}); });
function getPanel({ reducerType }: { reducerType?: string } = {}) {
const panel: any = {
type: 'graph',
options: { alertThreshold: true },
alert: {
conditions: [
{
type: 'query',
evaluator: { type: 'gt', params: [100] },
reducer: { type: reducerType },
},
],
},
};
return panel;
}
import { PanelModel } from 'app/features/dashboard/state'; import { PanelModel } from 'app/features/dashboard/state';
export const hiddenReducerTypes = ['percent_diff', 'percent_diff_abs'];
export class ThresholdMapper { export class ThresholdMapper {
static alertToGraphThresholds(panel: PanelModel) { static alertToGraphThresholds(panel: PanelModel) {
if (!panel.alert) { if (!panel.alert) {
...@@ -14,16 +15,17 @@ export class ThresholdMapper { ...@@ -14,16 +15,17 @@ export class ThresholdMapper {
const evaluator = condition.evaluator; const evaluator = condition.evaluator;
const thresholds: any[] = (panel.thresholds = []); const thresholds: any[] = (panel.thresholds = []);
const visible = hiddenReducerTypes.indexOf(condition.reducer?.type) === -1;
switch (evaluator.type) { switch (evaluator.type) {
case 'gt': { case 'gt': {
const value = evaluator.params[0]; const value = evaluator.params[0];
thresholds.push({ value: value, op: 'gt' }); thresholds.push({ value: value, op: 'gt', visible });
break; break;
} }
case 'lt': { case 'lt': {
const value = evaluator.params[0]; const value = evaluator.params[0];
thresholds.push({ value: value, op: 'lt' }); thresholds.push({ value: value, op: 'lt', visible });
break; break;
} }
case 'outside_range': { case 'outside_range': {
...@@ -31,11 +33,11 @@ export class ThresholdMapper { ...@@ -31,11 +33,11 @@ export class ThresholdMapper {
const value2 = evaluator.params[1]; const value2 = evaluator.params[1];
if (value1 > value2) { if (value1 > value2) {
thresholds.push({ value: value1, op: 'gt' }); thresholds.push({ value: value1, op: 'gt', visible });
thresholds.push({ value: value2, op: 'lt' }); thresholds.push({ value: value2, op: 'lt', visible });
} else { } else {
thresholds.push({ value: value1, op: 'lt' }); thresholds.push({ value: value1, op: 'lt', visible });
thresholds.push({ value: value2, op: 'gt' }); thresholds.push({ value: value2, op: 'gt', visible });
} }
break; break;
...@@ -45,11 +47,11 @@ export class ThresholdMapper { ...@@ -45,11 +47,11 @@ export class ThresholdMapper {
const value2 = evaluator.params[1]; const value2 = evaluator.params[1];
if (value1 > value2) { if (value1 > value2) {
thresholds.push({ value: value1, op: 'lt' }); thresholds.push({ value: value1, op: 'lt', visible });
thresholds.push({ value: value2, op: 'gt' }); thresholds.push({ value: value2, op: 'gt', visible });
} else { } else {
thresholds.push({ value: value1, op: 'gt' }); thresholds.push({ value: value1, op: 'gt', visible });
thresholds.push({ value: value2, op: 'lt' }); thresholds.push({ value: value2, op: 'lt', visible });
} }
break; break;
} }
......
...@@ -87,6 +87,10 @@ export class ThresholdManager { ...@@ -87,6 +87,10 @@ export class ThresholdManager {
renderHandle(handleIndex: number, defaultHandleTopPos: number) { renderHandle(handleIndex: number, defaultHandleTopPos: number) {
const model = this.thresholds[handleIndex]; const model = this.thresholds[handleIndex];
if (!model.visible) {
return;
}
const value = model.value; const value = model.value;
let valueStr = value; let valueStr = value;
let handleTopPos = 0; let handleTopPos = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment