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