Changed Calculated fields highlights and autocompletes

This commit is contained in:
mpetrov 2025-03-07 12:27:58 +02:00
parent e878fb7fe2
commit a76149d60d
4 changed files with 88 additions and 25 deletions

View File

@ -36,14 +36,20 @@
.tb-js-func { .tb-js-func {
.ace_tb { .ace_tb {
&.ace_calculated-field { &.ace_calculated-field {
&-key { &-ctx {
color: #C52F00; color: #C52F00;
} }
&-ts, &-time-window, &-values, &-value, &-func { &-args {
color: #185F2A;
}
&-key {
color: #c24c1a;
}
&-time-window, &-values, &-func, &-value, &-ts {
color: #7214D0; color: #7214D0;
} }
&-start-ts, &-end-ts, &-limit { &-start-ts, &-end-ts {
color: #185F2A; color: #2CAA00;
} }
} }
} }

View File

@ -70,7 +70,7 @@ export class CalculatedFieldDialogComponent extends DialogComponent<CalculatedFi
functionArgs$ = this.configFormGroup.get('arguments').valueChanges functionArgs$ = this.configFormGroup.get('arguments').valueChanges
.pipe( .pipe(
startWith(this.data.value?.configuration?.arguments ?? {}), startWith(this.data.value?.configuration?.arguments ?? {}),
map(argumentsObj => Object.keys(argumentsObj)) map(argumentsObj => ['ctx', ...Object.keys(argumentsObj)])
); );
argumentsEditorCompleter$ = this.configFormGroup.get('arguments').valueChanges argumentsEditorCompleter$ = this.configFormGroup.get('arguments').valueChanges

View File

@ -64,7 +64,7 @@ export class CalculatedFieldArgumentPanelComponent implements OnInit {
readonly defaultLimit = Math.floor(this.maxDataPointsPerRollingArg / 10); readonly defaultLimit = Math.floor(this.maxDataPointsPerRollingArg / 10);
argumentFormGroup = this.fb.group({ argumentFormGroup = this.fb.group({
argumentName: ['', [Validators.required, this.uniqNameRequired(), Validators.pattern(charsWithNumRegex), Validators.maxLength(255)]], argumentName: ['', [Validators.required, this.uniqNameRequired(), Validators.pattern(/^(?!ctx$).+$/), Validators.pattern(charsWithNumRegex), Validators.maxLength(255)]],
refEntityId: this.fb.group({ refEntityId: this.fb.group({
entityType: [ArgumentEntityType.Current], entityType: [ArgumentEntityType.Current],
id: [''] id: ['']

View File

@ -235,10 +235,10 @@ export type CalculatedFieldArgumentEventValue<ValueType = unknown> = CalculatedF
export type CalculatedFieldEventArguments<ValueType = unknown> = Record<string, CalculatedFieldArgumentEventValue<ValueType>>; export type CalculatedFieldEventArguments<ValueType = unknown> = Record<string, CalculatedFieldArgumentEventValue<ValueType>>;
export const CalculatedFieldLatestTelemetryArgumentAutocomplete = { export const CalculatedFieldCtxLatestTelemetryArgumentAutocomplete = {
meta: 'object', meta: 'object',
type: '{ ts: number; value: any; }', type: '{ ts: number; value: any; }',
description: 'Calculated field latest telemetry value argument.', description: 'Calculated field context latest telemetry value argument.',
children: { children: {
ts: { ts: {
meta: 'number', meta: 'number',
@ -253,10 +253,10 @@ export const CalculatedFieldLatestTelemetryArgumentAutocomplete = {
}, },
}; };
export const CalculatedFieldAttributeValueArgumentAutocomplete = { export const CalculatedFieldCtxAttributeValueArgumentAutocomplete = {
meta: 'object', meta: 'object',
type: '{ ts: number; value: any; }', type: '{ ts: number; value: any; }',
description: 'Calculated field attribute value argument.', description: 'Calculated field context attribute value argument.',
children: { children: {
ts: { ts: {
meta: 'number', meta: 'number',
@ -270,6 +270,19 @@ export const CalculatedFieldAttributeValueArgumentAutocomplete = {
} }
}, },
}; };
export const CalculatedFieldLatestTelemetryArgumentAutocomplete = {
meta: 'any',
type: 'any',
description: 'Calculated field latest telemetry argument value.',
};
export const CalculatedFieldAttributeValueArgumentAutocomplete = {
meta: 'any',
type: 'any',
description: 'Calculated field attribute argument value.',
};
export const CalculatedFieldRollingValueArgumentFunctionsAutocomplete = { export const CalculatedFieldRollingValueArgumentFunctionsAutocomplete = {
max: { max: {
meta: 'function', meta: 'function',
@ -513,35 +526,84 @@ export const getCalculatedFieldArgumentsEditorCompleter = (argumentsObj: Record<
switch (argumentsObj[key].refEntityKey.type) { switch (argumentsObj[key].refEntityKey.type) {
case ArgumentType.Attribute: case ArgumentType.Attribute:
acc[key] = CalculatedFieldAttributeValueArgumentAutocomplete; acc[key] = CalculatedFieldAttributeValueArgumentAutocomplete;
acc.ctx.children.args.children[key] = CalculatedFieldCtxAttributeValueArgumentAutocomplete;
break; break;
case ArgumentType.LatestTelemetry: case ArgumentType.LatestTelemetry:
acc[key] = CalculatedFieldLatestTelemetryArgumentAutocomplete; acc[key] = { ...CalculatedFieldLatestTelemetryArgumentAutocomplete, children: {} };
acc.ctx.children.args.children[key] = CalculatedFieldCtxLatestTelemetryArgumentAutocomplete;
break; break;
case ArgumentType.Rolling: case ArgumentType.Rolling:
acc[key] = CalculatedFieldRollingValueArgumentAutocomplete; acc[key] = CalculatedFieldRollingValueArgumentAutocomplete;
acc.ctx.children.args.children[key] = CalculatedFieldRollingValueArgumentAutocomplete;
break; break;
} }
return acc; return acc;
}, {})); }, {
ctx: {
meta: 'object',
type: '{ args: { [key: string]: object } }',
description: 'Calculated field context.',
children: {
args: {
meta: 'object',
type: '{ [key: string]: object }',
description: 'Calculated field context arguments.',
children: {}
}
}
}
}));
} }
export const getCalculatedFieldArgumentsHighlights = ( export const getCalculatedFieldArgumentsHighlights = (
argumentsObj: Record<string, CalculatedFieldArgument> argumentsObj: Record<string, CalculatedFieldArgument>
): AceHighlightRules => { ): AceHighlightRules => {
const calculatedFieldArgumentsKeys = Object.keys(argumentsObj).map(key => ({
token: 'tb.calculated-field-key',
regex: `\\b${key}\\b`,
next: argumentsObj[key].refEntityKey.type === ArgumentType.Rolling
? 'calculatedFieldRollingArgumentValue'
: 'start'
}));
const calculatedFieldCtxArgumentsHighlightRules = {
calculatedFieldCtxArgs: [
dotOperatorHighlightRule,
...calculatedFieldArgumentsKeys.map(argumentRule => argumentRule.next === 'start' ? {...argumentRule, next: 'calculatedFieldSingleArgumentValue' } : argumentRule),
endGroupHighlightRule
]
};
return { return {
start: Object.keys(argumentsObj).map(key => ({ start: [
token: 'tb.calculated-field-key', calculatedFieldArgumentsContextHighlightRules,
regex: `\\b${key}\\b`, ...calculatedFieldArgumentsKeys,
next: argumentsObj[key].refEntityKey.type === ArgumentType.Rolling ],
? 'calculatedFieldRollingArgumentValue' ...calculatedFieldArgumentsContextValueHighlightRules,
: 'calculatedFieldSingleArgumentValue' ...calculatedFieldCtxArgumentsHighlightRules,
})),
...calculatedFieldSingleArgumentValueHighlightRules, ...calculatedFieldSingleArgumentValueHighlightRules,
...calculatedFieldRollingArgumentValueHighlightRules, ...calculatedFieldRollingArgumentValueHighlightRules,
...calculatedFieldTimeWindowArgumentValueHighlightRules ...calculatedFieldTimeWindowArgumentValueHighlightRules
}; };
}; };
const calculatedFieldArgumentsContextHighlightRules: AceHighlightRule = {
token: 'tb.calculated-field-ctx',
regex: /ctx/,
next: 'calculatedFieldCtxValue'
}
const calculatedFieldArgumentsContextValueHighlightRules: AceHighlightRules = {
calculatedFieldCtxValue: [
dotOperatorHighlightRule,
{
token: 'tb.calculated-field-args',
regex: /args/,
next: 'calculatedFieldCtxArgs'
},
endGroupHighlightRule
]
}
const calculatedFieldSingleArgumentValueHighlightRules: AceHighlightRules = { const calculatedFieldSingleArgumentValueHighlightRules: AceHighlightRules = {
calculatedFieldSingleArgumentValue: [ calculatedFieldSingleArgumentValue: [
dotOperatorHighlightRule, dotOperatorHighlightRule,
@ -597,16 +659,11 @@ const calculatedFieldTimeWindowArgumentValueHighlightRules: AceHighlightRules =
regex: /endTs/, regex: /endTs/,
next: 'no_regex' next: 'no_regex'
}, },
{
token: 'tb.calculated-field-limit',
regex: /limit/,
next: 'no_regex'
},
endGroupHighlightRule endGroupHighlightRule
] ]
} }
export const calculatedFieldDefaultScript = 'return {\n' + export const calculatedFieldDefaultScript = 'return {\n' +
' // Convert Fahrenheit to Celsius\n' + ' // Convert Fahrenheit to Celsius\n' +
' "temperatureCelsius": (temperature.value - 32) / 1.8\n' + ' "temperatureCelsius": (temperature - 32) / 1.8\n' +
'};' '};'