2022-04-22 14:33:13 +01:00
|
|
|
import { isEmpty } from 'lodash';
|
|
|
|
|
|
2023-04-03 15:39:06 +02:00
|
|
|
import { Labels, PanelProps } from '@grafana/data';
|
|
|
|
|
import { labelsMatchMatchers, parseMatchers } from 'app/features/alerting/unified/utils/alertmanager';
|
|
|
|
|
import { replaceVariables } from 'app/plugins/datasource/prometheus/querybuilder/shared/parsingUtils';
|
2022-05-23 16:49:49 +08:00
|
|
|
import { Alert, hasAlertState } from 'app/types/unified-alerting';
|
2022-02-03 19:07:27 +01:00
|
|
|
import { GrafanaAlertState, PromAlertingRuleState } from 'app/types/unified-alerting-dto';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2022-02-03 19:07:27 +01:00
|
|
|
import { UnifiedAlertListOptions } from './types';
|
|
|
|
|
|
2023-04-03 15:39:06 +02:00
|
|
|
function hasLabelFilter(alertInstanceLabelFilter: string, labels: Labels) {
|
|
|
|
|
const replacedLabelFilter = replaceVariables(alertInstanceLabelFilter);
|
|
|
|
|
const matchers = parseMatchers(replacedLabelFilter);
|
|
|
|
|
return labelsMatchMatchers(labels, matchers);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 19:07:27 +01:00
|
|
|
export function filterAlerts(options: PanelProps<UnifiedAlertListOptions>['options'], alerts: Alert[]): Alert[] {
|
2023-04-03 15:39:06 +02:00
|
|
|
const { stateFilter, alertInstanceLabelFilter } = options;
|
2022-02-03 19:07:27 +01:00
|
|
|
|
|
|
|
|
if (isEmpty(stateFilter)) {
|
|
|
|
|
return alerts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return alerts.filter((alert) => {
|
|
|
|
|
return (
|
2023-04-03 15:39:06 +02:00
|
|
|
((stateFilter.firing &&
|
2022-05-23 16:49:49 +08:00
|
|
|
(hasAlertState(alert, GrafanaAlertState.Alerting) || hasAlertState(alert, PromAlertingRuleState.Firing))) ||
|
2023-04-03 15:39:06 +02:00
|
|
|
(stateFilter.pending &&
|
|
|
|
|
(hasAlertState(alert, GrafanaAlertState.Pending) || hasAlertState(alert, PromAlertingRuleState.Pending))) ||
|
|
|
|
|
(stateFilter.noData && hasAlertState(alert, GrafanaAlertState.NoData)) ||
|
|
|
|
|
(stateFilter.normal && hasAlertState(alert, GrafanaAlertState.Normal)) ||
|
|
|
|
|
(stateFilter.error && hasAlertState(alert, GrafanaAlertState.Error)) ||
|
|
|
|
|
(stateFilter.inactive && hasAlertState(alert, PromAlertingRuleState.Inactive))) &&
|
|
|
|
|
((alertInstanceLabelFilter && hasLabelFilter(options.alertInstanceLabelFilter, alert.labels)) ||
|
|
|
|
|
!alertInstanceLabelFilter)
|
2022-02-03 19:07:27 +01:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isPrivateLabel(label: string) {
|
|
|
|
|
return !(label.startsWith('__') && label.endsWith('__'));
|
|
|
|
|
}
|