All files / src/app/shared/services prometheus-silence-matcher.service.ts

100% Statements 33/33
93.75% Branches 15/16
100% Functions 12/12
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 838x   8x   8x                   8x 63x             63x   8x       31x     71x       106x 9x   62x 96x   62x     8x       96x 172x     8x 62x 62x 62x           8x 126x     8x 62x               62x             8x  
import { Injectable } from '@angular/core';
 
import * as _ from 'lodash';
 
import { I18n } from '@ngx-translate/i18n-polyfill';
import {
  AlertmanagerSilenceMatcher,
  AlertmanagerSilenceMatcherMatch
} from '../models/alertmanager-silence';
import { PrometheusRule } from '../models/prometheus-alerts';
 
@Injectable({
  providedIn: 'root'
})
export class PrometheusSilenceMatcherService {
  private valueAttributePath = {
    alertname: 'name',
    instance: 'alerts.0.labels.instance',
    job: 'alerts.0.labels.job',
    severity: 'labels.severity'
  };
 
  constructor(private i18n: I18n) {}
 
  singleMatch(
    matcher: AlertmanagerSilenceMatcher,
    rules: PrometheusRule[]
  ): AlertmanagerSilenceMatcherMatch {
    return this.multiMatch([matcher], rules);
  }
 
  multiMatch(
    matchers: AlertmanagerSilenceMatcher[],
    rules: PrometheusRule[]
  ): AlertmanagerSilenceMatcherMatch {
    if (matchers.some((matcher) => matcher.isRegex)) {
      return;
    }
    matchers.forEach((matcher) => {
      rules = this.getMatchedRules(matcher, rules);
    });
    return this.describeMatch(rules);
  }
 
  private getMatchedRules(
    matcher: AlertmanagerSilenceMatcher,
    rules: PrometheusRule[]
  ): PrometheusRule[] {
    const attributePath = this.getAttributePath(matcher.name);
    return rules.filter((r) => _.get(r, attributePath) === matcher.value);
  }
 
  private describeMatch(rules: PrometheusRule[]): AlertmanagerSilenceMatcherMatch {
    let alerts = 0;
    rules.forEach((r) => (alerts += r.alerts.length));
    return {
      status: this.getMatchText(rules.length, alerts),
      cssClass: alerts ? 'has-success' : 'has-warning'
    };
  }
 
  getAttributePath(name: string): string {
    return this.valueAttributePath[name];
  }
 
  private getMatchText(rules: number, alerts: number): string {
    const msg = {
      noRule: this.i18n('Your matcher seems to match no currently defined rule or active alert.'),
      noAlerts: this.i18n('no active alerts'),
      alert: this.i18n('1 active alert'),
      alerts: this.i18n('{{n}} active alerts', { n: alerts }),
      rule: this.i18n('Matches 1 rule'),
      rules: this.i18n('Matches {{n}} rules', { n: rules })
    };
    return rules
      ? this.i18n('{{rules}} with {{alerts}}.', {
          rules: rules > 1 ? msg.rules : msg.rule,
          alerts: alerts ? (alerts > 1 ? msg.alerts : msg.alert) : msg.noAlerts
        })
      : msg.noRule;
  }
}