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

113 lines
3.5 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 {
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';
2024-11-13 14:50:23 +02:00
import { UntypedFormBuilder } from '@angular/forms';
2024-11-12 17:13:58 +02:00
import { CommonModule } from '@angular/common';
import { SharedModule } from '@shared/shared.module';
import { MINUTE, 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-13 15:09:29 +02:00
import { shareReplay, timer } from 'rxjs';
2024-11-12 17:13:58 +02:00
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
2024-11-13 14:50:23 +02:00
import { HasDebugConfig } from '@shared/models/entity.models';
import { distinctUntilChanged, map, tap } from 'rxjs/operators';
2024-11-12 17:13:58 +02:00
@Component({
selector: 'tb-debug-config-panel',
templateUrl: './debug-config-panel.component.html',
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 DebugConfigPanelComponent extends PageComponent implements OnInit {
@Input() popover: TbPopoverComponent<DebugConfigPanelComponent>;
@Input() debugFailures = false;
@Input() debugAll = false;
@Input() debugAllUntil = 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;
2024-11-12 17:13:58 +02:00
2024-11-13 14:50:23 +02:00
isDebugAllActive$ = timer(0, SECOND).pipe(
map(() => {
this.cd.markForCheck();
2024-11-13 15:09:29 +02:00
return this.debugAllUntil > new Date().getTime();
2024-11-13 14:50:23 +02:00
}),
distinctUntilChanged(),
2024-11-13 15:09:29 +02:00
tap(isDebugOn => this.debugAllControl.patchValue(isDebugOn, { emitEvent: false })),
shareReplay(1),
2024-11-13 14:50:23 +02:00
);
2024-11-12 17:13:58 +02:00
2024-11-13 14:50:23 +02:00
onConfigApplied = new EventEmitter<HasDebugConfig>();
2024-11-12 17:13:58 +02:00
2024-11-13 14:50:23 +02:00
constructor(private fb: UntypedFormBuilder, private cd: ChangeDetectorRef) {
super();
2024-11-12 17:19:33 +02:00
this.observeDebugAllChange();
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];
2024-11-12 17:13:58 +02:00
this.onFailuresControl.patchValue(this.debugFailures);
}
2024-11-12 18:19:17 +02:00
onCancel(): void {
2024-11-12 17:13:58 +02:00
this.popover?.hide();
}
onApply(): void {
this.onConfigApplied.emit({
debugAll: this.debugAll,
debugFailures: this.onFailuresControl.value,
debugAllUntil: this.debugAllUntil
});
}
onReset(): void {
this.debugAll = true;
2024-11-13 15:09:29 +02:00
this.debugAllUntil = new Date().getTime() + this.maxDebugModeDurationMinutes * MINUTE;
2024-11-15 12:19:49 +02:00
this.cd.markForCheck();
2024-11-12 17:13:58 +02:00
}
2024-11-12 17:19:33 +02:00
private observeDebugAllChange(): void {
this.debugAllControl.valueChanges.pipe(takeUntilDestroyed()).subscribe(value => {
2024-11-13 15:09:29 +02:00
this.debugAllUntil = value? new Date().getTime() + this.maxDebugModeDurationMinutes * MINUTE : 0;
2024-11-12 17:19:33 +02:00
this.debugAll = value;
2024-11-15 12:19:49 +02:00
this.cd.markForCheck();
2024-11-12 17:19:33 +02:00
});
}
2024-11-12 17:13:58 +02:00
}