]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core' | |
2 | import { LocalStorageService, Notifier } from '@app/core' | |
3 | import { LogLevel } from '@shared/models' | |
4 | import { LogRow } from './log-row.model' | |
5 | import { LogsService } from './logs.service' | |
6 | ||
7 | @Component({ | |
8 | templateUrl: './logs.component.html', | |
9 | styleUrls: [ './logs.component.scss' ] | |
10 | }) | |
11 | export class LogsComponent implements OnInit { | |
12 | private static LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY = 'admin-logs-log-type-choice' | |
13 | ||
14 | @ViewChild('logsElement', { static: true }) logsElement: ElementRef<HTMLElement> | |
15 | ||
16 | loading = false | |
17 | ||
18 | logs: LogRow[] = [] | |
19 | timeChoices: { id: string, label: string, dateFormat: string }[] = [] | |
20 | levelChoices: { id: LogLevel, label: string }[] = [] | |
21 | logTypeChoices: { id: 'audit' | 'standard', label: string }[] = [] | |
22 | ||
23 | startDate: string | |
24 | level: LogLevel | |
25 | logType: 'audit' | 'standard' | |
26 | ||
27 | constructor ( | |
28 | private logsService: LogsService, | |
29 | private notifier: Notifier, | |
30 | private localStorage: LocalStorageService | |
31 | ) { } | |
32 | ||
33 | ngOnInit (): void { | |
34 | this.buildTimeChoices() | |
35 | this.buildLevelChoices() | |
36 | this.buildLogTypeChoices() | |
37 | ||
38 | this.loadPreviousChoices() | |
39 | ||
40 | this.load() | |
41 | } | |
42 | ||
43 | refresh () { | |
44 | this.logs = [] | |
45 | ||
46 | this.localStorage.setItem(LogsComponent.LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY, this.logType) | |
47 | ||
48 | this.load() | |
49 | } | |
50 | ||
51 | load () { | |
52 | this.loading = true | |
53 | ||
54 | this.logsService.getLogs({ isAuditLog: this.isAuditLog(), level: this.level, startDate: this.startDate }) | |
55 | .subscribe( | |
56 | logs => { | |
57 | this.logs = logs | |
58 | ||
59 | setTimeout(() => { | |
60 | this.logsElement.nativeElement.scrollIntoView({ block: 'end', inline: 'nearest' }) | |
61 | }) | |
62 | }, | |
63 | ||
64 | err => this.notifier.error(err.message), | |
65 | ||
66 | () => this.loading = false | |
67 | ) | |
68 | } | |
69 | ||
70 | isAuditLog () { | |
71 | return this.logType === 'audit' | |
72 | } | |
73 | ||
74 | buildTimeChoices () { | |
75 | const lastHour = new Date() | |
76 | lastHour.setHours(lastHour.getHours() - 1) | |
77 | ||
78 | const lastDay = new Date() | |
79 | lastDay.setDate(lastDay.getDate() - 1) | |
80 | ||
81 | const lastWeek = new Date() | |
82 | lastWeek.setDate(lastWeek.getDate() - 7) | |
83 | ||
84 | this.timeChoices = [ | |
85 | { | |
86 | id: lastWeek.toISOString(), | |
87 | label: $localize`Last week`, | |
88 | dateFormat: 'shortDate' | |
89 | }, | |
90 | { | |
91 | id: lastDay.toISOString(), | |
92 | label: $localize`Last day`, | |
93 | dateFormat: 'short' | |
94 | }, | |
95 | { | |
96 | id: lastHour.toISOString(), | |
97 | label: $localize`Last hour`, | |
98 | dateFormat: 'mediumTime' | |
99 | } | |
100 | ] | |
101 | ||
102 | this.startDate = lastHour.toISOString() | |
103 | } | |
104 | ||
105 | buildLevelChoices () { | |
106 | this.levelChoices = [ | |
107 | { | |
108 | id: 'debug', | |
109 | label: $localize`debug` | |
110 | }, | |
111 | { | |
112 | id: 'info', | |
113 | label: $localize`info` | |
114 | }, | |
115 | { | |
116 | id: 'warn', | |
117 | label: $localize`warning` | |
118 | }, | |
119 | { | |
120 | id: 'error', | |
121 | label: $localize`error` | |
122 | } | |
123 | ] | |
124 | ||
125 | this.level = 'warn' | |
126 | } | |
127 | ||
128 | buildLogTypeChoices () { | |
129 | this.logTypeChoices = [ | |
130 | { | |
131 | id: 'standard', | |
132 | label: $localize`Standard logs` | |
133 | }, | |
134 | { | |
135 | id: 'audit', | |
136 | label: $localize`Audit logs` | |
137 | } | |
138 | ] | |
139 | } | |
140 | ||
141 | private loadPreviousChoices () { | |
142 | this.logType = this.localStorage.getItem(LogsComponent.LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY) | |
143 | ||
144 | if (this.logType !== 'standard' && this.logType !== 'audit') this.logType = 'audit' | |
145 | } | |
146 | } |