Projekt

Obecné

Profil

« Předchozí | Další » 

Revize ba41c963

Přidáno uživatelem Jakub Hlaváč před téměř 4 roky(ů)

Re #8851 - Unit page - implementace

+ adding observations support
+ get for every selected sensor instead of get all
+ use stored data if date is not changed

Zobrazit rozdíly:

src/app/unit/components/unit.component.ts
1 1
import {Component, OnInit} from '@angular/core';
2 2
import {ActivatedRoute} from '@angular/router';
3 3
import {AnalyticsService} from '../../shared/api/endpoints/services/analytics.service';
4
import {map} from 'rxjs/operators';
4
import {map, tap} from 'rxjs/operators';
5 5
import {AggregationModel} from '../../shared/models/aggregationModel';
6 6
import * as moment from 'moment-timezone';
7 7
import {GraphLoader} from '../../shared/graph-loading/graphloader';
......
9 9
import {HttpResponse} from '@angular/common/http';
10 10
import {ToastService} from '../../shared/services/toast.service';
11 11
import {Sensor} from '../../shared/api/endpoints/models/sensor';
12
import {ObservationService} from '../../shared/api/endpoints/services/observation.service';
12 13

  
13 14

  
14 15
@Component({
......
29 30
  analyticsData: any[] = [];
30 31
  observationsData: any[] = [];
31 32
  sensorGroups = [];
32
  selectedSensors: any[] = [];
33
  selectedSensors: number[] = [];
33 34
  sensors: Sensor[];
34 35
  showAggregation = false;
35 36
  aggregationFunction: AggregationModel[];
36
  selectedAggregationFunction = 'HOUR';
37
  selectedAggregationFunction = 'DAY';
38
  useAnalyticsData = false;
37 39

  
38 40
  constructor(
39 41
    private activatedRoute: ActivatedRoute,
40 42
    private analyticsService: AnalyticsService,
41 43
    private sensorService: SensorsService,
42
    private toastService: ToastService
44
    private toastService: ToastService,
45
    private observationService: ObservationService
43 46
  ) {
44 47
    this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
45 48
    this.aggregationFunction = [
......
49 52
      {name: 'Year', code: 'YEAR'}
50 53
    ];
51 54
    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 => {
55
    this.sensorService.getUnitSensors({unit_id: this.unitId}).pipe(
56
      tap(sens => {
57
        this.sensors = sens;
58
      }),
59
      tap(() => {
60
        if (this.sensors && this.sensors.length > 0) {
61
          this.sensors.forEach(sensor => {
57 62
            const sensorType = sensor.sensorId.toString().slice(0, 5);
58 63
            if (!this.sensorGroups.some(group => group === sensorType)) {
59 64
              this.sensorGroups.push(sensorType);
60 65
              setTimeout(() => {
61
                  GraphLoader.getGraph(null, null, null, '#vega_container_' + sensor.sensorId.toString().slice(0, 5));
66
                GraphLoader.getGraph(null, null, null, '#vega_container_' + sensor.sensorId.toString().slice(0, 5));
62 67
              }, 0);
63 68
            }
64 69
            if (!this.selectedSensors.some(sens => sens.toString().slice(0, 5) === sensorType)) {
65 70
              this.selectedSensors.push(sensor.sensorId);
66 71
            }
67 72
          })
73
          this.showGraph();
68 74
        }
69
      }
70
    );
75
      })
76
    ).toPromise().then();
71 77
  }
72 78

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

  
79
  showGraph() {
80
    const range: Date[] = [moment().subtract(6, 'months').toDate(), moment().toDate()];
82
  showGraph(changedDate: boolean = true, changedSensor: number = null) {
83
    const range: Date[] = [moment().subtract(7, 'months').toDate(), moment().toDate()];
81 84

  
82 85
    if (this.from && !this.to || !this.from && this.to) {
83 86
      return;
......
89 92
    }
90 93

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

  
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);
105
  getAnalytics(range: Date[], changedDate: boolean, changedSensor: number) {
106
    if (changedDate) {
107
      this.selectedSensors.forEach(selectSens => {
108
        this.analyticsService.getAnalytics$Response({unit_id: this.unitId, sensor_id: selectSens,
109
          from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
110
          to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction}).pipe(
111
          map((response: HttpResponse<any>) => {
112
            if (response.status === 200) {
113
              return response.body;
114
            } else if (response.status === 204) {
115
              this.toastService.showWarningNoData();
116
              return response.body;
117
            } else {
118
              return false;
119
            }
120
          })
121
        ).subscribe(data => {
122
          if (data) {
123
            this.analyticsData.push({sensorId: selectSens, data: data[selectSens].data});
124
            const objectKeys = Object.keys(data);
125
            for(const key of objectKeys ) {
126
              if (data[key].data) {
127
                const view = '#vega_container_' + key.slice(0, 5);
128
                if (this.selectedSensors.some(sens => sens.toString() === key)) {
129
                  GraphLoader.getGraph(key, data[key].data, data[key].interval, view);
130
                } else {
131
                  GraphLoader.getGraph(null, null, null, view);
132
                }
133
              }
134
            }
135
          }
136
        }, err => this.toastService.showError(err.error.message));
137
      });
138
    } else  {
139
      this.analyticsService.getAnalytics$Response({unit_id: this.unitId, sensor_id: changedSensor,
140
        from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
141
        to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction}).pipe(
142
        map((response: HttpResponse<any>) => {
143
          if (response.status === 200) {
144
            return response.body;
145
          } else if (response.status === 204) {
146
            this.toastService.showWarningNoData();
147
            return response.body;
148
          } else {
149
            return false;
150
          }
151
        })
152
      ).subscribe(data => {
153
        if (data) {
154
          this.analyticsData.push({sensorId: changedSensor, data: data[changedSensor].data, interval: data[changedSensor].interval});
155
          const objectKeys = Object.keys(data);
118 156
          for(const key of objectKeys ) {
119 157
            if (data[key].data) {
120 158
              const view = '#vega_container_' + key.slice(0, 5);
121 159
              if (this.selectedSensors.some(sens => sens.toString() === key)) {
122 160
                GraphLoader.getGraph(key, data[key].data, data[key].interval, view);
161
              } else {
162
                GraphLoader.getGraph(null, null, null, view);
123 163
              }
124 164
            }
125 165
          }
126 166
        }
127 167
      }, err => this.toastService.showError(err.error.message));
168
    }
128 169
  }
129 170

  
130 171
  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);
172
    if (!this.selectedSensors.some(sensId => sensId.toString().slice(0, 5) === sensorId.toString().slice(0, 5))) {
173
      GraphLoader.getGraph(null, null, null, '#vega_container_' + sensorId.toString().slice(0, 5));
174
    } else {
175
      if (this.useAnalyticsData) {
176
        if (this.analyticsData.some(sens => sens.sensorId === sensorId)) {
177
          console.log(this.analyticsData.find(sens => sens.sensorId === sensorId).data);
178
          GraphLoader.getGraph(sensorId, this.analyticsData.find(sens => sens.sensorId === sensorId).data,
179
            this.analyticsData.find(sens => sens.sensorId === sensorId).interval, '#vega_container_' + sensorId.toString().slice(0, 5));
180
        } else {
181
          this.showGraph(false, sensorId);
182
        }
183
      } else {
184
        if (this.observationsData.some(sens => sens.sensorId === sensorId)) {
185
          GraphLoader.getObservationGraph(sensorId,
186
            this.observationsData.find(sens => sens.sensorId === sensorId).data, '#vega_container_' + sensorId.toString().slice(0, 5));
187
        } else {
188
          this.showGraph(false, sensorId);
137 189
        }
138 190
      }
139 191
    }
140 192
  }
