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