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

530 lines
20 KiB
TypeScript
Raw Normal View History

2019-08-12 19:34:23 +03:00
///
2024-01-09 10:46:16 +02:00
/// Copyright © 2016-2024 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, OnDestroy, OnInit, ViewContainerRef } from '@angular/core';
2019-08-12 19:34:23 +03:00
import {
aggregationTranslations,
AggregationType,
DAY,
HistoryWindowType,
2024-08-19 18:35:37 +03:00
historyWindowTypeTranslations,
quickTimeIntervalPeriod,
RealtimeWindowType,
2024-08-19 18:35:37 +03:00
realtimeWindowTypeTranslations,
2019-08-12 19:34:23 +03:00
Timewindow,
TimewindowType
} from '@shared/models/time/time.models';
import { PageComponent } from '@shared/components/page.component';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
2023-02-02 15:55:06 +02:00
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
2019-08-12 19:34:23 +03:00
import { TimeService } from '@core/services/time.service';
import { deepClone, isDefined } from '@core/utils';
2023-02-09 12:19:40 +02:00
import { OverlayRef } from '@angular/cdk/overlay';
import { ToggleHeaderOption } from '@shared/components/toggle-header.component';
import { TranslateService } from '@ngx-translate/core';
import { MatDialog } from '@angular/material/dialog';
import {
TimewindowConfigDialogComponent,
TimewindowConfigDialogData
} from '@shared/components/time/timewindow-config-dialog.component';
import { takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
2019-08-12 19:34:23 +03:00
export interface TimewindowPanelData {
historyOnly: boolean;
forAllTimeEnabled: boolean;
quickIntervalOnly: boolean;
2019-08-12 19:34:23 +03:00
timewindow: Timewindow;
aggregation: boolean;
2021-03-17 18:38:57 +02:00
timezone: boolean;
2020-02-25 19:11:25 +02:00
isEdit: boolean;
2019-08-12 19:34:23 +03:00
}
2023-02-09 12:19:40 +02:00
export const TIMEWINDOW_PANEL_DATA = new InjectionToken<any>('TimewindowPanelData');
2019-08-12 19:34:23 +03:00
@Component({
selector: 'tb-timewindow-panel',
templateUrl: './timewindow-panel.component.html',
2024-09-06 14:57:41 +03:00
styleUrls: ['./timewindow-panel.component.scss', './timewindow-form.scss']
2019-08-12 19:34:23 +03:00
})
export class TimewindowPanelComponent extends PageComponent implements OnInit, OnDestroy {
2019-08-12 19:34:23 +03:00
historyOnly = false;
forAllTimeEnabled = false;
quickIntervalOnly = false;
2019-08-12 19:34:23 +03:00
aggregation = false;
2021-03-17 18:38:57 +02:00
timezone = false;
2020-02-25 19:11:25 +02:00
isEdit = false;
2019-08-12 19:34:23 +03:00
timewindow: Timewindow;
2023-02-02 15:55:06 +02:00
timewindowForm: UntypedFormGroup;
2019-08-12 19:34:23 +03:00
historyTypes = HistoryWindowType;
realtimeTypes = RealtimeWindowType;
2019-08-12 19:34:23 +03:00
timewindowTypes = TimewindowType;
aggregationTypes = AggregationType;
aggregations = Object.keys(AggregationType);
aggregationTypesTranslations = aggregationTranslations;
2023-02-09 12:19:40 +02:00
result: Timewindow;
2023-02-07 17:45:03 +02:00
timewindowTypeOptions: ToggleHeaderOption[] = [{
name: this.translate.instant('timewindow.history'),
value: this.timewindowTypes.HISTORY
}];
realtimeTimewindowOptions: ToggleHeaderOption[] = [];
2024-08-19 18:35:37 +03:00
historyTimewindowOptions: ToggleHeaderOption[] = [];
2024-08-02 13:47:50 +03:00
realtimeTypeSelectionAvailable: boolean;
realtimeIntervalSelectionAvailable: boolean;
historyTypeSelectionAvailable: boolean;
historyIntervalSelectionAvailable: boolean;
2024-08-19 18:35:37 +03:00
aggregationOptionsAvailable: boolean;
2024-08-02 13:47:50 +03:00
private destroy$ = new Subject<void>();
2023-02-09 12:19:40 +02:00
constructor(@Inject(TIMEWINDOW_PANEL_DATA) public data: TimewindowPanelData,
public overlayRef: OverlayRef,
protected store: Store<AppState>,
2023-02-02 15:55:06 +02:00
public fb: UntypedFormBuilder,
2019-08-12 19:34:23 +03:00
private timeService: TimeService,
private translate: TranslateService,
public viewContainerRef: ViewContainerRef,
private dialog: MatDialog) {
2019-08-12 19:34:23 +03:00
super(store);
2023-02-09 12:19:40 +02:00
this.historyOnly = data.historyOnly;
this.forAllTimeEnabled = data.forAllTimeEnabled;
this.quickIntervalOnly = data.quickIntervalOnly;
this.timewindow = data.timewindow;
this.aggregation = data.aggregation;
this.timezone = data.timezone;
this.isEdit = data.isEdit;
if (!this.historyOnly) {
this.timewindowTypeOptions.unshift({
name: this.translate.instant('timewindow.realtime'),
value: this.timewindowTypes.REALTIME
});
}
2024-08-19 18:35:37 +03:00
if ((this.isEdit || !this.timewindow.realtime.hideLastInterval) && !this.quickIntervalOnly) {
this.realtimeTimewindowOptions.push({
name: this.translate.instant(realtimeWindowTypeTranslations.get(RealtimeWindowType.LAST_INTERVAL)),
value: this.realtimeTypes.LAST_INTERVAL
});
2024-08-16 18:38:35 +03:00
}
2024-08-19 18:35:37 +03:00
if (this.isEdit || !this.timewindow.realtime.hideQuickInterval || this.quickIntervalOnly) {
this.realtimeTimewindowOptions.push({
name: this.translate.instant(realtimeWindowTypeTranslations.get(RealtimeWindowType.INTERVAL)),
value: this.realtimeTypes.INTERVAL
});
2024-08-16 18:38:35 +03:00
}
if (this.forAllTimeEnabled) {
2024-08-19 18:35:37 +03:00
this.historyTimewindowOptions.push({
name: this.translate.instant(historyWindowTypeTranslations.get(HistoryWindowType.FOR_ALL_TIME)),
value: this.historyTypes.FOR_ALL_TIME
});
}
2024-08-19 18:35:37 +03:00
if (this.isEdit || !this.timewindow.history.hideLastInterval) {
this.historyTimewindowOptions.push({
name: this.translate.instant(historyWindowTypeTranslations.get(HistoryWindowType.LAST_INTERVAL)),
value: this.historyTypes.LAST_INTERVAL
});
}
2024-08-19 18:35:37 +03:00
if (this.isEdit || !this.timewindow.history.hideFixedInterval) {
this.historyTimewindowOptions.push({
name: this.translate.instant(historyWindowTypeTranslations.get(HistoryWindowType.FIXED)),
value: this.historyTypes.FIXED
});
}
if (this.isEdit || !this.timewindow.history.hideQuickInterval) {
this.historyTimewindowOptions.push({
name: this.translate.instant(historyWindowTypeTranslations.get(HistoryWindowType.INTERVAL)),
value: this.historyTypes.INTERVAL
});
}
2024-08-02 13:47:50 +03:00
this.realtimeTypeSelectionAvailable = this.realtimeTimewindowOptions.length > 1;
this.historyTypeSelectionAvailable = this.historyTimewindowOptions.length > 1;
2024-08-16 18:38:35 +03:00
this.realtimeIntervalSelectionAvailable = this.isEdit || !(this.timewindow.realtime.hideInterval ||
(this.timewindow.realtime.hideLastInterval && this.timewindow.realtime.hideQuickInterval));
2024-08-19 18:35:37 +03:00
this.historyIntervalSelectionAvailable = this.isEdit || !(this.timewindow.history.hideInterval ||
(this.timewindow.history.hideLastInterval && this.timewindow.history.hideQuickInterval && this.timewindow.history.hideFixedInterval));
this.aggregationOptionsAvailable = this.aggregation && (this.isEdit ||
!(this.timewindow.hideAggregation && this.timewindow.hideAggInterval));
2019-08-12 19:34:23 +03:00
}
ngOnInit(): void {
2020-02-25 19:11:25 +02:00
const hideAggregation = this.timewindow.hideAggregation || false;
const hideAggInterval = this.timewindow.hideAggInterval || false;
2021-03-17 18:38:57 +02:00
const hideTimezone = this.timewindow.hideTimezone || false;
2020-02-25 19:11:25 +02:00
2023-02-07 17:45:03 +02:00
const realtime = this.timewindow.realtime;
const history = this.timewindow.history;
const aggregation = this.timewindow.aggregation;
if (!this.isEdit) {
if (realtime.hideLastInterval && !realtime.hideQuickInterval) {
realtime.realtimeType = RealtimeWindowType.INTERVAL;
}
if (realtime.hideQuickInterval && !realtime.hideLastInterval) {
realtime.realtimeType = RealtimeWindowType.LAST_INTERVAL;
}
if (history.hideLastInterval) {
if (!history.hideFixedInterval) {
history.historyType = HistoryWindowType.FIXED;
} else if (!history.hideQuickInterval) {
history.historyType = HistoryWindowType.INTERVAL;
}
}
if (history.hideFixedInterval) {
if (!history.hideLastInterval) {
history.historyType = HistoryWindowType.LAST_INTERVAL;
} else if (!history.hideQuickInterval) {
history.historyType = HistoryWindowType.INTERVAL;
}
}
if (history.hideQuickInterval) {
if (!history.hideLastInterval) {
history.historyType = HistoryWindowType.LAST_INTERVAL;
} else if (!history.hideFixedInterval) {
history.historyType = HistoryWindowType.FIXED;
}
}
}
2019-08-12 19:34:23 +03:00
this.timewindowForm = this.fb.group({
selectedTab: [isDefined(this.timewindow.selectedTab) ? this.timewindow.selectedTab : TimewindowType.REALTIME],
2023-02-07 17:45:03 +02:00
realtime: this.fb.group({
realtimeType: [{
2024-08-19 18:35:37 +03:00
value: isDefined(realtime?.realtimeType) ? realtime.realtimeType : RealtimeWindowType.LAST_INTERVAL,
disabled: realtime.hideInterval
2023-02-07 17:45:03 +02:00
}],
timewindowMs: [{
2024-08-19 18:35:37 +03:00
value: isDefined(realtime?.timewindowMs) ? realtime.timewindowMs : null,
disabled: realtime.hideInterval || realtime.hideLastInterval
2023-02-07 17:45:03 +02:00
}],
interval: [{
2024-08-19 18:35:37 +03:00
value:isDefined(realtime?.interval) ? realtime.interval : null,
disabled: hideAggInterval
}],
2023-02-07 17:45:03 +02:00
quickInterval: [{
2024-08-19 18:35:37 +03:00
value: isDefined(realtime?.quickInterval) ? realtime.quickInterval : null,
disabled: realtime.hideInterval || realtime.hideQuickInterval
2023-02-07 17:45:03 +02:00
}]
}),
history: this.fb.group({
historyType: [{
2024-08-19 18:35:37 +03:00
value: isDefined(history?.historyType) ? history.historyType : HistoryWindowType.LAST_INTERVAL,
disabled: history.hideInterval
2023-02-07 17:45:03 +02:00
}],
timewindowMs: [{
2024-08-19 18:35:37 +03:00
value: isDefined(history?.timewindowMs) ? history.timewindowMs : null,
disabled: history.hideInterval || history.hideLastInterval
2023-02-07 17:45:03 +02:00
}],
interval: [{
2024-08-19 18:35:37 +03:00
value:isDefined(history?.interval) ? history.interval : null,
disabled: hideAggInterval
}],
2023-02-07 17:45:03 +02:00
fixedTimewindow: [{
2024-08-19 18:35:37 +03:00
value: isDefined(history?.fixedTimewindow) ? history.fixedTimewindow : null,
disabled: history.hideInterval || history.hideFixedInterval
2023-02-07 17:45:03 +02:00
}],
quickInterval: [{
2024-08-19 18:35:37 +03:00
value: isDefined(history?.quickInterval) ? history.quickInterval : null,
disabled: history.hideInterval || history.hideQuickInterval
2023-02-07 17:45:03 +02:00
}]
}),
aggregation: this.fb.group({
type: [{
2024-08-19 18:35:37 +03:00
value: isDefined(aggregation?.type) ? aggregation.type : null,
2023-02-07 17:45:03 +02:00
disabled: hideAggregation
}],
limit: [{
value: isDefined(aggregation?.limit) ? aggregation.limit : null,
2023-02-07 17:45:03 +02:00
disabled: hideAggInterval
}, []]
}),
timezone: [{
value: isDefined(this.timewindow.timezone) ? this.timewindow.timezone : null,
disabled: hideTimezone
}]
2019-08-12 19:34:23 +03:00
});
this.updateValidators(this.timewindowForm.get('aggregation.type').value);
this.timewindowForm.get('aggregation.type').valueChanges.pipe(
takeUntil(this.destroy$)
).subscribe((aggregationType: AggregationType) => {
this.updateValidators(aggregationType);
});
this.timewindowForm.get('selectedTab').valueChanges.pipe(
takeUntil(this.destroy$)
).subscribe((selectedTab: TimewindowType) => {
this.onTimewindowTypeChange(selectedTab);
});
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
private updateValidators(aggType: AggregationType) {
if (aggType !== AggregationType.NONE) {
this.timewindowForm.get('aggregation.limit').clearValidators();
} else {
this.timewindowForm.get('aggregation.limit').setValidators([Validators.min(this.minDatapointsLimit()),
Validators.max(this.maxDatapointsLimit())]);
}
this.timewindowForm.get('aggregation.limit').updateValueAndValidity({emitEvent: false});
2019-08-12 19:34:23 +03:00
}
private onTimewindowTypeChange(selectedTab: TimewindowType) {
const timewindowFormValue = this.timewindowForm.getRawValue();
if (selectedTab === TimewindowType.REALTIME) {
if (timewindowFormValue.history.historyType !== HistoryWindowType.FIXED
&& !((this.quickIntervalOnly || this.timewindow.realtime.hideLastInterval)
&& timewindowFormValue.history.historyType === HistoryWindowType.LAST_INTERVAL)
&& !(this.timewindow.realtime.hideQuickInterval && timewindowFormValue.history.historyType === HistoryWindowType.INTERVAL)) {
this.timewindowForm.get('realtime').patchValue({
realtimeType: Object.keys(RealtimeWindowType).includes(HistoryWindowType[timewindowFormValue.history.historyType]) ?
RealtimeWindowType[HistoryWindowType[timewindowFormValue.history.historyType]] :
timewindowFormValue.realtime.realtimeType,
timewindowMs: timewindowFormValue.history.timewindowMs,
quickInterval: timewindowFormValue.history.quickInterval.startsWith('CURRENT') ?
timewindowFormValue.history.quickInterval : timewindowFormValue.realtime.quickInterval
});
setTimeout(() => this.timewindowForm.get('realtime.interval').patchValue(timewindowFormValue.history.interval));
}
} else if (!(this.timewindow.history.hideLastInterval && timewindowFormValue.realtime.realtimeType === RealtimeWindowType.LAST_INTERVAL)
&& !(this.timewindow.history.hideQuickInterval && timewindowFormValue.realtime.realtimeType === RealtimeWindowType.INTERVAL)) {
this.timewindowForm.get('history').patchValue({
historyType: HistoryWindowType[RealtimeWindowType[timewindowFormValue.realtime.realtimeType]],
timewindowMs: timewindowFormValue.realtime.timewindowMs,
quickInterval: timewindowFormValue.realtime.quickInterval
});
setTimeout(() => this.timewindowForm.get('history.interval').patchValue(timewindowFormValue.realtime.interval));
}
this.timewindowForm.patchValue({
aggregation: {
type: timewindowFormValue.aggregation.type,
limit: timewindowFormValue.aggregation.limit
},
timezone: timewindowFormValue.timezone
});
}
2019-08-12 19:34:23 +03:00
update() {
this.prepareTimewindowConfig();
this.result = this.timewindow;
this.overlayRef.dispose();
}
private prepareTimewindowConfig() {
2020-02-25 19:11:25 +02:00
const timewindowFormValue = this.timewindowForm.getRawValue();
this.timewindow.selectedTab = timewindowFormValue.selectedTab;
2024-08-16 18:38:35 +03:00
this.timewindow.realtime = {...this.timewindow.realtime, ...{
realtimeType: timewindowFormValue.realtime.realtimeType,
timewindowMs: timewindowFormValue.realtime.timewindowMs,
quickInterval: timewindowFormValue.realtime.quickInterval,
interval: timewindowFormValue.realtime.interval
}};
this.timewindow.history = {...this.timewindow.history, ...{
historyType: timewindowFormValue.history.historyType,
timewindowMs: timewindowFormValue.history.timewindowMs,
interval: timewindowFormValue.history.interval,
fixedTimewindow: timewindowFormValue.history.fixedTimewindow,
quickInterval: timewindowFormValue.history.quickInterval,
}};
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
}
2021-03-17 18:38:57 +02:00
if (this.timezone) {
this.timewindow.timezone = timewindowFormValue.timezone;
}
2019-08-12 19:34:23 +03:00
}
private updateTimewindowForm() {
this.timewindowForm.patchValue(this.timewindow);
2024-08-19 18:35:37 +03:00
if (this.timewindow.realtime.hideInterval) {
this.timewindowForm.get('realtime.realtimeType').disable({emitEvent: false});
this.timewindowForm.get('realtime.timewindowMs').disable({emitEvent: false});
this.timewindowForm.get('realtime.quickInterval').disable({emitEvent: false});
} else {
this.timewindowForm.get('realtime.realtimeType').enable({emitEvent: false});
if (this.timewindow.realtime.hideLastInterval) {
this.timewindowForm.get('realtime.timewindowMs').disable({emitEvent: false});
} else {
this.timewindowForm.get('realtime.timewindowMs').enable({emitEvent: false});
}
if (this.timewindow.realtime.hideQuickInterval) {
this.timewindowForm.get('realtime.quickInterval').disable({emitEvent: false});
} else {
this.timewindowForm.get('realtime.quickInterval').enable({emitEvent: false});
}
}
if (this.timewindow.history.hideInterval) {
this.timewindowForm.get('history.historyType').disable({emitEvent: false});
this.timewindowForm.get('history.timewindowMs').disable({emitEvent: false});
this.timewindowForm.get('history.fixedTimewindow').disable({emitEvent: false});
this.timewindowForm.get('history.quickInterval').disable({emitEvent: false});
} else {
this.timewindowForm.get('history.historyType').enable({emitEvent: false});
if (this.timewindow.history.hideLastInterval) {
this.timewindowForm.get('history.timewindowMs').disable({emitEvent: false});
} else {
this.timewindowForm.get('history.timewindowMs').enable({emitEvent: false});
}
if (this.timewindow.history.hideFixedInterval) {
this.timewindowForm.get('history.fixedTimewindow').disable({emitEvent: false});
} else {
this.timewindowForm.get('history.fixedTimewindow').enable({emitEvent: false});
}
if (this.timewindow.history.hideQuickInterval) {
this.timewindowForm.get('history.quickInterval').disable({emitEvent: false});
} else {
this.timewindowForm.get('history.quickInterval').enable({emitEvent: false});
}
}
if (this.timewindow.hideAggregation) {
this.timewindowForm.get('aggregation.type').disable({emitEvent: false});
} else {
this.timewindowForm.get('aggregation.type').enable({emitEvent: false});
}
if (this.timewindow.hideAggInterval) {
this.timewindowForm.get('aggregation.limit').disable({emitEvent: false});
this.timewindowForm.get('realtime.interval').disable({emitEvent: false});
this.timewindowForm.get('history.interval').disable({emitEvent: false});
} else {
this.timewindowForm.get('aggregation.limit').enable({emitEvent: false});
this.timewindowForm.get('realtime.interval').enable({emitEvent: false});
this.timewindowForm.get('history.interval').enable({emitEvent: false});
}
if (this.timewindow.hideTimezone) {
this.timewindowForm.get('timezone').disable({emitEvent: false});
} else {
this.timewindowForm.get('timezone').enable({emitEvent: false});
}
this.timewindowForm.markAsDirty();
}
2023-02-09 12:19:40 +02:00
cancel() {
this.overlayRef.dispose();
2019-08-12 19:34:23 +03:00
}
minDatapointsLimit() {
return this.timeService.getMinDatapointsLimit();
}
maxDatapointsLimit() {
return this.timeService.getMaxDatapointsLimit();
}
minRealtimeAggInterval() {
return this.timeService.minIntervalLimit(this.currentRealtimeTimewindow());
2019-08-12 19:34:23 +03:00
}
maxRealtimeAggInterval() {
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;
} 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
}
}
openTimewindowConfig() {
this.prepareTimewindowConfig();
this.dialog.open<TimewindowConfigDialogComponent, TimewindowConfigDialogData, Timewindow>(
TimewindowConfigDialogComponent, {
autoFocus: false,
disableClose: true,
panelClass: ['tb-dialog', 'tb-fullscreen-dialog'],
data: {
quickIntervalOnly: this.quickIntervalOnly,
aggregation: this.aggregation,
timewindow: deepClone(this.timewindow)
}
}).afterClosed()
.subscribe((res) => {
if (res) {
this.timewindow = res;
this.updateTimewindowForm();
}
});
}
2019-08-12 19:34:23 +03:00
}