All files / src/app/shared/api prometheus.service.ts

100% Statements 36/36
72.22% Branches 13/18
100% Functions 12/12
100% Lines 30/30

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 7212x 12x                   12x 12x         12x 103x 103x         103x   12x 5x     12x 1x     12x 4x     12x 1x     12x 1x     12x 1x     12x 1x     12x 1x     12x 1x     12x     2x     2x   12x  
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
 
import { Observable } from 'rxjs';
 
import { AlertmanagerSilence } from '../models/alertmanager-silence';
import {
  AlertmanagerAlert,
  AlertmanagerNotification,
  PrometheusRule
} from '../models/prometheus-alerts';
import { ApiModule } from './api.module';
import { SettingsService } from './settings.service';
 
@Injectable({
  providedIn: ApiModule
})
export class PrometheusService {
  private baseURL = 'api/prometheus';
  private settingsKey = {
    alertmanager: 'api/settings/alertmanager-api-host',
    prometheus: 'api/settings/prometheus-api-host'
  };
 
  constructor(private http: HttpClient, private settingsService: SettingsService) {}
 
  ifAlertmanagerConfigured(fn, elseFn?): void {
    this.settingsService.ifSettingConfigured(this.settingsKey.alertmanager, fn, elseFn);
  }
 
  disableAlertmanagerConfig(): void {
    this.settingsService.disableSetting(this.settingsKey.alertmanager);
  }
 
  ifPrometheusConfigured(fn, elseFn?): void {
    this.settingsService.ifSettingConfigured(this.settingsKey.prometheus, fn, elseFn);
  }
 
  disablePrometheusConfig(): void {
    this.settingsService.disableSetting(this.settingsKey.prometheus);
  }
 
  getAlerts(Eparams = {}): Observable<AlertmanagerAlert[]> {
    return this.http.get<AlertmanagerAlert[]>(this.baseURL, { params });
  }
 
  getSilences(Eparams = {}): Observable<AlertmanagerSilence[]> {
    return this.http.get<AlertmanagerSilence[]>(`${this.baseURL}/silences`, { params });
  }
 
  getRules(Iparams = {}): Observable<PrometheusRule[]> {
    return this.http.get<PrometheusRule[]>(`${this.baseURL}/rules`, { params });
  }
 
  setSilence(silence: AlertmanagerSilence) {
    return this.http.post(`${this.baseURL}/silence`, silence, { observe: 'response' });
  }
 
  expireSilence(silenceId: string) {
    return this.http.delete(`${this.baseURL}/silence/${silenceId}`, { observe: 'response' });
  }
 
  getNotifications(
    notification?: AlertmanagerNotification
  ): Observable<AlertmanagerNotification[]> {
    const url = `${this.baseURL}/notifications?from=${
      notification && notification.id ? notification.id : 'last'
    }`;
    return this.http.get<AlertmanagerNotification[]>(url);
  }
}