]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/+admin/system/logs/logs.component.ts
Add ability for client to create server logs
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / system / logs / logs.component.ts
index c9c9dc3d147c0e81492a55829bb2b85fb7e2c46e..939e710d712384f42944d868714aab0bc0195d6b 100644 (file)
@@ -1,6 +1,6 @@
 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
-import { Notifier } from '@app/core'
-import { LogLevel } from '@shared/models'
+import { LocalStorageService, Notifier } from '@app/core'
+import { ServerLogLevel } from '@shared/models'
 import { LogRow } from './log-row.model'
 import { LogsService } from './logs.service'
 
@@ -9,54 +9,71 @@ import { LogsService } from './logs.service'
   styleUrls: [ './logs.component.scss' ]
 })
 export class LogsComponent implements OnInit {
+  private static LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY = 'admin-logs-log-type-choice'
+
   @ViewChild('logsElement', { static: true }) logsElement: ElementRef<HTMLElement>
 
   loading = false
 
   logs: LogRow[] = []
-  timeChoices: { id: string, label: string }[] = []
-  levelChoices: { id: LogLevel, label: string }[] = []
+  timeChoices: { id: string, label: string, dateFormat: string }[] = []
+  levelChoices: { id: ServerLogLevel, label: string }[] = []
   logTypeChoices: { id: 'audit' | 'standard', label: string }[] = []
 
   startDate: string
-  level: LogLevel
+  level: ServerLogLevel
   logType: 'audit' | 'standard'
+  tagsOneOf: string[] = []
 
   constructor (
     private logsService: LogsService,
-    private notifier: Notifier
-    ) { }
+    private notifier: Notifier,
+    private localStorage: LocalStorageService
+  ) { }
 
   ngOnInit (): void {
     this.buildTimeChoices()
     this.buildLevelChoices()
     this.buildLogTypeChoices()
 
+    this.loadPreviousChoices()
+
     this.load()
   }
 
   refresh () {
     this.logs = []
+
+    this.localStorage.setItem(LogsComponent.LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY, this.logType)
+
     this.load()
   }
 
   load () {
     this.loading = true
 
-    this.logsService.getLogs({ isAuditLog: this.isAuditLog(), level: this.level, startDate: this.startDate })
-        .subscribe(
-          logs => {
-            this.logs = logs
-
-            setTimeout(() => {
-              this.logsElement.nativeElement.scrollIntoView({ block: 'end', inline: 'nearest' })
-            })
-          },
+    const tagsOneOf = this.tagsOneOf.length !== 0
+      ? this.tagsOneOf
+      : undefined
+
+    this.logsService.getLogs({
+      isAuditLog: this.isAuditLog(),
+      level: this.level,
+      startDate: this.startDate,
+      tagsOneOf
+    }).subscribe({
+      next: logs => {
+        this.logs = logs
+
+        setTimeout(() => {
+          this.logsElement.nativeElement.scrollIntoView({ block: 'end', inline: 'nearest' })
+        })
+      },
 
-          err => this.notifier.error(err.message),
+      error: err => this.notifier.error(err.message),
 
-          () => this.loading = false
-        )
+      complete: () => this.loading = false
+    })
   }
 
   isAuditLog () {
@@ -76,15 +93,18 @@ export class LogsComponent implements OnInit {
     this.timeChoices = [
       {
         id: lastWeek.toISOString(),
-        label: $localize`Last week`
+        label: $localize`Last week`,
+        dateFormat: 'shortDate'
       },
       {
         id: lastDay.toISOString(),
-        label: $localize`Last day`
+        label: $localize`Last day`,
+        dateFormat: 'short'
       },
       {
         id: lastHour.toISOString(),
-        label: $localize`Last hour`
+        label: $localize`Last hour`,
+        dateFormat: 'mediumTime'
       }
     ]
 
@@ -95,19 +115,19 @@ export class LogsComponent implements OnInit {
     this.levelChoices = [
       {
         id: 'debug',
-        label: $localize`Debug`
+        label: $localize`debug`
       },
       {
         id: 'info',
-        label: $localize`Info`
+        label: $localize`info`
       },
       {
         id: 'warn',
-        label: $localize`Warning`
+        label: $localize`warning`
       },
       {
         id: 'error',
-        label: $localize`Error`
+        label: $localize`error`
       }
     ]
 
@@ -125,7 +145,11 @@ export class LogsComponent implements OnInit {
         label: $localize`Audit logs`
       }
     ]
+  }
+
+  private loadPreviousChoices () {
+    this.logType = this.localStorage.getItem(LogsComponent.LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY)
 
-    this.logType = 'audit'
+    if (this.logType !== 'standard' && this.logType !== 'audit') this.logType = 'audit'
   }
 }