2019-08-12 19:34:23 +03:00
|
|
|
///
|
2021-01-11 13:42:16 +02:00
|
|
|
/// Copyright © 2016-2021 The Thingsboard Authors
|
2019-08-12 19:34:23 +03:00
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
2020-04-14 11:38:30 +03:00
|
|
|
import { Component, Inject, InjectionToken, OnInit, ViewContainerRef } from '@angular/core';
|
2019-08-12 19:34:23 +03:00
|
|
|
import {
|
|
|
|
|
aggregationTranslations,
|
2020-04-14 11:38:30 +03:00
|
|
|
AggregationType,
|
|
|
|
|
DAY,
|
2021-03-16 12:33:45 +02:00
|
|
|
HistoryWindowType,
|
|
|
|
|
quickTimeIntervalPeriod,
|
|
|
|
|
RealtimeWindowType,
|
2019-08-12 19:34:23 +03:00
|
|
|
Timewindow,
|
|
|
|
|
TimewindowType
|
|
|
|
|
} from '@shared/models/time/time.models';
|
2020-04-14 11:38:30 +03:00
|
|
|
import { OverlayRef } from '@angular/cdk/overlay';
|
2019-08-12 19:34:23 +03:00
|
|
|
import { PageComponent } from '@shared/components/page.component';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
|
|
|
import { TimeService } from '@core/services/time.service';
|
|
|
|
|
|
|
|
|
|
export const TIMEWINDOW_PANEL_DATA = new InjectionToken<any>('TimewindowPanelData');
|
|
|
|
|
|
|
|
|
|
export interface TimewindowPanelData {
|
|
|
|
|
historyOnly: boolean;
|
|
|
|
|
timewindow: Timewindow;
|
|
|
|
|
aggregation: boolean;
|
2020-02-25 19:11:25 +02:00
|
|
|
isEdit: boolean;
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-timewindow-panel',
|
|
|
|
|
templateUrl: './timewindow-panel.component.html',
|
|
|
|
|
styleUrls: ['./timewindow-panel.component.scss']
|
|
|
|
|
})
|
|
|
|
|
export class TimewindowPanelComponent extends PageComponent implements OnInit {
|
|
|
|
|
|
|
|
|
|
historyOnly = false;
|
|
|
|
|
|
|
|
|
|
aggregation = false;
|
|
|
|
|
|
2020-02-25 19:11:25 +02:00
|
|
|
isEdit = false;
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
timewindow: Timewindow;
|
|
|
|
|
|
|
|
|
|
result: Timewindow;
|
|
|
|
|
|
|
|
|
|
timewindowForm: FormGroup;
|
|
|
|
|
|
|
|
|
|
historyTypes = HistoryWindowType;
|
|
|
|
|
|
2021-03-16 12:33:45 +02:00
|
|
|
realtimeTypes = RealtimeWindowType;
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
timewindowTypes = TimewindowType;
|
|
|
|
|
|
|
|
|
|
aggregationTypes = AggregationType;
|
|
|
|
|
|
|
|
|
|
aggregations = Object.keys(AggregationType);
|
|
|
|
|
|
|
|
|
|
aggregationTypesTranslations = aggregationTranslations;
|
|
|
|
|
|
|
|
|
|
constructor(@Inject(TIMEWINDOW_PANEL_DATA) public data: TimewindowPanelData,
|
|
|
|
|
public overlayRef: OverlayRef,
|
|
|
|
|
protected store: Store<AppState>,
|
|
|
|
|
public fb: FormBuilder,
|
|
|
|
|
private timeService: TimeService,
|
|
|
|
|
public viewContainerRef: ViewContainerRef) {
|
|
|
|
|
super(store);
|
|
|
|
|
this.historyOnly = data.historyOnly;
|
|
|
|
|
this.timewindow = data.timewindow;
|
|
|
|
|
this.aggregation = data.aggregation;
|
2020-02-25 19:11:25 +02:00
|
|
|
this.isEdit = data.isEdit;
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2020-02-25 19:11:25 +02:00
|
|
|
const hideInterval = this.timewindow.hideInterval || false;
|
|
|
|
|
const hideAggregation = this.timewindow.hideAggregation || false;
|
|
|
|
|
const hideAggInterval = this.timewindow.hideAggInterval || false;
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
this.timewindowForm = this.fb.group({
|
|
|
|
|
realtime: this.fb.group(
|
|
|
|
|
{
|
2021-03-16 12:33:45 +02:00
|
|
|
realtimeType: this.fb.control({
|
|
|
|
|
value: this.timewindow.realtime && typeof this.timewindow.realtime.realtimeType !== 'undefined'
|
|
|
|
|
? this.timewindow.realtime.realtimeType : RealtimeWindowType.LAST_INTERVAL,
|
|
|
|
|
disabled: hideInterval
|
|
|
|
|
}),
|
2019-08-12 19:34:23 +03:00
|
|
|
timewindowMs: [
|
|
|
|
|
this.timewindow.realtime && typeof this.timewindow.realtime.timewindowMs !== 'undefined'
|
|
|
|
|
? this.timewindow.realtime.timewindowMs : null
|
|
|
|
|
],
|
|
|
|
|
interval: [
|
|
|
|
|
this.timewindow.realtime && typeof this.timewindow.realtime.interval !== 'undefined'
|
|
|
|
|
? this.timewindow.realtime.interval : null
|
2021-03-16 12:33:45 +02:00
|
|
|
],
|
|
|
|
|
quickInterval: this.fb.control({
|
|
|
|
|
value: this.timewindow.realtime && typeof this.timewindow.realtime.quickInterval !== 'undefined'
|
|
|
|
|
? this.timewindow.realtime.quickInterval : null,
|
|
|
|
|
disabled: hideInterval
|
|
|
|
|
})
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
history: this.fb.group(
|
|
|
|
|
{
|
2020-02-25 19:11:25 +02:00
|
|
|
historyType: this.fb.control({
|
|
|
|
|
value: this.timewindow.history && typeof this.timewindow.history.historyType !== 'undefined'
|
|
|
|
|
? this.timewindow.history.historyType : HistoryWindowType.LAST_INTERVAL,
|
|
|
|
|
disabled: hideInterval
|
|
|
|
|
}),
|
|
|
|
|
timewindowMs: this.fb.control({
|
|
|
|
|
value: this.timewindow.history && typeof this.timewindow.history.timewindowMs !== 'undefined'
|
|
|
|
|
? this.timewindow.history.timewindowMs : null,
|
|
|
|
|
disabled: hideInterval
|
|
|
|
|
}),
|
2019-08-12 19:34:23 +03:00
|
|
|
interval: [
|
|
|
|
|
this.timewindow.history && typeof this.timewindow.history.interval !== 'undefined'
|
|
|
|
|
? this.timewindow.history.interval : null
|
|
|
|
|
],
|
2020-02-25 19:11:25 +02:00
|
|
|
fixedTimewindow: this.fb.control({
|
|
|
|
|
value: this.timewindow.history && typeof this.timewindow.history.fixedTimewindow !== 'undefined'
|
|
|
|
|
? this.timewindow.history.fixedTimewindow : null,
|
|
|
|
|
disabled: hideInterval
|
2021-03-15 16:29:42 +02:00
|
|
|
}),
|
|
|
|
|
quickInterval: this.fb.control({
|
|
|
|
|
value: this.timewindow.history && typeof this.timewindow.history.quickInterval !== 'undefined'
|
|
|
|
|
? this.timewindow.history.quickInterval : null,
|
|
|
|
|
disabled: hideInterval
|
2021-03-16 12:33:45 +02:00
|
|
|
})
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
aggregation: this.fb.group(
|
|
|
|
|
{
|
2020-02-25 19:11:25 +02:00
|
|
|
type: this.fb.control({
|
|
|
|
|
value: this.timewindow.aggregation && typeof this.timewindow.aggregation.type !== 'undefined'
|
|
|
|
|
? this.timewindow.aggregation.type : null,
|
|
|
|
|
disabled: hideAggregation
|
|
|
|
|
}),
|
|
|
|
|
limit: this.fb.control({
|
|
|
|
|
value: this.timewindow.aggregation && typeof this.timewindow.aggregation.limit !== 'undefined'
|
2019-08-12 19:34:23 +03:00
|
|
|
? this.timewindow.aggregation.limit : null,
|
2020-02-25 19:11:25 +02:00
|
|
|
disabled: hideAggInterval
|
|
|
|
|
}, [Validators.min(this.minDatapointsLimit()), Validators.max(this.maxDatapointsLimit())])
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update() {
|
2020-02-25 19:11:25 +02:00
|
|
|
const timewindowFormValue = this.timewindowForm.getRawValue();
|
2019-09-10 15:12:10 +03:00
|
|
|
this.timewindow.realtime = {
|
2021-03-16 12:33:45 +02:00
|
|
|
realtimeType: timewindowFormValue.realtime.realtimeType,
|
2019-09-10 15:12:10 +03:00
|
|
|
timewindowMs: timewindowFormValue.realtime.timewindowMs,
|
2021-03-16 12:33:45 +02:00
|
|
|
quickInterval: timewindowFormValue.realtime.quickInterval,
|
2019-09-10 15:12:10 +03:00
|
|
|
interval: timewindowFormValue.realtime.interval
|
|
|
|
|
};
|
|
|
|
|
this.timewindow.history = {
|
|
|
|
|
historyType: timewindowFormValue.history.historyType,
|
|
|
|
|
timewindowMs: timewindowFormValue.history.timewindowMs,
|
|
|
|
|
interval: timewindowFormValue.history.interval,
|
2021-03-15 16:29:42 +02:00
|
|
|
fixedTimewindow: timewindowFormValue.history.fixedTimewindow,
|
|
|
|
|
quickInterval: timewindowFormValue.history.quickInterval
|
2019-09-10 15:12:10 +03:00
|
|
|
};
|
2019-08-12 19:34:23 +03:00
|
|
|
if (this.aggregation) {
|
2019-09-10 15:12:10 +03:00
|
|
|
this.timewindow.aggregation = {
|
|
|
|
|
type: timewindowFormValue.aggregation.type,
|
|
|
|
|
limit: timewindowFormValue.aggregation.limit
|
|
|
|
|
};
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
this.result = this.timewindow;
|
|
|
|
|
this.overlayRef.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancel() {
|
|
|
|
|
this.overlayRef.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
minDatapointsLimit() {
|
|
|
|
|
return this.timeService.getMinDatapointsLimit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maxDatapointsLimit() {
|
|
|
|
|
return this.timeService.getMaxDatapointsLimit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
minRealtimeAggInterval() {
|
2021-03-16 12:33:45 +02:00
|
|
|
return this.timeService.minIntervalLimit(this.currentRealtimeTimewindow());
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maxRealtimeAggInterval() {
|
2021-03-16 12:33:45 +02:00
|
|
|
return this.timeService.maxIntervalLimit(this.currentRealtimeTimewindow());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentRealtimeTimewindow(): number {
|
|
|
|
|
const timeWindowFormValue = this.timewindowForm.getRawValue();
|
|
|
|
|
switch (timeWindowFormValue.realtime.realtimeType) {
|
|
|
|
|
case RealtimeWindowType.LAST_INTERVAL:
|
|
|
|
|
return timeWindowFormValue.realtime.timewindowMs;
|
|
|
|
|
case RealtimeWindowType.INTERVAL:
|
|
|
|
|
return quickTimeIntervalPeriod(timeWindowFormValue.realtime.quickInterval);
|
|
|
|
|
default:
|
|
|
|
|
return DAY;
|
|
|
|
|
}
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
minHistoryAggInterval() {
|
|
|
|
|
return this.timeService.minIntervalLimit(this.currentHistoryTimewindow());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maxHistoryAggInterval() {
|
|
|
|
|
return this.timeService.maxIntervalLimit(this.currentHistoryTimewindow());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentHistoryTimewindow() {
|
2020-02-25 19:11:25 +02:00
|
|
|
const timewindowFormValue = this.timewindowForm.getRawValue();
|
2019-08-12 19:34:23 +03:00
|
|
|
if (timewindowFormValue.history.historyType === HistoryWindowType.LAST_INTERVAL) {
|
|
|
|
|
return timewindowFormValue.history.timewindowMs;
|
2021-03-15 16:29:42 +02:00
|
|
|
} else if (timewindowFormValue.history.historyType === HistoryWindowType.INTERVAL) {
|
|
|
|
|
return quickTimeIntervalPeriod(timewindowFormValue.history.quickInterval);
|
2020-02-28 19:49:14 +02:00
|
|
|
} else if (timewindowFormValue.history.fixedTimewindow) {
|
2019-08-12 19:34:23 +03:00
|
|
|
return timewindowFormValue.history.fixedTimewindow.endTimeMs -
|
|
|
|
|
timewindowFormValue.history.fixedTimewindow.startTimeMs;
|
2020-02-28 19:49:14 +02:00
|
|
|
} else {
|
|
|
|
|
return DAY;
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 19:11:25 +02:00
|
|
|
onHideIntervalChanged() {
|
|
|
|
|
if (this.timewindow.hideInterval) {
|
2020-06-11 10:09:14 +03:00
|
|
|
this.timewindowForm.get('history.historyType').disable({emitEvent: false});
|
|
|
|
|
this.timewindowForm.get('history.timewindowMs').disable({emitEvent: false});
|
|
|
|
|
this.timewindowForm.get('history.fixedTimewindow').disable({emitEvent: false});
|
2021-03-15 16:29:42 +02:00
|
|
|
this.timewindowForm.get('history.quickInterval').disable({emitEvent: false});
|
2021-03-16 12:33:45 +02:00
|
|
|
this.timewindowForm.get('realtime.realtimeType').disable({emitEvent: false});
|
|
|
|
|
this.timewindowForm.get('realtime.timewindowMs').disable({emitEvent: false});
|
|
|
|
|
this.timewindowForm.get('realtime.quickInterval').disable({emitEvent: false});
|
2020-02-25 19:11:25 +02:00
|
|
|
} else {
|
2020-06-11 10:09:14 +03:00
|
|
|
this.timewindowForm.get('history.historyType').enable({emitEvent: false});
|
|
|
|
|
this.timewindowForm.get('history.timewindowMs').enable({emitEvent: false});
|
|
|
|
|
this.timewindowForm.get('history.fixedTimewindow').enable({emitEvent: false});
|
2021-03-15 16:29:42 +02:00
|
|
|
this.timewindowForm.get('history.quickInterval').enable({emitEvent: false});
|
2021-03-16 12:33:45 +02:00
|
|
|
this.timewindowForm.get('realtime.realtimeType').enable({emitEvent: false});
|
|
|
|
|
this.timewindowForm.get('realtime.timewindowMs').enable({emitEvent: false});
|
|
|
|
|
this.timewindowForm.get('realtime.quickInterval').enable({emitEvent: false});
|
2020-02-25 19:11:25 +02:00
|
|
|
}
|
|
|
|
|
this.timewindowForm.markAsDirty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onHideAggregationChanged() {
|
|
|
|
|
if (this.timewindow.hideAggregation) {
|
2020-06-11 10:09:14 +03:00
|
|
|
this.timewindowForm.get('aggregation.type').disable({emitEvent: false});
|
2020-02-25 19:11:25 +02:00
|
|
|
} else {
|
2020-06-11 10:09:14 +03:00
|
|
|
this.timewindowForm.get('aggregation.type').enable({emitEvent: false});
|
2020-02-25 19:11:25 +02:00
|
|
|
}
|
|
|
|
|
this.timewindowForm.markAsDirty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onHideAggIntervalChanged() {
|
|
|
|
|
if (this.timewindow.hideAggInterval) {
|
2020-06-11 10:09:14 +03:00
|
|
|
this.timewindowForm.get('aggregation.limit').disable({emitEvent: false});
|
2020-02-25 19:11:25 +02:00
|
|
|
} else {
|
2020-06-11 10:09:14 +03:00
|
|
|
this.timewindowForm.get('aggregation.limit').enable({emitEvent: false});
|
2020-02-25 19:11:25 +02:00
|
|
|
}
|
|
|
|
|
this.timewindowForm.markAsDirty();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|