]>
Commit | Line | Data |
---|---|---|
2c22613c | 1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core' |
288bcd7a | 2 | import { LocalStorageService, Notifier } from '@app/core' |
67ed6552 C |
3 | import { LogLevel } from '@shared/models' |
4 | import { LogRow } from './log-row.model' | |
5 | import { LogsService } from './logs.service' | |
2c22613c C |
6 | |
7 | @Component({ | |
8 | templateUrl: './logs.component.html', | |
9 | styleUrls: [ './logs.component.scss' ] | |
10 | }) | |
11 | export class LogsComponent implements OnInit { | |
288bcd7a C |
12 | private static LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY = 'admin-logs-log-type-choice' |
13 | ||
f36da21e | 14 | @ViewChild('logsElement', { static: true }) logsElement: ElementRef<HTMLElement> |
2c22613c C |
15 | |
16 | loading = false | |
17 | ||
18 | logs: LogRow[] = [] | |
7f0d8561 | 19 | timeChoices: { id: string, label: string, dateFormat: string }[] = [] |
2c22613c | 20 | levelChoices: { id: LogLevel, label: string }[] = [] |
566c125d | 21 | logTypeChoices: { id: 'audit' | 'standard', label: string }[] = [] |
2c22613c C |
22 | |
23 | startDate: string | |
24 | level: LogLevel | |
566c125d | 25 | logType: 'audit' | 'standard' |
64553e88 | 26 | tagsOneOf: string[] = [] |
2c22613c C |
27 | |
28 | constructor ( | |
29 | private logsService: LogsService, | |
288bcd7a C |
30 | private notifier: Notifier, |
31 | private localStorage: LocalStorageService | |
1227fe07 | 32 | ) { } |
2c22613c C |
33 | |
34 | ngOnInit (): void { | |
35 | this.buildTimeChoices() | |
36 | this.buildLevelChoices() | |
566c125d | 37 | this.buildLogTypeChoices() |
2c22613c | 38 | |
288bcd7a C |
39 | this.loadPreviousChoices() |
40 | ||
2c22613c C |
41 | this.load() |
42 | } | |
43 | ||
44 | refresh () { | |
45 | this.logs = [] | |
288bcd7a C |
46 | |
47 | this.localStorage.setItem(LogsComponent.LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY, this.logType) | |
48 | ||
2c22613c C |
49 | this.load() |
50 | } | |
51 | ||
52 | load () { | |
53 | this.loading = true | |
54 | ||
64553e88 C |
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 | }, | |
2c22613c | 72 | |
64553e88 | 73 | error: err => this.notifier.error(err.message), |
2c22613c | 74 | |
64553e88 C |
75 | complete: () => this.loading = false |
76 | }) | |
2c22613c C |
77 | } |
78 | ||
566c125d C |
79 | isAuditLog () { |
80 | return this.logType === 'audit' | |
81 | } | |
82 | ||
2c22613c C |
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(), | |
7f0d8561 RK |
96 | label: $localize`Last week`, |
97 | dateFormat: 'shortDate' | |
2c22613c C |
98 | }, |
99 | { | |
100 | id: lastDay.toISOString(), | |
7f0d8561 RK |
101 | label: $localize`Last day`, |
102 | dateFormat: 'short' | |
2c22613c C |
103 | }, |
104 | { | |
105 | id: lastHour.toISOString(), | |
7f0d8561 RK |
106 | label: $localize`Last hour`, |
107 | dateFormat: 'mediumTime' | |
2c22613c C |
108 | } |
109 | ] | |
110 | ||
111 | this.startDate = lastHour.toISOString() | |
112 | } | |
113 | ||
114 | buildLevelChoices () { | |
115 | this.levelChoices = [ | |
116 | { | |
117 | id: 'debug', | |
7f0d8561 | 118 | label: $localize`debug` |
2c22613c C |
119 | }, |
120 | { | |
121 | id: 'info', | |
7f0d8561 | 122 | label: $localize`info` |
2c22613c C |
123 | }, |
124 | { | |
125 | id: 'warn', | |
7f0d8561 | 126 | label: $localize`warning` |
2c22613c C |
127 | }, |
128 | { | |
129 | id: 'error', | |
7f0d8561 | 130 | label: $localize`error` |
2c22613c C |
131 | } |
132 | ] | |
133 | ||
28e0e40d | 134 | this.level = 'warn' |
2c22613c | 135 | } |
566c125d C |
136 | |
137 | buildLogTypeChoices () { | |
138 | this.logTypeChoices = [ | |
139 | { | |
140 | id: 'standard', | |
66357162 | 141 | label: $localize`Standard logs` |
566c125d C |
142 | }, |
143 | { | |
144 | id: 'audit', | |
66357162 | 145 | label: $localize`Audit logs` |
566c125d C |
146 | } |
147 | ] | |
288bcd7a C |
148 | } |
149 | ||
150 | private loadPreviousChoices () { | |
151 | this.logType = this.localStorage.getItem(LogsComponent.LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY) | |
566c125d | 152 | |
288bcd7a | 153 | if (this.logType !== 'standard' && this.logType !== 'audit') this.logType = 'audit' |
566c125d | 154 | } |
2c22613c | 155 | } |