1 import { DatePipe } from '@angular/common';
2 import { Component, NgZone, OnDestroy, OnInit } from '@angular/core';
4 import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
6 import { LogsService } from '~/app/shared/api/logs.service';
7 import { Icons } from '~/app/shared/enum/icons.enum';
11 templateUrl: './logs.component.html',
12 styleUrls: ['./logs.component.scss']
14 export class LogsComponent implements OnInit, OnDestroy {
17 audit_log: Array<any>;
23 priorities: Array<{ name: string; value: string }> = [
24 { name: 'Debug', value: '[DBG]' },
25 { name: 'Info', value: '[INF]' },
26 { name: 'Warning', value: '[WRN]' },
27 { name: 'Error', value: '[ERR]' },
28 { name: 'All', value: 'All' }
32 selectedDate: NgbDateStruct;
33 startTime = { hour: 0, minute: 0 };
34 endTime = { hour: 23, minute: 59 };
36 year: new Date().getFullYear(),
37 month: new Date().getMonth() + 1,
38 day: new Date().getDate()
42 private logsService: LogsService,
43 private datePipe: DatePipe,
44 private ngZone: NgZone
49 this.ngZone.runOutsideAngular(() => {
50 this.interval = window.setInterval(() => {
51 this.ngZone.run(() => {
59 clearInterval(this.interval);
63 this.logsService.getLogs().subscribe((data: any) => {
64 this.contentData = data;
65 this.clogText = this.logToText(this.contentData.clog);
66 this.auditLogText = this.logToText(this.contentData.audit_log);
71 abstractFilters(): any {
72 const priority = this.priority;
73 const key = this.search.toLowerCase();
74 let yearMonthDay: string;
75 if (this.selectedDate) {
76 const m = this.selectedDate.month;
77 const d = this.selectedDate.day;
79 const year = this.selectedDate.year;
80 const month = m <= 9 ? `0${m}` : `${m}`;
81 const day = d <= 9 ? `0${d}` : `${d}`;
82 yearMonthDay = `${year}-${month}-${day}`;
87 const sHour = this.startTime?.hour ?? 0;
88 const sMinutes = this.startTime?.minute ?? 0;
89 const sTime = sHour * 60 + sMinutes;
91 const eHour = this.endTime?.hour ?? 23;
92 const eMinutes = this.endTime?.minute ?? 59;
93 const eTime = eHour * 60 + eMinutes;
95 return { priority, key, yearMonthDay, sTime, eTime };
98 filterExecutor(logs: Array<any>, filters: any): Array<any> {
99 return logs.filter((line) => {
100 const localDate = this.datePipe.transform(line.stamp, 'mediumTime');
101 const hour = parseInt(localDate.split(':')[0], 10);
102 const minutes = parseInt(localDate.split(':')[1], 10);
103 let prio: string, y_m_d: string, timeSpan: number;
105 prio = filters.priority === 'All' ? line.priority : filters.priority;
106 y_m_d = filters.yearMonthDay ? filters.yearMonthDay : line.stamp;
107 timeSpan = hour * 60 + minutes;
109 line.priority === prio &&
110 line.message.toLowerCase().indexOf(filters.key) !== -1 &&
111 line.stamp.indexOf(y_m_d) !== -1 &&
112 timeSpan >= filters.sTime &&
113 timeSpan <= filters.eTime
119 const filters = this.abstractFilters();
120 this.clog = this.filterExecutor(this.contentData.clog, filters);
121 this.audit_log = this.filterExecutor(this.contentData.audit_log, filters);
129 this.selectedDate = null;
133 this.priority = 'All';
135 this.selectedDate = null;
136 this.startTime = { hour: 0, minute: 0 };
137 this.endTime = { hour: 23, minute: 59 };
143 logToText(log: object) {
145 for (const line of Object.keys(log)) {
148 this.datePipe.transform(log[line].stamp, 'medium') +