Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 086f94e9

Přidáno uživatelem Jakub Hlaváč před více než 3 roky(ů)

Re #8851 - Unit page - implementace

  • live data in multiselect graph

Zobrazit rozdíly:

src/app/unit/components/unit.component.ts
66 66
                GraphLoader.getGraph(null, null, null, '#vega_container_' + sensor.sensorId.toString().slice(0, 5));
67 67
              }, 0);
68 68
            }
69
            if (!this.selectedSensors.some(sens => sens.toString().slice(0, 5) === sensorType)) {
70
              this.selectedSensors.push(sensor.sensorId);
71
            }
72
          })
73
          this.showGraph();
69
          });
74 70
        }
75 71
      })
76 72
    ).toPromise().then();
......
169 165
    }
170 166
  }
171 167

  
172
  addSensorToGraph(sensorId: number) {
173
    if (!this.selectedSensors.some(sensId => sensId.toString().slice(0, 5) === sensorId.toString().slice(0, 5))) {
174
      GraphLoader.getGraph(null, null, null, '#vega_container_' + sensorId.toString().slice(0, 5));
168
  /**
169
   * Check button handler.
170
   * @param sensorId checked sensorId
171
   * @param event event for getting if checked or unchecked
172
   */
173
  addSensorToGraph(sensorId: number, event) {
174
    const groupId = sensorId.toString().slice(0, 5);
175
    const sensorGroupElement = '#vega_container_' + groupId;
176
    if (!this.selectedSensors.find(sensId => sensId.toString().slice(0, 5) === groupId)) { // if group of sensors is empty show empty graph
177
      GraphLoader.getGraph(null, null, null, sensorGroupElement);
175 178
    } else {
176
      if (this.useAnalyticsData) {
177
        if (this.analyticsData.some(sens => sens.sensorId === sensorId)) {
179
      if (this.useAnalyticsData) { // use analytics data
180
        if (event.checked) { // if checked > add to graph
181
          if (this.analyticsData.some(sens => sens.sensorId === sensorId)) { // if already data for selected sensor in memory
182
            GraphLoader.getGraph(sensorId, this.analyticsData.find(sens => sens.sensorId === sensorId).data,
183
              this.analyticsData.find(sens => sens.sensorId === sensorId).interval, sensorGroupElement);
184
          } else { // get data from server for added sensor and show graph for selected sensors
185
            this.showGraph(false, sensorId);
186
          }
187
        } else { // remove sensor from graph
178 188
          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);
189
            this.analyticsData.find(sens => sens.sensorId === sensorId).interval, sensorGroupElement);
182 190
        }
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
          GraphLoader.getMultilineGraph(this.selectedSensors, this.observationsData, '#vega_container_' + sensorId.toString().slice(0, 5))
188
        } else {
189
          this.showGraph(false, sensorId);
191
      } else { // use observations data
192
        if (event.checked) { // if checked > add to graph
193
          if (this.observationsData.some(sens => sens.sensorId === sensorId)) { // if already data for selected sensor in memory
194
            GraphLoader.getMultilineGraph(this.selectedSensors, this.filteredObservationData(groupId), sensorGroupElement)
195
          } else { // get data from server for added sensor and show graph for selected sensors
196
            this.showGraph(false, sensorId);
197
          }
198
        } else { // remove sensor from graph
199
          GraphLoader.getMultilineGraph(this.selectedSensors, this.filteredObservationData(groupId), sensorGroupElement)
190 200
        }
191 201
      }
192 202
    }
193 203
  }
194 204

  
195
  private getObservations(range: Date[], changedDate: boolean, changedSensor: number) {
196
    if (changedDate) {
205
  /**
206
   * Filter observations data only fro selected sensors.
207
   * @param sensorGroupId id of changed sensor group
208
   */
209
  filteredObservationData(sensorGroupId: string): any {
210
    return this.observationsData.filter(sen => this.selectedSensors.includes(sen.sensorId.toString()) &&
211
      sen.sensorId.toString().slice(0, 5) === sensorGroupId);
212
  }
213

  
214
  /**
215
   * Filter analytics data only fro selected sensors.
216
   */
217
  filteredAnalyticsData(): any {
218
    return this.analyticsData.filter(sen => this.selectedSensors.includes(sen.sensorId.toString()));
219
  }
220

  
221
  private getObservations(range: Date[], changedDate: boolean, changedSensorId: number) {
222
    if (changedDate) { // if changed date we need new data for all sensors
223
      this.observationsData = []; // empty observation data
197 224
      this.selectedSensors.forEach(selectSens => {
198 225
        this.observationService.getObservation$Response({
199 226
          unit_id: this.unitId,
......
210 237
            } else {
211 238
              return false;
212 239
            }
213
          })
214
        ).subscribe(
215
          observations => {
216
            this.observationsData.push({sensorId: selectSens, data: observations});
217
            const view = '#vega_container_' + selectSens.toString().slice(0, 5);
240
          }),
241
          tap((observations) => {
218 242
            if (observations) {
219
              GraphLoader.getObservationGraph(selectSens, observations, view);
220
            } else {
221
              GraphLoader.getObservationGraph(null, null, null);
243
              this.observationsData.push({sensorId: selectSens, sensor:
244
                this.sensors.find(sens => sens.sensorId.toString() === selectSens.toString()), data: observations});
222 245
            }
223
          }, err => this.toastService.showError(err.error.message));
246
          }),
247
          tap(() => {
248
            if (this.observationsData && this.observationsData.length > 1) {
249
              const view = '#vega_container_' + selectSens.toString().slice(0, 5);
250
              setTimeout(() => {
251
                console.log(this.selectedSensors);
252
                GraphLoader.getMultilineGraph(this.selectedSensors, this.filteredObservationData(selectSens.toString().slice(0, 5)), view);
253
              }, 0);
254
            }
255
          })
256
        ).toPromise().then().catch(err => this.toastService.showError(err));
224 257
      });
225 258
    } else {
226 259
      this.observationService.getObservation$Response({
227 260
        unit_id: this.unitId,
228
        sensor_id: changedSensor,
261
        sensor_id: changedSensorId,
229 262
        from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
230 263
        to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
231 264
      }).pipe(
......
241 274
        })
242 275
      ).subscribe(
243 276
        observations => {
244
          this.observationsData.push({sensorId: this.selectedSensors[0], data: observations});
245
          const view = '#vega_container_' + changedSensor.toString().slice(0, 5);
277
          this.observationsData.push({sensorId: changedSensorId, sensor:
278
              this.sensors.find(sens => sens.sensorId.toString() === changedSensorId.toString()), data: observations});
279
          const view = '#vega_container_' + changedSensorId.toString().slice(0, 5);
246 280
          if (observations) {
247
            //GraphLoader.getObservationGraph(changedSensor, observations, view);
248
            GraphLoader.getMultilineGraph(this.selectedSensors, this.observationsData, view);
281
            GraphLoader.getMultilineGraph(this.selectedSensors, this.filteredObservationData(changedSensorId.toString().slice(0, 5)), view);
249 282
          } else {
250
            GraphLoader.getObservationGraph(null, null, null);
283
            GraphLoader.getMultilineGraph(null, null, null);
251 284
          }
252 285
        }, err => this.toastService.showError(err.error.message));
253 286
    }

Také k dispozici: Unified diff