recent-log.component.ts 3.07 KB
Newer Older
Steven Zou's avatar
Steven Zou committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { AuditLog } from './audit-log';
import { SessionUser } from '../shared/session-user';

import { AuditLogService } from './audit-log.service';
import { SessionService } from '../shared/session.service';
import { MessageService } from '../global-message/message.service';
import { AlertType } from '../shared/shared.const';
import { errorHandler, accessErrorHandler } from '../shared/shared.utils';

@Component({
    selector: 'recent-log',
    templateUrl: './recent-log.component.html',
    styleUrls: ['recent-log.component.css']
})

export class RecentLogComponent implements OnInit {
    private sessionUser: SessionUser = null;
    private recentLogs: AuditLog[];
    private logsCache: AuditLog[];
    private onGoing: boolean = false;
    private lines: number = 10; //Support 10, 25 and 50

    constructor(
        private session: SessionService,
        private msgService: MessageService,
        private logService: AuditLogService) {
        this.sessionUser = this.session.getCurrentUser();//Initialize session
    }

    ngOnInit(): void {
        this.retrieveLogs();
    }

37
    private handleOnchange($event: any) {
kunw's avatar
kunw committed
38 39
        if ($event && $event.target && $event.target["value"]) {
            this.lines = $event.target["value"];
40 41 42 43 44
            if (this.lines < 10) {
                this.lines = 10;
            }
            this.retrieveLogs();
        }
Steven Zou's avatar
Steven Zou committed
45 46
    }

47 48 49
    public get logNumber(): number {
        return this.recentLogs?this.recentLogs.length:0;
    }
Steven Zou's avatar
Steven Zou committed
50

51 52
    public get inProgress(): boolean {
        return this.onGoing;
Steven Zou's avatar
Steven Zou committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    }

    public doFilter(terms: string): void {
        if (terms.trim() === "") {
            this.recentLogs = this.logsCache.filter(log => log.username != "");
            return;
        }

        this.recentLogs = this.logsCache.filter(log => this.isMatched(terms, log));
    }

    public refresh(): void {
        this.retrieveLogs();
    }

68
    public formatDateTime(dateTime: string) {
Steven Zou's avatar
Steven Zou committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        let dt: Date = new Date(dateTime);
        return dt.toLocaleString();
    }

    private retrieveLogs(): void {
        if (this.lines < 10) {
            this.lines = 10;
        }

        this.onGoing = true;
        this.logService.getRecentLogs(this.lines)
            .subscribe(
            response => {
                this.onGoing = false;
                this.logsCache = response; //Keep the data
                this.recentLogs = this.logsCache.filter(log => log.username != "");//To display
            },
            error => {
                this.onGoing = false;
                if (!accessErrorHandler(error, this.msgService)) {
                    this.msgService.announceMessage(error.status, errorHandler(error), AlertType.DANGER);
                }
            }
            );
    }

    private isMatched(terms: string, log: AuditLog): boolean {
        let reg = new RegExp('.*' + terms + '.*', 'i');
        return reg.test(log.username) ||
            reg.test(log.repo_name) ||
Steven Zou's avatar
Steven Zou committed
99 100
            reg.test(log.operation) ||
            reg.test(log.repo_tag);
Steven Zou's avatar
Steven Zou committed
101 102
    }
}