]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/system/logs/logs.component.ts
Remember last log type
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / system / logs / logs.component.ts
CommitLineData
2c22613c 1import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
288bcd7a 2import { LocalStorageService, Notifier } from '@app/core'
67ed6552
C
3import { LogLevel } from '@shared/models'
4import { LogRow } from './log-row.model'
5import { LogsService } from './logs.service'
2c22613c
C
6
7@Component({
8 templateUrl: './logs.component.html',
9 styleUrls: [ './logs.component.scss' ]
10})
11export 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'
2c22613c
C
26
27 constructor (
28 private logsService: LogsService,
288bcd7a
C
29 private notifier: Notifier,
30 private localStorage: LocalStorageService
1227fe07 31 ) { }
2c22613c
C
32
33 ngOnInit (): void {
34 this.buildTimeChoices()
35 this.buildLevelChoices()
566c125d 36 this.buildLogTypeChoices()
2c22613c 37
288bcd7a
C
38 this.loadPreviousChoices()
39
2c22613c
C
40 this.load()
41 }
42
43 refresh () {
44 this.logs = []
288bcd7a
C
45
46 this.localStorage.setItem(LogsComponent.LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY, this.logType)
47
2c22613c
C
48 this.load()
49 }
50
51 load () {
52 this.loading = true
53
566c125d 54 this.logsService.getLogs({ isAuditLog: this.isAuditLog(), level: this.level, startDate: this.startDate })
2c22613c
C
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
566c125d
C
70 isAuditLog () {
71 return this.logType === 'audit'
72 }
73
2c22613c
C
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(),
7f0d8561
RK
87 label: $localize`Last week`,
88 dateFormat: 'shortDate'
2c22613c
C
89 },
90 {
91 id: lastDay.toISOString(),
7f0d8561
RK
92 label: $localize`Last day`,
93 dateFormat: 'short'
2c22613c
C
94 },
95 {
96 id: lastHour.toISOString(),
7f0d8561
RK
97 label: $localize`Last hour`,
98 dateFormat: 'mediumTime'
2c22613c
C
99 }
100 ]
101
102 this.startDate = lastHour.toISOString()
103 }
104
105 buildLevelChoices () {
106 this.levelChoices = [
107 {
108 id: 'debug',
7f0d8561 109 label: $localize`debug`
2c22613c
C
110 },
111 {
112 id: 'info',
7f0d8561 113 label: $localize`info`
2c22613c
C
114 },
115 {
116 id: 'warn',
7f0d8561 117 label: $localize`warning`
2c22613c
C
118 },
119 {
120 id: 'error',
7f0d8561 121 label: $localize`error`
2c22613c
C
122 }
123 ]
124
28e0e40d 125 this.level = 'warn'
2c22613c 126 }
566c125d
C
127
128 buildLogTypeChoices () {
129 this.logTypeChoices = [
130 {
131 id: 'standard',
66357162 132 label: $localize`Standard logs`
566c125d
C
133 },
134 {
135 id: 'audit',
66357162 136 label: $localize`Audit logs`
566c125d
C
137 }
138 ]
288bcd7a
C
139 }
140
141 private loadPreviousChoices () {
142 this.logType = this.localStorage.getItem(LogsComponent.LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY)
566c125d 143
288bcd7a 144 if (this.logType !== 'standard' && this.logType !== 'audit') this.logType = 'audit'
566c125d 145 }
2c22613c 146}