util.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import { isEmpty } from 'lodash';
  2. import { PanelProps } from '@grafana/data';
  3. import { Alert, hasAlertState } from 'app/types/unified-alerting';
  4. import { GrafanaAlertState, PromAlertingRuleState } from 'app/types/unified-alerting-dto';
  5. import { UnifiedAlertListOptions } from './types';
  6. export function filterAlerts(options: PanelProps<UnifiedAlertListOptions>['options'], alerts: Alert[]): Alert[] {
  7. const { stateFilter } = options;
  8. if (isEmpty(stateFilter)) {
  9. return alerts;
  10. }
  11. return alerts.filter((alert) => {
  12. return (
  13. (stateFilter.firing &&
  14. (hasAlertState(alert, GrafanaAlertState.Alerting) || hasAlertState(alert, PromAlertingRuleState.Firing))) ||
  15. (stateFilter.pending &&
  16. (hasAlertState(alert, GrafanaAlertState.Pending) || hasAlertState(alert, PromAlertingRuleState.Pending))) ||
  17. (stateFilter.noData && hasAlertState(alert, GrafanaAlertState.NoData)) ||
  18. (stateFilter.normal && hasAlertState(alert, GrafanaAlertState.Normal)) ||
  19. (stateFilter.error && hasAlertState(alert, GrafanaAlertState.Error)) ||
  20. (stateFilter.inactive && hasAlertState(alert, PromAlertingRuleState.Inactive))
  21. );
  22. });
  23. }
  24. export function isPrivateLabel(label: string) {
  25. return !(label.startsWith('__') && label.endsWith('__'));
  26. }