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
|
import {GraphLoader} from '../../shared/graph-loading/graphloader';
|
9
|
|
10
|
declare var require: any
|
11
|
|
12
|
@Component({
|
13
|
selector: 'app-unit',
|
14
|
templateUrl: './unit.component.html',
|
15
|
styleUrls: ['./unit.component.scss']
|
16
|
})
|
17
|
export class UnitComponent implements OnInit {
|
18
|
|
19
|
preselectedSensors: string;
|
20
|
unitId: number;
|
21
|
viewCount = 0;
|
22
|
data;
|
23
|
sensorGroups = [];
|
24
|
rangeDates: Date[];
|
25
|
aggregationFunction: AggreationModel[];
|
26
|
selectedAggregationFunction: string;
|
27
|
|
28
|
constructor(
|
29
|
private activatedRoute: ActivatedRoute,
|
30
|
private analyticsService: AnalyticsService
|
31
|
) {
|
32
|
this.aggregationFunction = [
|
33
|
{name: 'Hour', code: 'HOUR'},
|
34
|
{name: 'Day', code: 'DAY'},
|
35
|
{name: 'Month', code: 'MONTH'},
|
36
|
{name: 'Year', code: 'YEAR'}
|
37
|
];
|
38
|
}
|
39
|
|
40
|
ngOnInit(): void {
|
41
|
/* this.route.queryParams.subscribe(params => {
|
42
|
if(params.sensors) {
|
43
|
this.preselectedSensors = params.sensors;
|
44
|
}
|
45
|
});*/
|
46
|
|
47
|
this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
|
48
|
|
49
|
this.showGraph()
|
50
|
}
|
51
|
|
52
|
showGraph() {
|
53
|
let agg = 'HOUR';
|
54
|
let range: Date[] = [moment().subtract(6, 'months').toDate(), moment().toDate()];
|
55
|
if (this.selectedAggregationFunction) {
|
56
|
agg = this.selectedAggregationFunction;
|
57
|
}
|
58
|
|
59
|
if (this.rangeDates) {
|
60
|
range = this.rangeDates;
|
61
|
}
|
62
|
|
63
|
this.analyticsService.getAnalytics({unit_id: this.unitId, sensor_id: null,
|
64
|
from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
|
65
|
to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: agg}).pipe(
|
66
|
tap(data => {
|
67
|
var objectKeys = Object.keys(data);
|
68
|
for(const key of objectKeys ) {
|
69
|
let element = this.createView();
|
70
|
GraphLoader.getGraph(key, data[key]["data"], data[key]["interval"], element);
|
71
|
}
|
72
|
})
|
73
|
).subscribe();
|
74
|
}
|
75
|
|
76
|
createView() {
|
77
|
let div = document.createElement("div");
|
78
|
let name = "view" + this.viewCount;
|
79
|
div.id = name;
|
80
|
this.viewCount++;
|
81
|
|
82
|
let container = document.getElementById("vega_container");
|
83
|
container.appendChild(div);
|
84
|
|
85
|
return '#' + name;
|
86
|
}
|
87
|
}
|