Change signature for ctx to be first parameter

This commit is contained in:
Andrii Shvaika 2025-03-06 17:04:18 +02:00
parent 651107be2f
commit e12b0ea8ea
6 changed files with 10 additions and 10 deletions

View File

@ -242,7 +242,7 @@ public class CalculatedFieldEntityMessageProcessor extends AbstractContextAwareM
justRestored = true; justRestored = true;
} }
if (state.isSizeOk()) { if (state.isSizeOk()) {
if (state.updateState(newArgValues, ctx) || justRestored) { if (state.updateState(ctx, newArgValues) || justRestored) {
cfIdList = new ArrayList<>(cfIdList); cfIdList = new ArrayList<>(cfIdList);
cfIdList.add(ctx.getCfId()); cfIdList.add(ctx.getCfId());
processStateIfReady(ctx, cfIdList, state, tbMsgId, tbMsgType, callback); processStateIfReady(ctx, cfIdList, state, tbMsgId, tbMsgType, callback);

View File

@ -125,7 +125,7 @@ public class DefaultCalculatedFieldProcessingService implements CalculatedFieldP
} }
return Futures.whenAllComplete(argFutures.values()).call(() -> { return Futures.whenAllComplete(argFutures.values()).call(() -> {
var result = createStateByType(ctx); var result = createStateByType(ctx);
result.updateState(argFutures.entrySet().stream() result.updateState(ctx, argFutures.entrySet().stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(
Entry::getKey, // Keep the key as is Entry::getKey, // Keep the key as is
entry -> { entry -> {
@ -136,7 +136,7 @@ public class DefaultCalculatedFieldProcessingService implements CalculatedFieldP
throw new RuntimeException("Error getting future result for key: " + entry.getKey(), e); throw new RuntimeException("Error getting future result for key: " + entry.getKey(), e);
} }
} }
)), ctx); )));
return result; return result;
}, calculatedFieldCallbackExecutor); }, calculatedFieldCallbackExecutor);
} }

View File

@ -45,7 +45,7 @@ public abstract class BaseCalculatedFieldState implements CalculatedFieldState {
} }
@Override @Override
public boolean updateState(Map<String, ArgumentEntry> argumentValues, CalculatedFieldCtx ctx) { public boolean updateState(CalculatedFieldCtx ctx, Map<String, ArgumentEntry> argumentValues) {
if (arguments == null) { if (arguments == null) {
arguments = new HashMap<>(); arguments = new HashMap<>();
} }

View File

@ -44,7 +44,7 @@ public interface CalculatedFieldState {
void setRequiredArguments(List<String> requiredArguments); void setRequiredArguments(List<String> requiredArguments);
boolean updateState(Map<String, ArgumentEntry> argumentValues, CalculatedFieldCtx ctx); boolean updateState(CalculatedFieldCtx ctx, Map<String, ArgumentEntry> argumentValues);
ListenableFuture<CalculatedFieldResult> performCalculation(CalculatedFieldCtx ctx); ListenableFuture<CalculatedFieldResult> performCalculation(CalculatedFieldCtx ctx);

View File

@ -90,7 +90,7 @@ public class ScriptCalculatedFieldStateTest {
state.arguments = new HashMap<>(Map.of("assetHumidity", assetHumidityArgEntry)); state.arguments = new HashMap<>(Map.of("assetHumidity", assetHumidityArgEntry));
Map<String, ArgumentEntry> newArgs = Map.of("deviceTemperature", deviceTemperatureArgEntry); Map<String, ArgumentEntry> newArgs = Map.of("deviceTemperature", deviceTemperatureArgEntry);
boolean stateUpdated = state.updateState(newArgs, ctx); boolean stateUpdated = state.updateState(ctx, newArgs);
assertThat(stateUpdated).isTrue(); assertThat(stateUpdated).isTrue();
assertThat(state.getArguments()).containsExactlyInAnyOrderEntriesOf( assertThat(state.getArguments()).containsExactlyInAnyOrderEntriesOf(
@ -107,7 +107,7 @@ public class ScriptCalculatedFieldStateTest {
SingleValueArgumentEntry newArgEntry = new SingleValueArgumentEntry(ts, new LongDataEntry("assetHumidity", 41L), 349L); SingleValueArgumentEntry newArgEntry = new SingleValueArgumentEntry(ts, new LongDataEntry("assetHumidity", 41L), 349L);
Map<String, ArgumentEntry> newArgs = Map.of("assetHumidity", newArgEntry); Map<String, ArgumentEntry> newArgs = Map.of("assetHumidity", newArgEntry);
boolean stateUpdated = state.updateState(newArgs, ctx); boolean stateUpdated = state.updateState(ctx, newArgs);
assertThat(stateUpdated).isTrue(); assertThat(stateUpdated).isTrue();
assertThat(state.getArguments()).containsExactlyInAnyOrderEntriesOf( assertThat(state.getArguments()).containsExactlyInAnyOrderEntriesOf(

View File

@ -88,7 +88,7 @@ public class SimpleCalculatedFieldStateTest {
)); ));
Map<String, ArgumentEntry> newArgs = Map.of("key3", key3ArgEntry); Map<String, ArgumentEntry> newArgs = Map.of("key3", key3ArgEntry);
boolean stateUpdated = state.updateState(newArgs, ctx); boolean stateUpdated = state.updateState(ctx, newArgs);
assertThat(stateUpdated).isTrue(); assertThat(stateUpdated).isTrue();
assertThat(state.getArguments()).containsExactlyInAnyOrderEntriesOf( assertThat(state.getArguments()).containsExactlyInAnyOrderEntriesOf(
@ -106,7 +106,7 @@ public class SimpleCalculatedFieldStateTest {
SingleValueArgumentEntry newArgEntry = new SingleValueArgumentEntry(System.currentTimeMillis(), new LongDataEntry("key1", 18L), 190L); SingleValueArgumentEntry newArgEntry = new SingleValueArgumentEntry(System.currentTimeMillis(), new LongDataEntry("key1", 18L), 190L);
Map<String, ArgumentEntry> newArgs = Map.of("key1", newArgEntry); Map<String, ArgumentEntry> newArgs = Map.of("key1", newArgEntry);
boolean stateUpdated = state.updateState(newArgs, ctx); boolean stateUpdated = state.updateState(ctx, newArgs);
assertThat(stateUpdated).isTrue(); assertThat(stateUpdated).isTrue();
assertThat(state.getArguments()).containsExactlyInAnyOrderEntriesOf(Map.of("key1", newArgEntry)); assertThat(state.getArguments()).containsExactlyInAnyOrderEntriesOf(Map.of("key1", newArgEntry));
@ -120,7 +120,7 @@ public class SimpleCalculatedFieldStateTest {
)); ));
Map<String, ArgumentEntry> newArgs = Map.of("key3", new TsRollingArgumentEntry(10, 30000L)); Map<String, ArgumentEntry> newArgs = Map.of("key3", new TsRollingArgumentEntry(10, 30000L));
assertThatThrownBy(() -> state.updateState(newArgs, ctx)) assertThatThrownBy(() -> state.updateState(ctx, newArgs))
.isInstanceOf(IllegalArgumentException.class) .isInstanceOf(IllegalArgumentException.class)
.hasMessage("Rolling argument entry is not supported for simple calculated fields."); .hasMessage("Rolling argument entry is not supported for simple calculated fields.");
} }