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

246 lines
8.3 KiB
TypeScript
Raw Normal View History

2019-08-12 19:34:23 +03:00
///
2020-02-20 10:26:43 +02:00
/// Copyright © 2016-2020 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,
ViewChild,
ViewContainerRef
} from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { MillisecondsToTimeStringPipe } from '@shared/pipe/milliseconds-to-time-string.pipe';
import {
Aggregation,
aggregationTranslations,
AggregationType,
HistoryWindow,
HistoryWindowType,
IntervalWindow,
Timewindow,
TimewindowType
} from '@shared/models/time/time.models';
import { DatePipe } from '@angular/common';
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
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() {
return this.timeService.minIntervalLimit(this.timewindowForm.get('realtime').get('timewindowMs').value);
}
maxRealtimeAggInterval() {
return this.timeService.maxIntervalLimit(this.timewindowForm.get('realtime').get('timewindowMs').value);
}
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 {
return timewindowFormValue.history.fixedTimewindow.endTimeMs -
timewindowFormValue.history.fixedTimewindow.startTimeMs;
}
}
2020-02-25 19:11:25 +02:00
onHideIntervalChanged() {
if (this.timewindow.hideInterval) {
this.timewindowForm.get('history').get('historyType').disable({emitEvent: false});
this.timewindowForm.get('history').get('timewindowMs').disable({emitEvent: false});
this.timewindowForm.get('history').get('fixedTimewindow').disable({emitEvent: false});
} else {
this.timewindowForm.get('history').get('historyType').enable({emitEvent: false});
this.timewindowForm.get('history').get('timewindowMs').enable({emitEvent: false});
this.timewindowForm.get('history').get('fixedTimewindow').enable({emitEvent: false});
}
this.timewindowForm.markAsDirty();
}
onHideAggregationChanged() {
if (this.timewindow.hideAggregation) {
this.timewindowForm.get('aggregation').get('type').disable({emitEvent: false});
} else {
this.timewindowForm.get('aggregation').get('type').enable({emitEvent: false});
}
this.timewindowForm.markAsDirty();
}
onHideAggIntervalChanged() {
if (this.timewindow.hideAggInterval) {
this.timewindowForm.get('aggregation').get('limit').disable({emitEvent: false});
} else {
this.timewindowForm.get('aggregation').get('limit').enable({emitEvent: false});
}
this.timewindowForm.markAsDirty();
}
2019-08-12 19:34:23 +03:00
}