thingsboard/ui-ngx/src/app/modules/home/components/debug-settings/debug-settings-panel.component.ts

135 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-11-12 17:13:58 +02:00
///
/// Copyright © 2016-2024 The Thingsboard Authors
///
/// 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.
///
2024-11-12 17:19:33 +02:00
import {
2024-11-29 15:59:36 +02:00
booleanAttribute,
2024-11-12 17:19:33 +02:00
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
EventEmitter,
Input,
OnInit
} from '@angular/core';
2024-11-12 17:13:58 +02:00
import { PageComponent } from '@shared/components/page.component';
import { TbPopoverComponent } from '@shared/components/popover.component';
import { FormBuilder } from '@angular/forms';
2024-11-12 17:13:58 +02:00
import { CommonModule } from '@angular/common';
import { SharedModule } from '@shared/shared.module';
import { SECOND } from '@shared/models/time/time.models';
2024-11-13 14:50:23 +02:00
import { DurationLeftPipe } from '@shared/pipe/duration-left.pipe';
2024-11-29 19:36:11 +02:00
import { of, shareReplay, timer } from 'rxjs';
2024-11-12 17:13:58 +02:00
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { DebugSettings } from '@shared/models/entity.models';
import { distinctUntilChanged, map, startWith, switchMap, takeWhile } from 'rxjs/operators';
2024-11-12 17:13:58 +02:00
@Component({
selector: 'tb-debug-settings-panel',
templateUrl: './debug-settings-panel.component.html',
2024-11-12 17:13:58 +02:00
standalone: true,
imports: [
SharedModule,
CommonModule,
2024-11-13 14:50:23 +02:00
DurationLeftPipe
2024-11-12 17:19:33 +02:00
],
changeDetection: ChangeDetectionStrategy.OnPush
2024-11-12 17:13:58 +02:00
})
export class DebugSettingsPanelComponent extends PageComponent implements OnInit {
2024-11-12 17:13:58 +02:00
@Input() popover: TbPopoverComponent<DebugSettingsPanelComponent>;
2024-11-29 15:59:36 +02:00
@Input({ transform: booleanAttribute }) failuresEnabled = false;
@Input({ transform: booleanAttribute }) allEnabled = false;
@Input() allEnabledUntil = 0;
2024-11-13 15:09:29 +02:00
@Input() maxDebugModeDurationMinutes: number;
@Input() debugLimitsConfiguration: string;
2024-11-12 17:13:58 +02:00
2024-11-13 14:50:23 +02:00
onFailuresControl = this.fb.control(false);
debugAllControl = this.fb.control(false);
2024-11-12 17:13:58 +02:00
2024-11-13 14:50:23 +02:00
maxMessagesCount: string;
maxTimeFrameSec: string;
initialAllEnabled: boolean;
2024-11-12 17:13:58 +02:00
2024-11-29 19:36:11 +02:00
isDebugAllActive$ = this.debugAllControl.valueChanges.pipe(
startWith(this.debugAllControl.value),
switchMap(value => {
if (value) {
return of(true);
} else {
return timer(0, SECOND).pipe(
map(() => this.allEnabledUntil > new Date().getTime()),
takeWhile(value => value, true)
);
2024-11-29 19:36:11 +02:00
}
2024-11-13 14:50:23 +02:00
}),
takeUntilDestroyed(),
2024-11-13 15:09:29 +02:00
shareReplay(1),
2024-11-13 14:50:23 +02:00
);
2024-11-12 17:13:58 +02:00
onSettingsApplied = new EventEmitter<DebugSettings>();
2024-11-12 17:13:58 +02:00
constructor(private fb: FormBuilder,
private cd: ChangeDetectorRef) {
2024-11-13 14:50:23 +02:00
super();
2024-11-12 17:19:33 +02:00
this.debugAllControl.valueChanges.pipe(
takeUntilDestroyed()
).subscribe(value => {
this.allEnabled = value;
this.cd.markForCheck();
});
this.isDebugAllActive$.pipe(
distinctUntilChanged(),
takeUntilDestroyed()
).subscribe(isDebugOn => this.debugAllControl.patchValue(isDebugOn, {emitEvent: false}))
2024-11-12 17:13:58 +02:00
}
ngOnInit(): void {
2024-11-13 15:09:29 +02:00
this.maxMessagesCount = this.debugLimitsConfiguration?.split(':')[0];
this.maxTimeFrameSec = this.debugLimitsConfiguration?.split(':')[1];
this.onFailuresControl.patchValue(this.failuresEnabled);
this.debugAllControl.patchValue(this.allEnabled);
this.initialAllEnabled = this.allEnabled || this.allEnabledUntil > new Date().getTime();
2024-11-12 17:13:58 +02:00
}
2024-11-12 18:19:17 +02:00
onCancel(): void {
2024-11-12 17:13:58 +02:00
this.popover?.hide();
}
onApply(): void {
const isDebugAllChanged = this.initialAllEnabled !== this.debugAllControl.value || this.initialAllEnabled !== this.allEnabledUntil > new Date().getTime();
if (isDebugAllChanged) {
this.onSettingsApplied.emit({
allEnabled: this.allEnabled,
failuresEnabled: this.onFailuresControl.value,
allEnabledUntil: 0,
});
} else {
this.onSettingsApplied.emit({
allEnabled: false,
failuresEnabled: this.onFailuresControl.value,
2024-12-02 18:48:46 +02:00
allEnabledUntil: this.allEnabledUntil,
});
}
2024-11-12 17:13:58 +02:00
}
onReset(): void {
2024-11-29 19:36:11 +02:00
this.debugAllControl.patchValue(true);
this.allEnabledUntil = 0;
2024-11-15 12:19:49 +02:00
this.cd.markForCheck();
2024-11-12 17:13:58 +02:00
}
}