thingsboard/ui-ngx/src/app/shared/components/time/timewindow-panel.component.ts

236 lines
8.0 KiB
TypeScript
Raw Normal View History

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.
///
import { Component, Inject, InjectionToken, OnInit, ViewContainerRef } from '@angular/core';
2019-08-12 19:34:23 +03:00
import {
aggregationTranslations,
AggregationType,
DAY,
2019-08-12 19:34:23 +03:00
HistoryWindowType,
Timewindow,
TimewindowType
} from '@shared/models/time/time.models';
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;
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(
{
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
]
}
),
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
})
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 = {
timewindowMs: timewindowFormValue.realtime.timewindowMs,
interval: timewindowFormValue.realtime.interval
};
this.timewindow.history = {
historyType: timewindowFormValue.history.historyType,
timewindowMs: timewindowFormValue.history.timewindowMs,
interval: timewindowFormValue.history.interval,
fixedTimewindow: timewindowFormValue.history.fixedTimewindow
};
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() {
2020-06-11 10:09:14 +03:00
return this.timeService.minIntervalLimit(this.timewindowForm.get('realtime.timewindowMs').value);
2019-08-12 19:34:23 +03:00
}
maxRealtimeAggInterval() {
2020-06-11 10:09:14 +03:00
return this.timeService.maxIntervalLimit(this.timewindowForm.get('realtime.timewindowMs').value);
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;
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});
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});
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
}