thingsboard/ui-ngx/src/app/modules/home/components/queue/queue-form.component.ts

198 lines
6.1 KiB
TypeScript
Raw Normal View History

2022-04-29 19:18:11 +03:00
///
/// Copyright © 2016-2022 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.
///
2022-05-05 11:02:45 +03:00
import { Component, forwardRef, Input, OnInit } from '@angular/core';
2022-04-29 19:18:11 +03:00
import {
ControlValueAccessor,
FormBuilder,
FormControl,
FormGroup,
NG_VALIDATORS,
NG_VALUE_ACCESSOR,
Validator,
Validators
} from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { UtilsService } from '@core/services/utils.service';
2022-05-05 11:02:45 +03:00
import { QueueInfo, QueueProcessingStrategyTypes, QueueSubmitStrategyTypes } from '@shared/models/queue.models';
import { isDefinedAndNotNull } from '@core/utils';
2022-04-29 19:18:11 +03:00
@Component({
2022-05-05 11:02:45 +03:00
selector: 'tb-queue-form',
templateUrl: './queue-form.component.html',
styleUrls: ['./queue-form.component.scss'],
2022-04-29 19:18:11 +03:00
providers: [
{
provide: NG_VALUE_ACCESSOR,
2022-05-05 11:02:45 +03:00
useExisting: forwardRef(() => QueueFormComponent),
2022-04-29 19:18:11 +03:00
multi: true
},
{
provide: NG_VALIDATORS,
2022-05-05 11:02:45 +03:00
useExisting: forwardRef(() => QueueFormComponent),
2022-04-29 19:18:11 +03:00
multi: true,
}
]
})
2022-05-05 11:02:45 +03:00
export class QueueFormComponent implements ControlValueAccessor, OnInit, Validator {
2022-04-29 19:18:11 +03:00
@Input()
disabled: boolean;
@Input()
2022-05-05 11:02:45 +03:00
newQueue = false;
2022-04-29 19:18:11 +03:00
@Input()
2022-05-05 11:02:45 +03:00
systemQueue = false;
2022-04-29 19:18:11 +03:00
2022-05-05 11:02:45 +03:00
private modelValue: QueueInfo;
2022-04-29 19:18:11 +03:00
queueFormGroup: FormGroup;
submitStrategies: string[] = [];
processingStrategies: string[] = [];
hideBatchSize = false;
private propagateChange = null;
private propagateChangePending = false;
constructor(private dialog: MatDialog,
private utils: UtilsService,
private fb: FormBuilder) {
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
if (this.propagateChangePending) {
this.propagateChangePending = false;
setTimeout(() => {
this.propagateChange(this.modelValue);
}, 0);
}
}
registerOnTouched(fn: any): void {
}
ngOnInit() {
this.submitStrategies = Object.values(QueueSubmitStrategyTypes);
this.processingStrategies = Object.values(QueueProcessingStrategyTypes);
this.queueFormGroup = this.fb.group(
{
name: ['', [Validators.required]],
pollInterval: [25, [Validators.min(1), Validators.required]],
partitions: [10, [Validators.min(1), Validators.required]],
consumerPerPartition: [false, []],
packProcessingTimeout: [2000, [Validators.min(1), Validators.required]],
submitStrategy: this.fb.group({
type: [null, [Validators.required]],
2022-05-05 11:02:45 +03:00
batchSize: [null],
2022-04-29 19:18:11 +03:00
}),
processingStrategy: this.fb.group({
type: [null, [Validators.required]],
retries: [3, [Validators.min(0), Validators.required]],
failurePercentage: [ 0, [Validators.min(0), Validators.required, Validators.max(100)]],
pauseBetweenRetries: [3, [Validators.min(1), Validators.required]],
maxPauseBetweenRetries: [3, [Validators.min(1), Validators.required]],
}),
topic: ['']
});
this.queueFormGroup.valueChanges.subscribe(() => {
this.updateModel();
});
this.queueFormGroup.get('name').valueChanges.subscribe((value) => this.queueFormGroup.patchValue({topic: `tb_rule_engine.${value}`}));
this.queueFormGroup.get('submitStrategy').get('type').valueChanges.subscribe(() => {
this.submitStrategyTypeChanged();
});
if (this.newQueue) {
this.queueFormGroup.get('name').enable({emitEvent: false});
} else {
this.queueFormGroup.get('name').disable({emitEvent: false});
}
}
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
if (this.disabled) {
this.queueFormGroup.disable({emitEvent: false});
} else {
this.queueFormGroup.enable({emitEvent: false});
this.queueFormGroup.get('name').disable({emitEvent: false});
}
}
2022-05-05 11:02:45 +03:00
writeValue(value: QueueInfo): void {
2022-04-29 19:18:11 +03:00
this.propagateChangePending = false;
this.modelValue = value;
2022-05-05 11:02:45 +03:00
if (isDefinedAndNotNull(this.modelValue)) {
this.queueFormGroup.patchValue(this.modelValue, {emitEvent: false});
2022-04-29 19:18:11 +03:00
}
2022-05-05 11:02:45 +03:00
this.submitStrategyTypeChanged();
2022-04-29 19:18:11 +03:00
if (!this.disabled && !this.queueFormGroup.valid) {
this.updateModel();
}
}
public validate(c: FormControl) {
2022-05-05 11:02:45 +03:00
if (c.parent && !this.systemQueue) {
2022-04-29 19:18:11 +03:00
const queueName = c.value.name;
const profileQueues = [];
c.parent.getRawValue().forEach((queue) => {
profileQueues.push(queue.name);
}
);
if (profileQueues.filter(profileQueue => profileQueue === queueName).length > 1) {
this.queueFormGroup.get('name').setErrors({
unique: true
});
}
}
return (this.queueFormGroup.valid) ? null : {
queue: {
valid: false,
},
};
}
private updateModel() {
const value = this.queueFormGroup.value;
this.modelValue = {...this.modelValue, ...value};
if (this.propagateChange) {
this.propagateChange(this.modelValue);
} else {
this.propagateChangePending = true;
}
}
submitStrategyTypeChanged() {
const form = this.queueFormGroup.get('submitStrategy') as FormGroup;
const type: QueueSubmitStrategyTypes = form.get('type').value;
const batchSizeField = form.get('batchSize');
if (type === QueueSubmitStrategyTypes.BATCH) {
2022-05-05 11:02:45 +03:00
batchSizeField.enable({emitEvent: false});
batchSizeField.patchValue(1000, {emitEvent: false});
batchSizeField.setValidators([Validators.min(1), Validators.required]);
2022-04-29 19:18:11 +03:00
this.hideBatchSize = true;
} else {
2022-05-05 11:02:45 +03:00
batchSizeField.patchValue(null, {emitEvent: false});
batchSizeField.disable({emitEvent: false});
batchSizeField.clearValidators();
2022-04-29 19:18:11 +03:00
this.hideBatchSize = false;
}
}
}