Projekt

Obecné

Profil

Stáhnout (4.56 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {Component, OnInit} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3
import {AnalyticsService} from '../../shared/api/endpoints/services/analytics.service';
4
import {map} from 'rxjs/operators';
5
import {AggregationModel} from '../../shared/models/aggregationModel';
6
import * as moment from 'moment-timezone';
7
import {GraphLoader} from '../../shared/graph-loading/graphloader';
8
import {SensorsService} from '../../shared/api/endpoints/services/sensors.service';
9
import {HttpResponse} from '@angular/common/http';
10
import {ToastService} from '../../shared/services/toast.service';
11
import {Sensor} from '../../shared/api/endpoints/models/sensor';
12

    
13

    
14
@Component({
15
  selector: 'app-unit',
16
  templateUrl: './unit.component.html',
17
  styleUrls: ['./unit.component.scss']
18
})
19
export class UnitComponent implements OnInit {
20

    
21
  preselectedSensors: string;
22
  unitId: number;
23
  viewCount = 0;
24
  data = [];
25
  time = [];
26
  from: Date;
27
  to: Date;
28
  today: Date;
29
  analyticsData: any[] = [];
30
  observationsData: any[] = [];
31
  sensorGroups = [];
32
  selectedSensors: any[] = [];
33
  sensors: Sensor[];
34
  showAggregation = false;
35
  aggregationFunction: AggregationModel[];
36
  selectedAggregationFunction = 'HOUR';
37

    
38
  constructor(
39
    private activatedRoute: ActivatedRoute,
40
    private analyticsService: AnalyticsService,
41
    private sensorService: SensorsService,
42
    private toastService: ToastService
43
  ) {
44
    this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
45
    this.aggregationFunction = [
46
      {name: 'Hour', code: 'HOUR'},
47
      {name: 'Day', code: 'DAY'},
48
      {name: 'Month', code: 'MONTH'},
49
      {name: 'Year', code: 'YEAR'}
50
    ];
51
    this.today = moment().toDate();
52
    this.sensorService.getUnitSensors({unit_id: this.unitId}).subscribe(
53
      sensors => {
54
        this.sensors = sensors;
55
        if (sensors) {
56
          sensors.forEach(sensor => {
57
            const sensorType = sensor.sensorId.toString().slice(0, 5);
58
            if (!this.sensorGroups.some(group => group === sensorType)) {
59
              this.sensorGroups.push(sensorType);
60
              setTimeout(() => {
61
                  GraphLoader.getGraph(null, null, null, '#vega_container_' + sensor.sensorId.toString().slice(0, 5));
62
              }, 0);
63
            }
64
            if (!this.selectedSensors.some(sens => sens.toString().slice(0, 5) === sensorType)) {
65
              this.selectedSensors.push(sensor.sensorId);
66
            }
67
          })
68
        }
69
      }
70
    );
71
  }
72

    
73
  ngOnInit(): void {
74
    setTimeout(() => {
75
      this.showGraph();
76
    }, 0);
77
  }
78

    
79
  showGraph() {
80
    const range: Date[] = [moment().subtract(6, 'months').toDate(), moment().toDate()];
81

    
82
    if (this.from && !this.to || !this.from && this.to) {
83
      return;
84
    }
85

    
86
    if (this.from && this.to) {
87
      range[0] = this.from;
88
      range[1] = this.to;
89
    }
90

    
91
    if (moment(range[1]).diff(moment(range[0]), 'days') > 7) {
92
      this.showAggregation = true;
93
      this.getAnalytics(range);
94
    } else {
95
      this.showAggregation = false;
96
      // this.getObservations(range);
97
    }
98
  }
99

    
100
  getAnalytics(range: Date[]) {
101
    this.analyticsService.getAnalytics$Response({unit_id: this.unitId, sensor_id: null,
102
      from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
103
      to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction}).pipe(
104
      map((response: HttpResponse<any>) => {
105
        if (response.status === 200) {
106
          return response.body;
107
        } else if (response.status === 204) {
108
          this.toastService.showWarningNoData();
109
          return response.body;
110
        } else {
111
          return false;
112
        }
113
      })
114
    ).subscribe(data => {
115
      if (data) {
116
        this.analyticsData = data;
117
        const objectKeys = Object.keys(data);
118
          for(const key of objectKeys ) {
119
            if (data[key].data) {
120
              const view = '#vega_container_' + key.slice(0, 5);
121
              if (this.selectedSensors.some(sens => sens.toString() === key)) {
122
                GraphLoader.getGraph(key, data[key].data, data[key].interval, view);
123
              }
124
            }
125
          }
126
        }
127
      }, err => this.toastService.showError(err.error.message));
128
  }
129

    
130
  addSensorToGraph(sensorId: number) {
131
    const objectKeys = Object.keys(this.analyticsData);
132
    for(const key of objectKeys ) {
133
      if (this.analyticsData[key].data) {
134
        const view = '#vega_container_' + key.slice(0, 5);
135
        if (sensorId.toString() === key) {
136
          GraphLoader.getGraph(key, this.analyticsData[key].data, this.analyticsData[key].interval, view);
137
        }
138
      }
139
    }
140
  }
141
}
(3-3/3)