1 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
2 import { LocalStorageService, Notifier } from '@app/core'
3 import { LogLevel } from '@shared/models'
4 import { LogRow } from './log-row.model'
5 import { LogsService } from './logs.service'
8 templateUrl: './logs.component.html',
9 styleUrls: [ './logs.component.scss' ]
11 export class LogsComponent implements OnInit {
12 private static LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY = 'admin-logs-log-type-choice'
14 @ViewChild('logsElement', { static: true }) logsElement: ElementRef<HTMLElement>
19 timeChoices: { id: string, label: string, dateFormat: string }[] = []
20 levelChoices: { id: LogLevel, label: string }[] = []
21 logTypeChoices: { id: 'audit' | 'standard', label: string }[] = []
25 logType: 'audit' | 'standard'
28 private logsService: LogsService,
29 private notifier: Notifier,
30 private localStorage: LocalStorageService
34 this.buildTimeChoices()
35 this.buildLevelChoices()
36 this.buildLogTypeChoices()
38 this.loadPreviousChoices()
46 this.localStorage.setItem(LogsComponent.LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY, this.logType)
54 this.logsService.getLogs({ isAuditLog: this.isAuditLog(), level: this.level, startDate: this.startDate })
60 this.logsElement.nativeElement.scrollIntoView({ block: 'end', inline: 'nearest' })
64 err => this.notifier.error(err.message),
66 () => this.loading = false
71 return this.logType === 'audit'
75 const lastHour = new Date()
76 lastHour.setHours(lastHour.getHours() - 1)
78 const lastDay = new Date()
79 lastDay.setDate(lastDay.getDate() - 1)
81 const lastWeek = new Date()
82 lastWeek.setDate(lastWeek.getDate() - 7)
86 id: lastWeek.toISOString(),
87 label: $localize`Last week`,
88 dateFormat: 'shortDate'
91 id: lastDay.toISOString(),
92 label: $localize`Last day`,
96 id: lastHour.toISOString(),
97 label: $localize`Last hour`,
98 dateFormat: 'mediumTime'
102 this.startDate = lastHour.toISOString()
105 buildLevelChoices () {
106 this.levelChoices = [
109 label: $localize`debug`
113 label: $localize`info`
117 label: $localize`warning`
121 label: $localize`error`
128 buildLogTypeChoices () {
129 this.logTypeChoices = [
132 label: $localize`Standard logs`
136 label: $localize`Audit logs`
141 private loadPreviousChoices () {
142 this.logType = this.localStorage.getItem(LogsComponent.LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY)
144 if (this.logType !== 'standard' && this.logType !== 'audit') this.logType = 'audit'