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'
26 tagsOneOf: string[] = []
29 private logsService: LogsService,
30 private notifier: Notifier,
31 private localStorage: LocalStorageService
35 this.buildTimeChoices()
36 this.buildLevelChoices()
37 this.buildLogTypeChoices()
39 this.loadPreviousChoices()
47 this.localStorage.setItem(LogsComponent.LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY, this.logType)
55 const tagsOneOf = this.tagsOneOf.length !== 0
59 this.logsService.getLogs({
60 isAuditLog: this.isAuditLog(),
62 startDate: this.startDate,
69 this.logsElement.nativeElement.scrollIntoView({ block: 'end', inline: 'nearest' })
73 error: err => this.notifier.error(err.message),
75 complete: () => this.loading = false
80 return this.logType === 'audit'
84 const lastHour = new Date()
85 lastHour.setHours(lastHour.getHours() - 1)
87 const lastDay = new Date()
88 lastDay.setDate(lastDay.getDate() - 1)
90 const lastWeek = new Date()
91 lastWeek.setDate(lastWeek.getDate() - 7)
95 id: lastWeek.toISOString(),
96 label: $localize`Last week`,
97 dateFormat: 'shortDate'
100 id: lastDay.toISOString(),
101 label: $localize`Last day`,
105 id: lastHour.toISOString(),
106 label: $localize`Last hour`,
107 dateFormat: 'mediumTime'
111 this.startDate = lastHour.toISOString()
114 buildLevelChoices () {
115 this.levelChoices = [
118 label: $localize`debug`
122 label: $localize`info`
126 label: $localize`warning`
130 label: $localize`error`
137 buildLogTypeChoices () {
138 this.logTypeChoices = [
141 label: $localize`Standard logs`
145 label: $localize`Audit logs`
150 private loadPreviousChoices () {
151 this.logType = this.localStorage.getItem(LogsComponent.LOCAL_STORAGE_LOG_TYPE_CHOICE_KEY)
153 if (this.logType !== 'standard' && this.logType !== 'audit') this.logType = 'audit'