]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | import { DatePipe } from '@angular/common'; |
2 | import { Component, NgZone, OnDestroy, OnInit } from '@angular/core'; | |
11fdf7f2 | 3 | |
f67539c2 TL |
4 | import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap'; |
5 | ||
6 | import { LogsService } from '~/app/shared/api/logs.service'; | |
7 | import { Icons } from '~/app/shared/enum/icons.enum'; | |
11fdf7f2 TL |
8 | |
9 | @Component({ | |
10 | selector: 'cd-logs', | |
11 | templateUrl: './logs.component.html', | |
12 | styleUrls: ['./logs.component.scss'] | |
13 | }) | |
14 | export class LogsComponent implements OnInit, OnDestroy { | |
15 | contentData: any; | |
16 | clog: Array<any>; | |
17 | audit_log: Array<any>; | |
9f95a23c | 18 | icons = Icons; |
f67539c2 TL |
19 | clogText: string; |
20 | auditLogText: string; | |
11fdf7f2 TL |
21 | |
22 | interval: number; | |
f67539c2 TL |
23 | priorities: Array<{ name: string; value: string }> = [ |
24 | { name: 'Debug', value: '[DBG]' }, | |
11fdf7f2 TL |
25 | { name: 'Info', value: '[INF]' }, |
26 | { name: 'Warning', value: '[WRN]' }, | |
27 | { name: 'Error', value: '[ERR]' }, | |
28 | { name: 'All', value: 'All' } | |
29 | ]; | |
30 | priority = 'All'; | |
31 | search = ''; | |
f67539c2 TL |
32 | selectedDate: NgbDateStruct; |
33 | startTime = { hour: 0, minute: 0 }; | |
34 | endTime = { hour: 23, minute: 59 }; | |
35 | maxDate = { | |
36 | year: new Date().getFullYear(), | |
37 | month: new Date().getMonth() + 1, | |
38 | day: new Date().getDate() | |
39 | }; | |
40 | ||
9f95a23c TL |
41 | constructor( |
42 | private logsService: LogsService, | |
43 | private datePipe: DatePipe, | |
44 | private ngZone: NgZone | |
f67539c2 | 45 | ) {} |
11fdf7f2 TL |
46 | |
47 | ngOnInit() { | |
48 | this.getInfo(); | |
9f95a23c TL |
49 | this.ngZone.runOutsideAngular(() => { |
50 | this.interval = window.setInterval(() => { | |
51 | this.ngZone.run(() => { | |
52 | this.getInfo(); | |
53 | }); | |
54 | }, 5000); | |
55 | }); | |
11fdf7f2 TL |
56 | } |
57 | ||
58 | ngOnDestroy() { | |
59 | clearInterval(this.interval); | |
60 | } | |
61 | ||
62 | getInfo() { | |
63 | this.logsService.getLogs().subscribe((data: any) => { | |
64 | this.contentData = data; | |
f67539c2 TL |
65 | this.clogText = this.logToText(this.contentData.clog); |
66 | this.auditLogText = this.logToText(this.contentData.audit_log); | |
11fdf7f2 TL |
67 | this.filterLogs(); |
68 | }); | |
69 | } | |
70 | ||
f67539c2 | 71 | abstractFilters(): any { |
11fdf7f2 | 72 | const priority = this.priority; |
33c7a0ef | 73 | const key = this.search.toLowerCase(); |
11fdf7f2 TL |
74 | let yearMonthDay: string; |
75 | if (this.selectedDate) { | |
f67539c2 TL |
76 | const m = this.selectedDate.month; |
77 | const d = this.selectedDate.day; | |
11fdf7f2 | 78 | |
f67539c2 | 79 | const year = this.selectedDate.year; |
11fdf7f2 TL |
80 | const month = m <= 9 ? `0${m}` : `${m}`; |
81 | const day = d <= 9 ? `0${d}` : `${d}`; | |
82 | yearMonthDay = `${year}-${month}-${day}`; | |
83 | } else { | |
84 | yearMonthDay = ''; | |
85 | } | |
86 | ||
f67539c2 TL |
87 | const sHour = this.startTime?.hour ?? 0; |
88 | const sMinutes = this.startTime?.minute ?? 0; | |
11fdf7f2 TL |
89 | const sTime = sHour * 60 + sMinutes; |
90 | ||
f67539c2 TL |
91 | const eHour = this.endTime?.hour ?? 23; |
92 | const eMinutes = this.endTime?.minute ?? 59; | |
11fdf7f2 TL |
93 | const eTime = eHour * 60 + eMinutes; |
94 | ||
95 | return { priority, key, yearMonthDay, sTime, eTime }; | |
96 | } | |
97 | ||
98 | filterExecutor(logs: Array<any>, filters: any): Array<any> { | |
99 | return logs.filter((line) => { | |
9f95a23c TL |
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); | |
11fdf7f2 TL |
103 | let prio: string, y_m_d: string, timeSpan: number; |
104 | ||
105 | prio = filters.priority === 'All' ? line.priority : filters.priority; | |
106 | y_m_d = filters.yearMonthDay ? filters.yearMonthDay : line.stamp; | |
107 | timeSpan = hour * 60 + minutes; | |
108 | return ( | |
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 | |
114 | ); | |
115 | }); | |
116 | } | |
117 | ||
118 | filterLogs() { | |
f67539c2 | 119 | const filters = this.abstractFilters(); |
11fdf7f2 TL |
120 | this.clog = this.filterExecutor(this.contentData.clog, filters); |
121 | this.audit_log = this.filterExecutor(this.contentData.audit_log, filters); | |
122 | } | |
123 | ||
124 | clearSearchKey() { | |
125 | this.search = ''; | |
126 | this.filterLogs(); | |
127 | } | |
128 | clearDate() { | |
129 | this.selectedDate = null; | |
130 | this.filterLogs(); | |
131 | } | |
f67539c2 TL |
132 | resetFilter() { |
133 | this.priority = 'All'; | |
134 | this.search = ''; | |
135 | this.selectedDate = null; | |
136 | this.startTime = { hour: 0, minute: 0 }; | |
137 | this.endTime = { hour: 23, minute: 59 }; | |
138 | this.filterLogs(); | |
139 | ||
140 | return false; | |
141 | } | |
142 | ||
143 | logToText(log: object) { | |
144 | let logText = ''; | |
145 | for (const line of Object.keys(log)) { | |
146 | logText = | |
147 | logText + | |
148 | this.datePipe.transform(log[line].stamp, 'medium') + | |
149 | '\t' + | |
150 | log[line].priority + | |
151 | '\t' + | |
152 | log[line].message + | |
153 | '\n'; | |
154 | } | |
155 | return logText; | |
156 | } | |
11fdf7f2 | 157 | } |