diff --git a/application/src/main/java/org/thingsboard/server/service/cf/CalculatedFieldCache.java b/application/src/main/java/org/thingsboard/server/service/cf/CalculatedFieldCache.java index 75da5c5d7a..17d9c1672b 100644 --- a/application/src/main/java/org/thingsboard/server/service/cf/CalculatedFieldCache.java +++ b/application/src/main/java/org/thingsboard/server/service/cf/CalculatedFieldCache.java @@ -33,15 +33,15 @@ public interface CalculatedFieldCache { CalculatedField getCalculatedField(CalculatedFieldId calculatedFieldId); - List getCalculatedFieldsByEntityId(EntityId entityId); + List getCalculatedFieldsByEntityId(TenantId tenantId, EntityId entityId); - List getCalculatedFieldLinksByEntityId(EntityId entityId); + List getCalculatedFieldLinksByEntityId(TenantId tenantId, EntityId entityId); CalculatedFieldCtx getCalculatedFieldCtx(CalculatedFieldId calculatedFieldId); - List getCalculatedFieldCtxsByEntityId(EntityId entityId); + List getCalculatedFieldCtxsByEntityId(TenantId tenantId, EntityId entityId); - Stream getCalculatedFieldCtxsByType(CalculatedFieldType cfType); + Stream getCalculatedFieldCtxsByType(TenantId tenantId, CalculatedFieldType cfType); boolean hasCalculatedFields(TenantId tenantId, EntityId entityId, Predicate filter); diff --git a/application/src/main/java/org/thingsboard/server/service/cf/DefaultCalculatedFieldCache.java b/application/src/main/java/org/thingsboard/server/service/cf/DefaultCalculatedFieldCache.java index ea55c2b60d..b52a989c64 100644 --- a/application/src/main/java/org/thingsboard/server/service/cf/DefaultCalculatedFieldCache.java +++ b/application/src/main/java/org/thingsboard/server/service/cf/DefaultCalculatedFieldCache.java @@ -108,13 +108,17 @@ public class DefaultCalculatedFieldCache implements CalculatedFieldCache { } @Override - public List getCalculatedFieldsByEntityId(EntityId entityId) { - return entityIdCalculatedFields.getOrDefault(entityId, Collections.emptyList()); + public List getCalculatedFieldsByEntityId(TenantId tenantId, EntityId entityId) { + return entityIdCalculatedFields.getOrDefault(entityId, Collections.emptyList()).stream() + .filter(cf -> cf.getTenantId().equals(tenantId)) + .toList(); } @Override - public List getCalculatedFieldLinksByEntityId(EntityId entityId) { - return entityIdCalculatedFieldLinks.getOrDefault(entityId, Collections.emptyList()); + public List getCalculatedFieldLinksByEntityId(TenantId tenantId, EntityId entityId) { + return entityIdCalculatedFieldLinks.getOrDefault(entityId, Collections.emptyList()).stream() + .filter(link -> link.tenantId().equals(tenantId)) + .toList(); } @Override @@ -142,25 +146,25 @@ public class DefaultCalculatedFieldCache implements CalculatedFieldCache { } @Override - public List getCalculatedFieldCtxsByEntityId(EntityId entityId) { + public List getCalculatedFieldCtxsByEntityId(TenantId tenantId, EntityId entityId) { if (entityId == null) { return Collections.emptyList(); } - return getCalculatedFieldsByEntityId(entityId).stream() + return getCalculatedFieldsByEntityId(tenantId, entityId).stream() .map(cf -> getCalculatedFieldCtx(cf.getId())) .toList(); } @Override - public Stream getCalculatedFieldCtxsByType(CalculatedFieldType cfType) { + public Stream getCalculatedFieldCtxsByType(TenantId tenantId, CalculatedFieldType cfType) { return calculatedFields.values().stream() - .filter(cf -> cfType.equals(cf.getType())) + .filter(cf -> cf.getTenantId().equals(tenantId) && cfType.equals(cf.getType())) .map(cf -> getCalculatedFieldCtx(cf.getId())); } @Override public boolean hasCalculatedFields(TenantId tenantId, EntityId entityId, Predicate filter) { - List entityCfs = getCalculatedFieldCtxsByEntityId(entityId); + List entityCfs = getCalculatedFieldCtxsByEntityId(tenantId, entityId); for (CalculatedFieldCtx ctx : entityCfs) { if (filter.test(ctx)) { return true; @@ -173,7 +177,7 @@ public class DefaultCalculatedFieldCache implements CalculatedFieldCache { public boolean hasCalculatedFieldsByProfile(TenantId tenantId, EntityId entityId, Predicate filter) { EntityId profileId = getProfileId(tenantId, entityId); if (profileId != null) { - List profileCfs = getCalculatedFieldCtxsByEntityId(profileId); + List profileCfs = getCalculatedFieldCtxsByEntityId(tenantId, profileId); for (CalculatedFieldCtx ctx : profileCfs) { if (filter.test(ctx)) { return true; diff --git a/application/src/main/java/org/thingsboard/server/service/cf/DefaultCalculatedFieldQueueService.java b/application/src/main/java/org/thingsboard/server/service/cf/DefaultCalculatedFieldQueueService.java index da79e41e5f..4dae1c6549 100644 --- a/application/src/main/java/org/thingsboard/server/service/cf/DefaultCalculatedFieldQueueService.java +++ b/application/src/main/java/org/thingsboard/server/service/cf/DefaultCalculatedFieldQueueService.java @@ -171,7 +171,7 @@ public class DefaultCalculatedFieldQueueService implements CalculatedFieldQueueS return true; } - List links = calculatedFieldCache.getCalculatedFieldLinksByEntityId(entityId); + List links = calculatedFieldCache.getCalculatedFieldLinksByEntityId(tenantId, entityId); for (CalculatedFieldLink link : links) { CalculatedFieldCtx ctx = calculatedFieldCache.getCalculatedFieldCtx(link.calculatedFieldId()); if (ctx != null && linkedEntityFilter.test(ctx)) { @@ -180,21 +180,21 @@ public class DefaultCalculatedFieldQueueService implements CalculatedFieldQueueS } for (EntityId dynamicEntity : calculatedFieldCache.getDynamicEntities(tenantId, entityId)) { - if (calculatedFieldCache.getCalculatedFieldCtxsByEntityId(dynamicEntity).stream().anyMatch(dynamicSourceFilter)) { + if (calculatedFieldCache.getCalculatedFieldCtxsByEntityId(tenantId, dynamicEntity).stream().anyMatch(dynamicSourceFilter)) { return true; } EntityId dynamicEntityProfileId = calculatedFieldCache.getProfileId(tenantId, dynamicEntity); - if (calculatedFieldCache.getCalculatedFieldCtxsByEntityId(dynamicEntityProfileId).stream().anyMatch(dynamicSourceFilter)) { + if (calculatedFieldCache.getCalculatedFieldCtxsByEntityId(tenantId, dynamicEntityProfileId).stream().anyMatch(dynamicSourceFilter)) { return true; } } - boolean hasMatchesEntityAggCfs = calculatedFieldCache.getCalculatedFieldCtxsByType(CalculatedFieldType.ENTITY_AGGREGATION).anyMatch(filter); + boolean hasMatchesEntityAggCfs = calculatedFieldCache.getCalculatedFieldCtxsByType(tenantId, CalculatedFieldType.ENTITY_AGGREGATION).anyMatch(filter); if (hasMatchesEntityAggCfs) { return true; } - List relatedEntitiesAggregationCfs = calculatedFieldCache.getCalculatedFieldCtxsByType(CalculatedFieldType.RELATED_ENTITIES_AGGREGATION) + List relatedEntitiesAggregationCfs = calculatedFieldCache.getCalculatedFieldCtxsByType(tenantId, CalculatedFieldType.RELATED_ENTITIES_AGGREGATION) .filter(relatedEntityFilter) .toList(); for (CalculatedFieldCtx cfCtx : relatedEntitiesAggregationCfs) {