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-06-01 19:14:09 +03:00
|
|
|
import { Component, forwardRef, Input, OnDestroy, 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 { UtilsService } from '@core/services/utils.service';
|
2022-06-01 19:14:09 +03:00
|
|
|
import {
|
|
|
|
|
QueueInfo,
|
|
|
|
|
QueueProcessingStrategyTypes,
|
|
|
|
|
QueueProcessingStrategyTypesMap,
|
|
|
|
|
QueueSubmitStrategyTypes,
|
|
|
|
|
QueueSubmitStrategyTypesMap
|
|
|
|
|
} from '@shared/models/queue.models';
|
2022-05-05 11:02:45 +03:00
|
|
|
import { isDefinedAndNotNull } from '@core/utils';
|
2022-05-25 18:45:00 +03:00
|
|
|
import { Subscription } from 'rxjs';
|
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-25 18:45:00 +03:00
|
|
|
export class QueueFormComponent implements ControlValueAccessor, OnInit, OnDestroy, 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-25 18:45:00 +03:00
|
|
|
queueFormGroup: FormGroup;
|
2022-04-29 19:18:11 +03:00
|
|
|
hideBatchSize = false;
|
|
|
|
|
|
2022-06-01 19:14:09 +03:00
|
|
|
queueSubmitStrategyTypes = QueueSubmitStrategyTypes;
|
|
|
|
|
queueProcessingStrategyTypes = QueueProcessingStrategyTypes;
|
|
|
|
|
submitStrategies: string[] = Object.values(this.queueSubmitStrategyTypes);
|
|
|
|
|
processingStrategies: string[] = Object.values(this.queueProcessingStrategyTypes);
|
|
|
|
|
queueSubmitStrategyTypesMap = QueueSubmitStrategyTypesMap;
|
|
|
|
|
queueProcessingStrategyTypesMap = QueueProcessingStrategyTypesMap;
|
|
|
|
|
|
2022-05-25 18:45:00 +03:00
|
|
|
private modelValue: QueueInfo;
|
2022-04-29 19:18:11 +03:00
|
|
|
private propagateChange = null;
|
|
|
|
|
private propagateChangePending = false;
|
2022-05-25 18:45:00 +03:00
|
|
|
private valueChange$: Subscription = null;
|
2022-04-29 19:18:11 +03:00
|
|
|
|
2022-06-01 19:14:09 +03:00
|
|
|
constructor(private utils: UtilsService,
|
2022-04-29 19:18:11 +03:00
|
|
|
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.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]],
|
|
|
|
|
}),
|
2022-05-11 14:19:00 +03:00
|
|
|
topic: [''],
|
|
|
|
|
additionalInfo: this.fb.group({
|
|
|
|
|
description: ['']
|
|
|
|
|
})
|
2022-04-29 19:18:11 +03:00
|
|
|
});
|
2022-05-25 18:45:00 +03:00
|
|
|
this.valueChange$ = this.queueFormGroup.valueChanges.subscribe(() => {
|
2022-04-29 19:18:11 +03:00
|
|
|
this.updateModel();
|
|
|
|
|
});
|
2022-05-25 18:45:00 +03:00
|
|
|
this.queueFormGroup.get('name').valueChanges.subscribe((value) => {
|
|
|
|
|
this.queueFormGroup.patchValue({topic: `tb_rule_engine.${value}`});
|
|
|
|
|
});
|
2022-04-29 19:18:11 +03:00
|
|
|
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});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 18:45:00 +03:00
|
|
|
ngOnDestroy() {
|
|
|
|
|
if (this.valueChange$) {
|
|
|
|
|
this.valueChange$.unsubscribe();
|
|
|
|
|
this.valueChange$ = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 19:18:11 +03:00
|
|
|
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-06-01 19:14:09 +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.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.clearValidators();
|
2022-04-29 19:18:11 +03:00
|
|
|
this.hideBatchSize = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|