Projekt

Obecné

Profil

Stáhnout (1.93 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 {tap} from 'rxjs/operators';
5
import {Sensors} from '../../shared/api/endpoints/models/sensors';
6
import {AggreationModel} from '../../shared/models/aggreation.model';
7
import * as moment from 'moment-timezone';
8

    
9
@Component({
10
  selector: 'app-unit',
11
  templateUrl: './unit.component.html',
12
  styleUrls: ['./unit.component.scss']
13
})
14
export class UnitComponent implements OnInit {
15

    
16
  preselectedSensors: string;
17
  unitId: number;
18
  data;
19
  rangeDates: Date[];
20
  aggregationFunction: AggreationModel[];
21
  selectedAggregationFunction: string;
22

    
23
  constructor(
24
    private activatedRoute: ActivatedRoute,
25
    private analyticsService: AnalyticsService
26
  ) {
27
    this.aggregationFunction = [
28
      {name: 'Hour', code: 'HOUR'},
29
      {name: 'Day', code: 'DAY'},
30
      {name: 'Month', code: 'MONTH'},
31
      {name: 'Year', code: 'YEAR'}
32
    ];
33
  }
34

    
35
  ngOnInit(): void {
36
/*    this.route.queryParams.subscribe(params => {
37
      if(params.sensors)  {
38
        this.preselectedSensors = params.sensors;
39
      }
40
    });*/
41

    
42
    this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
43

    
44
    this.showGraph()
45
  }
46

    
47
  showGraph() {
48
    let agg = 'HOUR';
49
    let range: Date[] = [moment().subtract(12, 'months').toDate(), moment().toDate()];
50
    if (this.selectedAggregationFunction) {
51
      agg = this.selectedAggregationFunction;
52
    }
53

    
54
    if (this.rangeDates) {
55
      range = this.rangeDates;
56
    }
57

    
58
    this.analyticsService.getAnalytics({unit_id: this.unitId, sensor_id: null,
59
      from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
60
      to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: agg}).pipe(
61
      tap(data => {
62
        this.data = Object.entries(data);
63
        console.log(this.data);
64
      })
65
    ).subscribe();
66
  }
67
}
(3-3/3)