1 import { HttpClient } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
4 import { Observable } from 'rxjs';
6 import { AlertmanagerSilence } from '../models/alertmanager-silence';
9 AlertmanagerNotification,
11 } from '../models/prometheus-alerts';
12 import { ApiModule } from './api.module';
13 import { SettingsService } from './settings.service';
18 export class PrometheusService {
19 private baseURL = 'api/prometheus';
20 private settingsKey = {
21 alertmanager: 'api/settings/alertmanager-api-host',
22 prometheus: 'api/settings/prometheus-api-host'
25 constructor(private http: HttpClient, private settingsService: SettingsService) {}
27 ifAlertmanagerConfigured(fn, elseFn?): void {
28 this.settingsService.ifSettingConfigured(this.settingsKey.alertmanager, fn, elseFn);
31 disableAlertmanagerConfig(): void {
32 this.settingsService.disableSetting(this.settingsKey.alertmanager);
35 ifPrometheusConfigured(fn, elseFn?): void {
36 this.settingsService.ifSettingConfigured(this.settingsKey.prometheus, fn, elseFn);
39 disablePrometheusConfig(): void {
40 this.settingsService.disableSetting(this.settingsKey.prometheus);
43 getAlerts(params = {}): Observable<AlertmanagerAlert[]> {
44 return this.http.get<AlertmanagerAlert[]>(this.baseURL, { params });
47 getSilences(params = {}): Observable<AlertmanagerSilence[]> {
48 return this.http.get<AlertmanagerSilence[]>(`${this.baseURL}/silences`, { params });
51 getRules(params = {}): Observable<PrometheusRule[]> {
52 return this.http.get<PrometheusRule[]>(`${this.baseURL}/rules`, { params });
55 setSilence(silence: AlertmanagerSilence) {
56 return this.http.post(`${this.baseURL}/silence`, silence, { observe: 'response' });
59 expireSilence(silenceId: string) {
60 return this.http.delete(`${this.baseURL}/silence/${silenceId}`, { observe: 'response' });
64 notification?: AlertmanagerNotification
65 ): Observable<AlertmanagerNotification[]> {
66 const url = `${this.baseURL}/notifications?from=${
67 notification && notification.id ? notification.id : 'last'
69 return this.http.get<AlertmanagerNotification[]>(url);