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