All files / src/app/ceph/cluster/logs logs.component.ts

88.89% Statements 56/63
77.78% Branches 21/27
69.23% Functions 9/13
88.14% Lines 52/59

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 1166x   6x             6x           8x       8x           8x 8x   8x 8x 8x 8x 8x     8x 8x 8x         6x 7x     8x 8x           6x 7x 7x     7x 2x 2x   2x 2x 2x 2x   5x     7x 7x 7x   7x 7x 7x   7x     6x 10x 20x 20x     20x 20x 20x 20x                   6x 5x 5x 5x     6x       6x       6x  
import { Component, OnDestroy, OnInit } from '@angular/core';
 
import { LogsService } from '../../../shared/api/logs.service';
 
@Component({
  selector: 'cd-logs',
  template: require('./logs.component.html'),
  styles: []
})
export class LogsComponent implements OnInit, OnDestroy {
  contentData: any;
  clog: Array<any>;
  audit_log: Array<any>;
 
  interval: number;
  bsConfig = {
    dateInputFormat: 'YYYY-MM-DD',
    containerClass: 'theme-default'
  };
  prioritys: Array<{ name: string; value: string }> = [
    { name: 'Info', value: '[INF]' },
    { name: 'Warning', value: '[WRN]' },
    { name: 'Error', value: '[ERR]' },
    { name: 'All', value: 'All' }
  ];
  priority = 'All';
  search = '';
  selectedDate: Date;
  startTime: Date = new Date();
  endTime: Date = new Date();
  constructor(private logsService: LogsService) {
    this.startTime.setHours(0, 0);
    this.endTime.setHours(23, 59);
  }
 
  ngOnInit() {
    this.getInfo();
    this.interval = window.setInterval(() => {
      this.getInfo();
    }, 5000);
  }
 
  ngOnDestroy() {
    clearInterval(this.interval);
  }
 
  getInfo() {
    this.logsService.getLogs().subscribe((data: any) => {
      this.contentData = data;
      this.filterLogs();
    });
  }
 
  abstractfilters(): any {
    const priority = this.priority;
    const key = this.search.toLowerCase().replace(/,/g, '');
 
    let yearMonthDay: string;
    if (this.selectedDate) {
      const m = this.selectedDate.getMonth() + 1;
      const d = this.selectedDate.getDate();
 
      const year = this.selectedDate.getFullYear().toString();
      const month = m <= 9 ? `0${m}` : `${m}`;
      const day = d <= 9 ? `0${d}` : `${d}`;
      yearMonthDay = `${year}-${month}-${day}`;
    } else {
      yearMonthDay = '';
    }
 
    const sHour = this.startTime ? this.startTime.getHours() : 0;
    const sMinutes = this.startTime ? this.startTime.getMinutes() : 0;
    const sTime = sHour * 60 + sMinutes;
 
    const eHour = this.endTime ? this.endTime.getHours() : 23;
    const eMinutes = this.endTime ? this.endTime.getMinutes() : 59;
    const eTime = eHour * 60 + eMinutes;
 
    return { priority, key, yearMonthDay, sTime, eTime };
  }
 
  filterExecutor(logs: Array<any>, filters: any): Array<any> {
    return logs.filter((line) => {
      const hour = parseInt(line.stamp.slice(11, 13), 10);
      const minutes = parseInt(line.stamp.slice(14, 16), 10);
      let prio: string, y_m_d: string, timeSpan: number;
 
      prio = filters.priority === 'All' ? line.priority : filters.priority;
      y_m_d = filters.yearMonthDay ? filters.yearMonthDay : line.stamp;
      timeSpan = hour * 60 + minutes;
      return (
        line.priority === prio &&
        line.message.toLowerCase().indexOf(filters.key) !== -1 &&
        line.stamp.indexOf(y_m_d) !== -1 &&
        timeSpan >= filters.sTime &&
        timeSpan <= filters.eTime
      );
    });
  }
 
  filterLogs() {
    const filters = this.abstractfilters();
    this.clog = this.filterExecutor(this.contentData.clog, filters);
    this.audit_log = this.filterExecutor(this.contentData.audit_log, filters);
  }
 
  clearSearchKey() {
    this.search = '';
    this.filterLogs();
  }
  clearDate() {
    this.selectedDate = null;
    this.filterLogs();
  }
}