]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/system/logs/logs.component.ts
jobs/logs view select and empty state visual improvements
[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, dateFormat: 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 dateFormat: 'shortDate'
81 },
82 {
83 id: lastDay.toISOString(),
84 label: $localize`Last day`,
85 dateFormat: 'short'
86 },
87 {
88 id: lastHour.toISOString(),
89 label: $localize`Last hour`,
90 dateFormat: 'mediumTime'
91 }
92 ]
93
94 this.startDate = lastHour.toISOString()
95 }
96
97 buildLevelChoices () {
98 this.levelChoices = [
99 {
100 id: 'debug',
101 label: $localize`debug`
102 },
103 {
104 id: 'info',
105 label: $localize`info`
106 },
107 {
108 id: 'warn',
109 label: $localize`warning`
110 },
111 {
112 id: 'error',
113 label: $localize`error`
114 }
115 ]
116
117 this.level = 'warn'
118 }
119
120 buildLogTypeChoices () {
121 this.logTypeChoices = [
122 {
123 id: 'standard',
124 label: $localize`Standard logs`
125 },
126 {
127 id: 'audit',
128 label: $localize`Audit logs`
129 }
130 ]
131
132 this.logType = 'audit'
133 }
134 }