Projekt

Obecné

Profil

Stáhnout (3.82 KB) Statistiky
| Větev: | Tag: | Revize:
1 370c3423 hlavja
import {Component, OnInit} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3
import {AnalyticsService} from '../../shared/api/endpoints/services/analytics.service';
4 cd2a65f3 hlavja
import {map, tap} from 'rxjs/operators';
5 b5525aef hlavja
import {AggregationModel} from '../../shared/models/aggregationModel';
6 370c3423 hlavja
import * as moment from 'moment-timezone';
7 11efc186 hlavja
import {GraphLoader} from '../../shared/graph-loading/graphloader';
8 cd2a65f3 hlavja
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 370c3423 hlavja
13 8b522708 Štěpán Červenka
declare var require: any
14 370c3423 hlavja
15
@Component({
16
  selector: 'app-unit',
17
  templateUrl: './unit.component.html',
18
  styleUrls: ['./unit.component.scss']
19
})
20
export class UnitComponent implements OnInit {
21
22
  preselectedSensors: string;
23
  unitId: number;
24 8b522708 Štěpán Červenka
  viewCount = 0;
25 cd2a65f3 hlavja
  data = [];
26
  time = [];
27
  from: Date;
28
  to: Date;
29
  today: Date;
30 11efc186 hlavja
  sensorGroups = [];
31 cd2a65f3 hlavja
  selectedSensors = [];
32
  sensors: Sensor[];
33
  showAggregation = false;
34 b5525aef hlavja
  aggregationFunction: AggregationModel[];
35 cd2a65f3 hlavja
  selectedAggregationFunction = 'HOUR';
36 370c3423 hlavja
37
  constructor(
38
    private activatedRoute: ActivatedRoute,
39 cd2a65f3 hlavja
    private analyticsService: AnalyticsService,
40
    private sensorService: SensorsService,
41
    private toastService: ToastService
42 370c3423 hlavja
  ) {
43 cd2a65f3 hlavja
    this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
44 370c3423 hlavja
    this.aggregationFunction = [
45
      {name: 'Hour', code: 'HOUR'},
46
      {name: 'Day', code: 'DAY'},
47
      {name: 'Month', code: 'MONTH'},
48
      {name: 'Year', code: 'YEAR'}
49
    ];
50 cd2a65f3 hlavja
    this.today = moment().toDate();
51
    this.sensorService.getUnitSensors({unit_id: this.unitId}).subscribe(
52
      sensors => {
53
        this.sensors = sensors;
54
        if (sensors) {
55
          sensors.forEach(sensor => {
56 b5525aef hlavja
            const sensorType = sensor.sensorId.toString().slice(0, 5);
57 cd2a65f3 hlavja
            if (!this.sensorGroups.some(group => group === sensorType)) {
58
              this.sensorGroups.push(sensorType);
59
            }
60 b5525aef hlavja
            if (!this.selectedSensors.some(sens => sens.toString().slice(0, 5) === sensorType)) {
61 cd2a65f3 hlavja
              this.selectedSensors.push(sensor.sensorId);
62
            }
63
          })
64
        }
65
      }
66
    )
67 370c3423 hlavja
  }
68
69
  ngOnInit(): void {
70 cd2a65f3 hlavja
    setTimeout(() => {
71
      this.showGraph();
72
    }, 0);
73 370c3423 hlavja
  }
74
75
  showGraph() {
76 cd2a65f3 hlavja
    const range: Date[] = [moment().subtract(6, 'months').toDate(), moment().toDate()];
77
78
    if (this.from && !this.to || !this.from && this.to) {
79
      return;
80
    }
81
82
    if (this.from && this.to) {
83
      range[0] = this.from;
84
      range[1] = this.to;
85 370c3423 hlavja
    }
86
87 cd2a65f3 hlavja
    if (moment(range[1]).diff(moment(range[0]), 'days') > 7) {
88
      this.showAggregation = true;
89
      this.getAnalytics(range);
90
    } else {
91
      this.showAggregation = false;
92
      // this.getObservations(range);
93 370c3423 hlavja
    }
94 cd2a65f3 hlavja
  }
95 370c3423 hlavja
96 cd2a65f3 hlavja
  getAnalytics(range: Date[]) {
97
    this.analyticsService.getAnalytics$Response({unit_id: this.unitId, sensor_id: null,
98 370c3423 hlavja
      from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
99 cd2a65f3 hlavja
      to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction}).pipe(
100
      map((response: HttpResponse<any>) => {
101
        if (response.status === 200) {
102
          return response.body;
103
        } else if (response.status === 204) {
104
          this.toastService.showWarningNoData();
105
          return response.body;
106
        } else {
107
          return false;
108 a3ae1cab hlavja
        }
109 370c3423 hlavja
      })
110 cd2a65f3 hlavja
    ).subscribe(data => {
111
        const objectKeys = Object.keys(data);
112
        for(const key of objectKeys ) {
113
          if (data[key].data) {
114 b5525aef hlavja
            const view = '#vega_container_' + key.slice(0, 5);
115 cd2a65f3 hlavja
            if (this.selectedSensors.some(sens => sens.toString() === key)) {
116
              GraphLoader.getGraph(key, data[key].data, data[key].interval, view);
117
            }
118
          }
119
        }
120
      });
121 8b522708 Štěpán Červenka
  }
122 370c3423 hlavja
}