All files / src/app/components/signals-graphs signals-graphs.component.ts

79.36% Statements 50/63
85.71% Branches 12/14
57.14% Functions 12/21
81.66% Lines 49/60

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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198                                                    43x 8x     8x 8x 8x 8x   8x   8x               8x 8x                     8x       8x 8x 8x     15x 6x     15x   15x 2x         8x       8x 8x   8x 8x   8x                     8x   8x                 8x   8x                   8x   8x 4x   8x 8x       20x 19x 19x 279x   19x   16x                                   6x     6x               6x       8x 7x   8x 8x 8x       5x                                
import { AfterViewInit, Component, Input, OnChanges, OnInit, ViewChild } from '@angular/core';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { DisplayModeOption, DurationOption, Signal } from '../../models/signals';
import { ListResponse } from '../../models/response';
import { FormsModule } from '@angular/forms';
import { LoaderComponent } from '../loader/loader.component';
import { NgIf } from '@angular/common';
import { SignalsGraphComponent } from '../signals-graph/signals-graph.component';
import { MultiSelectFilterEvent,  MultiSelectModule } from 'primeng/multiselect';
import { Select } from 'primeng/select';
import { FloatLabelModule } from 'primeng/floatlabel';
import { ToolbarModule } from 'primeng/toolbar';
import { ButtonModule } from 'primeng/button';
import { DatePicker } from 'primeng/datepicker';
import { SplitButtonModule } from 'primeng/splitbutton';
import { MenuItem } from 'primeng/api';
import { MultiSelectSignalsComponent } from '../multi-select-signals/multi-select-signals.component';
 
@Component({
    selector: 'app-signals-graphs',
    imports: [LoaderComponent, NgIf, FormsModule, SignalsGraphComponent, MultiSelectModule,
        Select, FloatLabelModule, ToolbarModule, ButtonModule,
        DatePicker, SplitButtonModule, MultiSelectSignalsComponent],
    templateUrl: './signals-graphs.component.html',
    styleUrl: './signals-graphs.component.css'
})
export class SignalsGraphsComponent implements OnInit, OnChanges, AfterViewInit {
  isFiltering: boolean = false;
  isLoading: boolean;
  signalIds: string[];
  totalSignals: number = 0;
  @Input() inputSignalIdsToPlot: string[] = [];
  @Input() showSignals: boolean = true;
  @Input() showLegend: boolean = true;
  @Input() inputZoomTime: number;
  @Input() inputIsLive: boolean=true;
 
  virtualScrollItemSize: number = 15;
  selectedSignal: Signal;
  refreshTimes: DurationOption[];
  replayWindowLengths: DurationOption[];
  windowLengths: DurationOption[];
  windowLength: DurationOption;
  refreshTime: DurationOption;
  replayWindowLength: DurationOption;
  dataRefresh: boolean = true;
  isResponseSlowerThanRefresh: boolean = false;
  zoomTime: number;
  isLive: boolean;
  displayModes: DisplayModeOption[];
  displayMode: DisplayModeOption;
  dataDateMin: Date;
  dataDateMax: Date;
 
  windowMinTs: number;
  windowMaxTs: number;
 
  exports: MenuItem[] = [{"label": "CSV", command: () => {this.downloadSignalsZip("csv");}},
                         {"label": "PrestoPlot", command: () => {this.downloadSignalsZip("prestoplot");}}];
 
 
  @ViewChild(MultiSelectSignalsComponent) multiSelectSignals!: MultiSelectSignalsComponent;
  @ViewChild(SignalsGraphComponent) graphComponent!:SignalsGraphComponent;
  constructor(private twinpadApiService: TwinpadApiService) {}
 
  applyInputs(){
    if(this.multiSelectSignals !== undefined){
      this.inputSignalIdsToPlot = this.multiSelectSignals.signalIdsToPlot;
    }
 
    this.isLive = this.inputIsLive;
 
    if (this.inputZoomTime !== undefined){
      this.zoomTime = this.inputZoomTime;
    }
  }
 
  ngAfterViewInit(): void {
    this.applyInputs();
  }
 
  ngOnInit() {
    this.isLoading = true;
    this.signalIds = [];
 
    this.displayModes = [{label: "Live mode", isLive:true}, {label: "Replay mode", isLive:false}];
    this.displayMode = this.displayModes[0];
 
    this.replayWindowLengths = [
      {label: "10s", duration: 10},
      {label: "1mn", duration: 60},
      {label: "5mn", duration: 300},
      {label: "15mn", duration: 900},
      {label: "1hr", duration: 3600},
      {label: "4hr", duration: 3600*4},
      {label: "12hr", duration: 3600*12},
      {label: "24hr", duration: 3600*24}
    ];
 
    this.replayWindowLength = this.replayWindowLengths[1];
 
    this.refreshTimes = [
      {label: "200ms", duration: 0.2},
      {label: "500ms", duration: 0.5},
      {label: "1s", duration: 1},
      {label: "2s", duration: 2},
      {label: "5s", duration: 5},
      {label: "10s", duration: 10}
    ];
 
    this.refreshTime = this.refreshTimes[3];
 
    this.windowLengths= [
      {label: "10s", duration: 10},
      {label: "30s", duration: 30},
      {label: "1mn", duration: 60},
      {label: "3mn", duration: 180},
      {label: "5mn", duration: 300},
      {label: "10mn", duration: 600},
      {label: "1hr", duration: 3600}
    ];
 
    this.windowLength = this.windowLengths[2];
 
    if(this.showSignals){
      this.loadRecursively();
    }
    this.isLoading = false;
    this.onDateChange();
  }
 
  loadRecursively(){
    this.twinpadApiService.getSignals(this.virtualScrollItemSize, this.signalIds.length, null, null, null).subscribe((data: ListResponse<Signal>) => {
      this.totalSignals = data.total;
      for (const signal of data.items){
        this.signalIds.push(signal.signal_id);
      }
      if(this.signalIds.length < this.totalSignals)
      {
        this.loadRecursively();
      }
    });
  }
 
  filterSignal(event:  MultiSelectFilterEvent){
    this.isFiltering = true;
    this.twinpadApiService.getSignals(0, 0, event.filter, null, null).subscribe((data: ListResponse<Signal>) => {
      for (const signal of data.items){
        if(!this.signalIds.includes(signal.signal_id)){
          this.signalIds.push(signal.signal_id);
        }
      }
    });
    setTimeout(() => this.isFiltering = false, 1000);
  }
 
  handleDataDateMin(date: Date) {
    this.dataDateMin = date;
  }
  handleDataDateMax(date: Date) {
    this.dataDateMax = date;
  }
 
  handleWindowMiddleDate(date: Date){
    this.zoomTime = date.getTime()/1000;
  }
 
  handleResponseSlowerThanRefresh(bool: boolean) {
    this.isResponseSlowerThanRefresh = bool;
  }
 
  onDateChange(){
    if (this.zoomTime === undefined){
      this.zoomTime = Date.now()- 500*this.replayWindowLength.duration;
    }
    const timestamp = this.zoomTime/1000;
    this.windowMinTs = timestamp - 0.5*this.replayWindowLength.duration;
    this.windowMaxTs = timestamp + 0.5*this.replayWindowLength.duration;
  }
 
  ngOnChanges(){
    this.applyInputs();
  }
 
  toggleDataRefresh(){
    this.dataRefresh = !this.dataRefresh;
  }
 
  toggleLive(){
    this.isLive = !this.isLive;
  }
 
  downloadSignalsZip(format: string){
    this.graphComponent.downloadSignalsZip(format);
  }
 
}