Browse Source

get all entity infos instead of getting for each separately

pull/14419/head
IrynaMatveieva 8 months ago
parent
commit
a164e074b3
  1. 4
      application/src/main/java/org/thingsboard/server/actors/calculatedField/CalculatedFieldEntityMessageProcessor.java
  2. 14
      application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/RelatedEntitiesAggregationCalculatedFieldState.java
  3. 19
      application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/single/EntityAggregationCalculatedFieldState.java
  4. 2
      common/data/src/main/java/org/thingsboard/server/common/data/DataConstants.java

4
application/src/main/java/org/thingsboard/server/actors/calculatedField/CalculatedFieldEntityMessageProcessor.java

@ -70,7 +70,7 @@ import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static org.thingsboard.server.common.data.DataConstants.CF_REEVALUATION_MSG;
import static org.thingsboard.server.common.data.DataConstants.REEVALUATION_MSG;
import static org.thingsboard.server.utils.CalculatedFieldArgumentUtils.createStateByType;
/**
@ -355,7 +355,7 @@ public class CalculatedFieldEntityMessageProcessor extends AbstractContextAwareM
}
if (state.isSizeOk()) {
log.debug("[{}][{}] Reevaluating CF state", entityId, cfId);
processStateIfReady(state, null, ctx, Collections.singletonList(cfId), null, CF_REEVALUATION_MSG, msg.getCallback());
processStateIfReady(state, null, ctx, Collections.singletonList(cfId), null, REEVALUATION_MSG, msg.getCallback());
} else {
throw new RuntimeException(ctx.getSizeExceedsLimitMessage());
}

14
application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/RelatedEntitiesAggregationCalculatedFieldState.java

@ -257,14 +257,14 @@ public class RelatedEntitiesAggregationCalculatedFieldState extends BaseCalculat
@Override
public JsonNode getArgumentsJson() {
Map<EntityId, Map<String, ArgumentEntry>> inputs = prepareInputs();
Map<EntityId, EntityInfo> entityIdEntityInfos = entityService.fetchEntityInfos(ctx.getTenantId(), null, inputs.keySet());
List<EntityArgument> entitiesArguments = new ArrayList<>();
prepareInputs().forEach((entityId, entityArguments) -> {
entityService.fetchEntityName(ctx.getTenantId(), entityId).ifPresent(entityName -> {
EntityInfo entityInfo = new EntityInfo(entityId, entityName);
JsonNode entityArgumentsJson = JacksonUtil.valueToTree(entityArguments.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, e -> e.getValue().jsonValue())));
entitiesArguments.add(new EntityArgument(entityInfo, entityArgumentsJson));
});
inputs.forEach((entityId, entityArguments) -> {
EntityInfo entityInfo = entityIdEntityInfos.get(entityId);
JsonNode entityArgumentsJson = JacksonUtil.valueToTree(entityArguments.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, e -> e.getValue().jsonValue())));
entitiesArguments.add(new EntityArgument(entityInfo, entityArgumentsJson));
});
return JacksonUtil.valueToTree(new RelatedEntitiesArgument(ArgumentEntryType.RELATED_ENTITIES, entitiesArguments));
}

19
application/src/main/java/org/thingsboard/server/service/cf/ctx/state/aggregation/single/EntityAggregationCalculatedFieldState.java

@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import org.apache.commons.lang3.concurrent.LazyInitializer;
import org.thingsboard.common.util.DebugModeUtil;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.script.api.tbel.TbUtils;
@ -63,7 +64,7 @@ public class EntityAggregationCalculatedFieldState extends BaseCalculatedFieldSt
private long checkInterval;
private Map<String, AggMetric> metrics;
private final EntityAggregationDebugArgumentsTracker debugTracker = new EntityAggregationDebugArgumentsTracker(new HashMap<>());
private EntityAggregationDebugArgumentsTracker debugTracker;
private CalculatedFieldProcessingService cfProcessingService;
@ -98,11 +99,14 @@ public class EntityAggregationCalculatedFieldState extends BaseCalculatedFieldSt
@Override
public ListenableFuture<CalculatedFieldResult> performCalculation(Map<String, ArgumentEntry> updatedArgs, CalculatedFieldCtx ctx) throws Exception {
debugTracker.reset();
createIntervalIfNotExist();
long now = System.currentTimeMillis();
if (DebugModeUtil.isDebugFailuresAvailable(ctx.getCalculatedField())) {
LazyInitializer<EntityAggregationDebugArgumentsTracker> lazy = LazyInitializer.<EntityAggregationDebugArgumentsTracker>builder()
.setInitializer(() -> new EntityAggregationDebugArgumentsTracker(new HashMap<>()))
.get();
debugTracker = lazy.get();
debugTracker.recordUpdatedArgs(updatedArgs, arguments);
}
@ -285,7 +289,9 @@ public class EntityAggregationCalculatedFieldState extends BaseCalculatedFieldSt
result.add(resultNode);
if (DebugModeUtil.isDebugFailuresAvailable(ctx.getCalculatedField())) {
debugTracker.addInterval(interval);
if (debugTracker != null) {
debugTracker.addInterval(interval);
}
}
}
});
@ -294,6 +300,9 @@ public class EntityAggregationCalculatedFieldState extends BaseCalculatedFieldSt
@Override
public JsonNode getArgumentsJson() {
if (debugTracker == null) {
return null;
}
EntityAggregationDebugArguments debugArguments = debugTracker.toDebugArguments();
return debugArguments == null ? null : JacksonUtil.valueToTree(debugArguments);
}
@ -305,10 +314,6 @@ public class EntityAggregationCalculatedFieldState extends BaseCalculatedFieldSt
record EntityAggregationDebugArgumentsTracker(Map<AggIntervalEntry, Map<String, TbelCfArg>> processedIntervals) {
public void reset() {
processedIntervals.clear();
}
public void addInterval(AggIntervalEntry interval) {
processedIntervals.computeIfAbsent(interval, k -> new HashMap<>());
}

2
common/data/src/main/java/org/thingsboard/server/common/data/DataConstants.java

@ -105,7 +105,7 @@ public class DataConstants {
public static final String RPC_FAILED = "RPC_FAILED";
public static final String RPC_DELETED = "RPC_DELETED";
public static final String CF_REEVALUATION_MSG = "CF_REEVALUATION_MSG";
public static final String REEVALUATION_MSG = "REEVALUATION_MSG";
public static final String DEFAULT_SECRET_KEY = "";
public static final String SECRET_KEY_FIELD_NAME = "secretKey";

Loading…
Cancel
Save