]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
07cf4614e573b221b677f0aab1ea8396790c648f
[ceph.git] /
1 import { ChangeDetectionStrategy, Component, OnInit, inject } from '@angular/core';
2 import { CommonModule } from '@angular/common';
3 import { combineLatest } from 'rxjs';
4
5 import { PrometheusAlertService } from '~/app/shared/services/prometheus-alert.service';
6 import { ButtonModule, GridModule, LinkModule, TilesModule } from 'carbon-components-angular';
7 import { RouterModule } from '@angular/router';
8 import { ProductiveCardComponent } from '~/app/shared/components/productive-card/productive-card.component';
9 import { ComponentsModule } from '~/app/shared/components/components.module';
10 import { map, shareReplay, startWith } from 'rxjs/operators';
11
12 const AlertIcon = {
13   error: 'error',
14   warning: 'warning',
15   success: 'success'
16 };
17
18 @Component({
19   selector: 'cd-overview-alerts-card',
20   standalone: true,
21   imports: [
22     CommonModule,
23     GridModule,
24     TilesModule,
25     ComponentsModule,
26     RouterModule,
27     ProductiveCardComponent,
28     ButtonModule,
29     LinkModule
30   ],
31   templateUrl: './overview-alerts-card.component.html',
32   styleUrl: './overview-alerts-card.component.scss',
33   changeDetection: ChangeDetectionStrategy.OnPush
34 })
35 export class OverviewAlertsCardComponent implements OnInit {
36   private readonly prometheusAlertService = inject(PrometheusAlertService);
37
38   ngOnInit(): void {
39     this.prometheusAlertService.getGroupedAlerts(true);
40   }
41
42   readonly vm$ = combineLatest([
43     this.prometheusAlertService.totalAlerts$.pipe(startWith(0)),
44     this.prometheusAlertService.criticalAlerts$.pipe(startWith(0)),
45     this.prometheusAlertService.warningAlerts$.pipe(startWith(0))
46   ]).pipe(
47     map(([total, critical, warning]) => {
48       const hasAlerts = total > 0;
49       const hasCritical = critical > 0;
50       const hasWarning = warning > 0;
51
52       const icon = !hasAlerts
53         ? AlertIcon.success
54         : hasCritical
55         ? AlertIcon.error
56         : AlertIcon.warning;
57
58       const statusText = hasAlerts ? $localize`Need attention` : $localize`No active alerts`;
59
60       const badges = [
61         hasCritical && { key: 'critical', icon: AlertIcon.error, count: critical },
62         hasWarning && { key: 'warning', icon: AlertIcon.warning, count: warning }
63       ].filter(Boolean);
64
65       return { total, icon, statusText, badges };
66     }),
67     shareReplay({ bufferSize: 1, refCount: true })
68   );
69 }