All files / src/app/components/device-states device-states.component.ts

83.72% Statements 36/43
63.63% Branches 14/22
73.33% Functions 11/15
82.05% Lines 32/39

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                                                36x       1x 1x 1x 1x 1x 1x         1x     1x 1x 1x 1x   1x     1x 1x   1x 1x 1x                                             2x       2x 2x   2x     2x                 1x 1x   1x 1x     1x 1x       1x 1x   1x      
import { Component, Input, OnInit } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { Table, TableLazyLoadEvent, TableModule, TablePageEvent } from 'primeng/table';
import { TagModule } from 'primeng/tag';
import { DeviceState } from '../../models/devices';
import { getSeverity } from '../../utils/utils';
import { ActivatedRoute, Router } from '@angular/router';
 
import { IconFieldModule } from 'primeng/iconfield';
import { InputIconModule } from 'primeng/inputicon';
import { InputTextModule } from 'primeng/inputtext';
import { FormsModule } from '@angular/forms';
import { NgIf, DatePipe } from '@angular/common';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { HttpErrorResponse } from '@angular/common/http';
import { ListResponse } from '../../models/response';
import { Subscription } from 'rxjs';
 
@Component({
    selector: 'app-device-states',
    imports: [TableModule, ButtonModule, TagModule, IconFieldModule, InputIconModule, InputTextModule, FormsModule, NgIf, DatePipe],
    templateUrl: './device-states.component.html',
    styleUrl: './device-states.component.css'
})
export class StatesTableComponent implements OnInit{
  @Input() deviceId!: string;
  deviceName: string;
  versions: ListResponse<DeviceState>;
  offset: number = 0;
  rows: number = 10;
  sortField: string = "timestamp";
  sortOrder: number = -1;
  defaultRowOptions = [10, 20, 50];
  filteredRowsPerPageOptions: number[] = [];
  signalsError: HttpErrorResponse;
 
  dataSubscription: Subscription;
 
  public constructor(private route: ActivatedRoute, private router: Router, private twinpadApiService: TwinpadApiService){}
 
  ngOnInit(): void {
    this.route.params.subscribe(params => {
      this.dataSubscription?.unsubscribe();
      this.deviceId = params['device_id'];
      this.twinpadApiService.getDevice(this.deviceId).subscribe(
        {next: device => {
          this.deviceName = device.name;
        }}
      );
      this.rows = params['rows'] ? +params['rows'] : 10;
      this.offset = params['offset']? +params['offset'] : 0;
    });
    this.updateRowsPerPageOptions();
    Eif(this.versions === undefined){
      this.getData();
    }
  }
 
  getSeverity(status: string){
    return getSeverity(status);
  }
 
  clear(table: Table) {
    table.clear();
  }
 
  onTableEvent(event: TablePageEvent){
    this.offset = event.first;
    this.rows = event.rows;
 
    this.router.navigate([], {
      queryParams: { offset: this.offset, rows: this.rows },
      queryParamsHandling: 'merge'
    });
  }
 
  updateRowsPerPageOptions(){
    this.filteredRowsPerPageOptions = [...new Set([...this.defaultRowOptions, this.rows])].sort((a, b) => a - b);
  }
 
  getData(){
    this.dataSubscription?.unsubscribe();
    this.dataSubscription = this.twinpadApiService.callPeriodically(
      () => {
        return this.twinpadApiService.getDeviceStates(this.deviceId, this.rows, this.offset, this.sortField + ":" + this.sortOrder);
      }, 5000).subscribe({
        next: (value) => {
        this.versions = value;
      },
      error: (error)=>{
        this.signalsError = error;
      }
    });
  }
 
  loadEvents(event: TableLazyLoadEvent){
    Eif (event.first !== null && event.first !== undefined){
      this.offset = event.first;
    }
    Eif (event.rows !== null && event.rows !== undefined){
      this.rows = event.rows;
    }
 
    let sortField = event.sortField ?? "";
    Iif (typeof sortField === "object") {
      sortField = sortField[0];
    }
 
    this.sortField = sortField;
    this.sortOrder = event.sortOrder ?? 1;
 
    this.getData();
  }
}