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