193

  
194
  private getObservations(range: Date[], changedDate: boolean, changedSensor: number) {
195
    if (changedDate) {
196
      this.selectedSensors.forEach(selectSens => {
197
        this.observationService.getObservation$Response({
198
          unit_id: this.unitId,
199
          sensor_id: selectSens,
200
          from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
201
          to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
202
        }).pipe(
203
          map((response: HttpResponse<any>) => {
204
            if (response.status === 200) {
205
              return response.body;
206
            } else if (response.status === 204) {
207
              this.toastService.showWarningNoData();
208
              return response.body;
209
            } else {
210
              return false;
211
            }
212
          })
213
        ).subscribe(
214
          observations => {
215
            this.observationsData.push({sensorId: selectSens, data: observations});
216
            const view = '#vega_container_' + selectSens.toString().slice(0, 5);
217
            if (observations) {
218
              GraphLoader.getObservationGraph(selectSens, observations, view);
219
            } else {
220
              GraphLoader.getObservationGraph(null, null, null);
221
            }
222
          }, err => this.toastService.showError(err.error.message));
223
      });
224
    } else {
225
      this.observationService.getObservation$Response({
226
        unit_id: this.unitId,
227
        sensor_id: changedSensor,
228
        from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
229
        to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
230
      }).pipe(
231
        map((response: HttpResponse<any>) => {
232
          if (response.status === 200) {
233
            return response.body;
234
          } else if (response.status === 204) {
235
            this.toastService.showWarningNoData();
236
            return response.body;
237
          } else {
238
            return false;
239
          }
240
        })
241
      ).subscribe(
242
        observations => {
243
          this.observationsData.push({sensorId: this.selectedSensors[0], data: observations});
244
          const view = '#vega_container_' + changedSensor.toString().slice(0, 5);
245
          if (observations) {
246
            GraphLoader.getObservationGraph(changedSensor, observations, view);
247
          } else {
248
            GraphLoader.getObservationGraph(null, null, null);
249
          }
250
        }, err => this.toastService.showError(err.error.message));
251
    }
252
  }
141 253
}

Také k dispozici: Unified diff