All files / tools/ReFlex.TrackingServer/ClientApp/src/app/log log.component.ts

100% Statements 34/34
100% Branches 22/22
100% Functions 12/12
100% Lines 31/31

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 86 87 88 89 90 91 92 93 94 952x                     2x 2x 2x 2x               8x 8x 8x   8x 8x 8x         8x   8x     8x         4x 4x     1x 1x           8x       1x 1x 1x       204x       15x     4x   7x   1x     2x     1x         14x     106x          
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  inject,
  OnDestroy,
  OnInit
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ValueSelectionComponent } from '@reflex/angular-components/dist';
import { LogLevel, LogMessageDetail } from '@reflex/shared-types';
import { interval, Subscription } from 'rxjs';
import { startWith, switchMap } from 'rxjs/operators';
import { markViewForCheck } from 'src/shared/util/mark-view-for-check';
import { LogService } from './log.service';
 
@Component({
  selector: 'app-log',
  templateUrl: './log.component.html',
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [FormsModule, ValueSelectionComponent]
})
export class LogComponent implements OnInit, OnDestroy {
  public messages = new Array<LogMessageDetail>();
  public filteredMessages = new Array<LogMessageDetail>();
 
  public filterLevel: LogLevel | -1 = -1;
  private readonly changeDetector = inject(ChangeDetectorRef);
  private readonly logService = inject(LogService);
 
  private logSubscription?: Subscription;
 
  public ngOnInit(): void {
    this.logService.reset();
 
    this.logSubscription = interval(500)
      .pipe(
        startWith(0),
        switchMap(() => this.logService.getLogs())
      )
      .pipe(markViewForCheck(this.changeDetector))
      .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 >= LogLevel.Trace && this.filterLevel <= LogLevel.Off
        ? this.messages.filter(
          (msg) => LogLevel[msg.level] === LogLevel[this.filterLevel]
        )
        : this.messages;
  }
}