grafana/public/app/features/alerting/state/alertDef.ts

164 lines
4.3 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import _ from 'lodash';
import { QueryPartDef, QueryPart } from 'app/core/components/query_part/query_part';
2016-08-11 08:18:21 -05:00
2018-08-29 07:26:50 -05:00
const alertQueryDef = new QueryPartDef({
2017-12-20 05:33:33 -06:00
type: 'query',
2016-08-11 08:18:21 -05:00
params: [
2017-12-20 05:33:33 -06:00
{ name: 'queryRefId', type: 'string', dynamicLookup: true },
{
2017-12-20 05:33:33 -06:00
name: 'from',
type: 'string',
options: ['10s', '1m', '5m', '10m', '15m', '1h', '24h', '48h'],
},
{ name: 'to', type: 'string', options: ['now', 'now-1m', 'now-5m', 'now-10m', 'now-1h'] },
2016-08-11 08:18:21 -05:00
],
2017-12-20 05:33:33 -06:00
defaultParams: ['#A', '15m', 'now', 'avg'],
2016-08-11 08:18:21 -05:00
});
2018-08-29 07:26:50 -05:00
const conditionTypes = [{ text: 'Query', value: 'query' }];
2016-08-11 08:18:21 -05:00
2018-08-29 07:26:50 -05:00
const alertStateSortScore = {
alerting: 1,
no_data: 2,
pending: 3,
ok: 4,
2017-12-20 05:33:33 -06:00
paused: 5,
};
2018-08-29 07:26:50 -05:00
const evalFunctions = [
2017-12-20 05:33:33 -06:00
{ text: 'IS ABOVE', value: 'gt' },
{ text: 'IS BELOW', value: 'lt' },
{ text: 'IS OUTSIDE RANGE', value: 'outside_range' },
{ text: 'IS WITHIN RANGE', value: 'within_range' },
{ text: 'HAS NO VALUE', value: 'no_value' },
2016-08-11 08:18:21 -05:00
];
2018-08-29 07:26:50 -05:00
const evalOperators = [{ text: 'OR', value: 'or' }, { text: 'AND', value: 'and' }];
2016-11-12 15:16:46 -06:00
2018-08-29 07:26:50 -05:00
const reducerTypes = [
2017-12-20 05:33:33 -06:00
{ text: 'avg()', value: 'avg' },
{ text: 'min()', value: 'min' },
{ text: 'max()', value: 'max' },
{ text: 'sum()', value: 'sum' },
{ text: 'count()', value: 'count' },
{ text: 'last()', value: 'last' },
{ text: 'median()', value: 'median' },
{ text: 'diff()', value: 'diff' },
{ text: 'percent_diff()', value: 'percent_diff' },
{ text: 'count_non_null()', value: 'count_non_null' },
];
2018-08-29 07:26:50 -05:00
const noDataModes = [
2017-12-20 05:33:33 -06:00
{ text: 'Alerting', value: 'alerting' },
{ text: 'No Data', value: 'no_data' },
{ text: 'Keep Last State', value: 'keep_state' },
{ text: 'Ok', value: 'ok' },
];
2018-08-29 07:26:50 -05:00
const executionErrorModes = [{ text: 'Alerting', value: 'alerting' }, { text: 'Keep Last State', value: 'keep_state' }];
2019-05-13 02:38:19 -05:00
function createReducerPart(model: any) {
2018-08-29 07:26:50 -05:00
const def = new QueryPartDef({ type: model.type, defaultParams: [] });
return new QueryPart(model, def);
}
2019-05-13 02:38:19 -05:00
function getStateDisplayModel(state: string) {
switch (state) {
2017-12-20 05:33:33 -06:00
case 'ok': {
return {
2017-12-20 05:33:33 -06:00
text: 'OK',
iconClass: 'icon-gf icon-gf-online',
stateClass: 'alert-state-ok',
};
}
2017-12-20 05:33:33 -06:00
case 'alerting': {
return {
2017-12-20 05:33:33 -06:00
text: 'ALERTING',
iconClass: 'icon-gf icon-gf-critical',
stateClass: 'alert-state-critical',
};
}
2017-12-20 05:33:33 -06:00
case 'no_data': {
return {
2017-12-20 05:33:33 -06:00
text: 'NO DATA',
iconClass: 'fa fa-question',
stateClass: 'alert-state-warning',
};
}
2017-12-20 05:33:33 -06:00
case 'paused': {
return {
2017-12-20 05:33:33 -06:00
text: 'PAUSED',
iconClass: 'fa fa-pause',
stateClass: 'alert-state-paused',
};
}
2017-12-20 05:33:33 -06:00
case 'pending': {
2016-11-01 09:58:38 -05:00
return {
2017-12-20 05:33:33 -06:00
text: 'PENDING',
iconClass: 'fa fa-exclamation',
stateClass: 'alert-state-warning',
2016-11-01 09:58:38 -05:00
};
}
case 'unknown': {
return {
text: 'UNKNOWN',
iconClass: 'fa fa-question',
stateClass: 'alert-state-paused',
};
}
}
2017-09-26 04:24:58 -05:00
2017-12-20 05:33:33 -06:00
throw { message: 'Unknown alert state' };
}
2016-08-11 08:18:21 -05:00
2019-05-13 02:38:19 -05:00
function joinEvalMatches(matches: any, separator: string) {
return _.reduce(
matches,
(res, ev) => {
if (ev.metric !== undefined && ev.value !== undefined) {
2017-12-20 05:33:33 -06:00
res.push(ev.metric + '=' + ev.value);
}
// For backwards compatibility . Should be be able to remove this after ~2017-06-01
if (ev.Metric !== undefined && ev.Value !== undefined) {
2017-12-20 05:33:33 -06:00
res.push(ev.Metric + '=' + ev.Value);
}
return res;
},
[]
).join(separator);
}
2019-05-13 02:38:19 -05:00
function getAlertAnnotationInfo(ah: any) {
2018-04-13 12:48:37 -05:00
// backward compatibility, can be removed in grafana 5.x
// old way stored evalMatches in data property directly,
// new way stores it in evalMatches property on new data object
if (_.isArray(ah.data)) {
2017-12-20 05:33:33 -06:00
return joinEvalMatches(ah.data, ', ');
} else if (_.isArray(ah.data.evalMatches)) {
2017-12-20 05:33:33 -06:00
return joinEvalMatches(ah.data.evalMatches, ', ');
}
if (ah.data.error) {
2017-12-20 05:33:33 -06:00
return 'Error: ' + ah.data.error;
}
2017-12-20 05:33:33 -06:00
return '';
}
export default {
2016-08-11 08:18:21 -05:00
alertQueryDef: alertQueryDef,
getStateDisplayModel: getStateDisplayModel,
2016-08-11 08:18:21 -05:00
conditionTypes: conditionTypes,
evalFunctions: evalFunctions,
2016-11-12 15:16:46 -06:00
evalOperators: evalOperators,
noDataModes: noDataModes,
executionErrorModes: executionErrorModes,
reducerTypes: reducerTypes,
createReducerPart: createReducerPart,
getAlertAnnotationInfo: getAlertAnnotationInfo,
2017-12-20 05:33:33 -06:00
alertStateSortScore: alertStateSortScore,
};