All files / src/app/log log.component.ts

100% Statements 25/25
100% Branches 12/12
100% Functions 11/11
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85                                    1x   7x 7x   7x       7x     7x   7x     7x     3x 3x   1x 1x         7x       1x 1x 1x       102x       11x     2x   5x   1x     2x     1x         13x 106x        
import { Component, OnDestroy, OnInit } from '@angular/core';
import { interval, Subscription } from 'rxjs';
import { startWith, switchMap } from 'rxjs/operators';
import { LogService } from './log.service';
import { LogLevel, LogMessageDetail } from '@reflex/shared-types';
import { ValueSelectionComponent } from '@reflex/angular-components/dist';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
 
@Component({
  selector: 'app-log',
  templateUrl: './log.component.html',
  imports: [
    CommonModule,
    FormsModule,
    ValueSelectionComponent
  ]
})
export class LogComponent implements OnInit, OnDestroy {
 
  public messages = new Array<LogMessageDetail>();
  public filteredMessages = new Array<LogMessageDetail>();
 
  public filterLevel = -1;
 
  private logSubscription?: Subscription;
 
  public constructor(private readonly logService: LogService) { }
 
  public ngOnInit(): void {
    this.logService.reset();
 
    this.logSubscription = interval(500)
      .pipe(
        startWith(0),
        switchMap(() => this.logService.getLogs())
      )
      .subscribe((result) => {
        this.messages = [...this.messages, result];
        this.filter();
      }, (error) => {
        console.log(error);
        this.logService.sendErrorLog(`${error}`);
      });
  }
 
  public ngOnDestroy(): void {
    this.logSubscription?.unsubscribe();
  }
 
  public refresh(): void {
    this.messages = [];
    this.filteredMessages = [];
    this.logService.reset();
  }
 
  public getLogLevel(level: LogLevel): string {
    return LogLevel[level];
  }
 
  public getClass(level: LogLevel): string {
    switch (level) {
      case LogLevel.Trace:
      case LogLevel.Info:
        return 'table-dark';
      case LogLevel.Debug:
        return 'table-info';
      case LogLevel.Warn:
        return 'table-warning';
      case LogLevel.Error:
      case LogLevel.Fatal:
        return 'table-danger';
      case LogLevel.Off:
      default:
        return 'table-light';
    }
  }
 
  public filter(): void {
    this.filteredMessages = this.filterLevel as LogLevel >= LogLevel.Trace && this.filterLevel as LogLevel <= LogLevel.Off
      ? this.messages.filter((msg) => LogLevel[msg.level] === LogLevel[this.filterLevel])
      : this.messages;
  }
}