1 import { DatePipe } from '@angular/common';
2 import { Component, NgZone, OnDestroy, OnInit } from '@angular/core';
4 import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
5 import { Observable } from 'rxjs';
6 import { map } from 'rxjs/operators';
8 import { CephServiceService } from '~/app/shared/api/ceph-service.service';
9 import { LogsService } from '~/app/shared/api/logs.service';
10 import { Icons } from '~/app/shared/enum/icons.enum';
14 templateUrl: './logs.component.html',
15 styleUrls: ['./logs.component.scss']
17 export class LogsComponent implements OnInit, OnDestroy {
20 audit_log: Array<any>;
24 lokiServiceStatus$: Observable<boolean>;
25 promtailServiceStatus$: Observable<boolean>;
28 priorities: Array<{ name: string; value: string }> = [
29 { name: 'Debug', value: '[DBG]' },
30 { name: 'Info', value: '[INF]' },
31 { name: 'Warning', value: '[WRN]' },
32 { name: 'Error', value: '[ERR]' },
33 { name: 'All', value: 'All' }
37 selectedDate: NgbDateStruct;
38 startTime = { hour: 0, minute: 0 };
39 endTime = { hour: 23, minute: 59 };
41 year: new Date().getFullYear(),
42 month: new Date().getMonth() + 1,
43 day: new Date().getDate()
47 private logsService: LogsService,
48 private cephService: CephServiceService,
49 private datePipe: DatePipe,
50 private ngZone: NgZone
55 this.ngZone.runOutsideAngular(() => {
56 this.getDaemonDetails();
57 this.interval = window.setInterval(() => {
58 this.ngZone.run(() => {
66 clearInterval(this.interval);
70 this.lokiServiceStatus$ = this.cephService.getDaemons('loki').pipe(
72 return data.length > 0 && data[0].status === 1;
75 this.promtailServiceStatus$ = this.cephService.getDaemons('promtail').pipe(
77 return data.length > 0 && data[0].status === 1;
83 this.logsService.getLogs().subscribe((data: any) => {
84 this.contentData = data;
85 this.clogText = this.logToText(this.contentData.clog);
86 this.auditLogText = this.logToText(this.contentData.audit_log);
91 abstractFilters(): any {
92 const priority = this.priority;
93 const key = this.search.toLowerCase();
94 let yearMonthDay: string;
95 if (this.selectedDate) {
96 const m = this.selectedDate.month;
97 const d = this.selectedDate.day;
99 const year = this.selectedDate.year;
100 const month = m <= 9 ? `0${m}` : `${m}`;
101 const day = d <= 9 ? `0${d}` : `${d}`;
102 yearMonthDay = `${year}-${month}-${day}`;
107 const sHour = this.startTime?.hour ?? 0;
108 const sMinutes = this.startTime?.minute ?? 0;
109 const sTime = sHour * 60 + sMinutes;
111 const eHour = this.endTime?.hour ?? 23;
112 const eMinutes = this.endTime?.minute ?? 59;
113 const eTime = eHour * 60 + eMinutes;
115 return { priority, key, yearMonthDay, sTime, eTime };
118 filterExecutor(logs: Array<any>, filters: any): Array<any> {
119 return logs.filter((line) => {
120 const localDate = this.datePipe.transform(line.stamp, 'mediumTime');
121 const hour = parseInt(localDate.split(':')[0], 10);
122 const minutes = parseInt(localDate.split(':')[1], 10);
123 let prio: string, y_m_d: string, timeSpan: number;
125 prio = filters.priority === 'All' ? line.priority : filters.priority;
126 y_m_d = filters.yearMonthDay ? filters.yearMonthDay : line.stamp;
127 timeSpan = hour * 60 + minutes;
129 line.priority === prio &&
130 line.message.toLowerCase().indexOf(filters.key) !== -1 &&
131 line.stamp.indexOf(y_m_d) !== -1 &&
132 timeSpan >= filters.sTime &&
133 timeSpan <= filters.eTime
139 const filters = this.abstractFilters();
140 this.clog = this.filterExecutor(this.contentData.clog, filters);
141 this.audit_log = this.filterExecutor(this.contentData.audit_log, filters);
149 this.selectedDate = null;
153 this.priority = 'All';
155 this.selectedDate = null;
156 this.startTime = { hour: 0, minute: 0 };
157 this.endTime = { hour: 23, minute: 59 };
163 logToText(log: object) {
165 for (const line of Object.keys(log)) {
168 this.datePipe.transform(log[line].stamp, 'medium') +