All files / src/app/components/dashboard dashboard.component.ts

81.96% Statements 50/61
50% Branches 5/10
72.72% Functions 8/11
80% Lines 44/55

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                                    35x                 83x         48x 48x 48x       48x     25x   25x 25x 25x 25x 25x 25x   525x 525x 525x 3675x 3675x 3675x 3675x 3675x     25x   25x 25x   25x   25x                                                                                                                     25x 25x         25x   25x 25x 81x 81x 81x 25x     56x     25x 25x 25x 25x 25x     25x 25x                               44x      
import { Component, OnDestroy, OnInit } from '@angular/core';
import { EChartsOption } from 'echarts';
import { NgxEchartsDirective, provideEchartsCore } from 'ngx-echarts';
import * as echarts from 'echarts/core';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { TwinPadStatus } from '../../models/status';
import { NgClass, NgIf } from '@angular/common';
import { Subscription } from 'rxjs';
import { getWeekNumber } from '../../utils/utils';
import { Router } from '@angular/router';
 
import { HeatmapChart } from 'echarts/charts';
import { GridComponent } from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
import { TooltipComponent } from 'echarts/components';
import { VisualMapComponent } from 'echarts/components';
import { HttpErrorResponse } from '@angular/common/http';
import { ErrorComponent } from '../error/error.component';
echarts.use([HeatmapChart, GridComponent, CanvasRenderer, TooltipComponent, VisualMapComponent]);
 
@Component({
    selector: 'app-dashboard',
    imports: [NgxEchartsDirective, NgClass, NgIf, ErrorComponent],
    providers: [provideEchartsCore({echarts})],
    templateUrl: './dashboard.component.html',
    styleUrl: './dashboard.component.css'
})
export class DashboardComponent implements OnInit, OnDestroy {
  isLoading: boolean;
  options: EChartsOption;
  status: TwinPadStatus;
  subscription: Subscription;
  dayToGrid: Map<string, [number, number]> = new Map();
  gridToDay: Map<string, string> = new Map();
  gridToTimestampBounds: Map<string, [number, number]> = new Map();
  updateOptions: EChartsOption;
  error: HttpErrorResponse;
 
  constructor(private twinpadApiService: TwinpadApiService, private router: Router){}
 
  ngOnInit() {
    this.isLoading = true;
 
    const weeks = [];
    const day = new Date();
    const daysUntilSunday = (7 - day.getDay()) % 7;
    day.setDate(day.getDate() + daysUntilSunday);
    day.setHours(0, 0, 0, 0);
    for (let iweek=20; iweek >= 0; iweek--){
 
      const weekNumber: number = getWeekNumber(day);
      weeks.push('W' + weekNumber.toString());
      for (let iday=0; iday < 7;iday++){
        this.dayToGrid.set(day.toDateString(), [iweek, iday]);
        this.gridToDay.set([iweek, iday].join(','), day.toDateString());
        const tsMin = day.getTime()/1000;
        this.gridToTimestampBounds.set([iweek, iday].join(','), [tsMin , tsMin+24*3600]);
        day.setDate(day.getDate() -1);
      }
    }
    weeks.reverse();
 
    const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'].reverse();
    const data: number[][] = [];
 
    const gridToDay = this.gridToDay;
 
    this.options = {
      title: {
        left: 'center',
        text: 'Pad activity'
      },
      tooltip: {
        position: 'top',
        formatter: function(params: any) {  // eslint-disable-line
            const dataItem = params.data as [number, number, number];
            const x = dataItem[0];
            const y = dataItem[1];
            const value = dataItem[2];
            const day = gridToDay.get([x, y].join(','));
            return day +'<br>Number events: ' + value;
        }
      },
      grid: {
        height: '50%',
        top: '10%'
      },
      xAxis: {
        type: 'category',
        data: weeks,
        splitArea: {
          show: true
        }
      },
      yAxis: {
        type: 'category',
        data: days,
        splitArea: {
          show: true
        }
      },
      visualMap: {
        min: 0,
        max: 250,
        calculable: true,
        orient: 'horizontal',
        left: 'center',
        bottom: '15%'
      },
      series: [
        {
          name: 'Events',
          type: 'heatmap',
          data: data,
          label: {
            show: true
          },
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };
    this.getEvents();
    this.subscription = this.twinpadApiService.callPeriodically(() => this.twinpadApiService.getStatus(), 10000).subscribe(value => this.status=value);
 
  }
 
  getEvents(){
    this.twinpadApiService.getEvents(10000, 0, ['timestamp:-1']).subscribe({next: response => {
 
      let dayData: Map<string, number> = new Map();
      for (const event of response.items){
          const day = new Date(1000*event.timestamp).toDateString();
          const existingData = dayData.get(day);
          if (existingData === undefined){
            dayData = dayData.set(day, 1);
          }
          else{
            dayData = dayData.set(day, existingData+1);
          }
      }
      const data = [];
      for (const [day, nevents] of dayData.entries()) {
        const grid = this.dayToGrid.get(day);
        Eif (grid !== undefined){
          data.push([grid[0], grid[1], nevents]);
        }
      }
      this.updateOptions = {series: [{data: data}]};
      this.isLoading = false;
    },
    error: error => {this.error = error;}
  });
  }
 
  onChartClick(event: any): void { // eslint-disable-line
    if (event.seriesType === 'heatmap') {
      const timestamps = this.gridToTimestampBounds.get([event.data[0], event.data[1]].join(','));
      if (timestamps !== undefined){
        this.router.navigate(['/events'], { queryParams: { min_timestamp: timestamps[0],  max_timestamp: timestamps[1] } });
      }
    }
  }
 
  ngOnDestroy(): void {
    this.subscription?.unsubscribe();
  }
}