From 19fa00a5fbb87c62eed42e62fd6a463c4678c046 Mon Sep 17 00:00:00 2001 From: ShvaykaD Date: Fri, 11 Oct 2024 17:42:20 +0300 Subject: [PATCH 01/37] Rule node debug strategies --- .../rule_chains/edge_root_rule_chain.json | 18 +- .../tenant/rule_chains/root_rule_chain.json | 14 +- .../main/data/upgrade/3.8.0/schema_update.sql | 21 + .../server/actors/ActorSystemContext.java | 4 + .../actors/ruleChain/DefaultTbContext.java | 82 +- .../RuleChainActorMessageProcessor.java | 44 +- .../actors/ruleChain/RuleNodeActor.java | 6 +- .../RuleNodeActorMessageProcessor.java | 37 +- .../server/actors/ruleChain/RuleNodeCtx.java | 17 +- .../RuleNodeToRuleChainTellNextMsg.java | 10 +- .../controller/TenantProfileController.java | 1 + .../BaseRuleChainMetadataConstructor.java | 4 +- .../tenant/TenantMsgConstructorV1.java | 1 + .../src/main/resources/thingsboard.yml | 3 + .../actors/rule/DefaultTbContextTest.java | 836 ++++++++++++++++++ .../server/edge/AbstractEdgeTest.java | 6 +- .../server/edge/RuleChainEdgeTest.java | 3 +- ...AbstractRuleEngineFlowIntegrationTest.java | 11 +- ...actRuleEngineLifecycleIntegrationTest.java | 3 +- .../housekeeper/HousekeeperServiceTest.java | 5 +- .../sync/ie/ExportImportServiceSqlTest.java | 9 +- .../service/sync/vc/VersionControlTest.java | 9 +- .../common/data/msg/TbNodeConnectionType.java | 4 + .../common/data/rule/DebugStrategy.java | 72 ++ .../server/common/data/rule/RuleNode.java | 24 +- .../DefaultTenantProfileConfiguration.java | 10 + .../profile/TenantProfileConfiguration.java | 3 + common/edge-api/src/main/proto/edge.proto | 10 +- .../server/dao/model/ModelConstants.java | 1 + .../server/dao/model/sql/RuleNodeEntity.java | 17 +- .../server/dao/rule/BaseRuleChainService.java | 5 +- .../main/resources/sql/schema-entities.sql | 3 +- 32 files changed, 1165 insertions(+), 128 deletions(-) create mode 100644 application/src/test/java/org/thingsboard/server/actors/rule/DefaultTbContextTest.java create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java diff --git a/application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json b/application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json index 6b7603c026..04a5f52f4e 100644 --- a/application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json +++ b/application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json @@ -20,7 +20,7 @@ }, "type": "org.thingsboard.rule.engine.profile.TbDeviceProfileNode", "name": "Device Profile Node", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "persistAlarmRulesState": false, "fetchAlarmRulesStateOnStart": false @@ -34,7 +34,7 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "defaultTTL": 0 }, @@ -47,7 +47,7 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", "name": "Save Client Attributes", - "debugMode": false, + "debugStrategy": "DISABLED", "configurationVersion": 2, "configuration": { "scope": "CLIENT_SCOPE", @@ -64,7 +64,7 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "Message Type Switch", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "version": 0 }, @@ -77,7 +77,7 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log RPC from Device", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "scriptLang": "TBEL", "jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);", @@ -92,7 +92,7 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log Other", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "scriptLang": "TBEL", "jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);", @@ -107,7 +107,7 @@ }, "type": "org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode", "name": "RPC Call Request", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "timeoutInSeconds": 60 }, @@ -120,7 +120,7 @@ }, "type": "org.thingsboard.rule.engine.edge.TbMsgPushToCloudNode", "name": "Push to cloud", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "scope": "SERVER_SCOPE" }, @@ -133,7 +133,7 @@ }, "type": "org.thingsboard.rule.engine.edge.TbMsgPushToCloudNode", "name": "Push to cloud", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "scope": "SERVER_SCOPE" }, diff --git a/application/src/main/data/json/tenant/rule_chains/root_rule_chain.json b/application/src/main/data/json/tenant/rule_chains/root_rule_chain.json index 0b70d087e7..3b9898d0b1 100644 --- a/application/src/main/data/json/tenant/rule_chains/root_rule_chain.json +++ b/application/src/main/data/json/tenant/rule_chains/root_rule_chain.json @@ -18,7 +18,7 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "defaultTTL": 0 } @@ -30,7 +30,7 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", "name": "Save Client Attributes", - "debugMode": false, + "debugStrategy": "DISABLED", "configurationVersion": 2, "configuration": { "scope": "CLIENT_SCOPE", @@ -46,7 +46,7 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "Message Type Switch", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "version": 0 } @@ -58,7 +58,7 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log RPC from Device", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "scriptLang": "TBEL", "jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);", @@ -72,7 +72,7 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log Other", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "scriptLang": "TBEL", "jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);", @@ -86,7 +86,7 @@ }, "type": "org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode", "name": "RPC Call Request", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "timeoutInSeconds": 60 } @@ -99,7 +99,7 @@ }, "type": "org.thingsboard.rule.engine.profile.TbDeviceProfileNode", "name": "Device Profile Node", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "persistAlarmRulesState": false, "fetchAlarmRulesStateOnStart": false diff --git a/application/src/main/data/upgrade/3.8.0/schema_update.sql b/application/src/main/data/upgrade/3.8.0/schema_update.sql index 6b87dc6dde..dc3bf2386e 100644 --- a/application/src/main/data/upgrade/3.8.0/schema_update.sql +++ b/application/src/main/data/upgrade/3.8.0/schema_update.sql @@ -14,3 +14,24 @@ -- limitations under the License. -- +-- UPDATE RULE NODE DEBUG MODE TO DEBUG STRATEGY START + +ALTER TABLE rule_node + ADD COLUMN IF NOT EXISTS debug_strategy varchar(32) DEFAULT 'DISABLED'; +ALTER TABLE rule_node + ADD COLUMN IF NOT EXISTS last_update_ts bigint NOT NULL DEFAULT extract(epoch from now()) * 1000; +DO +$$ + BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'rule_node' AND column_name = 'debug_mode') THEN + UPDATE rule_node + SET debug_strategy = CASE WHEN debug_mode = true THEN 'ALL_EVENTS' ELSE 'DISABLED' END; + ALTER TABLE rule_node + DROP COLUMN debug_mode; + END IF; + END +$$; + +-- UPDATE RULE NODE DEBUG MODE TO DEBUG STRATEGY END diff --git a/application/src/main/java/org/thingsboard/server/actors/ActorSystemContext.java b/application/src/main/java/org/thingsboard/server/actors/ActorSystemContext.java index 5966716ed8..2920fa400a 100644 --- a/application/src/main/java/org/thingsboard/server/actors/ActorSystemContext.java +++ b/application/src/main/java/org/thingsboard/server/actors/ActorSystemContext.java @@ -577,6 +577,10 @@ public class ActorSystemContext { @Getter private boolean externalNodeForceAck; + @Value("${actors.rule.node.max_debug_mode_duration:60}") + @Getter + private int maxRuleNodeDebugModeDurationMinutes; + @Value("${state.rule.node.deviceState.rateLimit:1:1,30:60,60:3600}") @Getter private String deviceStateNodeRateLimitConfig; diff --git a/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java b/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java index ec10402821..3be7524c85 100644 --- a/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java +++ b/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java @@ -63,6 +63,7 @@ import org.thingsboard.server.common.data.msg.TbMsgType; import org.thingsboard.server.common.data.msg.TbNodeConnectionType; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; +import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleNode; import org.thingsboard.server.common.data.rule.RuleNodeState; import org.thingsboard.server.common.data.script.ScriptLanguage; @@ -129,7 +130,7 @@ import static org.thingsboard.server.common.data.msg.TbMsgType.ENTITY_CREATED; * Created by ashvayka on 19.03.18. */ @Slf4j -class DefaultTbContext implements TbContext { +public class DefaultTbContext implements TbContext { private final ActorSystemContext mainCtx; private final String ruleChainName; @@ -143,25 +144,25 @@ class DefaultTbContext implements TbContext { @Override public void tellSuccess(TbMsg msg) { - tellNext(msg, Collections.singleton(TbNodeConnectionType.SUCCESS), null); + tellNext(msg, Collections.singleton(TbNodeConnectionType.SUCCESS)); } @Override public void tellNext(TbMsg msg, String relationType) { - tellNext(msg, Collections.singleton(relationType), null); + tellNext(msg, Collections.singleton(relationType)); } @Override public void tellNext(TbMsg msg, Set relationTypes) { - tellNext(msg, relationTypes, null); - } - - private void tellNext(TbMsg msg, Set relationTypes, Throwable th) { - if (nodeCtx.getSelf().isDebugMode()) { - relationTypes.forEach(relationType -> mainCtx.persistDebugOutput(nodeCtx.getTenantId(), nodeCtx.getSelf().getId(), msg, relationType, th)); + RuleNode ruleNode = nodeCtx.getSelf(); + DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); + if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), msg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { + relationTypes.forEach(relationType -> mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), msg, relationType)); + } else if (debugStrategy.shouldPersistDebugForFailureEventOnly(relationTypes)) { + mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), msg, TbNodeConnectionType.FAILURE); } - msg.getCallback().onProcessingEnd(nodeCtx.getSelf().getId()); - nodeCtx.getChainActor().tell(new RuleNodeToRuleChainTellNextMsg(nodeCtx.getSelf().getRuleChainId(), nodeCtx.getSelf().getId(), relationTypes, msg, th != null ? th.getMessage() : null)); + msg.getCallback().onProcessingEnd(ruleNode.getId()); + nodeCtx.getChainActor().tell(new RuleNodeToRuleChainTellNextMsg(ruleNode.getRuleChainId(), ruleNode.getId(), relationTypes, msg, null)); } @Override @@ -182,8 +183,12 @@ class DefaultTbContext implements TbContext { if (item == null) { ack(msg); } else { - if (nodeCtx.getSelf().isDebugMode()) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), nodeCtx.getSelf().getId(), msg, relationType); + RuleNode ruleNode = nodeCtx.getSelf(); + DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); + if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), msg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { + mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), msg, relationType); + } else if (debugStrategy.shouldPersistDebugForFailureEventOnly(relationType)) { + mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), msg, relationType); } nodeCtx.getChainActor().tell(new RuleChainOutputMsg(item.getRuleChainId(), item.getRuleNodeId(), relationType, msg)); } @@ -213,11 +218,13 @@ class DefaultTbContext implements TbContext { .setTenantIdMSB(getTenantId().getId().getMostSignificantBits()) .setTenantIdLSB(getTenantId().getId().getLeastSignificantBits()) .setTbMsg(TbMsg.toByteString(tbMsg)).build(); - if (nodeCtx.getSelf().isDebugMode()) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), nodeCtx.getSelf().getId(), tbMsg, "To Root Rule Chain"); - } mainCtx.getClusterService().pushMsgToRuleEngine(tpi, tbMsg.getId(), msg, new SimpleTbQueueCallback( metadata -> { + RuleNode ruleNode = nodeCtx.getSelf(); + DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); + if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), tbMsg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { + mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), tbMsg, TbNodeConnectionType.TO_ROOT_RULE_CHAIN); + } if (onSuccess != null) { onSuccess.run(); } @@ -299,8 +306,9 @@ class DefaultTbContext implements TbContext { } return; } - RuleChainId ruleChainId = nodeCtx.getSelf().getRuleChainId(); - RuleNodeId ruleNodeId = nodeCtx.getSelf().getId(); + RuleNode ruleNode = nodeCtx.getSelf(); + RuleChainId ruleChainId = ruleNode.getRuleChainId(); + RuleNodeId ruleNodeId = ruleNode.getId(); TbMsg tbMsg = TbMsg.newMsg(source, queueName, ruleChainId, ruleNodeId); TransportProtos.ToRuleEngineMsg.Builder msg = TransportProtos.ToRuleEngineMsg.newBuilder() .setTenantIdMSB(getTenantId().getId().getMostSignificantBits()) @@ -310,12 +318,15 @@ class DefaultTbContext implements TbContext { if (failureMessage != null) { msg.setFailureMessage(failureMessage); } - if (nodeCtx.getSelf().isDebugMode()) { - relationTypes.forEach(relationType -> - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), nodeCtx.getSelf().getId(), tbMsg, relationType, null, failureMessage)); - } mainCtx.getClusterService().pushMsgToRuleEngine(tpi, tbMsg.getId(), msg.build(), new SimpleTbQueueCallback( metadata -> { + DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); + if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), tbMsg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { + relationTypes.forEach(relationType -> + mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), tbMsg, relationType, null, failureMessage)); + } else if (debugStrategy.shouldPersistDebugForFailureEventOnly(relationTypes)) { + mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), tbMsg, TbNodeConnectionType.FAILURE, null, failureMessage); + } if (onSuccess != null) { onSuccess.run(); } @@ -331,10 +342,12 @@ class DefaultTbContext implements TbContext { @Override public void ack(TbMsg tbMsg) { - if (nodeCtx.getSelf().isDebugMode()) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), nodeCtx.getSelf().getId(), tbMsg, "ACK", null); + RuleNode ruleNode = nodeCtx.getSelf(); + DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); + if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), tbMsg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { + mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), tbMsg, TbNodeConnectionType.ACK); } - tbMsg.getCallback().onProcessingEnd(nodeCtx.getSelf().getId()); + tbMsg.getCallback().onProcessingEnd(ruleNode.getId()); tbMsg.getCallback().onSuccess(); } @@ -349,12 +362,13 @@ class DefaultTbContext implements TbContext { @Override public void tellFailure(TbMsg msg, Throwable th) { - if (nodeCtx.getSelf().isDebugMode()) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), nodeCtx.getSelf().getId(), msg, TbNodeConnectionType.FAILURE, th); + RuleNode ruleNode = nodeCtx.getSelf(); + if (ruleNode.getDebugStrategy().shouldPersistDebugForFailureEventOnly(ruleNode.getLastUpdateTs(), msg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { + mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), msg, TbNodeConnectionType.FAILURE, th); } String failureMessage = getFailureMessage(th); - nodeCtx.getChainActor().tell(new RuleNodeToRuleChainTellNextMsg(nodeCtx.getSelf().getRuleChainId(), - nodeCtx.getSelf().getId(), Collections.singleton(TbNodeConnectionType.FAILURE), + nodeCtx.getChainActor().tell(new RuleNodeToRuleChainTellNextMsg(ruleNode.getRuleChainId(), + ruleNode.getId(), Collections.singleton(TbNodeConnectionType.FAILURE), msg, failureMessage)); } @@ -1004,4 +1018,14 @@ class DefaultTbContext implements TbContext { return failureMessage; } + private int getMaxRuleNodeDebugDurationMinutes() { + if (!DebugStrategy.ALL_EVENTS.equals(nodeCtx.getSelf().getDebugStrategy())) { + return 0; + } + var configuration = mainCtx.getTenantProfileCache() + .get(getTenantId()).getProfileData().getConfiguration(); + int systemMaxRuleNodeDebugModeDurationMinutes = mainCtx.getMaxRuleNodeDebugModeDurationMinutes(); + return configuration.getMaxRuleNodeDebugModeDurationMinutes(systemMaxRuleNodeDebugModeDurationMinutes); + } + } diff --git a/application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleChainActorMessageProcessor.java b/application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleChainActorMessageProcessor.java index eaa2218116..2213daa770 100644 --- a/application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleChainActorMessageProcessor.java +++ b/application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleChainActorMessageProcessor.java @@ -43,7 +43,6 @@ import org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg; import org.thingsboard.server.common.msg.queue.RuleEngineException; import org.thingsboard.server.common.msg.queue.RuleNodeException; import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; -import org.thingsboard.server.common.stats.TbApiUsageReportClient; import org.thingsboard.server.dao.rule.RuleChainService; import org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg; import org.thingsboard.server.queue.TbQueueCallback; @@ -72,7 +71,6 @@ public class RuleChainActorMessageProcessor extends ComponentMsgProcessor> nodeRoutes; private final RuleChainService service; private final TbClusterService clusterService; - private final TbApiUsageReportClient apiUsageClient; private String ruleChainName; private RuleNodeId firstId; @@ -81,7 +79,6 @@ public class RuleChainActorMessageProcessor extends ComponentMsgProcessor existingNodes = ruleNodeList.stream().map(RuleNode::getId).collect(Collectors.toSet()); - List removedRules = nodeActors.keySet().stream().filter(node -> !existingNodes.contains(node)).collect(Collectors.toList()); + List removedRules = nodeActors.keySet().stream().filter(node -> !existingNodes.contains(node)).toList(); removedRules.forEach(ruleNodeId -> { log.trace("[{}][{}] Removing rule node [{}]", tenantId, entityId, ruleNodeId); RuleNodeCtx removed = nodeActors.remove(ruleNodeId); @@ -177,7 +174,7 @@ public class RuleChainActorMessageProcessor extends ComponentMsgProcessor relations = service.getRuleNodeRelations(TenantId.SYS_TENANT_ID, ruleNode.getId()); log.trace("[{}][{}][{}] Processing rule node relations [{}]", tenantId, entityId, ruleNode.getId(), relations.size()); - if (relations.size() == 0) { + if (relations.isEmpty()) { nodeRoutes.put(ruleNode.getId(), Collections.emptyList()); } else { for (EntityRelation relation : relations) { @@ -238,45 +235,53 @@ public class RuleChainActorMessageProcessor extends ComponentMsgProcessor relationTypes; - @Getter private final String failureMessage; public RuleNodeToRuleChainTellNextMsg(RuleChainId ruleChainId, RuleNodeId originator, Set relationTypes, TbMsg tbMsg, String failureMessage) { diff --git a/application/src/main/java/org/thingsboard/server/controller/TenantProfileController.java b/application/src/main/java/org/thingsboard/server/controller/TenantProfileController.java index 007f77aa20..85660b5914 100644 --- a/application/src/main/java/org/thingsboard/server/controller/TenantProfileController.java +++ b/application/src/main/java/org/thingsboard/server/controller/TenantProfileController.java @@ -151,6 +151,7 @@ public class TenantProfileController extends BaseController { " \"maxJSExecutions\": 5000000,\n" + " \"maxDPStorageDays\": 0,\n" + " \"maxRuleNodeExecutionsPerMessage\": 50,\n" + + " \"maxRuleNodeDebugDurationMinutes\": 15,\n" + " \"maxEmails\": 0,\n" + " \"maxSms\": 0,\n" + " \"maxCreatedAlarms\": 1000,\n" + diff --git a/application/src/main/java/org/thingsboard/server/service/edge/rpc/constructor/rule/BaseRuleChainMetadataConstructor.java b/application/src/main/java/org/thingsboard/server/service/edge/rpc/constructor/rule/BaseRuleChainMetadataConstructor.java index cb66992565..38b2d959ea 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/rpc/constructor/rule/BaseRuleChainMetadataConstructor.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/rpc/constructor/rule/BaseRuleChainMetadataConstructor.java @@ -24,6 +24,7 @@ import org.thingsboard.server.common.data.rule.NodeConnectionInfo; import org.thingsboard.server.common.data.rule.RuleChainConnectionInfo; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleNode; +import org.thingsboard.server.gen.edge.v1.DebugStrategy; import org.thingsboard.server.gen.edge.v1.EdgeVersion; import org.thingsboard.server.gen.edge.v1.NodeConnectionInfoProto; import org.thingsboard.server.gen.edge.v1.RuleChainConnectionInfoProto; @@ -88,7 +89,8 @@ public abstract class BaseRuleChainMetadataConstructor implements RuleChainMetad .setIdLSB(node.getId().getId().getLeastSignificantBits()) .setType(node.getType()) .setName(node.getName()) - .setDebugMode(node.isDebugMode()) + .setLastUpdateTs(node.getLastUpdateTs()) + .setDebugStrategy(DebugStrategy.forNumber(node.getDebugStrategy().getProtoNumber())) .setConfiguration(JacksonUtil.toString(node.getConfiguration())) .setAdditionalInfo(JacksonUtil.toString(node.getAdditionalInfo())) .setSingletonMode(node.isSingletonMode()) diff --git a/application/src/main/java/org/thingsboard/server/service/edge/rpc/constructor/tenant/TenantMsgConstructorV1.java b/application/src/main/java/org/thingsboard/server/service/edge/rpc/constructor/tenant/TenantMsgConstructorV1.java index fc244417bc..5d60c69903 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/rpc/constructor/tenant/TenantMsgConstructorV1.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/rpc/constructor/tenant/TenantMsgConstructorV1.java @@ -88,6 +88,7 @@ public class TenantMsgConstructorV1 implements TenantMsgConstructor { configuration.setMaxTransportDataPoints(0); configuration.setRuleEngineExceptionsTtlDays(0); configuration.setMaxRuleNodeExecutionsPerMessage(0); + configuration.setMaxRuleNodeDebugDurationMinutes(0); tenantProfileData.setConfiguration(configuration); tenantProfile.setProfileData(tenantProfileData); diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index e9c8668958..581a59fa36 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -468,6 +468,9 @@ actors: node: # Errors for particular actor are persisted once per specified amount of milliseconds error_persist_frequency: "${ACTORS_RULE_NODE_ERROR_FREQUENCY:3000}" + # The maximum allowed duration (in minutes) for the debug mode to be enabled if the debug strategy is set to ALL_EVENTS. + # If a specific value is set in the tenant profile, the minimum between value from profile and this setting will be used. + max_debug_mode_duration: "${ACTORS_RULE_NODE_MAX_DEBUG_MODE_DURATION_MINUTES:60}" transaction: # Size of queues that store messages for transaction rule nodes queue_size: "${ACTORS_RULE_TRANSACTION_QUEUE_SIZE:15000}" diff --git a/application/src/test/java/org/thingsboard/server/actors/rule/DefaultTbContextTest.java b/application/src/test/java/org/thingsboard/server/actors/rule/DefaultTbContextTest.java new file mode 100644 index 0000000000..e9b344866f --- /dev/null +++ b/application/src/test/java/org/thingsboard/server/actors/rule/DefaultTbContextTest.java @@ -0,0 +1,836 @@ +/** + * Copyright © 2016-2024 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. + */ +package org.thingsboard.server.actors.rule; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.thingsboard.server.actors.ActorSystemContext; +import org.thingsboard.server.actors.TbActorRef; +import org.thingsboard.server.actors.ruleChain.DefaultTbContext; +import org.thingsboard.server.actors.ruleChain.RuleChainOutputMsg; +import org.thingsboard.server.actors.ruleChain.RuleNodeCtx; +import org.thingsboard.server.actors.ruleChain.RuleNodeToRuleChainTellNextMsg; +import org.thingsboard.server.cluster.TbClusterService; +import org.thingsboard.server.common.data.DataConstants; +import org.thingsboard.server.common.data.TenantProfile; +import org.thingsboard.server.common.data.id.EntityId; +import org.thingsboard.server.common.data.id.RuleChainId; +import org.thingsboard.server.common.data.id.RuleNodeId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.msg.TbMsgType; +import org.thingsboard.server.common.data.msg.TbNodeConnectionType; +import org.thingsboard.server.common.data.rule.DebugStrategy; +import org.thingsboard.server.common.data.rule.RuleNode; +import org.thingsboard.server.common.data.tenant.profile.TenantProfileConfiguration; +import org.thingsboard.server.common.data.tenant.profile.TenantProfileData; +import org.thingsboard.server.common.msg.TbMsg; +import org.thingsboard.server.common.msg.TbMsgMetaData; +import org.thingsboard.server.common.msg.TbMsgProcessingStackItem; +import org.thingsboard.server.common.msg.queue.ServiceType; +import org.thingsboard.server.common.msg.queue.TbMsgCallback; +import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; +import org.thingsboard.server.dao.tenant.TbTenantProfileCache; +import org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg; +import org.thingsboard.server.queue.common.SimpleTbQueueCallback; + +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.ArgumentMatchers.notNull; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; + +@SuppressWarnings("ResultOfMethodCallIgnored") +@ExtendWith(MockitoExtension.class) +class DefaultTbContextTest { + + private final String EXCEPTION_MSG = "Some runtime exception!"; + private final RuntimeException EXCEPTION = new RuntimeException(EXCEPTION_MSG); + + private final TenantId TENANT_ID = TenantId.fromUUID(UUID.fromString("c7bf4c85-923c-4688-a4b5-0f8a0feb7cd5")); + private final RuleNodeId RULE_NODE_ID = new RuleNodeId(UUID.fromString("1ca5e2ef-1309-41d9-bafa-709e9df0e2a6")); + private final RuleChainId RULE_CHAIN_ID = new RuleChainId(UUID.fromString("b87c4123-f9f2-41a6-9a09-e3a5b6580b11")); + + @Mock + private ActorSystemContext mainCtxMock; + @Mock + private RuleNodeCtx nodeCtxMock; + @Mock + private TbActorRef chainActorMock; + + private DefaultTbContext defaultTbContext; + + @BeforeEach + public void setUp() { + defaultTbContext = new DefaultTbContext(mainCtxMock, "Test rule chain name", nodeCtxMock); + } + + @Test + public void givenDebugStrategyOnlyFailureEvents_whenTellSuccess_thenVerifyDebugOutputNotPersisted() { + // GIVEN + var callbackMock = mock(TbMsgCallback.class); + var msg = getTbMsgWithCallback(callbackMock); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ONLY_FAILURE_EVENTS); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + defaultTbContext.tellSuccess(msg); + + // THEN + then(nodeCtxMock).should().getChainActor(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + then(mainCtxMock).shouldHaveNoInteractions(); + checkTellNextCommonLogic(callbackMock, TbNodeConnectionType.SUCCESS, msg); + } + + @Test + public void givenDebugStrategyOnlyFailureEventsAndSuccessConnection_whenTellNext_thenVerifyDebugOutputNotPersisted() { + // GIVEN + var callbackMock = mock(TbMsgCallback.class); + var msg = getTbMsgWithCallback(callbackMock); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ONLY_FAILURE_EVENTS); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + defaultTbContext.tellNext(msg, TbNodeConnectionType.SUCCESS); + + // THEN + then(nodeCtxMock).should().getChainActor(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + then(mainCtxMock).shouldHaveNoInteractions(); + checkTellNextCommonLogic(callbackMock, TbNodeConnectionType.SUCCESS, msg); + } + + @MethodSource + @ParameterizedTest + void givenDebugStrategyOnlyFailureEventsAndConnections_whenTellNext_thenVerifyDebugOutputPersisted(Set connections) { + // GIVEN + var callbackMock = mock(TbMsgCallback.class); + var msg = getTbMsgWithCallback(callbackMock); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ONLY_FAILURE_EVENTS); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + defaultTbContext.tellNext(msg, connections); + + // THEN + then(nodeCtxMock).should().getChainActor(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, TbNodeConnectionType.FAILURE); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + checkTellNextCommonLogic(callbackMock, connections, msg); + } + + private static Stream> givenDebugStrategyOnlyFailureEventsAndConnections_whenTellNext_thenVerifyDebugOutputPersisted() { + return Stream.of( + Collections.singleton(TbNodeConnectionType.FAILURE), + Set.of(TbNodeConnectionType.FAILURE, TbNodeConnectionType.SUCCESS) + ); + } + + @MethodSource + @ParameterizedTest + void givenDebugStrategyDisabledAndConnections_whenTellNext_thenVerifyDebugOutputNotPersisted(Set connections) { + // GIVEN + var callbackMock = mock(TbMsgCallback.class); + var msg = getTbMsgWithCallback(callbackMock); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.DISABLED); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + defaultTbContext.tellNext(msg, connections); + + // THEN + then(nodeCtxMock).should().getChainActor(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + then(mainCtxMock).shouldHaveNoInteractions(); + checkTellNextCommonLogic(callbackMock, connections, msg); + } + + private static Stream> givenDebugStrategyDisabledAndConnections_whenTellNext_thenVerifyDebugOutputNotPersisted() { + return Stream.of( + Collections.singleton(TbNodeConnectionType.FAILURE), + Collections.singleton(TbNodeConnectionType.SUCCESS), + Set.of(TbNodeConnectionType.FAILURE, TbNodeConnectionType.SUCCESS) + ); + } + + @MethodSource + @ParameterizedTest + void givenDebugStrategyAllEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted(String connection) { + // GIVEN + var callbackMock = mock(TbMsgCallback.class); + var msg = getTbMsgWithCallback(callbackMock); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setLastUpdateTs(System.currentTimeMillis()); + ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + mockGetMaxRuleNodeDebugModeDurationMinutes(); + + // WHEN + defaultTbContext.tellNext(msg, connection); + + // THEN + then(nodeCtxMock).should().getChainActor(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + then(mainCtxMock).should().getTenantProfileCache(); + then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, connection); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + checkTellNextCommonLogic(callbackMock, connection, msg); + } + + private static Stream givenDebugStrategyAllEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted() { + return failureAndSuccessConnection(); + } + + @Test + public void givenDebugStrategyAllEventsAndFailureAndSuccessConnection_whenTellNext_thenVerifyDebugOutputPersistedForAllEvents() { + // GIVEN + var callbackMock = mock(TbMsgCallback.class); + var msg = getTbMsgWithCallback(callbackMock); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setLastUpdateTs(System.currentTimeMillis()); + ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + mockGetMaxRuleNodeDebugModeDurationMinutes(); + + // WHEN + Set connections = failureAndSuccessConnection().collect(Collectors.toSet()); + defaultTbContext.tellNext(msg, connections); + + // THEN + then(nodeCtxMock).should().getChainActor(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + then(mainCtxMock).should().getTenantProfileCache(); + then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); + var nodeConnectionsCaptor = ArgumentCaptor.forClass(String.class); + int wantedNumberOfInvocations = connections.size(); + then(mainCtxMock).should(times(wantedNumberOfInvocations)).persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), eq(msg), nodeConnectionsCaptor.capture()); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + assertThat(nodeConnectionsCaptor.getAllValues()).hasSize(wantedNumberOfInvocations); + assertThat(nodeConnectionsCaptor.getAllValues()).containsExactlyInAnyOrderElementsOf(connections); + checkTellNextCommonLogic(callbackMock, connections, msg); + } + + private static Stream failureAndSuccessConnection() { + return Stream.of(TbNodeConnectionType.FAILURE, TbNodeConnectionType.SUCCESS); + } + + @Test + public void givenDebugStrategyOnlyFailureEventsAndFailureConnection_whenOutput_thenVerifyDebugOutputPersisted() { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ONLY_FAILURE_EVENTS); + given(msgMock.popFormStack()).willReturn(new TbMsgProcessingStackItem(RULE_CHAIN_ID, RULE_NODE_ID)); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + defaultTbContext.output(msgMock, TbNodeConnectionType.FAILURE); + + // THEN + checkOutputCommonLogic(msgMock, TbNodeConnectionType.FAILURE); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, TbNodeConnectionType.FAILURE); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + + @Test + public void givenDebugStrategyOnlyFailureEventsAndSuccessConnection_whenOutput_thenVerifyDebugOutputNotPersisted() { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ONLY_FAILURE_EVENTS); + given(msgMock.popFormStack()).willReturn(new TbMsgProcessingStackItem(RULE_CHAIN_ID, RULE_NODE_ID)); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + defaultTbContext.output(msgMock, TbNodeConnectionType.SUCCESS); + + // THEN + checkOutputCommonLogic(msgMock, TbNodeConnectionType.SUCCESS); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + + @ParameterizedTest + @ValueSource(strings = {TbNodeConnectionType.SUCCESS, TbNodeConnectionType.FAILURE}) + void givenDebugStrategyDisabled_whenOutput_thenVerifyDebugOutputNotPersisted(String nodeConnection) { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.DISABLED); + given(msgMock.popFormStack()).willReturn(new TbMsgProcessingStackItem(RULE_CHAIN_ID, RULE_NODE_ID)); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + defaultTbContext.output(msgMock, nodeConnection); + + // THEN + checkOutputCommonLogic(msgMock, nodeConnection); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + + @ParameterizedTest + @ValueSource(strings = {TbNodeConnectionType.SUCCESS, TbNodeConnectionType.FAILURE}) + void givenDebugStrategyAllEvents_whenOutput_thenVerifyDebugOutputPersisted(String nodeConnection) { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); + given(msgMock.popFormStack()).willReturn(new TbMsgProcessingStackItem(RULE_CHAIN_ID, RULE_NODE_ID)); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + mockGetMaxRuleNodeDebugModeDurationMinutes(); + + // WHEN + defaultTbContext.output(msgMock, nodeConnection); + + // THEN + checkOutputCommonLogic(msgMock, nodeConnection); + then(mainCtxMock).should().getTenantProfileCache(); + then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, nodeConnection); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + + @Test + public void givenEmptyStack_whenOutput_thenVerifyMsgAck() { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.DISABLED); + given(msgMock.popFormStack()).willReturn(null); + TbMsgCallback callbackMock = mock(TbMsgCallback.class); + given(msgMock.getCallback()).willReturn(callbackMock); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + + // WHEN + defaultTbContext.output(msgMock, TbNodeConnectionType.SUCCESS); + + // THEN + then(msgMock).should().popFormStack(); + then(callbackMock).should().onProcessingEnd(RULE_NODE_ID); + then(callbackMock).should().onSuccess(); + then(nodeCtxMock).should(never()).getChainActor(); + } + + @Test + public void givenEmptyStackAndDebugStrategyAllEvents_whenOutput_thenVerifyMsgAckAndDebugOutputPersisted() { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode.setLastUpdateTs(System.currentTimeMillis()); + given(msgMock.popFormStack()).willReturn(null); + TbMsgCallback callbackMock = mock(TbMsgCallback.class); + given(msgMock.getCallback()).willReturn(callbackMock); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + mockGetMaxRuleNodeDebugModeDurationMinutes(); + + // WHEN + defaultTbContext.output(msgMock, TbNodeConnectionType.SUCCESS); + + // THEN + then(msgMock).should().popFormStack(); + then(callbackMock).should().onProcessingEnd(RULE_NODE_ID); + then(callbackMock).should().onSuccess(); + then(nodeCtxMock).should(never()).getChainActor(); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, TbNodeConnectionType.ACK); + } + + @Test + public void givenDebugStrategyOnlyFailureEvents_whenEnqueueForTellFailure_thenVerifyDebugOutputPersisted() { + // GIVEN + var msg = getTbMsgWithQueueName(); + var tpi = new TopicPartitionInfo(DataConstants.MAIN_QUEUE_TOPIC, TENANT_ID, 0, true); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ONLY_FAILURE_EVENTS); + var tbClusterServiceMock = mock(TbClusterService.class); + + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(mainCtxMock.resolve(any(ServiceType.class), anyString(), any(TenantId.class), any(EntityId.class))).willReturn(tpi); + given(mainCtxMock.getClusterService()).willReturn(tbClusterServiceMock); + + // WHEN + defaultTbContext.enqueueForTellFailure(msg, EXCEPTION); + + // THEN + then(mainCtxMock).should().resolve(ServiceType.TB_RULE_ENGINE, DataConstants.MAIN_QUEUE_NAME, TENANT_ID, TENANT_ID); + TbMsg expectedTbMsg = TbMsg.newMsg(msg, msg.getQueueName(), RULE_CHAIN_ID, RULE_NODE_ID); + checkEnqueueForTellFailurePushMsgToRuleEngine(tbClusterServiceMock, tpi, expectedTbMsg); + ArgumentCaptor tbMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); + then(mainCtxMock).should().persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), tbMsgCaptor.capture(), eq(TbNodeConnectionType.FAILURE), isNull(), eq(EXCEPTION_MSG)); + TbMsg actualTbMsg = tbMsgCaptor.getValue(); + assertThat(actualTbMsg).usingRecursiveComparison() + .ignoringFields("id", "ctx") + .isEqualTo(expectedTbMsg); + then(mainCtxMock).should().getClusterService(); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(tbClusterServiceMock).shouldHaveNoMoreInteractions(); + } + + @Test + public void givenDebugStrategyDisabled_whenEnqueueForTellFailure_thenVerifyDebugOutputNotPersisted() { + // GIVEN + var msg = getTbMsgWithQueueName(); + var tpi = new TopicPartitionInfo(DataConstants.MAIN_QUEUE_TOPIC, TENANT_ID, 0, true); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.DISABLED); + var tbClusterServiceMock = mock(TbClusterService.class); + + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(mainCtxMock.resolve(any(ServiceType.class), anyString(), any(TenantId.class), any(EntityId.class))).willReturn(tpi); + given(mainCtxMock.getClusterService()).willReturn(tbClusterServiceMock); + + // WHEN + defaultTbContext.enqueueForTellFailure(msg, EXCEPTION); + + // THEN + then(mainCtxMock).should().resolve(ServiceType.TB_RULE_ENGINE, DataConstants.MAIN_QUEUE_NAME, TENANT_ID, TENANT_ID); + TbMsg expectedTbMsg = TbMsg.newMsg(msg, msg.getQueueName(), RULE_CHAIN_ID, RULE_NODE_ID); + checkEnqueueForTellFailurePushMsgToRuleEngine(tbClusterServiceMock, tpi, expectedTbMsg); + then(mainCtxMock).should().getClusterService(); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(tbClusterServiceMock).shouldHaveNoMoreInteractions(); + } + + @Test + public void givenDebugStrategyAllEvents_whenEnqueueForTellFailure_thenVerifyDebugOutputPersisted() { + // GIVEN + var msg = getTbMsgWithQueueName(); + var tpi = new TopicPartitionInfo(DataConstants.MAIN_QUEUE_TOPIC, TENANT_ID, 0, true); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode.setLastUpdateTs(System.currentTimeMillis()); + var tbClusterServiceMock = mock(TbClusterService.class); + + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(mainCtxMock.resolve(any(ServiceType.class), anyString(), any(TenantId.class), any(EntityId.class))).willReturn(tpi); + given(mainCtxMock.getClusterService()).willReturn(tbClusterServiceMock); + mockGetMaxRuleNodeDebugModeDurationMinutes(); + + // WHEN + defaultTbContext.enqueueForTellFailure(msg, EXCEPTION); + + // THEN + then(mainCtxMock).should().resolve(ServiceType.TB_RULE_ENGINE, DataConstants.MAIN_QUEUE_NAME, TENANT_ID, TENANT_ID); + TbMsg expectedTbMsg = TbMsg.newMsg(msg, msg.getQueueName(), RULE_CHAIN_ID, RULE_NODE_ID); + checkEnqueueForTellFailurePushMsgToRuleEngine(tbClusterServiceMock, tpi, expectedTbMsg); + ArgumentCaptor tbMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); + then(mainCtxMock).should().persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), tbMsgCaptor.capture(), eq(TbNodeConnectionType.FAILURE), isNull(), eq(EXCEPTION_MSG)); + TbMsg actualTbMsg = tbMsgCaptor.getValue(); + assertThat(actualTbMsg).usingRecursiveComparison() + .ignoringFields("id", "ctx") + .isEqualTo(expectedTbMsg); + then(mainCtxMock).should().getClusterService(); + then(mainCtxMock).should().getTenantProfileCache(); + then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(tbClusterServiceMock).shouldHaveNoMoreInteractions(); + } + + @Test + public void givenInvalidMsg_whenEnqueueForTellFailure_thenDoNothing() { + // GIVEN + var msgMock = mock(TbMsg.class); + var tpi = new TopicPartitionInfo(DataConstants.MAIN_QUEUE_TOPIC, TENANT_ID, 0, true); + + given(msgMock.getOriginator()).willReturn(TENANT_ID); + given(msgMock.getQueueName()).willReturn(DataConstants.MAIN_QUEUE_NAME); + given(msgMock.isValid()).willReturn(false); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(mainCtxMock.resolve(any(ServiceType.class), anyString(), any(TenantId.class), any(EntityId.class))).willReturn(tpi); + + // WHEN + defaultTbContext.enqueueForTellFailure(msgMock, EXCEPTION); + + // THEN + then(msgMock).should(times(2)).getQueueName(); + then(msgMock).should().getOriginator(); + then(msgMock).should().isValid(); + then(msgMock).shouldHaveNoMoreInteractions(); + + then(mainCtxMock).should().resolve(ServiceType.TB_RULE_ENGINE, DataConstants.MAIN_QUEUE_NAME, TENANT_ID, TENANT_ID); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + + then(nodeCtxMock).should(times(2)).getTenantId(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + then(chainActorMock).shouldHaveNoInteractions(); + } + + @MethodSource + @ParameterizedTest + void givenDebugStrategyOptions_whenEnqueueForTellNext_thenVerifyDebugOutputPersistedOnlyForAllEventsDebugStrategy(DebugStrategy debugStrategy, String connectionType) { + // GIVEN + var msg = getTbMsgWithQueueName(); + var tpi = new TopicPartitionInfo(DataConstants.MAIN_QUEUE_TOPIC, TENANT_ID, 0, true); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(debugStrategy); + ruleNode.setLastUpdateTs(System.currentTimeMillis()); + var tbClusterServiceMock = mock(TbClusterService.class); + + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(mainCtxMock.resolve(any(ServiceType.class), anyString(), any(TenantId.class), any(EntityId.class))).willReturn(tpi); + given(mainCtxMock.getClusterService()).willReturn(tbClusterServiceMock); + if (DebugStrategy.ALL_EVENTS.equals(debugStrategy)) { + mockGetMaxRuleNodeDebugModeDurationMinutes(); + } + + // WHEN + defaultTbContext.enqueueForTellNext(msg, connectionType); + + // THEN + then(mainCtxMock).should().resolve(ServiceType.TB_RULE_ENGINE, DataConstants.MAIN_QUEUE_NAME, TENANT_ID, TENANT_ID); + TbMsg expectedTbMsg = TbMsg.newMsg(msg, msg.getQueueName(), RULE_CHAIN_ID, RULE_NODE_ID); + + ArgumentCaptor toRuleEngineMsgCaptor = ArgumentCaptor.forClass(ToRuleEngineMsg.class); + ArgumentCaptor simpleTbQueueCallbackCaptor = ArgumentCaptor.forClass(SimpleTbQueueCallback.class); + then(tbClusterServiceMock).should().pushMsgToRuleEngine(eq(tpi), notNull(UUID.class), toRuleEngineMsgCaptor.capture(), simpleTbQueueCallbackCaptor.capture()); + + ToRuleEngineMsg actualToRuleEngineMsg = toRuleEngineMsgCaptor.getValue(); + assertThat(actualToRuleEngineMsg).usingRecursiveComparison() + .ignoringFields("tbMsg_") + .isEqualTo(ToRuleEngineMsg.newBuilder() + .setTenantIdMSB(TENANT_ID.getId().getMostSignificantBits()) + .setTenantIdLSB(TENANT_ID.getId().getLeastSignificantBits()) + .setTbMsg(TbMsg.toByteString(expectedTbMsg)) + .addAllRelationTypes(List.of(connectionType)).build()); + + var simpleTbQueueCallback = simpleTbQueueCallbackCaptor.getValue(); + assertThat(simpleTbQueueCallback).isNotNull(); + simpleTbQueueCallback.onSuccess(null); + + if (DebugStrategy.ALL_EVENTS.equals(debugStrategy)) { + then(mainCtxMock).should().getTenantProfileCache(); + then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); + ArgumentCaptor tbMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); + then(mainCtxMock).should().persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), tbMsgCaptor.capture(), eq(connectionType), isNull(), isNull()); + TbMsg actualTbMsg = tbMsgCaptor.getValue(); + assertThat(actualTbMsg).usingRecursiveComparison() + .ignoringFields("id", "ctx") + .isEqualTo(expectedTbMsg); + } + then(mainCtxMock).should().getClusterService(); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(tbClusterServiceMock).shouldHaveNoMoreInteractions(); + } + + @MethodSource + @ParameterizedTest + void givenDebugStrategyOptions_whenEnqueue_thenVerifyDebugOutputPersistedOnlyForAllEventsDebugStrategy(DebugStrategy debugStrategy) { + // GIVEN + var msg = getTbMsgWithQueueName(); + var tpi = new TopicPartitionInfo(DataConstants.MAIN_QUEUE_TOPIC, TENANT_ID, 0, true); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setQueueName(DataConstants.MAIN_QUEUE_NAME); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(debugStrategy); + ruleNode.setLastUpdateTs(System.currentTimeMillis()); + var tbClusterServiceMock = mock(TbClusterService.class); + + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(mainCtxMock.resolve(any(ServiceType.class), anyString(), any(TenantId.class), any(EntityId.class))).willReturn(tpi); + given(mainCtxMock.getClusterService()).willReturn(tbClusterServiceMock); + if (DebugStrategy.ALL_EVENTS.equals(debugStrategy)) { + mockGetMaxRuleNodeDebugModeDurationMinutes(); + } + + Consumer onFailure = mock(Consumer.class); + Runnable onSuccess = mock(Runnable.class); + + // WHEN + defaultTbContext.enqueue(msg, onSuccess, onFailure); + + // THEN + then(mainCtxMock).should().resolve(ServiceType.TB_RULE_ENGINE, DataConstants.MAIN_QUEUE_NAME, TENANT_ID, TENANT_ID); + TbMsg expectedTbMsg = TbMsg.newMsg(msg, msg.getQueueName(), RULE_CHAIN_ID, RULE_NODE_ID); + + ArgumentCaptor toRuleEngineMsgCaptor = ArgumentCaptor.forClass(ToRuleEngineMsg.class); + ArgumentCaptor simpleTbQueueCallbackCaptor = ArgumentCaptor.forClass(SimpleTbQueueCallback.class); + then(tbClusterServiceMock).should().pushMsgToRuleEngine(eq(tpi), notNull(UUID.class), toRuleEngineMsgCaptor.capture(), simpleTbQueueCallbackCaptor.capture()); + + ToRuleEngineMsg actualToRuleEngineMsg = toRuleEngineMsgCaptor.getValue(); + assertThat(actualToRuleEngineMsg).usingRecursiveComparison() + .ignoringFields("tbMsg_") + .isEqualTo(ToRuleEngineMsg.newBuilder() + .setTenantIdMSB(TENANT_ID.getId().getMostSignificantBits()) + .setTenantIdLSB(TENANT_ID.getId().getLeastSignificantBits()) + .setTbMsg(TbMsg.toByteString(expectedTbMsg)) + .build()); + + var simpleTbQueueCallback = simpleTbQueueCallbackCaptor.getValue(); + assertThat(simpleTbQueueCallback).isNotNull(); + simpleTbQueueCallback.onSuccess(null); + + if (DebugStrategy.ALL_EVENTS.equals(debugStrategy)) { + then(mainCtxMock).should().getTenantProfileCache(); + then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); + then(mainCtxMock).should().persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), eq(msg), eq(TbNodeConnectionType.TO_ROOT_RULE_CHAIN)); + } + then(mainCtxMock).should().getClusterService(); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(tbClusterServiceMock).shouldHaveNoMoreInteractions(); + } + + @Test + public void givenDebugStrategyOnlyFailures_whenTellFailure_thenVerifyDebugOutputPersisted() { + // GIVEN + var msg = getTbMsg(); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ONLY_FAILURE_EVENTS); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + defaultTbContext.tellFailure(msg, EXCEPTION); + + // THEN + var expectedRuleNodeToRuleChainTellNextMsg = new RuleNodeToRuleChainTellNextMsg( + RULE_CHAIN_ID, + RULE_NODE_ID, + Collections.singleton(TbNodeConnectionType.FAILURE), + msg, + EXCEPTION_MSG + ); + then(chainActorMock).should().tell(expectedRuleNodeToRuleChainTellNextMsg); + then(chainActorMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).should().getChainActor(); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, TbNodeConnectionType.FAILURE, EXCEPTION); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + + @Test + public void givenDebugStrategyDisabled_whenTellFailure_thenVerifyDebugOutputNotPersisted() { + // GIVEN + var msg = getTbMsg(); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.DISABLED); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + defaultTbContext.tellFailure(msg, EXCEPTION); + + // THEN + var expectedRuleNodeToRuleChainTellNextMsg = new RuleNodeToRuleChainTellNextMsg( + RULE_CHAIN_ID, + RULE_NODE_ID, + Collections.singleton(TbNodeConnectionType.FAILURE), + msg, + EXCEPTION_MSG + ); + then(chainActorMock).should().tell(expectedRuleNodeToRuleChainTellNextMsg); + then(chainActorMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).should().getChainActor(); + then(mainCtxMock).shouldHaveNoInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + + @Test + public void givenDebugStrategyAllEvents_whenTellFailure_thenVerifyDebugOutputPersisted() { + // GIVEN + var msg = getTbMsg(); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode.setLastUpdateTs(System.currentTimeMillis()); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + mockGetMaxRuleNodeDebugModeDurationMinutes(); + + // WHEN + defaultTbContext.tellFailure(msg, EXCEPTION); + + // THEN + var expectedRuleNodeToRuleChainTellNextMsg = new RuleNodeToRuleChainTellNextMsg( + RULE_CHAIN_ID, + RULE_NODE_ID, + Collections.singleton(TbNodeConnectionType.FAILURE), + msg, + EXCEPTION_MSG + ); + then(chainActorMock).should().tell(expectedRuleNodeToRuleChainTellNextMsg); + then(chainActorMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).should().getChainActor(); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, TbNodeConnectionType.FAILURE, EXCEPTION); + then(mainCtxMock).should().getTenantProfileCache(); + then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + + private void checkTellNextCommonLogic(TbMsgCallback callbackMock, String nodeConnection, TbMsg msg) { + checkTellNextCommonLogic(callbackMock, Collections.singleton(nodeConnection), msg); + } + + private void checkTellNextCommonLogic(TbMsgCallback callbackMock, Set nodeConnections, TbMsg msg) { + then(callbackMock).should().onProcessingEnd(RULE_NODE_ID); + then(callbackMock).shouldHaveNoMoreInteractions(); + var expectedRuleNodeToRuleChainTellNextMsg = new RuleNodeToRuleChainTellNextMsg( + RULE_CHAIN_ID, + RULE_NODE_ID, + nodeConnections, + msg, + null); + then(chainActorMock).should().tell(expectedRuleNodeToRuleChainTellNextMsg); + then(chainActorMock).shouldHaveNoMoreInteractions(); + } + + private void checkOutputCommonLogic(TbMsg msg, String nodeConnection) { + then(msg).should().popFormStack(); + var expectedRuleChainOutputMsg = new RuleChainOutputMsg( + RULE_CHAIN_ID, + RULE_NODE_ID, + nodeConnection, + msg); + then(chainActorMock).should().tell(expectedRuleChainOutputMsg); + then(chainActorMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).should().getChainActor(); + } + + private void checkEnqueueForTellFailurePushMsgToRuleEngine(TbClusterService tbClusterService, TopicPartitionInfo tpi, TbMsg expectedTbMsg) { + ArgumentCaptor toRuleEngineMsgCaptor = ArgumentCaptor.forClass(ToRuleEngineMsg.class); + ArgumentCaptor simpleTbQueueCallbackCaptor = ArgumentCaptor.forClass(SimpleTbQueueCallback.class); + then(tbClusterService).should().pushMsgToRuleEngine(eq(tpi), notNull(UUID.class), toRuleEngineMsgCaptor.capture(), simpleTbQueueCallbackCaptor.capture()); + + ToRuleEngineMsg actualToRuleEngineMsg = toRuleEngineMsgCaptor.getValue(); + assertThat(actualToRuleEngineMsg).usingRecursiveComparison() + .ignoringFields("tbMsg_") + .isEqualTo(ToRuleEngineMsg.newBuilder() + .setTenantIdMSB(TENANT_ID.getId().getMostSignificantBits()) + .setTenantIdLSB(TENANT_ID.getId().getLeastSignificantBits()) + .setTbMsg(TbMsg.toByteString(expectedTbMsg)) + .setFailureMessage(EXCEPTION_MSG) + .addAllRelationTypes(List.of(TbNodeConnectionType.FAILURE)).build()); + + var simpleTbQueueCallback = simpleTbQueueCallbackCaptor.getValue(); + assertThat(simpleTbQueueCallback).isNotNull(); + simpleTbQueueCallback.onSuccess(null); + } + + private static Stream givenDebugStrategyOptions_whenEnqueueForTellNext_thenVerifyDebugOutputPersistedOnlyForAllEventsDebugStrategy() { + return Stream.of( + Arguments.of(DebugStrategy.ALL_EVENTS, TbNodeConnectionType.OTHER), + Arguments.of(DebugStrategy.ONLY_FAILURE_EVENTS, TbNodeConnectionType.TRUE), + Arguments.of(DebugStrategy.DISABLED, TbNodeConnectionType.FALSE) + ); + } + + private static Stream givenDebugStrategyOptions_whenEnqueue_thenVerifyDebugOutputPersistedOnlyForAllEventsDebugStrategy() { + return Stream.of( + Arguments.of(DebugStrategy.ALL_EVENTS), + Arguments.of(DebugStrategy.ONLY_FAILURE_EVENTS), + Arguments.of(DebugStrategy.DISABLED) + ); + } + + private TbMsg getTbMsgWithCallback(TbMsgCallback callback) { + return TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, TENANT_ID, TbMsgMetaData.EMPTY, TbMsg.EMPTY_STRING, callback); + } + + private TbMsg getTbMsgWithQueueName() { + return TbMsg.newMsg(DataConstants.MAIN_QUEUE_NAME, TbMsgType.POST_TELEMETRY_REQUEST, TENANT_ID, TbMsgMetaData.EMPTY, TbMsg.EMPTY_STRING); + } + + private TbMsg getTbMsg() { + return TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, TENANT_ID, TbMsgMetaData.EMPTY, TbMsg.EMPTY_STRING); + } + + private void mockGetMaxRuleNodeDebugModeDurationMinutes() { + var tbTenantProfileCacheMock = mock(TbTenantProfileCache.class); + var tenantProfileMock = mock(TenantProfile.class); + var tenantProfileDataMock = mock(TenantProfileData.class); + var tenantProfileConfigurationMock = mock(TenantProfileConfiguration.class); + + given(mainCtxMock.getTenantProfileCache()).willReturn(tbTenantProfileCacheMock); + given(tbTenantProfileCacheMock.get(TENANT_ID)).willReturn(tenantProfileMock); + given(tenantProfileMock.getProfileData()).willReturn(tenantProfileDataMock); + given(tenantProfileDataMock.getConfiguration()).willReturn(tenantProfileConfigurationMock); + given(tenantProfileConfigurationMock.getMaxRuleNodeDebugModeDurationMinutes(anyInt())).willReturn(15); + } + +} diff --git a/application/src/test/java/org/thingsboard/server/edge/AbstractEdgeTest.java b/application/src/test/java/org/thingsboard/server/edge/AbstractEdgeTest.java index 5484c29305..a09ca1d148 100644 --- a/application/src/test/java/org/thingsboard/server/edge/AbstractEdgeTest.java +++ b/application/src/test/java/org/thingsboard/server/edge/AbstractEdgeTest.java @@ -71,6 +71,7 @@ import org.thingsboard.server.common.data.query.EntityKeyValueType; import org.thingsboard.server.common.data.query.FilterPredicateValue; import org.thingsboard.server.common.data.query.NumericFilterPredicate; import org.thingsboard.server.common.data.queue.Queue; +import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleChainType; @@ -103,7 +104,6 @@ import org.thingsboard.server.gen.edge.v1.UserUpdateMsg; import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.Random; import java.util.TreeMap; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -125,8 +125,6 @@ abstract public class AbstractEdgeTest extends AbstractControllerTest { protected EdgeImitator edgeImitator; protected Edge edge; - private Random random = new Random(); - @Autowired protected EdgeEventService edgeEventService; @@ -210,7 +208,7 @@ abstract public class AbstractEdgeTest extends AbstractControllerTest { protected void updateRootRuleChainMetadata() throws Exception { RuleChainId rootRuleChainId = getEdgeRootRuleChainId(); RuleChainMetaData rootRuleChainMetadata = doGet("/api/ruleChain/" + rootRuleChainId.getId().toString() + "/metadata", RuleChainMetaData.class); - rootRuleChainMetadata.getNodes().forEach(n -> n.setDebugMode(random.nextBoolean())); + rootRuleChainMetadata.getNodes().forEach(n -> n.setDebugStrategy(DebugStrategy.ALL_EVENTS)); doPost("/api/ruleChain/metadata", rootRuleChainMetadata, RuleChainMetaData.class); } diff --git a/application/src/test/java/org/thingsboard/server/edge/RuleChainEdgeTest.java b/application/src/test/java/org/thingsboard/server/edge/RuleChainEdgeTest.java index eaf4468156..2dc2bd42f3 100644 --- a/application/src/test/java/org/thingsboard/server/edge/RuleChainEdgeTest.java +++ b/application/src/test/java/org/thingsboard/server/edge/RuleChainEdgeTest.java @@ -23,6 +23,7 @@ import org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration; import org.thingsboard.rule.engine.util.TbMsgSource; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.id.RuleChainId; +import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleChainType; @@ -228,7 +229,7 @@ public class RuleChainEdgeTest extends AbstractEdgeTest { // update metadata for root rule chain edgeImitator.expectMessageAmount(1); - metaData.getNodes().forEach(n -> n.setDebugMode(true)); + metaData.getNodes().forEach(n -> n.setDebugStrategy(DebugStrategy.ALL_EVENTS)); doPost("/api/ruleChain/metadata", metaData, RuleChainMetaData.class); Assert.assertTrue(edgeImitator.waitForMessages()); ruleChainUpdateMsgOpt = edgeImitator.findMessageByType(RuleChainUpdateMsg.class); diff --git a/application/src/test/java/org/thingsboard/server/rules/flow/AbstractRuleEngineFlowIntegrationTest.java b/application/src/test/java/org/thingsboard/server/rules/flow/AbstractRuleEngineFlowIntegrationTest.java index 9e36fed677..3af4c012e0 100644 --- a/application/src/test/java/org/thingsboard/server/rules/flow/AbstractRuleEngineFlowIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/rules/flow/AbstractRuleEngineFlowIntegrationTest.java @@ -43,6 +43,7 @@ import org.thingsboard.server.common.data.kv.StringDataEntry; import org.thingsboard.server.common.data.msg.TbMsgType; import org.thingsboard.server.common.data.msg.TbNodeConnectionType; import org.thingsboard.server.common.data.page.PageData; +import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.NodeConnectionInfo; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; @@ -142,7 +143,7 @@ public abstract class AbstractRuleEngineFlowIntegrationTest extends AbstractRule ruleNode1.setName("Simple Rule Node 1"); ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode1.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode1.setDebugMode(true); + ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setFetchTo(TbMsgSource.METADATA); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); @@ -152,7 +153,7 @@ public abstract class AbstractRuleEngineFlowIntegrationTest extends AbstractRule ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugMode(true); + ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setFetchTo(TbMsgSource.METADATA); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); @@ -248,7 +249,7 @@ public abstract class AbstractRuleEngineFlowIntegrationTest extends AbstractRule ruleNode1.setName("Simple Rule Node 1"); ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode1.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode1.setDebugMode(true); + ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setFetchTo(TbMsgSource.METADATA); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); @@ -257,7 +258,7 @@ public abstract class AbstractRuleEngineFlowIntegrationTest extends AbstractRule RuleNode ruleNode12 = new RuleNode(); ruleNode12.setName("Simple Rule Node 1"); ruleNode12.setType(org.thingsboard.rule.engine.flow.TbRuleChainInputNode.class.getName()); - ruleNode12.setDebugMode(true); + ruleNode12.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbRuleChainInputNodeConfiguration configuration12 = new TbRuleChainInputNodeConfiguration(); configuration12.setRuleChainId(secondaryRuleChain.getId().getId().toString()); ruleNode12.setConfiguration(JacksonUtil.valueToTree(configuration12)); @@ -282,7 +283,7 @@ public abstract class AbstractRuleEngineFlowIntegrationTest extends AbstractRule ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugMode(true); + ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setFetchTo(TbMsgSource.METADATA); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); diff --git a/application/src/test/java/org/thingsboard/server/rules/lifecycle/AbstractRuleEngineLifecycleIntegrationTest.java b/application/src/test/java/org/thingsboard/server/rules/lifecycle/AbstractRuleEngineLifecycleIntegrationTest.java index 4c4e887644..dd148748ca 100644 --- a/application/src/test/java/org/thingsboard/server/rules/lifecycle/AbstractRuleEngineLifecycleIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/rules/lifecycle/AbstractRuleEngineLifecycleIntegrationTest.java @@ -36,6 +36,7 @@ import org.thingsboard.server.common.data.event.EventType; import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; import org.thingsboard.server.common.data.kv.StringDataEntry; import org.thingsboard.server.common.data.msg.TbMsgType; +import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleNode; @@ -97,7 +98,7 @@ public abstract class AbstractRuleEngineLifecycleIntegrationTest extends Abstrac ruleNode.setName("Simple Rule Node"); ruleNode.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode.setDebugMode(true); + ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration = new TbGetAttributesNodeConfiguration(); configuration.setFetchTo(TbMsgSource.METADATA); configuration.setServerAttributeNames(Collections.singletonList("serverAttributeKey")); diff --git a/application/src/test/java/org/thingsboard/server/service/housekeeper/HousekeeperServiceTest.java b/application/src/test/java/org/thingsboard/server/service/housekeeper/HousekeeperServiceTest.java index d3b1916def..752624fa51 100644 --- a/application/src/test/java/org/thingsboard/server/service/housekeeper/HousekeeperServiceTest.java +++ b/application/src/test/java/org/thingsboard/server/service/housekeeper/HousekeeperServiceTest.java @@ -58,6 +58,7 @@ import org.thingsboard.server.common.data.msg.TbNodeConnectionType; import org.thingsboard.server.common.data.page.TimePageLink; import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.RelationTypeGroup; +import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleChainType; @@ -462,7 +463,7 @@ public class HousekeeperServiceTest extends AbstractControllerTest { ruleNode1.setName("Simple Rule Node 1"); ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode1.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode1.setDebugMode(true); + ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); ruleNode1.setConfiguration(JacksonUtil.valueToTree(configuration1)); @@ -471,7 +472,7 @@ public class HousekeeperServiceTest extends AbstractControllerTest { ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugMode(true); + ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); ruleNode2.setConfiguration(JacksonUtil.valueToTree(configuration2)); diff --git a/application/src/test/java/org/thingsboard/server/service/sync/ie/ExportImportServiceSqlTest.java b/application/src/test/java/org/thingsboard/server/service/sync/ie/ExportImportServiceSqlTest.java index 55f15516f4..06bc670804 100644 --- a/application/src/test/java/org/thingsboard/server/service/sync/ie/ExportImportServiceSqlTest.java +++ b/application/src/test/java/org/thingsboard/server/service/sync/ie/ExportImportServiceSqlTest.java @@ -65,6 +65,7 @@ import org.thingsboard.server.common.data.ota.OtaPackageType; import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.RelationTypeGroup; +import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleChainType; @@ -471,7 +472,7 @@ public class ExportImportServiceSqlTest extends AbstractControllerTest { RuleNode ruleNode1 = new RuleNode(); ruleNode1.setName("Generator 1"); ruleNode1.setType(TbMsgGeneratorNode.class.getName()); - ruleNode1.setDebugMode(true); + ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbMsgGeneratorNodeConfiguration configuration1 = new TbMsgGeneratorNodeConfiguration(); configuration1.setOriginatorType(originatorId.getEntityType()); configuration1.setOriginatorId(originatorId.getId().toString()); @@ -481,7 +482,7 @@ public class ExportImportServiceSqlTest extends AbstractControllerTest { ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugMode(true); + ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); ruleNode2.setConfiguration(JacksonUtil.valueToTree(configuration2)); @@ -510,7 +511,7 @@ public class ExportImportServiceSqlTest extends AbstractControllerTest { ruleNode1.setName("Simple Rule Node 1"); ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode1.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode1.setDebugMode(true); + ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); ruleNode1.setConfiguration(JacksonUtil.valueToTree(configuration1)); @@ -519,7 +520,7 @@ public class ExportImportServiceSqlTest extends AbstractControllerTest { ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugMode(true); + ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); ruleNode2.setConfiguration(JacksonUtil.valueToTree(configuration2)); diff --git a/application/src/test/java/org/thingsboard/server/service/sync/vc/VersionControlTest.java b/application/src/test/java/org/thingsboard/server/service/sync/vc/VersionControlTest.java index 2e01d700a9..21f6363f5f 100644 --- a/application/src/test/java/org/thingsboard/server/service/sync/vc/VersionControlTest.java +++ b/application/src/test/java/org/thingsboard/server/service/sync/vc/VersionControlTest.java @@ -65,6 +65,7 @@ import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.RelationTypeGroup; +import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleChainType; @@ -870,7 +871,7 @@ public class VersionControlTest extends AbstractControllerTest { RuleNode ruleNode1 = new RuleNode(); ruleNode1.setName("Generator 1"); ruleNode1.setType(TbMsgGeneratorNode.class.getName()); - ruleNode1.setDebugMode(true); + ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbMsgGeneratorNodeConfiguration configuration1 = new TbMsgGeneratorNodeConfiguration(); configuration1.setOriginatorType(originatorId.getEntityType()); configuration1.setOriginatorId(originatorId.getId().toString()); @@ -880,7 +881,7 @@ public class VersionControlTest extends AbstractControllerTest { ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugMode(true); + ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); ruleNode2.setConfiguration(JacksonUtil.valueToTree(configuration2)); @@ -908,7 +909,7 @@ public class VersionControlTest extends AbstractControllerTest { ruleNode1.setName("Simple Rule Node 1"); ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode1.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode1.setDebugMode(true); + ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); ruleNode1.setConfiguration(JacksonUtil.valueToTree(configuration1)); @@ -917,7 +918,7 @@ public class VersionControlTest extends AbstractControllerTest { ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugMode(true); + ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); ruleNode2.setConfiguration(JacksonUtil.valueToTree(configuration2)); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/msg/TbNodeConnectionType.java b/common/data/src/main/java/org/thingsboard/server/common/data/msg/TbNodeConnectionType.java index 2f466c76b0..a6d56ec221 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/msg/TbNodeConnectionType.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/msg/TbNodeConnectionType.java @@ -23,9 +23,13 @@ public final class TbNodeConnectionType { public static final String SUCCESS = "Success"; public static final String FAILURE = "Failure"; + public static final String ACK = "ACK"; + public static final String TRUE = "True"; public static final String FALSE = "False"; public static final String OTHER = "Other"; + public static final String TO_ROOT_RULE_CHAIN = "To Root Rule Chain"; + } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java b/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java new file mode 100644 index 0000000000..333ee9c670 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java @@ -0,0 +1,72 @@ +/** + * Copyright © 2016-2024 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. + */ +package org.thingsboard.server.common.data.rule; + +import lombok.Getter; +import org.thingsboard.server.common.data.msg.TbNodeConnectionType; + +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@Getter +public enum DebugStrategy { + DISABLED(0), ALL_EVENTS(1), ONLY_FAILURE_EVENTS(2); + + private final int protoNumber; + + DebugStrategy(int protoNumber) { + this.protoNumber = protoNumber; + } + + public boolean shouldPersistDebugInput(long lastUpdateTs, long msgTs, int debugModeDurationMinutes) { + return isAllEventsStrategyAndMsgTsWithinDebugDuration(lastUpdateTs, msgTs, debugModeDurationMinutes); + } + + public boolean shouldPersistDebugOutputForAllEvents(long lastUpdateTs, long msgTs, int debugModeDurationMinutes) { + return isAllEventsStrategyAndMsgTsWithinDebugDuration(lastUpdateTs, msgTs, debugModeDurationMinutes); + } + + public boolean shouldPersistDebugForFailureEventOnly(long lastUpdateTs, long msgTs, int debugModeDurationMinutes) { + return shouldPersistDebugOutputForAllEvents(lastUpdateTs, msgTs, debugModeDurationMinutes) || isFailureOnlyStrategy(); + } + + public boolean shouldPersistDebugForFailureEventOnly(Set nodeConnections) { + return isFailureOnlyStrategy() && nodeConnections.contains(TbNodeConnectionType.FAILURE); + } + + public boolean shouldPersistDebugForFailureEventOnly(String nodeConnection) { + return isFailureOnlyStrategy() && TbNodeConnectionType.FAILURE.equals(nodeConnection); + } + + private boolean isFailureOnlyStrategy() { + return DebugStrategy.ONLY_FAILURE_EVENTS.equals(this); + } + + private boolean isAllEventsStrategyAndMsgTsWithinDebugDuration(long lastUpdateTs, long msgTs, int debugModeDurationMinutes) { + if (!DebugStrategy.ALL_EVENTS.equals(this)) { + return false; + } + return isMsgTsWithinDebugDuration(lastUpdateTs, msgTs, debugModeDurationMinutes); + } + + private boolean isMsgTsWithinDebugDuration(long lastUpdateTs, long msgCreationTs, int debugModeDurationMinutes) { + if (debugModeDurationMinutes <= 0) { + return true; + } + return msgCreationTs < lastUpdateTs + TimeUnit.MINUTES.toMillis(debugModeDurationMinutes); + } + +} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java b/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java index 542debcfdd..c147da1359 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java @@ -45,15 +45,17 @@ public class RuleNode extends BaseDataWithAdditionalInfo implements @Length(fieldName = "name") @Schema(description = "User defined name of the rule node. Used on UI and for logging. ", example = "Process sensor reading") private String name; - @Schema(description = "Enable/disable debug. ", example = "false") - private boolean debugMode; + @Schema(description = "Timestamp of the last rule node update.") + private long lastUpdateTs; + @Schema(description = "Debug strategy. ", example = "ALL_EVENTS") + private DebugStrategy debugStrategy; @Schema(description = "Enable/disable singleton mode. ", example = "false") private boolean singletonMode; @Schema(description = "Queue name. ", example = "Main") private String queueName; @Schema(description = "Version of rule node configuration. ", example = "0") private int configurationVersion; - @Schema(description = "JSON with the rule node configuration. Structure depends on the rule node implementation.", implementation = com.fasterxml.jackson.databind.JsonNode.class) + @Schema(description = "JSON with the rule node configuration. Structure depends on the rule node implementation.", implementation = JsonNode.class) private transient JsonNode configuration; @JsonIgnore private byte[] configurationBytes; @@ -73,7 +75,8 @@ public class RuleNode extends BaseDataWithAdditionalInfo implements this.ruleChainId = ruleNode.getRuleChainId(); this.type = ruleNode.getType(); this.name = ruleNode.getName(); - this.debugMode = ruleNode.isDebugMode(); + this.lastUpdateTs = ruleNode.getLastUpdateTs(); + this.debugStrategy = ruleNode.getDebugStrategy(); this.singletonMode = ruleNode.isSingletonMode(); this.setConfiguration(ruleNode.getConfiguration()); this.externalId = ruleNode.getExternalId(); @@ -84,6 +87,10 @@ public class RuleNode extends BaseDataWithAdditionalInfo implements return name; } + public DebugStrategy getDebugStrategy() { + return debugStrategy == null ? DebugStrategy.DISABLED : debugStrategy; + } + public JsonNode getConfiguration() { return BaseDataWithAdditionalInfo.getJson(() -> configuration, () -> configurationBytes); } @@ -93,9 +100,9 @@ public class RuleNode extends BaseDataWithAdditionalInfo implements } @Schema(description = "JSON object with the Rule Node Id. " + - "Specify this field to update the Rule Node. " + - "Referencing non-existing Rule Node Id will cause error. " + - "Omit this field to create new rule node.") + "Specify this field to update the Rule Node. " + + "Referencing non-existing Rule Node Id will cause error. " + + "Omit this field to create new rule node.") @Override public RuleNodeId getId() { return super.getId(); @@ -107,10 +114,9 @@ public class RuleNode extends BaseDataWithAdditionalInfo implements return super.getCreatedTime(); } - @Schema(description = "Additional parameters of the rule node. Contains 'layoutX' and 'layoutY' properties for visualization.", implementation = com.fasterxml.jackson.databind.JsonNode.class) + @Schema(description = "Additional parameters of the rule node. Contains 'layoutX' and 'layoutY' properties for visualization.", implementation = JsonNode.class) @Override public JsonNode getAdditionalInfo() { return super.getAdditionalInfo(); } - } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/tenant/profile/DefaultTenantProfileConfiguration.java b/common/data/src/main/java/org/thingsboard/server/common/data/tenant/profile/DefaultTenantProfileConfiguration.java index 51738c75be..ff5c0ea426 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/tenant/profile/DefaultTenantProfileConfiguration.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/tenant/profile/DefaultTenantProfileConfiguration.java @@ -24,6 +24,8 @@ import org.thingsboard.server.common.data.ApiUsageRecordKey; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.TenantProfileType; +import java.io.Serial; + @Schema @AllArgsConstructor @NoArgsConstructor @@ -31,6 +33,7 @@ import org.thingsboard.server.common.data.TenantProfileType; @Data public class DefaultTenantProfileConfiguration implements TenantProfileConfiguration { + @Serial private static final long serialVersionUID = -7134932690332578595L; private long maxDevices; @@ -91,6 +94,8 @@ public class DefaultTenantProfileConfiguration implements TenantProfileConfigura private long maxDPStorageDays; @Schema(example = "50") private int maxRuleNodeExecutionsPerMessage; + @Schema(example = "15") + private int maxRuleNodeDebugDurationMinutes; @Schema(example = "0") private long maxEmails; @Schema(example = "true") @@ -197,4 +202,9 @@ public class DefaultTenantProfileConfiguration implements TenantProfileConfigura public int getMaxRuleNodeExecsPerMessage() { return maxRuleNodeExecutionsPerMessage; } + + @Override + public int getMaxRuleNodeDebugModeDurationMinutes(int systemMaxRuleNodeDebugModeDurationMinutes) { + return Math.min(systemMaxRuleNodeDebugModeDurationMinutes, maxRuleNodeDebugDurationMinutes); + } } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/tenant/profile/TenantProfileConfiguration.java b/common/data/src/main/java/org/thingsboard/server/common/data/tenant/profile/TenantProfileConfiguration.java index 0662765455..af36a15c4f 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/tenant/profile/TenantProfileConfiguration.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/tenant/profile/TenantProfileConfiguration.java @@ -51,4 +51,7 @@ public interface TenantProfileConfiguration extends Serializable { @JsonIgnore int getMaxRuleNodeExecsPerMessage(); + @JsonIgnore + int getMaxRuleNodeDebugModeDurationMinutes(int systemMaxRuleNodeDebugModeDurationMinutes); + } diff --git a/common/edge-api/src/main/proto/edge.proto b/common/edge-api/src/main/proto/edge.proto index 5fb51de2ab..f5765f3103 100644 --- a/common/edge-api/src/main/proto/edge.proto +++ b/common/edge-api/src/main/proto/edge.proto @@ -162,17 +162,25 @@ message RuleChainMetadataUpdateMsg { string entity = 8; } +enum DebugStrategy { + DISABLED = 0; + ALL_EVENTS = 1; + ONLY_FAILURE_EVENTS = 2; +} + message RuleNodeProto { option deprecated = true; int64 idMSB = 1; int64 idLSB = 2; string type = 3; string name = 4; - bool debugMode = 5; + bool debugMode = 5 [deprecated = true]; string configuration = 6; string additionalInfo = 7; bool singletonMode = 8; int32 configurationVersion = 9; + int64 lastUpdateTs = 10; + DebugStrategy debugStrategy = 11; } message NodeConnectionInfoProto { diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java b/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java index b920160cc0..a7da9bf516 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java @@ -397,6 +397,7 @@ public class ModelConstants { public static final String EVENT_MESSAGE_COLUMN_NAME = "e_message"; public static final String DEBUG_MODE = "debug_mode"; + public static final String DEBUG_STRATEGY = "debug_strategy"; public static final String SINGLETON_MODE = "singleton_mode"; public static final String QUEUE_NAME = "queue_name"; diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/RuleNodeEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/RuleNodeEntity.java index bdba4549bb..278fde0d6d 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/RuleNodeEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/RuleNodeEntity.java @@ -19,11 +19,14 @@ import com.fasterxml.jackson.databind.JsonNode; import jakarta.persistence.Column; import jakarta.persistence.Convert; import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; import jakarta.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; import org.thingsboard.server.common.data.id.RuleChainId; import org.thingsboard.server.common.data.id.RuleNodeId; +import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleNode; import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.model.BaseSqlEntity; @@ -58,8 +61,12 @@ public class RuleNodeEntity extends BaseSqlEntity { @Column(name = ModelConstants.ADDITIONAL_INFO_PROPERTY) private JsonNode additionalInfo; - @Column(name = ModelConstants.DEBUG_MODE) - private boolean debugMode; + @Column(name = ModelConstants.LAST_UPDATE_TS_COLUMN) + private long lastUpdateTs; + + @Enumerated(EnumType.STRING) + @Column(name = ModelConstants.DEBUG_STRATEGY) + private DebugStrategy debugStrategy; @Column(name = ModelConstants.SINGLETON_MODE) private boolean singletonMode; @@ -83,7 +90,8 @@ public class RuleNodeEntity extends BaseSqlEntity { } this.type = ruleNode.getType(); this.name = ruleNode.getName(); - this.debugMode = ruleNode.isDebugMode(); + this.lastUpdateTs = ruleNode.getLastUpdateTs(); + this.debugStrategy = ruleNode.getDebugStrategy(); this.singletonMode = ruleNode.isSingletonMode(); this.queueName = ruleNode.getQueueName(); this.configurationVersion = ruleNode.getConfigurationVersion(); @@ -103,7 +111,8 @@ public class RuleNodeEntity extends BaseSqlEntity { } ruleNode.setType(type); ruleNode.setName(name); - ruleNode.setDebugMode(debugMode); + ruleNode.setLastUpdateTs(lastUpdateTs); + ruleNode.setDebugStrategy(debugStrategy); ruleNode.setSingletonMode(singletonMode); ruleNode.setQueueName(queueName); ruleNode.setConfigurationVersion(configurationVersion); diff --git a/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java b/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java index 6f34437e0d..f141ef627e 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java @@ -44,6 +44,7 @@ import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.plugin.ComponentClusteringMode; import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.RelationTypeGroup; +import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.NodeConnectionInfo; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainConnectionInfo; @@ -214,9 +215,11 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC } RuleChainId ruleChainId = ruleChain.getId(); if (nodes != null) { + long lastUpdateTs = System.currentTimeMillis(); for (RuleNode node : toAddOrUpdate) { node.setRuleChainId(ruleChainId); node = ruleNodeUpdater.apply(node); + node.setLastUpdateTs(lastUpdateTs); RuleChainDataValidator.validateRuleNode(node); RuleNode savedNode = ruleNodeDao.save(tenantId, node); relations.add(new EntityRelation(ruleChainMetaData.getRuleChainId(), savedNode.getId(), @@ -261,7 +264,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC layout.remove("description"); layout.remove("ruleChainNodeId"); targetNode.setAdditionalInfo(layout); - targetNode.setDebugMode(false); + targetNode.setDebugStrategy(DebugStrategy.DISABLED); targetNode = ruleNodeDao.save(tenantId, targetNode); EntityRelation sourceRuleChainToRuleNode = new EntityRelation(); diff --git a/dao/src/main/resources/sql/schema-entities.sql b/dao/src/main/resources/sql/schema-entities.sql index 9c95f385f8..9772736191 100644 --- a/dao/src/main/resources/sql/schema-entities.sql +++ b/dao/src/main/resources/sql/schema-entities.sql @@ -193,7 +193,8 @@ CREATE TABLE IF NOT EXISTS rule_node ( configuration varchar(10000000), type varchar(255), name varchar(255), - debug_mode boolean, + last_update_ts bigint NOT NULL, + debug_strategy varchar(32) DEFAULT 'DISABLED', singleton_mode boolean, queue_name varchar(255), external_id uuid From 54a7400611c0c5ea8ad5408fda004f7a19397086 Mon Sep 17 00:00:00 2001 From: ShvaykaD Date: Tue, 15 Oct 2024 14:39:34 +0300 Subject: [PATCH 02/37] Fixed stored rule chain json. Added ignore uknown properties for rule node class --- .../device_profile/rule_chain_template.json | 14 ++++---- .../server/common/data/rule/RuleNode.java | 2 ++ .../src/main/resources/root_rule_chain.json | 36 +++++++++---------- .../resources/MqttRuleNodeTestMetadata.json | 6 ++-- .../RpcResponseRuleChainMetadata.json | 6 ++-- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/application/src/main/data/json/tenant/device_profile/rule_chain_template.json b/application/src/main/data/json/tenant/device_profile/rule_chain_template.json index 0256a2ccf2..b005331862 100644 --- a/application/src/main/data/json/tenant/device_profile/rule_chain_template.json +++ b/application/src/main/data/json/tenant/device_profile/rule_chain_template.json @@ -19,7 +19,7 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "defaultTTL": 0 } @@ -31,7 +31,7 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", "name": "Save Client Attributes", - "debugMode": false, + "debugStrategy": "DISABLED", "configurationVersion": 2, "configuration": { "scope": "CLIENT_SCOPE", @@ -47,7 +47,7 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "Message Type Switch", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "version": 0 } @@ -59,7 +59,7 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log RPC from Device", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "scriptLang": "TBEL", "jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);", @@ -73,7 +73,7 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log Other", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "scriptLang": "TBEL", "jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);", @@ -87,7 +87,7 @@ }, "type": "org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode", "name": "RPC Call Request", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "timeoutInSeconds": 60 } @@ -100,7 +100,7 @@ }, "type": "org.thingsboard.rule.engine.profile.TbDeviceProfileNode", "name": "Device Profile Node", - "debugMode": false, + "debugStrategy": "DISABLED", "configuration": { "persistAlarmRulesState": false, "fetchAlarmRulesStateOnStart": false diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java b/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java index c147da1359..35601afe02 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java @@ -16,6 +16,7 @@ package org.thingsboard.server.common.data.rule; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.JsonNode; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; @@ -32,6 +33,7 @@ import org.thingsboard.server.common.data.validation.NoXss; @Data @EqualsAndHashCode(callSuper = true) @Slf4j +@JsonIgnoreProperties(ignoreUnknown = true) public class RuleNode extends BaseDataWithAdditionalInfo implements HasName { private static final long serialVersionUID = -5656679015121235465L; diff --git a/monitoring/src/main/resources/root_rule_chain.json b/monitoring/src/main/resources/root_rule_chain.json index ed8a93fb63..1da16c9b09 100644 --- a/monitoring/src/main/resources/root_rule_chain.json +++ b/monitoring/src/main/resources/root_rule_chain.json @@ -20,7 +20,7 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries", - "debugMode": true, + "debugStrategy": "ALL_EVENTS", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -35,7 +35,7 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", "name": "Save Attributes", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 1, "configuration": { @@ -53,7 +53,7 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "Message Type Switch", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -68,7 +68,7 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log RPC from Device", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -85,7 +85,7 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log Other", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -102,7 +102,7 @@ }, "type": "org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode", "name": "RPC Call Request", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -117,7 +117,7 @@ }, "type": "org.thingsboard.rule.engine.filter.TbOriginatorTypeFilterNode", "name": "Is Entity Group", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -134,7 +134,7 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeFilterNode", "name": "Post attributes or RPC request", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -152,7 +152,7 @@ }, "type": "org.thingsboard.rule.engine.transform.TbDuplicateMsgToGroupNode", "name": "Duplicate To Group Entities", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -169,7 +169,7 @@ }, "type": "org.thingsboard.rule.engine.profile.TbDeviceProfileNode", "name": "Device Profile Node", - "debugMode": true, + "debugStrategy": "ALL_EVENTS", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -186,7 +186,7 @@ }, "type": "org.thingsboard.rule.engine.filter.TbJsFilterNode", "name": "Test JS script", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -204,7 +204,7 @@ }, "type": "org.thingsboard.rule.engine.filter.TbJsFilterNode", "name": "Test TBEL script", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -222,7 +222,7 @@ }, "type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode", "name": "Add arrival timestamp", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -240,7 +240,7 @@ }, "type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode", "name": "Calculate additional latencies", - "debugMode": true, + "debugStrategy": "ALL_EVENTS", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -258,7 +258,7 @@ }, "type": "org.thingsboard.rule.engine.transform.TbChangeOriginatorNode", "name": "To latencies asset", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -287,7 +287,7 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries", - "debugMode": true, + "debugStrategy": "ALL_EVENTS", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -303,7 +303,7 @@ }, "type": "org.thingsboard.rule.engine.filter.TbCheckMessageNode", "name": "Has testData", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -323,7 +323,7 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries with TTL", - "debugMode": true, + "debugStrategy": "ALL_EVENTS", "singletonMode": false, "configurationVersion": 0, "configuration": { diff --git a/msa/black-box-tests/src/test/resources/MqttRuleNodeTestMetadata.json b/msa/black-box-tests/src/test/resources/MqttRuleNodeTestMetadata.json index 7a5015add3..dc6a394a39 100644 --- a/msa/black-box-tests/src/test/resources/MqttRuleNodeTestMetadata.json +++ b/msa/black-box-tests/src/test/resources/MqttRuleNodeTestMetadata.json @@ -9,7 +9,7 @@ }, "type": "org.thingsboard.rule.engine.mqtt.TbMqttNode", "name": "test mqtt", - "debugMode": true, + "debugStrategy": "ALL_EVENTS", "singletonMode": true, "queueName": "HighPriority", "configurationVersion": 0, @@ -36,7 +36,7 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "save timeseries", - "debugMode": true, + "debugStrategy": "ALL_EVENTS", "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -54,7 +54,7 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "switch", - "debugMode": false, + "debugStrategy": "DISABLED", "singletonMode": false, "configurationVersion": 0, "configuration": { diff --git a/msa/black-box-tests/src/test/resources/RpcResponseRuleChainMetadata.json b/msa/black-box-tests/src/test/resources/RpcResponseRuleChainMetadata.json index 09178ef781..31eb3149f3 100644 --- a/msa/black-box-tests/src/test/resources/RpcResponseRuleChainMetadata.json +++ b/msa/black-box-tests/src/test/resources/RpcResponseRuleChainMetadata.json @@ -8,7 +8,7 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "msgTypeSwitch", - "debugMode": true, + "debugStrategy": "ALL_EVENTS", "configuration": { "version": 0 } @@ -20,7 +20,7 @@ }, "type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode", "name": "formResponse", - "debugMode": true, + "debugStrategy": "ALL_EVENTS", "configuration": { "jsScript": "if (msg.method == \"getResponse\") {\n return {msg: {\"response\": \"requestReceived\"}, metadata: metadata, msgType: msgType};\n}\n\nreturn {msg: msg, metadata: metadata, msgType: msgType};" } @@ -32,7 +32,7 @@ }, "type": "org.thingsboard.rule.engine.rpc.TbSendRPCReplyNode", "name": "rpcReply", - "debugMode": true, + "debugStrategy": "ALL_EVENTS", "configuration": { "requestIdMetaDataAttribute": "requestId" } From cc977f42b2ddb46a82affb96226c41b4a426d199 Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Tue, 29 Oct 2024 18:46:34 +0100 Subject: [PATCH 03/37] minor refactoring --- .../actors/ruleChain/DefaultTbContext.java | 56 ++++++++----------- .../actors/rule/DefaultTbContextTest.java | 19 ++++--- .../common/data/rule/DebugStrategy.java | 4 -- 3 files changed, 34 insertions(+), 45 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java b/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java index 3be7524c85..bd57f7da38 100644 --- a/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java +++ b/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java @@ -155,12 +155,7 @@ public class DefaultTbContext implements TbContext { @Override public void tellNext(TbMsg msg, Set relationTypes) { RuleNode ruleNode = nodeCtx.getSelf(); - DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); - if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), msg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { - relationTypes.forEach(relationType -> mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), msg, relationType)); - } else if (debugStrategy.shouldPersistDebugForFailureEventOnly(relationTypes)) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), msg, TbNodeConnectionType.FAILURE); - } + persistDebugOutput(msg, relationTypes); msg.getCallback().onProcessingEnd(ruleNode.getId()); nodeCtx.getChainActor().tell(new RuleNodeToRuleChainTellNextMsg(ruleNode.getRuleChainId(), ruleNode.getId(), relationTypes, msg, null)); } @@ -183,13 +178,7 @@ public class DefaultTbContext implements TbContext { if (item == null) { ack(msg); } else { - RuleNode ruleNode = nodeCtx.getSelf(); - DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); - if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), msg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), msg, relationType); - } else if (debugStrategy.shouldPersistDebugForFailureEventOnly(relationType)) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), msg, relationType); - } + persistDebugOutput(msg, relationType); nodeCtx.getChainActor().tell(new RuleChainOutputMsg(item.getRuleChainId(), item.getRuleNodeId(), relationType, msg)); } } @@ -220,11 +209,7 @@ public class DefaultTbContext implements TbContext { .setTbMsg(TbMsg.toByteString(tbMsg)).build(); mainCtx.getClusterService().pushMsgToRuleEngine(tpi, tbMsg.getId(), msg, new SimpleTbQueueCallback( metadata -> { - RuleNode ruleNode = nodeCtx.getSelf(); - DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); - if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), tbMsg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), tbMsg, TbNodeConnectionType.TO_ROOT_RULE_CHAIN); - } + persistDebugOutput(tbMsg, TbNodeConnectionType.TO_ROOT_RULE_CHAIN); if (onSuccess != null) { onSuccess.run(); } @@ -320,13 +305,7 @@ public class DefaultTbContext implements TbContext { } mainCtx.getClusterService().pushMsgToRuleEngine(tpi, tbMsg.getId(), msg.build(), new SimpleTbQueueCallback( metadata -> { - DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); - if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), tbMsg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { - relationTypes.forEach(relationType -> - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), tbMsg, relationType, null, failureMessage)); - } else if (debugStrategy.shouldPersistDebugForFailureEventOnly(relationTypes)) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), tbMsg, TbNodeConnectionType.FAILURE, null, failureMessage); - } + persistDebugOutput(tbMsg, relationTypes, null, failureMessage); if (onSuccess != null) { onSuccess.run(); } @@ -343,10 +322,7 @@ public class DefaultTbContext implements TbContext { @Override public void ack(TbMsg tbMsg) { RuleNode ruleNode = nodeCtx.getSelf(); - DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); - if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), tbMsg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), tbMsg, TbNodeConnectionType.ACK); - } + persistDebugOutput(tbMsg, TbNodeConnectionType.ACK); tbMsg.getCallback().onProcessingEnd(ruleNode.getId()); tbMsg.getCallback().onSuccess(); } @@ -363,9 +339,7 @@ public class DefaultTbContext implements TbContext { @Override public void tellFailure(TbMsg msg, Throwable th) { RuleNode ruleNode = nodeCtx.getSelf(); - if (ruleNode.getDebugStrategy().shouldPersistDebugForFailureEventOnly(ruleNode.getLastUpdateTs(), msg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), ruleNode.getId(), msg, TbNodeConnectionType.FAILURE, th); - } + persistDebugOutput(msg, Set.of(TbNodeConnectionType.FAILURE), th, null); String failureMessage = getFailureMessage(th); nodeCtx.getChainActor().tell(new RuleNodeToRuleChainTellNextMsg(ruleNode.getRuleChainId(), ruleNode.getId(), Collections.singleton(TbNodeConnectionType.FAILURE), @@ -1018,6 +992,24 @@ public class DefaultTbContext implements TbContext { return failureMessage; } + private void persistDebugOutput(TbMsg msg, String relationType) { + persistDebugOutput(msg, Set.of(relationType)); + } + + private void persistDebugOutput(TbMsg msg, Set relationTypes) { + persistDebugOutput(msg, relationTypes, null, null); + } + + private void persistDebugOutput(TbMsg msg, Set relationTypes, Throwable error, String failureMessage) { + RuleNode ruleNode = nodeCtx.getSelf(); + DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); + if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), msg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { + relationTypes.forEach(relationType -> mainCtx.persistDebugOutput(getTenantId(), ruleNode.getId(), msg, relationType, error, failureMessage)); + } else if (debugStrategy.shouldPersistDebugForFailureEventOnly(relationTypes)) { + mainCtx.persistDebugOutput(getTenantId(), ruleNode.getId(), msg, TbNodeConnectionType.FAILURE, error, failureMessage); + } + } + private int getMaxRuleNodeDebugDurationMinutes() { if (!DebugStrategy.ALL_EVENTS.equals(nodeCtx.getSelf().getDebugStrategy())) { return 0; diff --git a/application/src/test/java/org/thingsboard/server/actors/rule/DefaultTbContextTest.java b/application/src/test/java/org/thingsboard/server/actors/rule/DefaultTbContextTest.java index e9b344866f..f9ddf7d3fd 100644 --- a/application/src/test/java/org/thingsboard/server/actors/rule/DefaultTbContextTest.java +++ b/application/src/test/java/org/thingsboard/server/actors/rule/DefaultTbContextTest.java @@ -69,6 +69,7 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.ArgumentMatchers.notNull; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.mock; @@ -161,7 +162,7 @@ class DefaultTbContextTest { // THEN then(nodeCtxMock).should().getChainActor(); then(nodeCtxMock).shouldHaveNoMoreInteractions(); - then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, TbNodeConnectionType.FAILURE); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, TbNodeConnectionType.FAILURE, null, null); then(mainCtxMock).shouldHaveNoMoreInteractions(); checkTellNextCommonLogic(callbackMock, connections, msg); } @@ -226,7 +227,7 @@ class DefaultTbContextTest { then(nodeCtxMock).shouldHaveNoMoreInteractions(); then(mainCtxMock).should().getTenantProfileCache(); then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); - then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, connection); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, connection, null, null); then(mainCtxMock).shouldHaveNoMoreInteractions(); checkTellNextCommonLogic(callbackMock, connection, msg); } @@ -260,7 +261,7 @@ class DefaultTbContextTest { then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); var nodeConnectionsCaptor = ArgumentCaptor.forClass(String.class); int wantedNumberOfInvocations = connections.size(); - then(mainCtxMock).should(times(wantedNumberOfInvocations)).persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), eq(msg), nodeConnectionsCaptor.capture()); + then(mainCtxMock).should(times(wantedNumberOfInvocations)).persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), eq(msg), nodeConnectionsCaptor.capture(), nullable(Throwable.class), nullable(String.class)); then(mainCtxMock).shouldHaveNoMoreInteractions(); assertThat(nodeConnectionsCaptor.getAllValues()).hasSize(wantedNumberOfInvocations); assertThat(nodeConnectionsCaptor.getAllValues()).containsExactlyInAnyOrderElementsOf(connections); @@ -288,7 +289,7 @@ class DefaultTbContextTest { // THEN checkOutputCommonLogic(msgMock, TbNodeConnectionType.FAILURE); - then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, TbNodeConnectionType.FAILURE); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, TbNodeConnectionType.FAILURE, null, null); then(mainCtxMock).shouldHaveNoMoreInteractions(); then(nodeCtxMock).shouldHaveNoMoreInteractions(); } @@ -355,7 +356,7 @@ class DefaultTbContextTest { checkOutputCommonLogic(msgMock, nodeConnection); then(mainCtxMock).should().getTenantProfileCache(); then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); - then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, nodeConnection); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, nodeConnection, null, null); then(mainCtxMock).shouldHaveNoMoreInteractions(); then(nodeCtxMock).shouldHaveNoMoreInteractions(); } @@ -405,7 +406,7 @@ class DefaultTbContextTest { then(callbackMock).should().onProcessingEnd(RULE_NODE_ID); then(callbackMock).should().onSuccess(); then(nodeCtxMock).should(never()).getChainActor(); - then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, TbNodeConnectionType.ACK); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, TbNodeConnectionType.ACK, null, null); } @Test @@ -644,7 +645,7 @@ class DefaultTbContextTest { if (DebugStrategy.ALL_EVENTS.equals(debugStrategy)) { then(mainCtxMock).should().getTenantProfileCache(); then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); - then(mainCtxMock).should().persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), eq(msg), eq(TbNodeConnectionType.TO_ROOT_RULE_CHAIN)); + then(mainCtxMock).should().persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), eq(msg), eq(TbNodeConnectionType.TO_ROOT_RULE_CHAIN), nullable(Throwable.class), nullable(String.class)); } then(mainCtxMock).should().getClusterService(); then(mainCtxMock).shouldHaveNoMoreInteractions(); @@ -676,7 +677,7 @@ class DefaultTbContextTest { then(chainActorMock).should().tell(expectedRuleNodeToRuleChainTellNextMsg); then(chainActorMock).shouldHaveNoMoreInteractions(); then(nodeCtxMock).should().getChainActor(); - then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, TbNodeConnectionType.FAILURE, EXCEPTION); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, TbNodeConnectionType.FAILURE, EXCEPTION, null); then(mainCtxMock).shouldHaveNoMoreInteractions(); then(nodeCtxMock).shouldHaveNoMoreInteractions(); } @@ -736,7 +737,7 @@ class DefaultTbContextTest { then(chainActorMock).should().tell(expectedRuleNodeToRuleChainTellNextMsg); then(chainActorMock).shouldHaveNoMoreInteractions(); then(nodeCtxMock).should().getChainActor(); - then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, TbNodeConnectionType.FAILURE, EXCEPTION); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, TbNodeConnectionType.FAILURE, EXCEPTION, null); then(mainCtxMock).should().getTenantProfileCache(); then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); then(mainCtxMock).shouldHaveNoMoreInteractions(); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java b/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java index 333ee9c670..e23b1bea94 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java @@ -39,10 +39,6 @@ public enum DebugStrategy { return isAllEventsStrategyAndMsgTsWithinDebugDuration(lastUpdateTs, msgTs, debugModeDurationMinutes); } - public boolean shouldPersistDebugForFailureEventOnly(long lastUpdateTs, long msgTs, int debugModeDurationMinutes) { - return shouldPersistDebugOutputForAllEvents(lastUpdateTs, msgTs, debugModeDurationMinutes) || isFailureOnlyStrategy(); - } - public boolean shouldPersistDebugForFailureEventOnly(Set nodeConnections) { return isFailureOnlyStrategy() && nodeConnections.contains(TbNodeConnectionType.FAILURE); } From c676f79fc32d5ab32a9a202c0d059d3da9114968 Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Mon, 4 Nov 2024 20:23:35 +0100 Subject: [PATCH 04/37] added maxRuleNodeDebugDurationMinutes to the SystemParams --- .../thingsboard/server/controller/SystemInfoController.java | 5 +++++ .../org/thingsboard/server/common/data/SystemParams.java | 1 + ui-ngx/src/app/core/auth/auth.models.ts | 1 + 3 files changed, 7 insertions(+) diff --git a/application/src/main/java/org/thingsboard/server/controller/SystemInfoController.java b/application/src/main/java/org/thingsboard/server/controller/SystemInfoController.java index 9600ac0221..2c15aeec95 100644 --- a/application/src/main/java/org/thingsboard/server/controller/SystemInfoController.java +++ b/application/src/main/java/org/thingsboard/server/controller/SystemInfoController.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.swagger.v3.oas.annotations.Hidden; import jakarta.annotation.PostConstruct; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -70,6 +71,9 @@ public class SystemInfoController extends BaseController { @Value("${ui.dashboard.max_datapoints_limit}") private long maxDatapointsLimit; + @Value("${actors.rule.node.max_debug_mode_duration:60}") + private int maxRuleNodeDebugModeDurationMinutes; + @Autowired(required = false) private BuildProperties buildProperties; @@ -141,6 +145,7 @@ public class SystemInfoController extends BaseController { if (!currentUser.isSystemAdmin()) { DefaultTenantProfileConfiguration tenantProfileConfiguration = tenantProfileCache.get(tenantId).getDefaultProfileConfiguration(); systemParams.setMaxResourceSize(tenantProfileConfiguration.getMaxResourceSize()); + systemParams.setMaxRuleNodeDebugDurationMinutes(tenantProfileConfiguration.getMaxRuleNodeDebugModeDurationMinutes(maxRuleNodeDebugModeDurationMinutes)); } systemParams.setMobileQrEnabled(Optional.ofNullable(mobileAppSettingsService.getMobileAppSettings(TenantId.SYS_TENANT_ID)) .map(MobileAppSettings::getQrCodeConfig).map(QRCodeConfig::isShowOnHomePage) diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/SystemParams.java b/common/data/src/main/java/org/thingsboard/server/common/data/SystemParams.java index 5b4312f7b0..a3c9c0d51b 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/SystemParams.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/SystemParams.java @@ -32,4 +32,5 @@ public class SystemParams { long maxDatapointsLimit; long maxResourceSize; boolean mobileQrEnabled; + int maxRuleNodeDebugDurationMinutes; } diff --git a/ui-ngx/src/app/core/auth/auth.models.ts b/ui-ngx/src/app/core/auth/auth.models.ts index b73847ef83..cfc3fed4ba 100644 --- a/ui-ngx/src/app/core/auth/auth.models.ts +++ b/ui-ngx/src/app/core/auth/auth.models.ts @@ -27,6 +27,7 @@ export interface SysParamsState { mobileQrEnabled: boolean; userSettings: UserSettings; maxResourceSize: number; + maxRuleNodeDebugDurationMinutes: number; } export interface SysParams extends SysParamsState { From 2e28696068d289ec994f607db87257d6d29cbb04 Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Tue, 5 Nov 2024 10:26:08 +0100 Subject: [PATCH 05/37] reverted changes on UI --- ui-ngx/src/app/core/auth/auth.models.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/ui-ngx/src/app/core/auth/auth.models.ts b/ui-ngx/src/app/core/auth/auth.models.ts index cfc3fed4ba..b73847ef83 100644 --- a/ui-ngx/src/app/core/auth/auth.models.ts +++ b/ui-ngx/src/app/core/auth/auth.models.ts @@ -27,7 +27,6 @@ export interface SysParamsState { mobileQrEnabled: boolean; userSettings: UserSettings; maxResourceSize: number; - maxRuleNodeDebugDurationMinutes: number; } export interface SysParams extends SysParamsState { From 5c6cd2dda89bc2d4b3b2f83a9bbff6f4f26bfae8 Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Tue, 5 Nov 2024 20:18:15 +0100 Subject: [PATCH 06/37] added new debug strategy ALL_THEN_ONLY_FAILURE_EVENTS --- .../actors/ruleChain/DefaultTbContext.java | 14 +- .../RuleChainActorMessageProcessor.java | 2 +- .../actors/rule/DefaultTbContextTest.java | 192 +++++++++++++++++- .../common/data/rule/DebugStrategy.java | 28 +-- common/edge-api/src/main/proto/edge.proto | 3 +- 5 files changed, 212 insertions(+), 27 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java b/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java index bd57f7da38..119d1d2900 100644 --- a/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java +++ b/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java @@ -1005,19 +1005,19 @@ public class DefaultTbContext implements TbContext { DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), msg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { relationTypes.forEach(relationType -> mainCtx.persistDebugOutput(getTenantId(), ruleNode.getId(), msg, relationType, error, failureMessage)); - } else if (debugStrategy.shouldPersistDebugForFailureEventOnly(relationTypes)) { + } else if (debugStrategy.shouldPersistDebugForFailureEvent(relationTypes)) { mainCtx.persistDebugOutput(getTenantId(), ruleNode.getId(), msg, TbNodeConnectionType.FAILURE, error, failureMessage); } } private int getMaxRuleNodeDebugDurationMinutes() { - if (!DebugStrategy.ALL_EVENTS.equals(nodeCtx.getSelf().getDebugStrategy())) { - return 0; + if (nodeCtx.getSelf().getDebugStrategy().isHasDuration()) { + var configuration = mainCtx.getTenantProfileCache() + .get(getTenantId()).getProfileData().getConfiguration(); + int systemMaxRuleNodeDebugModeDurationMinutes = mainCtx.getMaxRuleNodeDebugModeDurationMinutes(); + return configuration.getMaxRuleNodeDebugModeDurationMinutes(systemMaxRuleNodeDebugModeDurationMinutes); } - var configuration = mainCtx.getTenantProfileCache() - .get(getTenantId()).getProfileData().getConfiguration(); - int systemMaxRuleNodeDebugModeDurationMinutes = mainCtx.getMaxRuleNodeDebugModeDurationMinutes(); - return configuration.getMaxRuleNodeDebugModeDurationMinutes(systemMaxRuleNodeDebugModeDurationMinutes); + return 0; } } diff --git a/application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleChainActorMessageProcessor.java b/application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleChainActorMessageProcessor.java index 2213daa770..01caa50860 100644 --- a/application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleChainActorMessageProcessor.java +++ b/application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleChainActorMessageProcessor.java @@ -260,7 +260,7 @@ public class RuleChainActorMessageProcessor extends ComponentMsgProcessor givenDebugStrategyAllThenOnlyFailureEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted() { + return failureAndSuccessConnection(); + } + + @Test + public void givenDebugStrategyAllThenOnlyEventsAndFailureAndSuccessConnection_whenTellNext_thenVerifyDebugOutputPersistedForAllEvents() { + // GIVEN + var callbackMock = mock(TbMsgCallback.class); + var msg = getTbMsgWithCallback(callbackMock); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setLastUpdateTs(System.currentTimeMillis()); + ruleNode.setDebugStrategy(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + mockGetMaxRuleNodeDebugModeDurationMinutes(); + + // WHEN + Set connections = failureAndSuccessConnection().collect(Collectors.toSet()); + defaultTbContext.tellNext(msg, connections); + + // THEN + then(nodeCtxMock).should().getChainActor(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + then(mainCtxMock).should().getTenantProfileCache(); + then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); + var nodeConnectionsCaptor = ArgumentCaptor.forClass(String.class); + int wantedNumberOfInvocations = connections.size(); + then(mainCtxMock).should(times(wantedNumberOfInvocations)).persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), eq(msg), nodeConnectionsCaptor.capture(), nullable(Throwable.class), nullable(String.class)); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + assertThat(nodeConnectionsCaptor.getAllValues()).hasSize(wantedNumberOfInvocations); + assertThat(nodeConnectionsCaptor.getAllValues()).containsExactlyInAnyOrderElementsOf(connections); + checkTellNextCommonLogic(callbackMock, connections, msg); + } + private static Stream failureAndSuccessConnection() { return Stream.of(TbNodeConnectionType.FAILURE, TbNodeConnectionType.SUCCESS); } @@ -361,6 +427,32 @@ class DefaultTbContextTest { then(nodeCtxMock).shouldHaveNoMoreInteractions(); } + @ParameterizedTest + @ValueSource(strings = {TbNodeConnectionType.SUCCESS, TbNodeConnectionType.FAILURE}) + void givenDebugStrategyAllThenOnlyFailureEvents_whenOutput_thenVerifyDebugOutputPersisted(String nodeConnection) { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); + given(msgMock.popFormStack()).willReturn(new TbMsgProcessingStackItem(RULE_CHAIN_ID, RULE_NODE_ID)); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + mockGetMaxRuleNodeDebugModeDurationMinutes(); + + // WHEN + defaultTbContext.output(msgMock, nodeConnection); + + // THEN + checkOutputCommonLogic(msgMock, nodeConnection); + then(mainCtxMock).should().getTenantProfileCache(); + then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, nodeConnection, null, null); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + @Test public void givenEmptyStack_whenOutput_thenVerifyMsgAck() { // GIVEN @@ -409,6 +501,32 @@ class DefaultTbContextTest { then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, TbNodeConnectionType.ACK, null, null); } + @Test + public void givenEmptyStackAndDebugStrategyAllThenOnlyFailureEvents_whenOutput_thenVerifyMsgAckAndDebugOutputPersisted() { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugStrategy(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS); + ruleNode.setLastUpdateTs(System.currentTimeMillis()); + given(msgMock.popFormStack()).willReturn(null); + TbMsgCallback callbackMock = mock(TbMsgCallback.class); + given(msgMock.getCallback()).willReturn(callbackMock); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + mockGetMaxRuleNodeDebugModeDurationMinutes(); + + // WHEN + defaultTbContext.output(msgMock, TbNodeConnectionType.SUCCESS); + + // THEN + then(msgMock).should().popFormStack(); + then(callbackMock).should().onProcessingEnd(RULE_NODE_ID); + then(callbackMock).should().onSuccess(); + then(nodeCtxMock).should(never()).getChainActor(); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, TbNodeConnectionType.ACK, null, null); + } + @Test public void givenDebugStrategyOnlyFailureEvents_whenEnqueueForTellFailure_thenVerifyDebugOutputPersisted() { // GIVEN @@ -551,7 +669,7 @@ class DefaultTbContextTest { given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(mainCtxMock.resolve(any(ServiceType.class), anyString(), any(TenantId.class), any(EntityId.class))).willReturn(tpi); given(mainCtxMock.getClusterService()).willReturn(tbClusterServiceMock); - if (DebugStrategy.ALL_EVENTS.equals(debugStrategy)) { + if (DebugStrategy.ALL_EVENTS.equals(debugStrategy) || DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS.equals(debugStrategy)) { mockGetMaxRuleNodeDebugModeDurationMinutes(); } @@ -579,7 +697,7 @@ class DefaultTbContextTest { assertThat(simpleTbQueueCallback).isNotNull(); simpleTbQueueCallback.onSuccess(null); - if (DebugStrategy.ALL_EVENTS.equals(debugStrategy)) { + if (DebugStrategy.ALL_EVENTS.equals(debugStrategy) || DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS.equals(debugStrategy)) { then(mainCtxMock).should().getTenantProfileCache(); then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); ArgumentCaptor tbMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); @@ -611,7 +729,7 @@ class DefaultTbContextTest { given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(mainCtxMock.resolve(any(ServiceType.class), anyString(), any(TenantId.class), any(EntityId.class))).willReturn(tpi); given(mainCtxMock.getClusterService()).willReturn(tbClusterServiceMock); - if (DebugStrategy.ALL_EVENTS.equals(debugStrategy)) { + if (DebugStrategy.ALL_EVENTS.equals(debugStrategy) || DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS.equals(debugStrategy)) { mockGetMaxRuleNodeDebugModeDurationMinutes(); } @@ -642,7 +760,7 @@ class DefaultTbContextTest { assertThat(simpleTbQueueCallback).isNotNull(); simpleTbQueueCallback.onSuccess(null); - if (DebugStrategy.ALL_EVENTS.equals(debugStrategy)) { + if (debugStrategy.isHasDuration()) { then(mainCtxMock).should().getTenantProfileCache(); then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); then(mainCtxMock).should().persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), eq(msg), eq(TbNodeConnectionType.TO_ROOT_RULE_CHAIN), nullable(Throwable.class), nullable(String.class)); @@ -744,6 +862,51 @@ class DefaultTbContextTest { then(nodeCtxMock).shouldHaveNoMoreInteractions(); } + @MethodSource + @ParameterizedTest + void givenDebugStrategyAndConnectionAndPersistedResultOptions_whenTellNext_thenVerifyDebugOutputPersistence(DebugStrategy debugStrategy, + String connection, + boolean shouldPersist, + boolean shouldPersistAfterDurationTime) { + // GIVEN + var callbackMock = mock(TbMsgCallback.class); + var msg = getTbMsgWithCallback(callbackMock); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setLastUpdateTs(System.currentTimeMillis()); + ruleNode.setDebugStrategy(debugStrategy); + if (shouldPersist) { + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + } + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + if (debugStrategy.isHasDuration()) { + mockGetMaxRuleNodeDebugModeDurationMinutes(); + } + + // WHEN + defaultTbContext.tellNext(msg, connection); + + // THEN + if (shouldPersist) { + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, connection, null, null); + } + + // GIVEN + Mockito.clearInvocations(mainCtxMock); + if (debugStrategy.isHasDuration()) { + mockGetMaxRuleNodeDebugModeDurationMinutes(0); + } + + // WHEN + defaultTbContext.tellNext(msg, connection); + + // THEN + if (shouldPersistAfterDurationTime) { + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, connection, null, null); + } + } + private void checkTellNextCommonLogic(TbMsgCallback callbackMock, String nodeConnection, TbMsg msg) { checkTellNextCommonLogic(callbackMock, Collections.singleton(nodeConnection), msg); } @@ -796,6 +959,7 @@ class DefaultTbContextTest { private static Stream givenDebugStrategyOptions_whenEnqueueForTellNext_thenVerifyDebugOutputPersistedOnlyForAllEventsDebugStrategy() { return Stream.of( Arguments.of(DebugStrategy.ALL_EVENTS, TbNodeConnectionType.OTHER), + Arguments.of(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS, TbNodeConnectionType.OTHER), Arguments.of(DebugStrategy.ONLY_FAILURE_EVENTS, TbNodeConnectionType.TRUE), Arguments.of(DebugStrategy.DISABLED, TbNodeConnectionType.FALSE) ); @@ -804,11 +968,25 @@ class DefaultTbContextTest { private static Stream givenDebugStrategyOptions_whenEnqueue_thenVerifyDebugOutputPersistedOnlyForAllEventsDebugStrategy() { return Stream.of( Arguments.of(DebugStrategy.ALL_EVENTS), + Arguments.of(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS), Arguments.of(DebugStrategy.ONLY_FAILURE_EVENTS), Arguments.of(DebugStrategy.DISABLED) ); } + private static Stream givenDebugStrategyAndConnectionAndPersistedResultOptions_whenTellNext_thenVerifyDebugOutputPersistence() { + return Stream.of( + Arguments.of(DebugStrategy.ALL_EVENTS, TbNodeConnectionType.SUCCESS, true, false), + Arguments.of(DebugStrategy.ALL_EVENTS, TbNodeConnectionType.FAILURE, true, false), + Arguments.of(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS, TbNodeConnectionType.SUCCESS, true, false), + Arguments.of(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS, TbNodeConnectionType.FAILURE, true, true), + Arguments.of(DebugStrategy.ONLY_FAILURE_EVENTS, TbNodeConnectionType.SUCCESS, false, false), + Arguments.of(DebugStrategy.ONLY_FAILURE_EVENTS, TbNodeConnectionType.FAILURE, true, true), + Arguments.of(DebugStrategy.DISABLED, TbNodeConnectionType.SUCCESS, false, false), + Arguments.of(DebugStrategy.DISABLED, TbNodeConnectionType.FAILURE, false, false) + ); + } + private TbMsg getTbMsgWithCallback(TbMsgCallback callback) { return TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, TENANT_ID, TbMsgMetaData.EMPTY, TbMsg.EMPTY_STRING, callback); } @@ -822,6 +1000,10 @@ class DefaultTbContextTest { } private void mockGetMaxRuleNodeDebugModeDurationMinutes() { + mockGetMaxRuleNodeDebugModeDurationMinutes(15); + } + + private void mockGetMaxRuleNodeDebugModeDurationMinutes(int maxRuleNodeDebugModeDurationMinutes) { var tbTenantProfileCacheMock = mock(TbTenantProfileCache.class); var tenantProfileMock = mock(TenantProfile.class); var tenantProfileDataMock = mock(TenantProfileData.class); @@ -831,7 +1013,7 @@ class DefaultTbContextTest { given(tbTenantProfileCacheMock.get(TENANT_ID)).willReturn(tenantProfileMock); given(tenantProfileMock.getProfileData()).willReturn(tenantProfileDataMock); given(tenantProfileDataMock.getConfiguration()).willReturn(tenantProfileConfigurationMock); - given(tenantProfileConfigurationMock.getMaxRuleNodeDebugModeDurationMinutes(anyInt())).willReturn(15); + given(tenantProfileConfigurationMock.getMaxRuleNodeDebugModeDurationMinutes(anyInt())).willReturn(maxRuleNodeDebugModeDurationMinutes); } } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java b/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java index e23b1bea94..a065c9065f 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java @@ -23,12 +23,17 @@ import java.util.concurrent.TimeUnit; @Getter public enum DebugStrategy { - DISABLED(0), ALL_EVENTS(1), ONLY_FAILURE_EVENTS(2); + DISABLED(0, false), + ALL_EVENTS(1, true), + ALL_THEN_ONLY_FAILURE_EVENTS(2, true), + ONLY_FAILURE_EVENTS(3, false); private final int protoNumber; + private final boolean hasDuration; - DebugStrategy(int protoNumber) { + DebugStrategy(int protoNumber, boolean hasDuration) { this.protoNumber = protoNumber; + this.hasDuration = hasDuration; } public boolean shouldPersistDebugInput(long lastUpdateTs, long msgTs, int debugModeDurationMinutes) { @@ -36,26 +41,23 @@ public enum DebugStrategy { } public boolean shouldPersistDebugOutputForAllEvents(long lastUpdateTs, long msgTs, int debugModeDurationMinutes) { - return isAllEventsStrategyAndMsgTsWithinDebugDuration(lastUpdateTs, msgTs, debugModeDurationMinutes); + return this.isAllEventsStrategyAndMsgTsWithinDebugDuration(lastUpdateTs, msgTs, debugModeDurationMinutes); } - public boolean shouldPersistDebugForFailureEventOnly(Set nodeConnections) { - return isFailureOnlyStrategy() && nodeConnections.contains(TbNodeConnectionType.FAILURE); + public boolean shouldPersistDebugForFailureEvent(Set nodeConnections) { + return isFailureStrategy() && nodeConnections.contains(TbNodeConnectionType.FAILURE); } - public boolean shouldPersistDebugForFailureEventOnly(String nodeConnection) { - return isFailureOnlyStrategy() && TbNodeConnectionType.FAILURE.equals(nodeConnection); + public boolean shouldPersistDebugForFailureEvent(String nodeConnection) { + return isFailureStrategy() && TbNodeConnectionType.FAILURE.equals(nodeConnection); } - private boolean isFailureOnlyStrategy() { - return DebugStrategy.ONLY_FAILURE_EVENTS.equals(this); + private boolean isFailureStrategy() { + return DebugStrategy.ONLY_FAILURE_EVENTS.equals(this) || DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS.equals(this); } private boolean isAllEventsStrategyAndMsgTsWithinDebugDuration(long lastUpdateTs, long msgTs, int debugModeDurationMinutes) { - if (!DebugStrategy.ALL_EVENTS.equals(this)) { - return false; - } - return isMsgTsWithinDebugDuration(lastUpdateTs, msgTs, debugModeDurationMinutes); + return this.hasDuration && isMsgTsWithinDebugDuration(lastUpdateTs, msgTs, debugModeDurationMinutes); } private boolean isMsgTsWithinDebugDuration(long lastUpdateTs, long msgCreationTs, int debugModeDurationMinutes) { diff --git a/common/edge-api/src/main/proto/edge.proto b/common/edge-api/src/main/proto/edge.proto index f5765f3103..167e160648 100644 --- a/common/edge-api/src/main/proto/edge.proto +++ b/common/edge-api/src/main/proto/edge.proto @@ -165,7 +165,8 @@ message RuleChainMetadataUpdateMsg { enum DebugStrategy { DISABLED = 0; ALL_EVENTS = 1; - ONLY_FAILURE_EVENTS = 2; + ALL_THEN_ONLY_FAILURE_EVENTS = 2; + ONLY_FAILURE_EVENTS = 3; } message RuleNodeProto { From 9dbade0b78043ed9588590c42285e6194579adf2 Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Tue, 5 Nov 2024 20:24:28 +0100 Subject: [PATCH 07/37] added ruleChainDebugPerTenantLimitsConfiguration to the system params --- .../server/controller/SystemInfoController.java | 11 +++++++++++ .../thingsboard/server/common/data/SystemParams.java | 1 + 2 files changed, 12 insertions(+) diff --git a/application/src/main/java/org/thingsboard/server/controller/SystemInfoController.java b/application/src/main/java/org/thingsboard/server/controller/SystemInfoController.java index 2c15aeec95..7c73a26a90 100644 --- a/application/src/main/java/org/thingsboard/server/controller/SystemInfoController.java +++ b/application/src/main/java/org/thingsboard/server/controller/SystemInfoController.java @@ -74,6 +74,14 @@ public class SystemInfoController extends BaseController { @Value("${actors.rule.node.max_debug_mode_duration:60}") private int maxRuleNodeDebugModeDurationMinutes; + @Value("${actors.rule.chain.debug_mode_rate_limits_per_tenant.enabled:true}") + @Getter + private boolean ruleChainDebugPerTenantLimitsEnabled; + + @Value("${actors.rule.chain.debug_mode_rate_limits_per_tenant.configuration:50000:3600}") + @Getter + private String ruleChainDebugPerTenantLimitsConfiguration; + @Autowired(required = false) private BuildProperties buildProperties; @@ -146,6 +154,9 @@ public class SystemInfoController extends BaseController { DefaultTenantProfileConfiguration tenantProfileConfiguration = tenantProfileCache.get(tenantId).getDefaultProfileConfiguration(); systemParams.setMaxResourceSize(tenantProfileConfiguration.getMaxResourceSize()); systemParams.setMaxRuleNodeDebugDurationMinutes(tenantProfileConfiguration.getMaxRuleNodeDebugModeDurationMinutes(maxRuleNodeDebugModeDurationMinutes)); + if (ruleChainDebugPerTenantLimitsEnabled) { + systemParams.setRuleChainDebugPerTenantLimitsConfiguration(ruleChainDebugPerTenantLimitsConfiguration); + } } systemParams.setMobileQrEnabled(Optional.ofNullable(mobileAppSettingsService.getMobileAppSettings(TenantId.SYS_TENANT_ID)) .map(MobileAppSettings::getQrCodeConfig).map(QRCodeConfig::isShowOnHomePage) diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/SystemParams.java b/common/data/src/main/java/org/thingsboard/server/common/data/SystemParams.java index a3c9c0d51b..0f84567800 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/SystemParams.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/SystemParams.java @@ -33,4 +33,5 @@ public class SystemParams { long maxResourceSize; boolean mobileQrEnabled; int maxRuleNodeDebugDurationMinutes; + String ruleChainDebugPerTenantLimitsConfiguration; } From 3c7dc0cc0396385ff2bf6f34c7a67deb327adacd Mon Sep 17 00:00:00 2001 From: mpetrov Date: Wed, 6 Nov 2024 19:53:06 +0200 Subject: [PATCH 08/37] UI implementation of Rule Node debug strategy --- ui-ngx/src/app/core/auth/auth.models.ts | 1 + ui-ngx/src/app/core/auth/auth.reducer.ts | 3 +- ...enant-profile-configuration.component.html | 16 +++ ...-tenant-profile-configuration.component.ts | 1 + .../rulechain/debug-duration-left.pipe.ts | 37 ++++++ .../debug-strategy-button.component.html | 32 +++++ .../debug-strategy-button.component.ts | 120 ++++++++++++++++++ .../debug-strategy-panel.component.html | 50 ++++++++ .../debug-strategy-panel.component.ts | 94 ++++++++++++++ .../rule-node-details.component.html | 22 ++-- .../rule-node-details.component.scss | 7 - .../rulechain/rule-node-details.component.ts | 11 +- .../rulechain/rulechain-page.component.ts | 14 +- .../home/pages/rulechain/rulechain.module.ts | 6 +- .../import-export/import-export.service.ts | 4 +- .../src/app/shared/models/rule-node.models.ts | 13 +- .../assets/locale/locale.constant-ar_AE.json | 1 - .../assets/locale/locale.constant-en_US.json | 19 ++- .../assets/locale/locale.constant-es_ES.json | 1 - .../assets/locale/locale.constant-lt_LT.json | 1 - .../assets/locale/locale.constant-nl_BE.json | 1 - .../assets/locale/locale.constant-pl_PL.json | 1 - .../assets/locale/locale.constant-zh_CN.json | 1 - ui-ngx/src/styles.scss | 17 +++ 24 files changed, 438 insertions(+), 35 deletions(-) create mode 100644 ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts create mode 100644 ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.html create mode 100644 ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.ts create mode 100644 ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.html create mode 100644 ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.ts diff --git a/ui-ngx/src/app/core/auth/auth.models.ts b/ui-ngx/src/app/core/auth/auth.models.ts index b73847ef83..cfc3fed4ba 100644 --- a/ui-ngx/src/app/core/auth/auth.models.ts +++ b/ui-ngx/src/app/core/auth/auth.models.ts @@ -27,6 +27,7 @@ export interface SysParamsState { mobileQrEnabled: boolean; userSettings: UserSettings; maxResourceSize: number; + maxRuleNodeDebugDurationMinutes: number; } export interface SysParams extends SysParamsState { diff --git a/ui-ngx/src/app/core/auth/auth.reducer.ts b/ui-ngx/src/app/core/auth/auth.reducer.ts index 73e0d04eb1..e2ff1fae25 100644 --- a/ui-ngx/src/app/core/auth/auth.reducer.ts +++ b/ui-ngx/src/app/core/auth/auth.reducer.ts @@ -31,7 +31,8 @@ const emptyUserAuthState: AuthPayload = { persistDeviceStateToTelemetry: false, mobileQrEnabled: false, maxResourceSize: 0, - userSettings: initialUserSettings + userSettings: initialUserSettings, + maxRuleNodeDebugDurationMinutes: 0, }; export const initialState: AuthState = { diff --git a/ui-ngx/src/app/modules/home/components/profile/tenant/default-tenant-profile-configuration.component.html b/ui-ngx/src/app/modules/home/components/profile/tenant/default-tenant-profile-configuration.component.html index d511125eec..2e5d698121 100644 --- a/ui-ngx/src/app/modules/home/components/profile/tenant/default-tenant-profile-configuration.component.html +++ b/ui-ngx/src/app/modules/home/components/profile/tenant/default-tenant-profile-configuration.component.html @@ -147,6 +147,22 @@ +
+ + tenant-profile.maximum-rule-node-debug-duration-min + + + {{ 'tenant-profile.maximum-rule-node-debug-duration-min-range' | translate}} + + + {{ 'tenant-profile.maximum-rule-node-debug-duration-min-required' | translate}} + + + +
+
diff --git a/ui-ngx/src/app/modules/home/components/profile/tenant/default-tenant-profile-configuration.component.ts b/ui-ngx/src/app/modules/home/components/profile/tenant/default-tenant-profile-configuration.component.ts index f7261e7957..860328fb80 100644 --- a/ui-ngx/src/app/modules/home/components/profile/tenant/default-tenant-profile-configuration.component.ts +++ b/ui-ngx/src/app/modules/home/components/profile/tenant/default-tenant-profile-configuration.component.ts @@ -85,6 +85,7 @@ export class DefaultTenantProfileConfigurationComponent implements ControlValueA tenantNotificationRequestsRateLimit: [null, []], tenantNotificationRequestsPerRuleRateLimit: [null, []], maxTransportMessages: [null, [Validators.required, Validators.min(0)]], + maxRuleNodeDebugDurationMinutes: [null, [Validators.required, Validators.min(0)]], maxTransportDataPoints: [null, [Validators.required, Validators.min(0)]], maxREExecutions: [null, [Validators.required, Validators.min(0)]], maxJSExecutions: [null, [Validators.required, Validators.min(0)]], diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts b/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts new file mode 100644 index 0000000000..17176bd4af --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts @@ -0,0 +1,37 @@ +/// +/// Copyright © 2016-2024 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. +/// + +import { Pipe, PipeTransform } from '@angular/core'; +import { MINUTE } from '@shared/models/time/time.models'; + +@Pipe({ + name: 'debugDurationLeft', + pure: false, + standalone: true, +}) +export class DebugDurationLeftPipe implements PipeTransform { + transform(lastUpdateTs: number, maxRuleNodeDebugDurationMinutes: number): string { + const minutes = Math.max(0, Math.floor(this.getDebugTime(lastUpdateTs, maxRuleNodeDebugDurationMinutes) / MINUTE)); + return `${minutes} min left`; + } + + getDebugTime(lastUpdateTs: number, maxRuleNodeDebugDurationMinutes: number): number { + const maxDuration = maxRuleNodeDebugDurationMinutes * MINUTE; + const updateDuration = lastUpdateTs ? new Date().getTime() - lastUpdateTs : 0; + const leftTime = maxDuration - updateDuration; + return leftTime > 0 ? leftTime : 0; + } +} diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.html new file mode 100644 index 0000000000..800f54ae3e --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.html @@ -0,0 +1,32 @@ + + diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.ts new file mode 100644 index 0000000000..9a158d6a85 --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.ts @@ -0,0 +1,120 @@ +/// +/// Copyright © 2016-2024 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. +/// + +import { + Component, + Input, + forwardRef, + Renderer2, + ViewContainerRef, + DestroyRef, + ChangeDetectionStrategy, + ChangeDetectorRef +} from '@angular/core'; +import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR, UntypedFormBuilder } from '@angular/forms'; +import { CommonModule } from '@angular/common'; +import { SharedModule } from '@shared/shared.module'; +import { DebugStrategy } from '@shared/models/rule-node.models'; +import { DebugDurationLeftPipe } from '@home/pages/rulechain/debug-duration-left.pipe'; +import { getCurrentAuthState } from '@core/auth/auth.selectors'; +import { Store } from '@ngrx/store'; +import { AppState } from '@core/core.state'; +import { TbPopoverService } from '@shared/components/popover.service'; +import { MatButton } from '@angular/material/button'; +import { DebugStrategyPanelComponent } from '@home/pages/rulechain/debug-strategy-panel.component'; +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; +import { interval } from 'rxjs'; +import { MINUTE } from '@shared/models/time/time.models'; + +@Component({ + selector: 'tb-debug-strategy-button', + templateUrl: './debug-strategy-button.component.html', + providers: [ + { + provide: NG_VALUE_ACCESSOR, + useExisting: forwardRef(() => DebugStrategyButtonComponent), + multi: true + } + ], + standalone: true, + imports: [ + CommonModule, + SharedModule, + DebugDurationLeftPipe, + ], + changeDetection: ChangeDetectionStrategy.OnPush +}) +export class DebugStrategyButtonComponent implements ControlValueAccessor { + + @Input() disabled = false; + @Input() lastUpdateTs: number; + + debugStrategyFormControl: FormControl; + + private onChange: (debugStrategy: DebugStrategy) => void; + + readonly maxRuleNodeDebugDurationMinutes = getCurrentAuthState(this.store).maxRuleNodeDebugDurationMinutes; + readonly DebugStrategy = DebugStrategy; + + constructor(protected store: Store, + private fb: UntypedFormBuilder, + private popoverService: TbPopoverService, + private renderer: Renderer2, + private viewContainerRef: ViewContainerRef, + private destroyRef: DestroyRef, + private cdr: ChangeDetectorRef + ) { + this.debugStrategyFormControl = this.fb.control(DebugStrategy.DISABLED); + this.debugStrategyFormControl.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(value => this.onChange(value)); + interval(0.5 * MINUTE) + .pipe(takeUntilDestroyed(this.destroyRef)) + .subscribe(() => this.cdr.markForCheck()); + } + + openDebugStrategyPanel($event: Event, matButton: MatButton): void { + if ($event) { + $event.stopPropagation(); + } + const trigger = matButton._elementRef.nativeElement; + const debugStrategy = this.debugStrategyFormControl.value; + if (this.popoverService.hasPopover(trigger)) { + this.popoverService.hidePopover(trigger); + } else { + const debugStrategyPopover = this.popoverService.displayPopover(trigger, this.renderer, + this.viewContainerRef, DebugStrategyPanelComponent, 'bottom', true, null, + { debugStrategy }, + {}, + {}, {}, true); + debugStrategyPopover.tbComponentRef.instance.popover = debugStrategyPopover; + debugStrategyPopover.tbComponentRef.instance.onStrategyApplied.subscribe((strategy: DebugStrategy) => { + this.debugStrategyFormControl.patchValue(strategy); + this.cdr.markForCheck(); + debugStrategyPopover.hide(); + }); + } + } + + registerOnChange(onChange: (debugStrategy: DebugStrategy) => void): void { + this.onChange = onChange; + } + + registerOnTouched(_: () => {}): void {} + + writeValue(value: DebugStrategy): void { + this.debugStrategyFormControl.patchValue(value, { emitEvent: false }); + this.cdr.markForCheck(); + } +} diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.html new file mode 100644 index 0000000000..4f8402ff3f --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.html @@ -0,0 +1,50 @@ + +
+
debug-strategy.label
+
+
{{ 'debug-strategy.hint.main' | translate }}
+
+
+ + + {{ 'debug-strategy.on-failure' | translate }} + + + + + {{ 'debug-strategy.all-messages' | translate }} {{ ' (' + maxRuleNodeDebugDurationMinutes + ' ' }} {{ ('debug-strategy.min' | translate) + ')'}} + + +
+
+ + +
+
+ diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.ts new file mode 100644 index 0000000000..87851067de --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.ts @@ -0,0 +1,94 @@ +/// +/// Copyright © 2016-2024 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. +/// + +import { Component, EventEmitter, Input, OnInit } from '@angular/core'; +import { PageComponent } from '@shared/components/page.component'; +import { TbPopoverComponent } from '@shared/components/popover.component'; +import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; +import { Store } from '@ngrx/store'; +import { AppState } from '@core/core.state'; +import { CommonModule } from '@angular/common'; +import { SharedModule } from '@shared/shared.module'; +import { getCurrentAuthState } from '@core/auth/auth.selectors'; +import { DebugStrategy } from '@shared/models/rule-node.models'; + +@Component({ + selector: 'tb-debug-strategy-panel', + templateUrl: './debug-strategy-panel.component.html', + standalone: true, + imports: [ + SharedModule, + CommonModule + ] +}) +export class DebugStrategyPanelComponent extends PageComponent implements OnInit { + + @Input() popover: TbPopoverComponent; + @Input() debugStrategy: DebugStrategy; + + debugStrategyFormGroup: UntypedFormGroup; + + onStrategyApplied = new EventEmitter() + + readonly maxRuleNodeDebugDurationMinutes = getCurrentAuthState(this.store).maxRuleNodeDebugDurationMinutes; + + constructor(private fb: UntypedFormBuilder, + protected store: Store) { + super(store); + + this.debugStrategyFormGroup = this.fb.group({ + allMessages: [false], + onFailure: [false] + }); + } + + ngOnInit(): void { + this.updatePanelStrategy(); + } + + onCancel() { + this.popover?.hide(); + } + + onApply(): void { + const allMessages = this.debugStrategyFormGroup.get('allMessages').value; + const onFailure = this.debugStrategyFormGroup.get('onFailure').value; + if (allMessages && onFailure) { + this.onStrategyApplied.emit(DebugStrategy.ALL_WITH_FAILURES); + } else if (allMessages) { + this.onStrategyApplied.emit(DebugStrategy.ALL_EVENTS); + } else if (onFailure) { + this.onStrategyApplied.emit(DebugStrategy.ONLY_FAILURE_EVENTS); + } else { + this.onStrategyApplied.emit(DebugStrategy.DISABLED); + } + } + + private updatePanelStrategy(): void { + switch (this.debugStrategy) { + case DebugStrategy.ALL_WITH_FAILURES: + this.debugStrategyFormGroup.get('allMessages').patchValue(true, { emitEvent: false }); + this.debugStrategyFormGroup.get('onFailure').patchValue(true, { emitEvent: false }); + break; + case DebugStrategy.ONLY_FAILURE_EVENTS: + this.debugStrategyFormGroup.get('onFailure').patchValue(true, { emitEvent: false }); + break; + case DebugStrategy.ALL_EVENTS: + this.debugStrategyFormGroup.get('allMessages').patchValue(true, { emitEvent: false }); + break; + } + } +} diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html index 014a73973c..c64fd2e422 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html @@ -22,7 +22,7 @@
-
+
rulenode.name @@ -34,13 +34,19 @@ {{ 'rulenode.name-max-length' | translate }} -
- - {{ 'rulenode.debug-mode' | translate }} - - - {{ 'rulenode.singleton-mode' | translate }} - +
+ +
{ ruleChainId?: RuleChainId; type: string; name: string; - debugMode: boolean; + debugStrategy: DebugStrategy; + lastUpdateTs?: number; singletonMode: boolean; queueName?: string; configurationVersion?: number; @@ -345,13 +346,21 @@ export interface FcRuleNode extends FcRuleNodeType { ruleNodeId?: RuleNodeId; additionalInfo?: any; configuration?: RuleNodeConfiguration; - debugMode?: boolean; + debugStrategy?: DebugStrategy; + lastUpdateTs?: number; error?: string; highlighted?: boolean; componentClazz?: string; ruleChainType?: RuleChainType; } +export enum DebugStrategy { + DISABLED = 'DISABLED', + ALL_EVENTS = 'ALL_EVENTS', + ONLY_FAILURE_EVENTS = 'ONLY_FAILURE_EVENTS', + ALL_WITH_FAILURES = 'ALL_WITH_FAILURES', +} + export interface FcRuleEdge extends FcEdge { labels?: string[]; } diff --git a/ui-ngx/src/assets/locale/locale.constant-ar_AE.json b/ui-ngx/src/assets/locale/locale.constant-ar_AE.json index 0afd671082..c45a5b4be4 100644 --- a/ui-ngx/src/assets/locale/locale.constant-ar_AE.json +++ b/ui-ngx/src/assets/locale/locale.constant-ar_AE.json @@ -4556,7 +4556,6 @@ "deselect-all": "إلغاء تحديد الكل", "rulenode-details": "تفاصيل عقدة القاعدة", "debug-mode": "وضع التصحيح", - "singleton-mode": "وضع المفرد", "configuration": "التكوين", "link": "رابط", "link-details": "تفاصيل رابط عقدة القاعدة", diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index 0fa19cc176..6185257663 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -995,6 +995,19 @@ "type-timeseries-deleted": "Telemetry deleted", "type-sms-sent": "SMS sent" }, + "debug-strategy": { + "min": "min", + "label": "Debug strategy", + "on-failure": "Failures only (24/7)", + "all-messages": "All messages", + "failures": "Failures", + "all": "All", + "hint": { + "main": "All node debug messages rate limited with:", + "on-failure": "Save all failure debug events without time limit.", + "all-messages": "Save all debug events during time limit." + } + }, "confirm-on-exit": { "message": "You have unsaved changes. Are you sure you want to leave this page?", "html-message": "You have unsaved changes.
Are you sure you want to leave this page?", @@ -2071,6 +2084,7 @@ "column": "Column", "row": "Row" }, + "disabled": "Disabled", "edge": { "edge": "Edge", "edge-instances": "Edge instances", @@ -4014,7 +4028,7 @@ "deselect-all": "Deselect all", "rulenode-details": "Rule node details", "debug-mode": "Debug mode", - "singleton-mode": "Singleton mode", + "singleton": "Singleton", "configuration": "Configuration", "link": "Link", "link-details": "Rule node link details", @@ -4292,6 +4306,9 @@ "maximum-ota-packages-sum-data-size": "Maximum total size of OTA package files (bytes)", "maximum-ota-package-sum-data-size-required": "Maximum total size of OTA package files is required.", "maximum-ota-package-sum-data-size-range": "Maximum total size of OTA package files can't be negative", + "maximum-rule-node-debug-duration-min": "Maximum rule node debug duration (min)", + "maximum-rule-node-debug-duration-min-required": "Maximum rule node debug duration is required.", + "maximum-rule-node-debug-duration-min-range": "Maximum rule node debug duration can't be negative", "rest-requests-for-tenant": "REST requests for tenant", "transport-tenant-telemetry-msg-rate-limit": "Transport tenant telemetry messages", "transport-tenant-telemetry-data-points-rate-limit": "Transport tenant telemetry data points", diff --git a/ui-ngx/src/assets/locale/locale.constant-es_ES.json b/ui-ngx/src/assets/locale/locale.constant-es_ES.json index f653e7179a..1ae18bd51c 100644 --- a/ui-ngx/src/assets/locale/locale.constant-es_ES.json +++ b/ui-ngx/src/assets/locale/locale.constant-es_ES.json @@ -3485,7 +3485,6 @@ "deselect-all": "Deshacer selección de todos", "rulenode-details": "Detalles del nodo de reglas", "debug-mode": "Modo Debug", - "singleton-mode": "Módo único", "configuration": "Configuración", "link": "Enlace", "link-details": "Detalles del enlace del nodo de reglas", diff --git a/ui-ngx/src/assets/locale/locale.constant-lt_LT.json b/ui-ngx/src/assets/locale/locale.constant-lt_LT.json index cb3a96f989..4791ab251f 100644 --- a/ui-ngx/src/assets/locale/locale.constant-lt_LT.json +++ b/ui-ngx/src/assets/locale/locale.constant-lt_LT.json @@ -4458,7 +4458,6 @@ "deselect-all": "Deselect all", "rulenode-details": "Rule node details", "debug-mode": "Debug mode", - "singleton-mode": "Singleton mode", "configuration": "Configuration", "link": "Link", "link-details": "Rule node link details", diff --git a/ui-ngx/src/assets/locale/locale.constant-nl_BE.json b/ui-ngx/src/assets/locale/locale.constant-nl_BE.json index c16c9c5e40..67d73b8be9 100644 --- a/ui-ngx/src/assets/locale/locale.constant-nl_BE.json +++ b/ui-ngx/src/assets/locale/locale.constant-nl_BE.json @@ -4260,7 +4260,6 @@ "deselect-all": "Alles deselecteren", "rulenode-details": "Details van rule nodes", "debug-mode": "Foutopsporingsmodus", - "singleton-mode": "Singleton-modus", "configuration": "Configuratie", "link": "Verbinden", "link-details": "Details van rule nodes links", diff --git a/ui-ngx/src/assets/locale/locale.constant-pl_PL.json b/ui-ngx/src/assets/locale/locale.constant-pl_PL.json index ac27313248..bb98170a92 100644 --- a/ui-ngx/src/assets/locale/locale.constant-pl_PL.json +++ b/ui-ngx/src/assets/locale/locale.constant-pl_PL.json @@ -4474,7 +4474,6 @@ "deselect-all": "Odznacz wszystkie", "rulenode-details": "Szczegóły węzła reguły", "debug-mode": "Tryb debugowania", - "singleton-mode": "Tryb singletonowy", "configuration": "Konfiguracja", "link": "Połączenie", "link-details": "Szczegóły połączenia węzła reguły", diff --git a/ui-ngx/src/assets/locale/locale.constant-zh_CN.json b/ui-ngx/src/assets/locale/locale.constant-zh_CN.json index ac302a78bb..bd2ff3c820 100644 --- a/ui-ngx/src/assets/locale/locale.constant-zh_CN.json +++ b/ui-ngx/src/assets/locale/locale.constant-zh_CN.json @@ -3968,7 +3968,6 @@ "deselect-all": "取消选择", "rulenode-details": "规则节点详情", "debug-mode": "调试模式", - "singleton-mode": "单例模式", "configuration": "配置", "link": "链接", "link-details": "规则节点链接详情", diff --git a/ui-ngx/src/styles.scss b/ui-ngx/src/styles.scss index 37068909e0..c3240d7d26 100644 --- a/ui-ngx/src/styles.scss +++ b/ui-ngx/src/styles.scss @@ -1267,6 +1267,23 @@ pre.tb-highlight { .no-wrap { white-space: nowrap; } + + .tb-rounded-btn { + border-radius: 20px; + padding: 0 16px; + + &.active:not(:disabled) { + border-color: $primary !important; + } + + &:disabled, &:not(.active) { + background: rgba(0, 0, 0, 0.06); + } + + &:not(.active) { + color: rgba(0, 0, 0, 0.76); + } + } } /*************** From bc5d15553e6104e1523a10a2cbecb4f2c6a8240c Mon Sep 17 00:00:00 2001 From: mpetrov Date: Thu, 7 Nov 2024 11:44:54 +0200 Subject: [PATCH 09/37] refactoring --- .../rulechain/debug-strategy-button.component.ts | 2 +- .../pages/rulechain/rule-node-details.component.ts | 8 +++++--- ui-ngx/src/styles.scss | 11 ++++++----- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.ts index 9a158d6a85..64194d8c93 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.ts @@ -99,7 +99,7 @@ export class DebugStrategyButtonComponent implements ControlValueAccessor { {}, {}, {}, true); debugStrategyPopover.tbComponentRef.instance.popover = debugStrategyPopover; - debugStrategyPopover.tbComponentRef.instance.onStrategyApplied.subscribe((strategy: DebugStrategy) => { + debugStrategyPopover.tbComponentRef.instance.onStrategyApplied.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((strategy: DebugStrategy) => { this.debugStrategyFormControl.patchValue(strategy); this.cdr.markForCheck(); debugStrategyPopover.hide(); diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts index ead0acb880..02fa18bd83 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts @@ -29,7 +29,7 @@ import { PageComponent } from '@shared/components/page.component'; import { Store } from '@ngrx/store'; import { AppState } from '@core/core.state'; import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; -import { DebugStrategy, FcRuleNode, RuleNodeType } from '@shared/models/rule-node.models'; +import { FcRuleNode, RuleNodeType } from '@shared/models/rule-node.models'; import { EntityType } from '@shared/models/entity-type.models'; import { Subject } from 'rxjs'; import { RuleNodeConfigComponent } from './rule-node-config.component'; @@ -92,7 +92,7 @@ export class RuleNodeDetailsComponent extends PageComponent implements OnInit, O if (this.ruleNode) { this.ruleNodeFormGroup = this.fb.group({ name: [this.ruleNode.name, [Validators.required, Validators.pattern('(.|\\s)*\\S(.|\\s)*'), Validators.maxLength(255)]], - debugStrategy: [this.ruleNode.debugStrategy ?? DebugStrategy.DISABLED], + debugStrategy: [this.ruleNode.debugStrategy], singletonMode: [this.ruleNode.singletonMode, []], configuration: [this.ruleNode.configuration, [Validators.required]], additionalInfo: this.fb.group( @@ -171,7 +171,9 @@ export class RuleNodeDetailsComponent extends PageComponent implements OnInit, O if ($event) { $event.stopPropagation(); } - this.ruleNodeFormGroup.get('singletonMode').patchValue(!this.ruleNodeFormGroup.get('singletonMode').value); + const singleModeControl = this.ruleNodeFormGroup.get('singletonMode'); + singleModeControl.patchValue(!singleModeControl.value); + singleModeControl.markAsDirty(); } openRuleChain($event: Event) { diff --git a/ui-ngx/src/styles.scss b/ui-ngx/src/styles.scss index c3240d7d26..50172eaa43 100644 --- a/ui-ngx/src/styles.scss +++ b/ui-ngx/src/styles.scss @@ -1272,17 +1272,18 @@ pre.tb-highlight { border-radius: 20px; padding: 0 16px; + &:not(.active) { + color: rgba(0, 0, 0, 0.76); + background: rgba(0, 0, 0, 0.06); + } + &.active:not(:disabled) { border-color: $primary !important; } - &:disabled, &:not(.active) { + &:disabled { background: rgba(0, 0, 0, 0.06); } - - &:not(.active) { - color: rgba(0, 0, 0, 0.76); - } } } From 0a00b3939849853857c4fa2402ec6e86a570a62e Mon Sep 17 00:00:00 2001 From: mpetrov Date: Thu, 7 Nov 2024 16:26:38 +0200 Subject: [PATCH 10/37] major refactoring --- ui-ngx/src/app/core/auth/auth.models.ts | 1 + .../rulechain/debug-duration-left.pipe.ts | 9 ++- .../debug-strategy-button.component.html | 6 +- .../debug-strategy-button.component.ts | 6 +- .../debug-strategy-panel.component.html | 19 +++-- .../debug-strategy-panel.component.ts | 7 +- .../rule-node-details.component.html | 8 +- .../src/app/shared/models/rule-node.models.ts | 2 +- .../pipe/milliseconds-to-time-string.pipe.ts | 75 ++++++++----------- .../assets/locale/locale.constant-en_US.json | 6 +- 10 files changed, 74 insertions(+), 65 deletions(-) diff --git a/ui-ngx/src/app/core/auth/auth.models.ts b/ui-ngx/src/app/core/auth/auth.models.ts index cfc3fed4ba..e4d05e7d21 100644 --- a/ui-ngx/src/app/core/auth/auth.models.ts +++ b/ui-ngx/src/app/core/auth/auth.models.ts @@ -28,6 +28,7 @@ export interface SysParamsState { userSettings: UserSettings; maxResourceSize: number; maxRuleNodeDebugDurationMinutes: number; + ruleChainDebugPerTenantLimitsConfiguration?: string; } export interface SysParams extends SysParamsState { diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts b/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts index 17176bd4af..7086bb504d 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts @@ -16,6 +16,8 @@ import { Pipe, PipeTransform } from '@angular/core'; import { MINUTE } from '@shared/models/time/time.models'; +import { TranslateService } from '@ngx-translate/core'; +import { MillisecondsToTimeStringPipe } from '@shared/pipe/milliseconds-to-time-string.pipe'; @Pipe({ name: 'debugDurationLeft', @@ -23,9 +25,12 @@ import { MINUTE } from '@shared/models/time/time.models'; standalone: true, }) export class DebugDurationLeftPipe implements PipeTransform { + + constructor(private translate: TranslateService, private millisecondsToTimeString: MillisecondsToTimeStringPipe) { + } transform(lastUpdateTs: number, maxRuleNodeDebugDurationMinutes: number): string { - const minutes = Math.max(0, Math.floor(this.getDebugTime(lastUpdateTs, maxRuleNodeDebugDurationMinutes) / MINUTE)); - return `${minutes} min left`; + const time = this.millisecondsToTimeString.transform(this.getDebugTime(lastUpdateTs, maxRuleNodeDebugDurationMinutes), true, true); + return `${time} ` + this.translate.instant('common.left'); } getDebugTime(lastUpdateTs: number, maxRuleNodeDebugDurationMinutes: number): number { diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.html index 800f54ae3e..ea94a99c74 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.html +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.html @@ -16,7 +16,7 @@ -->
diff --git a/ui-ngx/src/app/shared/models/rule-node.models.ts b/ui-ngx/src/app/shared/models/rule-node.models.ts index 56f76b33fe..c68ecc3d1e 100644 --- a/ui-ngx/src/app/shared/models/rule-node.models.ts +++ b/ui-ngx/src/app/shared/models/rule-node.models.ts @@ -358,7 +358,7 @@ export enum DebugStrategy { DISABLED = 'DISABLED', ALL_EVENTS = 'ALL_EVENTS', ONLY_FAILURE_EVENTS = 'ONLY_FAILURE_EVENTS', - ALL_WITH_FAILURES = 'ALL_WITH_FAILURES', + ALL_THEN_ONLY_FAILURE_EVENTS = 'ALL_THEN_ONLY_FAILURE_EVENTS', } export interface FcRuleEdge extends FcEdge { diff --git a/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts b/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts index c767be3dc5..d7c5902898 100644 --- a/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts +++ b/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts @@ -24,53 +24,42 @@ export class MillisecondsToTimeStringPipe implements PipeTransform { constructor(private translate: TranslateService) { } + transform(millseconds: number, shortFormat = false, onlyFirstDigit = false): string { + const { days, hours, minutes, seconds } = this.extractTimeUnits(millseconds); + return this.formatTimeString(days, hours, minutes, seconds, shortFormat, onlyFirstDigit); + } - transform(millseconds: number, shortFormat = false): string { - let seconds = Math.floor(millseconds / 1000); + private extractTimeUnits(milliseconds: number): { days: number; hours: number; minutes: number; seconds: number } { + const seconds = Math.floor(milliseconds / 1000); const days = Math.floor(seconds / 86400); - let hours = Math.floor((seconds % 86400) / 3600); - let minutes = Math.floor(((seconds % 86400) % 3600) / 60); - seconds = seconds % 60; + const hours = Math.floor((seconds % 86400) / 3600); + const minutes = Math.floor(((seconds % 86400) % 3600) / 60); + return { days, hours, minutes, seconds: seconds % 60 }; + } + + private formatTimeString( + days: number, + hours: number, + minutes: number, + seconds: number, + shortFormat: boolean, + onlyFirstDigit: boolean + ): string { + const timeUnits = [ + { value: days, key: 'days', shortKey: 'short.days' }, + { value: hours, key: 'hours', shortKey: 'short.hours' }, + { value: minutes, key: 'minutes', shortKey: 'short.minutes' }, + { value: seconds, key: 'seconds', shortKey: 'short.seconds' } + ]; + let timeString = ''; - if (shortFormat) { - if (days > 0) { - timeString += this.translate.instant('timewindow.short.days', {days}); - } - if (hours > 0) { - timeString += this.translate.instant('timewindow.short.hours', {hours}); - } - if (minutes > 0) { - timeString += this.translate.instant('timewindow.short.minutes', {minutes}); - } - if (seconds > 0) { - timeString += this.translate.instant('timewindow.short.seconds', {seconds}); - } - if (!timeString.length) { - timeString += this.translate.instant('timewindow.short.seconds', {seconds: 0}); - } - } else { - if (days > 0) { - timeString += this.translate.instant('timewindow.days', {days}); - } - if (hours > 0) { - if (timeString.length === 0 && hours === 1) { - hours = 0; - } - timeString += this.translate.instant('timewindow.hours', {hours}); - } - if (minutes > 0) { - if (timeString.length === 0 && minutes === 1) { - minutes = 0; - } - timeString += this.translate.instant('timewindow.minutes', {minutes}); - } - if (seconds > 0) { - if (timeString.length === 0 && seconds === 1) { - seconds = 0; - } - timeString += this.translate.instant('timewindow.seconds', {seconds}); + for (const { value, key, shortKey } of timeUnits) { + if (value > 0) { + timeString += this.translate.instant(shortFormat ? `timewindow.${shortKey}` : `timewindow.${key}`, { [key]: value }); + if (onlyFirstDigit) return timeString; } } - return timeString; + + return timeString.length > 0 ? timeString : this.translate.instant('timewindow.short.seconds', { seconds: 0 }); } } diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index 6185257663..161a5194bc 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -1004,6 +1004,7 @@ "all": "All", "hint": { "main": "All node debug messages rate limited with:", + "main-limited": "All node debug messages will be rate-limited, with a maximum of {{msg}} messages allowed per {{sec}} seconds.", "on-failure": "Save all failure debug events without time limit.", "all-messages": "Save all debug events during time limit." } @@ -1038,11 +1039,13 @@ "enter-password": "Enter password", "enter-search": "Enter search", "created-time": "Created time", + "disabled": "Disabled", "loading": "Loading...", "proceed": "Proceed", "open-details-page": "Open details page", "not-found": "Not found", - "documentation": "Documentation" + "documentation": "Documentation", + "left": "left" }, "content-type": { "json": "Json", @@ -2084,7 +2087,6 @@ "column": "Column", "row": "Row" }, - "disabled": "Disabled", "edge": { "edge": "Edge", "edge-instances": "Edge instances", From 107c95fbe2d215c589a95b58c101478e893cf872 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Thu, 7 Nov 2024 16:39:46 +0200 Subject: [PATCH 11/37] refactoring --- .../home/pages/rulechain/debug-duration-left.pipe.ts | 3 ++- .../shared/pipe/milliseconds-to-time-string.pipe.ts | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts b/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts index 7086bb504d..d6615da476 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts @@ -28,12 +28,13 @@ export class DebugDurationLeftPipe implements PipeTransform { constructor(private translate: TranslateService, private millisecondsToTimeString: MillisecondsToTimeStringPipe) { } + transform(lastUpdateTs: number, maxRuleNodeDebugDurationMinutes: number): string { const time = this.millisecondsToTimeString.transform(this.getDebugTime(lastUpdateTs, maxRuleNodeDebugDurationMinutes), true, true); return `${time} ` + this.translate.instant('common.left'); } - getDebugTime(lastUpdateTs: number, maxRuleNodeDebugDurationMinutes: number): number { + private getDebugTime(lastUpdateTs: number, maxRuleNodeDebugDurationMinutes: number): number { const maxDuration = maxRuleNodeDebugDurationMinutes * MINUTE; const updateDuration = lastUpdateTs ? new Date().getTime() - lastUpdateTs : 0; const leftTime = maxDuration - updateDuration; diff --git a/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts b/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts index d7c5902898..d00fd3f3fc 100644 --- a/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts +++ b/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts @@ -16,6 +16,7 @@ import { Pipe, PipeTransform } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; +import { DAY, HOUR, MINUTE, SECOND } from '@shared/models/time/time.models'; @Pipe({ name: 'milliSecondsToTimeString' @@ -24,17 +25,18 @@ export class MillisecondsToTimeStringPipe implements PipeTransform { constructor(private translate: TranslateService) { } + transform(millseconds: number, shortFormat = false, onlyFirstDigit = false): string { const { days, hours, minutes, seconds } = this.extractTimeUnits(millseconds); return this.formatTimeString(days, hours, minutes, seconds, shortFormat, onlyFirstDigit); } private extractTimeUnits(milliseconds: number): { days: number; hours: number; minutes: number; seconds: number } { - const seconds = Math.floor(milliseconds / 1000); - const days = Math.floor(seconds / 86400); - const hours = Math.floor((seconds % 86400) / 3600); - const minutes = Math.floor(((seconds % 86400) % 3600) / 60); - return { days, hours, minutes, seconds: seconds % 60 }; + const days = Math.floor(milliseconds / DAY); + const hours = Math.floor((milliseconds % DAY) / HOUR); + const minutes = Math.floor((milliseconds % HOUR) / MINUTE); + const seconds = Math.floor((milliseconds % MINUTE) / SECOND); + return { days, hours, minutes, seconds }; } private formatTimeString( From 76351f793a2464e11b98f3c6b7ab5e955a2d5a01 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Thu, 7 Nov 2024 16:40:52 +0200 Subject: [PATCH 12/37] refactoring --- .../src/app/shared/pipe/milliseconds-to-time-string.pipe.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts b/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts index d00fd3f3fc..1774d70fcb 100644 --- a/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts +++ b/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts @@ -26,8 +26,8 @@ export class MillisecondsToTimeStringPipe implements PipeTransform { constructor(private translate: TranslateService) { } - transform(millseconds: number, shortFormat = false, onlyFirstDigit = false): string { - const { days, hours, minutes, seconds } = this.extractTimeUnits(millseconds); + transform(millSeconds: number, shortFormat = false, onlyFirstDigit = false): string { + const { days, hours, minutes, seconds } = this.extractTimeUnits(millSeconds); return this.formatTimeString(days, hours, minutes, seconds, shortFormat, onlyFirstDigit); } From 94f563f1857ddf863d65e21e1280e510b23c79cf Mon Sep 17 00:00:00 2001 From: mpetrov Date: Thu, 7 Nov 2024 16:41:09 +0200 Subject: [PATCH 13/37] refactoring --- .../src/app/shared/pipe/milliseconds-to-time-string.pipe.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts b/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts index 1774d70fcb..de25f15514 100644 --- a/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts +++ b/ui-ngx/src/app/shared/pipe/milliseconds-to-time-string.pipe.ts @@ -26,8 +26,8 @@ export class MillisecondsToTimeStringPipe implements PipeTransform { constructor(private translate: TranslateService) { } - transform(millSeconds: number, shortFormat = false, onlyFirstDigit = false): string { - const { days, hours, minutes, seconds } = this.extractTimeUnits(millSeconds); + transform(milliSeconds: number, shortFormat = false, onlyFirstDigit = false): string { + const { days, hours, minutes, seconds } = this.extractTimeUnits(milliSeconds); return this.formatTimeString(days, hours, minutes, seconds, shortFormat, onlyFirstDigit); } From c15e8e7fe74baafce3ce3b81080d1557b99a0b27 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Thu, 7 Nov 2024 17:18:43 +0200 Subject: [PATCH 14/37] refactoring --- .../home/pages/rulechain/rule-node-details.component.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts index 02fa18bd83..1c0712c778 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts @@ -29,7 +29,7 @@ import { PageComponent } from '@shared/components/page.component'; import { Store } from '@ngrx/store'; import { AppState } from '@core/core.state'; import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; -import { FcRuleNode, RuleNodeType } from '@shared/models/rule-node.models'; +import { DebugStrategy, FcRuleNode, RuleNodeType } from '@shared/models/rule-node.models'; import { EntityType } from '@shared/models/entity-type.models'; import { Subject } from 'rxjs'; import { RuleNodeConfigComponent } from './rule-node-config.component'; @@ -92,7 +92,7 @@ export class RuleNodeDetailsComponent extends PageComponent implements OnInit, O if (this.ruleNode) { this.ruleNodeFormGroup = this.fb.group({ name: [this.ruleNode.name, [Validators.required, Validators.pattern('(.|\\s)*\\S(.|\\s)*'), Validators.maxLength(255)]], - debugStrategy: [this.ruleNode.debugStrategy], + debugStrategy: [this.ruleNode.debugStrategy ?? DebugStrategy.DISABLED], singletonMode: [this.ruleNode.singletonMode, []], configuration: [this.ruleNode.configuration, [Validators.required]], additionalInfo: this.fb.group( From 637fe2a258856172e2aaa2fd86cde14a57a55478 Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Mon, 11 Nov 2024 22:15:23 +0100 Subject: [PATCH 15/37] Used debugFailures and debugAll params instead of DebugStrategies --- .../rule_chains/edge_root_rule_chain.json | 27 +- .../device_profile/rule_chain_template.json | 21 +- .../tenant/rule_chains/root_rule_chain.json | 30 ++- .../main/data/upgrade/3.8.1/schema_update.sql | 6 +- .../actors/ruleChain/DefaultTbContext.java | 17 +- .../RuleChainActorMessageProcessor.java | 9 +- .../RuleNodeActorMessageProcessor.java | 3 +- .../BaseRuleChainMetadataConstructor.java | 5 +- .../actors/rule/DefaultTbContextTest.java | 251 +++++++----------- .../server/edge/AbstractEdgeTest.java | 6 +- .../server/edge/RuleChainEdgeTest.java | 3 +- ...AbstractRuleEngineFlowIntegrationTest.java | 11 +- ...actRuleEngineLifecycleIntegrationTest.java | 3 +- .../housekeeper/HousekeeperServiceTest.java | 5 +- .../sync/ie/ExportImportServiceSqlTest.java | 9 +- .../service/sync/vc/VersionControlTest.java | 9 +- .../common/data/rule/DebugStrategy.java | 70 ----- .../server/common/data/rule/RuleNode.java | 19 +- .../common/data/rule/RuleNodeDebugUtil.java | 45 ++++ .../DefaultTenantProfileConfiguration.java | 5 +- common/edge-api/src/main/proto/edge.proto | 11 +- .../server/dao/model/ModelConstants.java | 3 +- .../server/dao/model/sql/RuleNodeEntity.java | 20 +- .../server/dao/rule/BaseRuleChainService.java | 21 +- .../main/resources/sql/schema-entities.sql | 4 +- .../src/main/resources/root_rule_chain.json | 54 ++-- .../resources/MqttRuleNodeTestMetadata.json | 9 +- .../RpcResponseRuleChainMetadata.json | 9 +- 28 files changed, 326 insertions(+), 359 deletions(-) delete mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNodeDebugUtil.java diff --git a/application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json b/application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json index 04a5f52f4e..05dff1678f 100644 --- a/application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json +++ b/application/src/main/data/json/edge/rule_chains/edge_root_rule_chain.json @@ -20,7 +20,8 @@ }, "type": "org.thingsboard.rule.engine.profile.TbDeviceProfileNode", "name": "Device Profile Node", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "persistAlarmRulesState": false, "fetchAlarmRulesStateOnStart": false @@ -34,7 +35,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "defaultTTL": 0 }, @@ -47,7 +49,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", "name": "Save Client Attributes", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configurationVersion": 2, "configuration": { "scope": "CLIENT_SCOPE", @@ -64,7 +67,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "Message Type Switch", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "version": 0 }, @@ -77,7 +81,8 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log RPC from Device", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "scriptLang": "TBEL", "jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);", @@ -92,7 +97,8 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log Other", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "scriptLang": "TBEL", "jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);", @@ -107,7 +113,8 @@ }, "type": "org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode", "name": "RPC Call Request", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "timeoutInSeconds": 60 }, @@ -120,7 +127,8 @@ }, "type": "org.thingsboard.rule.engine.edge.TbMsgPushToCloudNode", "name": "Push to cloud", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "scope": "SERVER_SCOPE" }, @@ -133,7 +141,8 @@ }, "type": "org.thingsboard.rule.engine.edge.TbMsgPushToCloudNode", "name": "Push to cloud", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "scope": "SERVER_SCOPE" }, diff --git a/application/src/main/data/json/tenant/device_profile/rule_chain_template.json b/application/src/main/data/json/tenant/device_profile/rule_chain_template.json index b005331862..3e265591de 100644 --- a/application/src/main/data/json/tenant/device_profile/rule_chain_template.json +++ b/application/src/main/data/json/tenant/device_profile/rule_chain_template.json @@ -19,7 +19,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "defaultTTL": 0 } @@ -31,7 +32,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", "name": "Save Client Attributes", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configurationVersion": 2, "configuration": { "scope": "CLIENT_SCOPE", @@ -47,7 +49,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "Message Type Switch", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "version": 0 } @@ -59,7 +62,8 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log RPC from Device", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "scriptLang": "TBEL", "jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);", @@ -73,7 +77,8 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log Other", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "scriptLang": "TBEL", "jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);", @@ -87,7 +92,8 @@ }, "type": "org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode", "name": "RPC Call Request", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "timeoutInSeconds": 60 } @@ -100,7 +106,8 @@ }, "type": "org.thingsboard.rule.engine.profile.TbDeviceProfileNode", "name": "Device Profile Node", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "configuration": { "persistAlarmRulesState": false, "fetchAlarmRulesStateOnStart": false diff --git a/application/src/main/data/json/tenant/rule_chains/root_rule_chain.json b/application/src/main/data/json/tenant/rule_chains/root_rule_chain.json index 3b9898d0b1..4a799cf957 100644 --- a/application/src/main/data/json/tenant/rule_chains/root_rule_chain.json +++ b/application/src/main/data/json/tenant/rule_chains/root_rule_chain.json @@ -18,7 +18,9 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, + "debugAllUntil": 0, "configuration": { "defaultTTL": 0 } @@ -30,7 +32,9 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", "name": "Save Client Attributes", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, + "debugAllUntil": 0, "configurationVersion": 2, "configuration": { "scope": "CLIENT_SCOPE", @@ -46,7 +50,9 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "Message Type Switch", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, + "debugAllUntil": 0, "configuration": { "version": 0 } @@ -58,7 +64,9 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log RPC from Device", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, + "debugAllUntil": 0, "configuration": { "scriptLang": "TBEL", "jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);", @@ -72,7 +80,9 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log Other", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, + "debugAllUntil": 0, "configuration": { "scriptLang": "TBEL", "jsScript": "return '\\nIncoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);", @@ -86,7 +96,9 @@ }, "type": "org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode", "name": "RPC Call Request", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, + "debugAllUntil": 0, "configuration": { "timeoutInSeconds": 60 } @@ -99,7 +111,9 @@ }, "type": "org.thingsboard.rule.engine.profile.TbDeviceProfileNode", "name": "Device Profile Node", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, + "debugAllUntil": 0, "configuration": { "persistAlarmRulesState": false, "fetchAlarmRulesStateOnStart": false @@ -140,4 +154,4 @@ ], "ruleChainConnections": null } -} \ No newline at end of file +} diff --git a/application/src/main/data/upgrade/3.8.1/schema_update.sql b/application/src/main/data/upgrade/3.8.1/schema_update.sql index dab9505d36..cbe0e6103a 100644 --- a/application/src/main/data/upgrade/3.8.1/schema_update.sql +++ b/application/src/main/data/upgrade/3.8.1/schema_update.sql @@ -28,9 +28,9 @@ UPDATE tb_user SET additional_info = (additional_info::jsonb - 'lastLoginTs' - ' -- UPDATE RULE NODE DEBUG MODE TO DEBUG STRATEGY START ALTER TABLE rule_node - ADD COLUMN IF NOT EXISTS debug_strategy varchar(32) DEFAULT 'DISABLED'; + ADD COLUMN IF NOT EXISTS debug_failures boolean DEFAULT false; ALTER TABLE rule_node - ADD COLUMN IF NOT EXISTS last_update_ts bigint NOT NULL DEFAULT extract(epoch from now()) * 1000; + ADD COLUMN IF NOT EXISTS debug_all_until bigint NOT NULL DEFAULT 0; DO $$ BEGIN @@ -38,7 +38,7 @@ $$ FROM information_schema.columns WHERE table_name = 'rule_node' AND column_name = 'debug_mode') THEN UPDATE rule_node - SET debug_strategy = CASE WHEN debug_mode = true THEN 'ALL_EVENTS' ELSE 'DISABLED' END; + SET debug_all_until = CASE WHEN debug_mode = true THEN extract(epoch from now() + 3600) * 1000 ELSE 0 END; ALTER TABLE rule_node DROP COLUMN debug_mode; END IF; diff --git a/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java b/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java index 119d1d2900..000c260769 100644 --- a/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java +++ b/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java @@ -63,8 +63,8 @@ import org.thingsboard.server.common.data.msg.TbMsgType; import org.thingsboard.server.common.data.msg.TbNodeConnectionType; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; -import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleNode; +import org.thingsboard.server.common.data.rule.RuleNodeDebugUtil; import org.thingsboard.server.common.data.rule.RuleNodeState; import org.thingsboard.server.common.data.script.ScriptLanguage; import org.thingsboard.server.common.msg.TbActorMsg; @@ -1002,22 +1002,11 @@ public class DefaultTbContext implements TbContext { private void persistDebugOutput(TbMsg msg, Set relationTypes, Throwable error, String failureMessage) { RuleNode ruleNode = nodeCtx.getSelf(); - DebugStrategy debugStrategy = ruleNode.getDebugStrategy(); - if (debugStrategy.shouldPersistDebugOutputForAllEvents(ruleNode.getLastUpdateTs(), msg.getTs(), getMaxRuleNodeDebugDurationMinutes())) { + if (RuleNodeDebugUtil.isDebugAllAvailable(ruleNode)) { relationTypes.forEach(relationType -> mainCtx.persistDebugOutput(getTenantId(), ruleNode.getId(), msg, relationType, error, failureMessage)); - } else if (debugStrategy.shouldPersistDebugForFailureEvent(relationTypes)) { + } else if (RuleNodeDebugUtil.isDebugFailuresAvailable(ruleNode, relationTypes)) { mainCtx.persistDebugOutput(getTenantId(), ruleNode.getId(), msg, TbNodeConnectionType.FAILURE, error, failureMessage); } } - private int getMaxRuleNodeDebugDurationMinutes() { - if (nodeCtx.getSelf().getDebugStrategy().isHasDuration()) { - var configuration = mainCtx.getTenantProfileCache() - .get(getTenantId()).getProfileData().getConfiguration(); - int systemMaxRuleNodeDebugModeDurationMinutes = mainCtx.getMaxRuleNodeDebugModeDurationMinutes(); - return configuration.getMaxRuleNodeDebugModeDurationMinutes(systemMaxRuleNodeDebugModeDurationMinutes); - } - return 0; - } - } diff --git a/application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleChainActorMessageProcessor.java b/application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleChainActorMessageProcessor.java index 01caa50860..636ea3fd07 100644 --- a/application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleChainActorMessageProcessor.java +++ b/application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleChainActorMessageProcessor.java @@ -35,6 +35,7 @@ import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainType; import org.thingsboard.server.common.data.rule.RuleNode; +import org.thingsboard.server.common.data.rule.RuleNodeDebugUtil; import org.thingsboard.server.common.msg.TbMsg; import org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg; import org.thingsboard.server.common.msg.plugin.RuleNodeUpdatedMsg; @@ -255,13 +256,7 @@ public class RuleChainActorMessageProcessor extends ComponentMsgProcessor connections) { + void givenDebugFailuresEventsAndConnections_whenTellNext_thenVerifyDebugOutputPersisted(Set connections) { // GIVEN var callbackMock = mock(TbMsgCallback.class); var msg = getTbMsgWithCallback(callbackMock); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.ONLY_FAILURE_EVENTS); + ruleNode.setDebugFailures(true); + ruleNode.setDebugAllUntil(0); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); @@ -169,7 +166,7 @@ class DefaultTbContextTest { checkTellNextCommonLogic(callbackMock, connections, msg); } - private static Stream> givenDebugStrategyOnlyFailureEventsAndConnections_whenTellNext_thenVerifyDebugOutputPersisted() { + private static Stream> givenDebugFailuresEventsAndConnections_whenTellNext_thenVerifyDebugOutputPersisted() { return Stream.of( Collections.singleton(TbNodeConnectionType.FAILURE), Set.of(TbNodeConnectionType.FAILURE, TbNodeConnectionType.SUCCESS) @@ -178,13 +175,14 @@ class DefaultTbContextTest { @MethodSource @ParameterizedTest - void givenDebugStrategyDisabledAndConnections_whenTellNext_thenVerifyDebugOutputNotPersisted(Set connections) { + void givenDebugDisabledAndConnections_whenTellNext_thenVerifyDebugOutputNotPersisted(Set connections) { // GIVEN var callbackMock = mock(TbMsgCallback.class); var msg = getTbMsgWithCallback(callbackMock); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.DISABLED); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(0); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); @@ -198,7 +196,7 @@ class DefaultTbContextTest { checkTellNextCommonLogic(callbackMock, connections, msg); } - private static Stream> givenDebugStrategyDisabledAndConnections_whenTellNext_thenVerifyDebugOutputNotPersisted() { + private static Stream> givenDebugDisabledAndConnections_whenTellNext_thenVerifyDebugOutputNotPersisted() { return Stream.of( Collections.singleton(TbNodeConnectionType.FAILURE), Collections.singleton(TbNodeConnectionType.SUCCESS), @@ -208,18 +206,17 @@ class DefaultTbContextTest { @MethodSource @ParameterizedTest - void givenDebugStrategyAllEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted(String connection) { + void givenDebugAllEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted(String connection) { // GIVEN var callbackMock = mock(TbMsgCallback.class); var msg = getTbMsgWithCallback(callbackMock); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setLastUpdateTs(System.currentTimeMillis()); - ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(getUntilTime()); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); - mockGetMaxRuleNodeDebugModeDurationMinutes(); // WHEN defaultTbContext.tellNext(msg, connection); @@ -227,30 +224,27 @@ class DefaultTbContextTest { // THEN then(nodeCtxMock).should().getChainActor(); then(nodeCtxMock).shouldHaveNoMoreInteractions(); - then(mainCtxMock).should().getTenantProfileCache(); - then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, connection, null, null); then(mainCtxMock).shouldHaveNoMoreInteractions(); checkTellNextCommonLogic(callbackMock, connection, msg); } - private static Stream givenDebugStrategyAllEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted() { + private static Stream givenDebugAllEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted() { return failureAndSuccessConnection(); } @Test - public void givenDebugStrategyAllEventsAndFailureAndSuccessConnection_whenTellNext_thenVerifyDebugOutputPersistedForAllEvents() { + public void givenDebugAllEventsAndFailureAndSuccessConnection_whenTellNext_thenVerifyDebugOutputPersistedForAllEvents() { // GIVEN var callbackMock = mock(TbMsgCallback.class); var msg = getTbMsgWithCallback(callbackMock); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setLastUpdateTs(System.currentTimeMillis()); - ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(getUntilTime()); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); - mockGetMaxRuleNodeDebugModeDurationMinutes(); // WHEN Set connections = failureAndSuccessConnection().collect(Collectors.toSet()); @@ -259,8 +253,6 @@ class DefaultTbContextTest { // THEN then(nodeCtxMock).should().getChainActor(); then(nodeCtxMock).shouldHaveNoMoreInteractions(); - then(mainCtxMock).should().getTenantProfileCache(); - then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); var nodeConnectionsCaptor = ArgumentCaptor.forClass(String.class); int wantedNumberOfInvocations = connections.size(); then(mainCtxMock).should(times(wantedNumberOfInvocations)).persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), eq(msg), nodeConnectionsCaptor.capture(), nullable(Throwable.class), nullable(String.class)); @@ -272,18 +264,17 @@ class DefaultTbContextTest { @MethodSource @ParameterizedTest - void givenDebugStrategyAllThenOnlyFailureEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted(String connection) { + void givenDebugAllThenOnlyFailureEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted(String connection) { // GIVEN var callbackMock = mock(TbMsgCallback.class); var msg = getTbMsgWithCallback(callbackMock); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setLastUpdateTs(System.currentTimeMillis()); - ruleNode.setDebugStrategy(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(getUntilTime()); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); - mockGetMaxRuleNodeDebugModeDurationMinutes(); // WHEN defaultTbContext.tellNext(msg, connection); @@ -291,30 +282,27 @@ class DefaultTbContextTest { // THEN then(nodeCtxMock).should().getChainActor(); then(nodeCtxMock).shouldHaveNoMoreInteractions(); - then(mainCtxMock).should().getTenantProfileCache(); - then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, connection, null, null); then(mainCtxMock).shouldHaveNoMoreInteractions(); checkTellNextCommonLogic(callbackMock, connection, msg); } - private static Stream givenDebugStrategyAllThenOnlyFailureEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted() { + private static Stream givenDebugAllThenOnlyFailureEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted() { return failureAndSuccessConnection(); } @Test - public void givenDebugStrategyAllThenOnlyEventsAndFailureAndSuccessConnection_whenTellNext_thenVerifyDebugOutputPersistedForAllEvents() { + public void givenDebugAllThenOnlyEventsAndFailureAndSuccessConnection_whenTellNext_thenVerifyDebugOutputPersistedForAllEvents() { // GIVEN var callbackMock = mock(TbMsgCallback.class); var msg = getTbMsgWithCallback(callbackMock); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setLastUpdateTs(System.currentTimeMillis()); - ruleNode.setDebugStrategy(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS); + ruleNode.setDebugFailures(true); + ruleNode.setDebugAllUntil(getUntilTime()); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); - mockGetMaxRuleNodeDebugModeDurationMinutes(); // WHEN Set connections = failureAndSuccessConnection().collect(Collectors.toSet()); @@ -323,8 +311,6 @@ class DefaultTbContextTest { // THEN then(nodeCtxMock).should().getChainActor(); then(nodeCtxMock).shouldHaveNoMoreInteractions(); - then(mainCtxMock).should().getTenantProfileCache(); - then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); var nodeConnectionsCaptor = ArgumentCaptor.forClass(String.class); int wantedNumberOfInvocations = connections.size(); then(mainCtxMock).should(times(wantedNumberOfInvocations)).persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), eq(msg), nodeConnectionsCaptor.capture(), nullable(Throwable.class), nullable(String.class)); @@ -339,12 +325,12 @@ class DefaultTbContextTest { } @Test - public void givenDebugStrategyOnlyFailureEventsAndFailureConnection_whenOutput_thenVerifyDebugOutputPersisted() { + public void givenDebugFailuresEventsAndFailureConnection_whenOutput_thenVerifyDebugOutputPersisted() { // GIVEN var msgMock = mock(TbMsg.class); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.ONLY_FAILURE_EVENTS); + ruleNode.setDebugFailures(true); given(msgMock.popFormStack()).willReturn(new TbMsgProcessingStackItem(RULE_CHAIN_ID, RULE_NODE_ID)); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); @@ -361,12 +347,12 @@ class DefaultTbContextTest { } @Test - public void givenDebugStrategyOnlyFailureEventsAndSuccessConnection_whenOutput_thenVerifyDebugOutputNotPersisted() { + public void givenDebugFailuresEventsAndSuccessConnection_whenOutput_thenVerifyDebugOutputNotPersisted() { // GIVEN var msgMock = mock(TbMsg.class); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.ONLY_FAILURE_EVENTS); + ruleNode.setDebugFailures(true); given(msgMock.popFormStack()).willReturn(new TbMsgProcessingStackItem(RULE_CHAIN_ID, RULE_NODE_ID)); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); @@ -382,12 +368,13 @@ class DefaultTbContextTest { @ParameterizedTest @ValueSource(strings = {TbNodeConnectionType.SUCCESS, TbNodeConnectionType.FAILURE}) - void givenDebugStrategyDisabled_whenOutput_thenVerifyDebugOutputNotPersisted(String nodeConnection) { + void givenDebugDisabled_whenOutput_thenVerifyDebugOutputNotPersisted(String nodeConnection) { // GIVEN var msgMock = mock(TbMsg.class); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.DISABLED); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(0); given(msgMock.popFormStack()).willReturn(new TbMsgProcessingStackItem(RULE_CHAIN_ID, RULE_NODE_ID)); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); @@ -403,25 +390,23 @@ class DefaultTbContextTest { @ParameterizedTest @ValueSource(strings = {TbNodeConnectionType.SUCCESS, TbNodeConnectionType.FAILURE}) - void givenDebugStrategyAllEvents_whenOutput_thenVerifyDebugOutputPersisted(String nodeConnection) { + void givenDebugAllEvents_whenOutput_thenVerifyDebugOutputPersisted(String nodeConnection) { // GIVEN var msgMock = mock(TbMsg.class); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(getUntilTime()); given(msgMock.popFormStack()).willReturn(new TbMsgProcessingStackItem(RULE_CHAIN_ID, RULE_NODE_ID)); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); - mockGetMaxRuleNodeDebugModeDurationMinutes(); // WHEN defaultTbContext.output(msgMock, nodeConnection); // THEN checkOutputCommonLogic(msgMock, nodeConnection); - then(mainCtxMock).should().getTenantProfileCache(); - then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, nodeConnection, null, null); then(mainCtxMock).shouldHaveNoMoreInteractions(); then(nodeCtxMock).shouldHaveNoMoreInteractions(); @@ -429,25 +414,23 @@ class DefaultTbContextTest { @ParameterizedTest @ValueSource(strings = {TbNodeConnectionType.SUCCESS, TbNodeConnectionType.FAILURE}) - void givenDebugStrategyAllThenOnlyFailureEvents_whenOutput_thenVerifyDebugOutputPersisted(String nodeConnection) { + void givenDebugAllThenOnlyFailureEvents_whenOutput_thenVerifyDebugOutputPersisted(String nodeConnection) { // GIVEN var msgMock = mock(TbMsg.class); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(getUntilTime()); given(msgMock.popFormStack()).willReturn(new TbMsgProcessingStackItem(RULE_CHAIN_ID, RULE_NODE_ID)); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); - mockGetMaxRuleNodeDebugModeDurationMinutes(); // WHEN defaultTbContext.output(msgMock, nodeConnection); // THEN checkOutputCommonLogic(msgMock, nodeConnection); - then(mainCtxMock).should().getTenantProfileCache(); - then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, nodeConnection, null, null); then(mainCtxMock).shouldHaveNoMoreInteractions(); then(nodeCtxMock).shouldHaveNoMoreInteractions(); @@ -459,7 +442,8 @@ class DefaultTbContextTest { var msgMock = mock(TbMsg.class); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.DISABLED); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(0); given(msgMock.popFormStack()).willReturn(null); TbMsgCallback callbackMock = mock(TbMsgCallback.class); given(msgMock.getCallback()).willReturn(callbackMock); @@ -476,19 +460,18 @@ class DefaultTbContextTest { } @Test - public void givenEmptyStackAndDebugStrategyAllEvents_whenOutput_thenVerifyMsgAckAndDebugOutputPersisted() { + public void givenEmptyStackAndDebugAllEvents_whenOutput_thenVerifyMsgAckAndDebugOutputPersisted() { // GIVEN var msgMock = mock(TbMsg.class); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); - ruleNode.setLastUpdateTs(System.currentTimeMillis()); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(getUntilTime()); given(msgMock.popFormStack()).willReturn(null); TbMsgCallback callbackMock = mock(TbMsgCallback.class); given(msgMock.getCallback()).willReturn(callbackMock); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); - mockGetMaxRuleNodeDebugModeDurationMinutes(); // WHEN defaultTbContext.output(msgMock, TbNodeConnectionType.SUCCESS); @@ -502,19 +485,18 @@ class DefaultTbContextTest { } @Test - public void givenEmptyStackAndDebugStrategyAllThenOnlyFailureEvents_whenOutput_thenVerifyMsgAckAndDebugOutputPersisted() { + public void givenEmptyStackAndDebugAllThenOnlyFailureEvents_whenOutput_thenVerifyMsgAckAndDebugOutputPersisted() { // GIVEN var msgMock = mock(TbMsg.class); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS); - ruleNode.setLastUpdateTs(System.currentTimeMillis()); + ruleNode.setDebugFailures(true); + ruleNode.setDebugAllUntil(getUntilTime()); given(msgMock.popFormStack()).willReturn(null); TbMsgCallback callbackMock = mock(TbMsgCallback.class); given(msgMock.getCallback()).willReturn(callbackMock); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); - mockGetMaxRuleNodeDebugModeDurationMinutes(); // WHEN defaultTbContext.output(msgMock, TbNodeConnectionType.SUCCESS); @@ -528,13 +510,13 @@ class DefaultTbContextTest { } @Test - public void givenDebugStrategyOnlyFailureEvents_whenEnqueueForTellFailure_thenVerifyDebugOutputPersisted() { + public void givenDebugFailuresEvents_whenEnqueueForTellFailure_thenVerifyDebugOutputPersisted() { // GIVEN var msg = getTbMsgWithQueueName(); var tpi = new TopicPartitionInfo(DataConstants.MAIN_QUEUE_TOPIC, TENANT_ID, 0, true); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.ONLY_FAILURE_EVENTS); + ruleNode.setDebugFailures(true); var tbClusterServiceMock = mock(TbClusterService.class); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); @@ -561,13 +543,14 @@ class DefaultTbContextTest { } @Test - public void givenDebugStrategyDisabled_whenEnqueueForTellFailure_thenVerifyDebugOutputNotPersisted() { + public void givenDebugDisabled_whenEnqueueForTellFailure_thenVerifyDebugOutputNotPersisted() { // GIVEN var msg = getTbMsgWithQueueName(); var tpi = new TopicPartitionInfo(DataConstants.MAIN_QUEUE_TOPIC, TENANT_ID, 0, true); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.DISABLED); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(0); var tbClusterServiceMock = mock(TbClusterService.class); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); @@ -588,21 +571,20 @@ class DefaultTbContextTest { } @Test - public void givenDebugStrategyAllEvents_whenEnqueueForTellFailure_thenVerifyDebugOutputPersisted() { + public void givenDebugAllEvents_whenEnqueueForTellFailure_thenVerifyDebugOutputPersisted() { // GIVEN var msg = getTbMsgWithQueueName(); var tpi = new TopicPartitionInfo(DataConstants.MAIN_QUEUE_TOPIC, TENANT_ID, 0, true); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); - ruleNode.setLastUpdateTs(System.currentTimeMillis()); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(getUntilTime()); var tbClusterServiceMock = mock(TbClusterService.class); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(mainCtxMock.resolve(any(ServiceType.class), anyString(), any(TenantId.class), any(EntityId.class))).willReturn(tpi); given(mainCtxMock.getClusterService()).willReturn(tbClusterServiceMock); - mockGetMaxRuleNodeDebugModeDurationMinutes(); // WHEN defaultTbContext.enqueueForTellFailure(msg, EXCEPTION); @@ -618,8 +600,6 @@ class DefaultTbContextTest { .ignoringFields("id", "ctx") .isEqualTo(expectedTbMsg); then(mainCtxMock).should().getClusterService(); - then(mainCtxMock).should().getTenantProfileCache(); - then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); then(mainCtxMock).shouldHaveNoMoreInteractions(); then(tbClusterServiceMock).shouldHaveNoMoreInteractions(); } @@ -655,23 +635,20 @@ class DefaultTbContextTest { @MethodSource @ParameterizedTest - void givenDebugStrategyOptions_whenEnqueueForTellNext_thenVerifyDebugOutputPersistedOnlyForAllEventsDebugStrategy(DebugStrategy debugStrategy, String connectionType) { + void givenDebugOptions_whenEnqueueForTellNext_thenVerifyDebugOutputPersistedOnlyForDebugAll(boolean debugFailures, long debugAllUntil, String connectionType) { // GIVEN var msg = getTbMsgWithQueueName(); var tpi = new TopicPartitionInfo(DataConstants.MAIN_QUEUE_TOPIC, TENANT_ID, 0, true); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(debugStrategy); - ruleNode.setLastUpdateTs(System.currentTimeMillis()); + ruleNode.setDebugFailures(debugFailures); + ruleNode.setDebugAllUntil(debugAllUntil); var tbClusterServiceMock = mock(TbClusterService.class); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(mainCtxMock.resolve(any(ServiceType.class), anyString(), any(TenantId.class), any(EntityId.class))).willReturn(tpi); given(mainCtxMock.getClusterService()).willReturn(tbClusterServiceMock); - if (DebugStrategy.ALL_EVENTS.equals(debugStrategy) || DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS.equals(debugStrategy)) { - mockGetMaxRuleNodeDebugModeDurationMinutes(); - } // WHEN defaultTbContext.enqueueForTellNext(msg, connectionType); @@ -697,9 +674,7 @@ class DefaultTbContextTest { assertThat(simpleTbQueueCallback).isNotNull(); simpleTbQueueCallback.onSuccess(null); - if (DebugStrategy.ALL_EVENTS.equals(debugStrategy) || DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS.equals(debugStrategy)) { - then(mainCtxMock).should().getTenantProfileCache(); - then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); + if (debugAllUntil > 0) { ArgumentCaptor tbMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); then(mainCtxMock).should().persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), tbMsgCaptor.capture(), eq(connectionType), isNull(), isNull()); TbMsg actualTbMsg = tbMsgCaptor.getValue(); @@ -714,24 +689,21 @@ class DefaultTbContextTest { @MethodSource @ParameterizedTest - void givenDebugStrategyOptions_whenEnqueue_thenVerifyDebugOutputPersistedOnlyForAllEventsDebugStrategy(DebugStrategy debugStrategy) { + void givenDebugOptions_whenEnqueue_thenVerifyDebugOutputPersistedOnlyForDebugAll(boolean debugFailures, long debugAllUntil) { // GIVEN var msg = getTbMsgWithQueueName(); var tpi = new TopicPartitionInfo(DataConstants.MAIN_QUEUE_TOPIC, TENANT_ID, 0, true); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setQueueName(DataConstants.MAIN_QUEUE_NAME); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(debugStrategy); - ruleNode.setLastUpdateTs(System.currentTimeMillis()); + ruleNode.setDebugFailures(debugFailures); + ruleNode.setDebugAllUntil(debugAllUntil); var tbClusterServiceMock = mock(TbClusterService.class); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(mainCtxMock.resolve(any(ServiceType.class), anyString(), any(TenantId.class), any(EntityId.class))).willReturn(tpi); given(mainCtxMock.getClusterService()).willReturn(tbClusterServiceMock); - if (DebugStrategy.ALL_EVENTS.equals(debugStrategy) || DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS.equals(debugStrategy)) { - mockGetMaxRuleNodeDebugModeDurationMinutes(); - } Consumer onFailure = mock(Consumer.class); Runnable onSuccess = mock(Runnable.class); @@ -760,9 +732,7 @@ class DefaultTbContextTest { assertThat(simpleTbQueueCallback).isNotNull(); simpleTbQueueCallback.onSuccess(null); - if (debugStrategy.isHasDuration()) { - then(mainCtxMock).should().getTenantProfileCache(); - then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); + if (debugAllUntil > 0) { then(mainCtxMock).should().persistDebugOutput(eq(TENANT_ID), eq(RULE_NODE_ID), eq(msg), eq(TbNodeConnectionType.TO_ROOT_RULE_CHAIN), nullable(Throwable.class), nullable(String.class)); } then(mainCtxMock).should().getClusterService(); @@ -771,12 +741,13 @@ class DefaultTbContextTest { } @Test - public void givenDebugStrategyOnlyFailures_whenTellFailure_thenVerifyDebugOutputPersisted() { + public void givenDebugFailuress_whenTellFailure_thenVerifyDebugOutputPersisted() { // GIVEN var msg = getTbMsg(); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.ONLY_FAILURE_EVENTS); + ruleNode.setDebugFailures(true); + ruleNode.setDebugAllUntil(0); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); @@ -801,12 +772,13 @@ class DefaultTbContextTest { } @Test - public void givenDebugStrategyDisabled_whenTellFailure_thenVerifyDebugOutputNotPersisted() { + public void givenDebugDisabled_whenTellFailure_thenVerifyDebugOutputNotPersisted() { // GIVEN var msg = getTbMsg(); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.DISABLED); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(0); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); @@ -829,17 +801,16 @@ class DefaultTbContextTest { } @Test - public void givenDebugStrategyAllEvents_whenTellFailure_thenVerifyDebugOutputPersisted() { + public void givenDebugAllEvents_whenTellFailure_thenVerifyDebugOutputPersisted() { // GIVEN var msg = getTbMsg(); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); - ruleNode.setLastUpdateTs(System.currentTimeMillis()); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(getUntilTime()); given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); - mockGetMaxRuleNodeDebugModeDurationMinutes(); // WHEN defaultTbContext.tellFailure(msg, EXCEPTION); @@ -856,15 +827,14 @@ class DefaultTbContextTest { then(chainActorMock).shouldHaveNoMoreInteractions(); then(nodeCtxMock).should().getChainActor(); then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, TbNodeConnectionType.FAILURE, EXCEPTION, null); - then(mainCtxMock).should().getTenantProfileCache(); - then(mainCtxMock).should().getMaxRuleNodeDebugModeDurationMinutes(); then(mainCtxMock).shouldHaveNoMoreInteractions(); then(nodeCtxMock).shouldHaveNoMoreInteractions(); } @MethodSource @ParameterizedTest - void givenDebugStrategyAndConnectionAndPersistedResultOptions_whenTellNext_thenVerifyDebugOutputPersistence(DebugStrategy debugStrategy, + void givenDebugFailuresAndDebugAllAndConnectionAndPersistedResultOptions_whenTellNext_thenVerifyDebugOutputPersistence(boolean debugFailures, + long debugAllUntil, String connection, boolean shouldPersist, boolean shouldPersistAfterDurationTime) { @@ -873,16 +843,13 @@ class DefaultTbContextTest { var msg = getTbMsgWithCallback(callbackMock); var ruleNode = new RuleNode(RULE_NODE_ID); ruleNode.setRuleChainId(RULE_CHAIN_ID); - ruleNode.setLastUpdateTs(System.currentTimeMillis()); - ruleNode.setDebugStrategy(debugStrategy); + ruleNode.setDebugFailures(debugFailures); + ruleNode.setDebugAllUntil(debugAllUntil); if (shouldPersist) { given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); } given(nodeCtxMock.getSelf()).willReturn(ruleNode); given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); - if (debugStrategy.isHasDuration()) { - mockGetMaxRuleNodeDebugModeDurationMinutes(); - } // WHEN defaultTbContext.tellNext(msg, connection); @@ -894,9 +861,7 @@ class DefaultTbContextTest { // GIVEN Mockito.clearInvocations(mainCtxMock); - if (debugStrategy.isHasDuration()) { - mockGetMaxRuleNodeDebugModeDurationMinutes(0); - } + ruleNode.setDebugAllUntil(0); // WHEN defaultTbContext.tellNext(msg, connection); @@ -956,34 +921,34 @@ class DefaultTbContextTest { simpleTbQueueCallback.onSuccess(null); } - private static Stream givenDebugStrategyOptions_whenEnqueueForTellNext_thenVerifyDebugOutputPersistedOnlyForAllEventsDebugStrategy() { + private static Stream givenDebugOptions_whenEnqueueForTellNext_thenVerifyDebugOutputPersistedOnlyForDebugAll() { return Stream.of( - Arguments.of(DebugStrategy.ALL_EVENTS, TbNodeConnectionType.OTHER), - Arguments.of(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS, TbNodeConnectionType.OTHER), - Arguments.of(DebugStrategy.ONLY_FAILURE_EVENTS, TbNodeConnectionType.TRUE), - Arguments.of(DebugStrategy.DISABLED, TbNodeConnectionType.FALSE) + Arguments.of(false, getUntilTime(), TbNodeConnectionType.OTHER), + Arguments.of(true, getUntilTime(), TbNodeConnectionType.OTHER), + Arguments.of(true, 0, TbNodeConnectionType.TRUE), + Arguments.of(false, 0, TbNodeConnectionType.FALSE) ); } - private static Stream givenDebugStrategyOptions_whenEnqueue_thenVerifyDebugOutputPersistedOnlyForAllEventsDebugStrategy() { + private static Stream givenDebugOptions_whenEnqueue_thenVerifyDebugOutputPersistedOnlyForDebugAll() { return Stream.of( - Arguments.of(DebugStrategy.ALL_EVENTS), - Arguments.of(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS), - Arguments.of(DebugStrategy.ONLY_FAILURE_EVENTS), - Arguments.of(DebugStrategy.DISABLED) + Arguments.of(false, getUntilTime()), + Arguments.of(true, getUntilTime()), + Arguments.of(true, 0), + Arguments.of(false, 0) ); } - private static Stream givenDebugStrategyAndConnectionAndPersistedResultOptions_whenTellNext_thenVerifyDebugOutputPersistence() { + private static Stream givenDebugFailuresAndDebugAllAndConnectionAndPersistedResultOptions_whenTellNext_thenVerifyDebugOutputPersistence() { return Stream.of( - Arguments.of(DebugStrategy.ALL_EVENTS, TbNodeConnectionType.SUCCESS, true, false), - Arguments.of(DebugStrategy.ALL_EVENTS, TbNodeConnectionType.FAILURE, true, false), - Arguments.of(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS, TbNodeConnectionType.SUCCESS, true, false), - Arguments.of(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS, TbNodeConnectionType.FAILURE, true, true), - Arguments.of(DebugStrategy.ONLY_FAILURE_EVENTS, TbNodeConnectionType.SUCCESS, false, false), - Arguments.of(DebugStrategy.ONLY_FAILURE_EVENTS, TbNodeConnectionType.FAILURE, true, true), - Arguments.of(DebugStrategy.DISABLED, TbNodeConnectionType.SUCCESS, false, false), - Arguments.of(DebugStrategy.DISABLED, TbNodeConnectionType.FAILURE, false, false) + Arguments.of(false, getUntilTime(), TbNodeConnectionType.SUCCESS, true, false), + Arguments.of(false, getUntilTime(), TbNodeConnectionType.FAILURE, true, false), + Arguments.of(true, getUntilTime(), TbNodeConnectionType.SUCCESS, true, false), + Arguments.of(true, getUntilTime(), TbNodeConnectionType.FAILURE, true, true), + Arguments.of(true, 0, TbNodeConnectionType.SUCCESS, false, false), + Arguments.of(true, 0, TbNodeConnectionType.FAILURE, true, true), + Arguments.of(false, 0, TbNodeConnectionType.SUCCESS, false, false), + Arguments.of(false, 0, TbNodeConnectionType.FAILURE, false, false) ); } @@ -999,21 +964,11 @@ class DefaultTbContextTest { return TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, TENANT_ID, TbMsgMetaData.EMPTY, TbMsg.EMPTY_STRING); } - private void mockGetMaxRuleNodeDebugModeDurationMinutes() { - mockGetMaxRuleNodeDebugModeDurationMinutes(15); + private static long getUntilTime() { + return getUntilTime(15); } - private void mockGetMaxRuleNodeDebugModeDurationMinutes(int maxRuleNodeDebugModeDurationMinutes) { - var tbTenantProfileCacheMock = mock(TbTenantProfileCache.class); - var tenantProfileMock = mock(TenantProfile.class); - var tenantProfileDataMock = mock(TenantProfileData.class); - var tenantProfileConfigurationMock = mock(TenantProfileConfiguration.class); - - given(mainCtxMock.getTenantProfileCache()).willReturn(tbTenantProfileCacheMock); - given(tbTenantProfileCacheMock.get(TENANT_ID)).willReturn(tenantProfileMock); - given(tenantProfileMock.getProfileData()).willReturn(tenantProfileDataMock); - given(tenantProfileDataMock.getConfiguration()).willReturn(tenantProfileConfigurationMock); - given(tenantProfileConfigurationMock.getMaxRuleNodeDebugModeDurationMinutes(anyInt())).willReturn(maxRuleNodeDebugModeDurationMinutes); + private static long getUntilTime(int maxRuleNodeDebugModeDurationMinutes) { + return System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(maxRuleNodeDebugModeDurationMinutes); } - } diff --git a/application/src/test/java/org/thingsboard/server/edge/AbstractEdgeTest.java b/application/src/test/java/org/thingsboard/server/edge/AbstractEdgeTest.java index a09ca1d148..3220620dfd 100644 --- a/application/src/test/java/org/thingsboard/server/edge/AbstractEdgeTest.java +++ b/application/src/test/java/org/thingsboard/server/edge/AbstractEdgeTest.java @@ -71,7 +71,6 @@ import org.thingsboard.server.common.data.query.EntityKeyValueType; import org.thingsboard.server.common.data.query.FilterPredicateValue; import org.thingsboard.server.common.data.query.NumericFilterPredicate; import org.thingsboard.server.common.data.queue.Queue; -import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleChainType; @@ -164,7 +163,8 @@ abstract public class AbstractEdgeTest extends AbstractControllerTest { } private RuleChainId getEdgeRootRuleChainId() throws Exception { - return doGetTypedWithPageLink("/api/ruleChains?type={type}&", new TypeReference>() {}, + return doGetTypedWithPageLink("/api/ruleChains?type={type}&", new TypeReference>() { + }, new PageLink(100, 0, "Edge Root Rule Chain"), "EDGE") .getData().get(0).getId(); @@ -208,7 +208,7 @@ abstract public class AbstractEdgeTest extends AbstractControllerTest { protected void updateRootRuleChainMetadata() throws Exception { RuleChainId rootRuleChainId = getEdgeRootRuleChainId(); RuleChainMetaData rootRuleChainMetadata = doGet("/api/ruleChain/" + rootRuleChainId.getId().toString() + "/metadata", RuleChainMetaData.class); - rootRuleChainMetadata.getNodes().forEach(n -> n.setDebugStrategy(DebugStrategy.ALL_EVENTS)); + rootRuleChainMetadata.getNodes().forEach(n -> n.setDebugAll(true)); doPost("/api/ruleChain/metadata", rootRuleChainMetadata, RuleChainMetaData.class); } diff --git a/application/src/test/java/org/thingsboard/server/edge/RuleChainEdgeTest.java b/application/src/test/java/org/thingsboard/server/edge/RuleChainEdgeTest.java index 2dc2bd42f3..224c21ffc5 100644 --- a/application/src/test/java/org/thingsboard/server/edge/RuleChainEdgeTest.java +++ b/application/src/test/java/org/thingsboard/server/edge/RuleChainEdgeTest.java @@ -23,7 +23,6 @@ import org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration; import org.thingsboard.rule.engine.util.TbMsgSource; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.id.RuleChainId; -import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleChainType; @@ -229,7 +228,7 @@ public class RuleChainEdgeTest extends AbstractEdgeTest { // update metadata for root rule chain edgeImitator.expectMessageAmount(1); - metaData.getNodes().forEach(n -> n.setDebugStrategy(DebugStrategy.ALL_EVENTS)); + metaData.getNodes().forEach(n -> n.setDebugAll(true)); doPost("/api/ruleChain/metadata", metaData, RuleChainMetaData.class); Assert.assertTrue(edgeImitator.waitForMessages()); ruleChainUpdateMsgOpt = edgeImitator.findMessageByType(RuleChainUpdateMsg.class); diff --git a/application/src/test/java/org/thingsboard/server/rules/flow/AbstractRuleEngineFlowIntegrationTest.java b/application/src/test/java/org/thingsboard/server/rules/flow/AbstractRuleEngineFlowIntegrationTest.java index 3af4c012e0..0798ff8fdb 100644 --- a/application/src/test/java/org/thingsboard/server/rules/flow/AbstractRuleEngineFlowIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/rules/flow/AbstractRuleEngineFlowIntegrationTest.java @@ -43,7 +43,6 @@ import org.thingsboard.server.common.data.kv.StringDataEntry; import org.thingsboard.server.common.data.msg.TbMsgType; import org.thingsboard.server.common.data.msg.TbNodeConnectionType; import org.thingsboard.server.common.data.page.PageData; -import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.NodeConnectionInfo; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; @@ -143,7 +142,7 @@ public abstract class AbstractRuleEngineFlowIntegrationTest extends AbstractRule ruleNode1.setName("Simple Rule Node 1"); ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode1.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode1.setDebugAll(true); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setFetchTo(TbMsgSource.METADATA); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); @@ -153,7 +152,7 @@ public abstract class AbstractRuleEngineFlowIntegrationTest extends AbstractRule ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode2.setDebugAll(true); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setFetchTo(TbMsgSource.METADATA); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); @@ -249,7 +248,7 @@ public abstract class AbstractRuleEngineFlowIntegrationTest extends AbstractRule ruleNode1.setName("Simple Rule Node 1"); ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode1.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode1.setDebugAll(true); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setFetchTo(TbMsgSource.METADATA); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); @@ -258,7 +257,7 @@ public abstract class AbstractRuleEngineFlowIntegrationTest extends AbstractRule RuleNode ruleNode12 = new RuleNode(); ruleNode12.setName("Simple Rule Node 1"); ruleNode12.setType(org.thingsboard.rule.engine.flow.TbRuleChainInputNode.class.getName()); - ruleNode12.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode12.setDebugAll(true); TbRuleChainInputNodeConfiguration configuration12 = new TbRuleChainInputNodeConfiguration(); configuration12.setRuleChainId(secondaryRuleChain.getId().getId().toString()); ruleNode12.setConfiguration(JacksonUtil.valueToTree(configuration12)); @@ -283,7 +282,7 @@ public abstract class AbstractRuleEngineFlowIntegrationTest extends AbstractRule ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode2.setDebugAll(true); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setFetchTo(TbMsgSource.METADATA); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); diff --git a/application/src/test/java/org/thingsboard/server/rules/lifecycle/AbstractRuleEngineLifecycleIntegrationTest.java b/application/src/test/java/org/thingsboard/server/rules/lifecycle/AbstractRuleEngineLifecycleIntegrationTest.java index dd148748ca..efc817f6d2 100644 --- a/application/src/test/java/org/thingsboard/server/rules/lifecycle/AbstractRuleEngineLifecycleIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/rules/lifecycle/AbstractRuleEngineLifecycleIntegrationTest.java @@ -36,7 +36,6 @@ import org.thingsboard.server.common.data.event.EventType; import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; import org.thingsboard.server.common.data.kv.StringDataEntry; import org.thingsboard.server.common.data.msg.TbMsgType; -import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleNode; @@ -98,7 +97,7 @@ public abstract class AbstractRuleEngineLifecycleIntegrationTest extends Abstrac ruleNode.setName("Simple Rule Node"); ruleNode.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode.setDebugAll(true); TbGetAttributesNodeConfiguration configuration = new TbGetAttributesNodeConfiguration(); configuration.setFetchTo(TbMsgSource.METADATA); configuration.setServerAttributeNames(Collections.singletonList("serverAttributeKey")); diff --git a/application/src/test/java/org/thingsboard/server/service/housekeeper/HousekeeperServiceTest.java b/application/src/test/java/org/thingsboard/server/service/housekeeper/HousekeeperServiceTest.java index 752624fa51..a3a2b55c76 100644 --- a/application/src/test/java/org/thingsboard/server/service/housekeeper/HousekeeperServiceTest.java +++ b/application/src/test/java/org/thingsboard/server/service/housekeeper/HousekeeperServiceTest.java @@ -58,7 +58,6 @@ import org.thingsboard.server.common.data.msg.TbNodeConnectionType; import org.thingsboard.server.common.data.page.TimePageLink; import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.RelationTypeGroup; -import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleChainType; @@ -463,7 +462,7 @@ public class HousekeeperServiceTest extends AbstractControllerTest { ruleNode1.setName("Simple Rule Node 1"); ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode1.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode1.setDebugAll(true); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); ruleNode1.setConfiguration(JacksonUtil.valueToTree(configuration1)); @@ -472,7 +471,7 @@ public class HousekeeperServiceTest extends AbstractControllerTest { ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode2.setDebugAll(true); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); ruleNode2.setConfiguration(JacksonUtil.valueToTree(configuration2)); diff --git a/application/src/test/java/org/thingsboard/server/service/sync/ie/ExportImportServiceSqlTest.java b/application/src/test/java/org/thingsboard/server/service/sync/ie/ExportImportServiceSqlTest.java index 06bc670804..fdbf6b1e68 100644 --- a/application/src/test/java/org/thingsboard/server/service/sync/ie/ExportImportServiceSqlTest.java +++ b/application/src/test/java/org/thingsboard/server/service/sync/ie/ExportImportServiceSqlTest.java @@ -65,7 +65,6 @@ import org.thingsboard.server.common.data.ota.OtaPackageType; import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.RelationTypeGroup; -import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleChainType; @@ -472,7 +471,7 @@ public class ExportImportServiceSqlTest extends AbstractControllerTest { RuleNode ruleNode1 = new RuleNode(); ruleNode1.setName("Generator 1"); ruleNode1.setType(TbMsgGeneratorNode.class.getName()); - ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode1.setDebugAllUntil(System.currentTimeMillis()); TbMsgGeneratorNodeConfiguration configuration1 = new TbMsgGeneratorNodeConfiguration(); configuration1.setOriginatorType(originatorId.getEntityType()); configuration1.setOriginatorId(originatorId.getId().toString()); @@ -482,7 +481,7 @@ public class ExportImportServiceSqlTest extends AbstractControllerTest { ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode2.setDebugAllUntil(System.currentTimeMillis()); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); ruleNode2.setConfiguration(JacksonUtil.valueToTree(configuration2)); @@ -511,7 +510,7 @@ public class ExportImportServiceSqlTest extends AbstractControllerTest { ruleNode1.setName("Simple Rule Node 1"); ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode1.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode1.setDebugAllUntil(System.currentTimeMillis()); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); ruleNode1.setConfiguration(JacksonUtil.valueToTree(configuration1)); @@ -520,7 +519,7 @@ public class ExportImportServiceSqlTest extends AbstractControllerTest { ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode2.setDebugAllUntil(System.currentTimeMillis()); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); ruleNode2.setConfiguration(JacksonUtil.valueToTree(configuration2)); diff --git a/application/src/test/java/org/thingsboard/server/service/sync/vc/VersionControlTest.java b/application/src/test/java/org/thingsboard/server/service/sync/vc/VersionControlTest.java index 21f6363f5f..194aaee8e0 100644 --- a/application/src/test/java/org/thingsboard/server/service/sync/vc/VersionControlTest.java +++ b/application/src/test/java/org/thingsboard/server/service/sync/vc/VersionControlTest.java @@ -65,7 +65,6 @@ import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.RelationTypeGroup; -import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainMetaData; import org.thingsboard.server.common.data.rule.RuleChainType; @@ -871,7 +870,7 @@ public class VersionControlTest extends AbstractControllerTest { RuleNode ruleNode1 = new RuleNode(); ruleNode1.setName("Generator 1"); ruleNode1.setType(TbMsgGeneratorNode.class.getName()); - ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode1.setDebugAll(true); TbMsgGeneratorNodeConfiguration configuration1 = new TbMsgGeneratorNodeConfiguration(); configuration1.setOriginatorType(originatorId.getEntityType()); configuration1.setOriginatorId(originatorId.getId().toString()); @@ -881,7 +880,7 @@ public class VersionControlTest extends AbstractControllerTest { ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode2.setDebugAll(true); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); ruleNode2.setConfiguration(JacksonUtil.valueToTree(configuration2)); @@ -909,7 +908,7 @@ public class VersionControlTest extends AbstractControllerTest { ruleNode1.setName("Simple Rule Node 1"); ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode1.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode1.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode1.setDebugAll(true); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); ruleNode1.setConfiguration(JacksonUtil.valueToTree(configuration1)); @@ -918,7 +917,7 @@ public class VersionControlTest extends AbstractControllerTest { ruleNode2.setName("Simple Rule Node 2"); ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName()); ruleNode2.setConfigurationVersion(TbGetAttributesNode.class.getAnnotation(org.thingsboard.rule.engine.api.RuleNode.class).version()); - ruleNode2.setDebugStrategy(DebugStrategy.ALL_EVENTS); + ruleNode2.setDebugAll(true); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); ruleNode2.setConfiguration(JacksonUtil.valueToTree(configuration2)); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java b/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java deleted file mode 100644 index a065c9065f..0000000000 --- a/common/data/src/main/java/org/thingsboard/server/common/data/rule/DebugStrategy.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright © 2016-2024 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. - */ -package org.thingsboard.server.common.data.rule; - -import lombok.Getter; -import org.thingsboard.server.common.data.msg.TbNodeConnectionType; - -import java.util.Set; -import java.util.concurrent.TimeUnit; - -@Getter -public enum DebugStrategy { - DISABLED(0, false), - ALL_EVENTS(1, true), - ALL_THEN_ONLY_FAILURE_EVENTS(2, true), - ONLY_FAILURE_EVENTS(3, false); - - private final int protoNumber; - private final boolean hasDuration; - - DebugStrategy(int protoNumber, boolean hasDuration) { - this.protoNumber = protoNumber; - this.hasDuration = hasDuration; - } - - public boolean shouldPersistDebugInput(long lastUpdateTs, long msgTs, int debugModeDurationMinutes) { - return isAllEventsStrategyAndMsgTsWithinDebugDuration(lastUpdateTs, msgTs, debugModeDurationMinutes); - } - - public boolean shouldPersistDebugOutputForAllEvents(long lastUpdateTs, long msgTs, int debugModeDurationMinutes) { - return this.isAllEventsStrategyAndMsgTsWithinDebugDuration(lastUpdateTs, msgTs, debugModeDurationMinutes); - } - - public boolean shouldPersistDebugForFailureEvent(Set nodeConnections) { - return isFailureStrategy() && nodeConnections.contains(TbNodeConnectionType.FAILURE); - } - - public boolean shouldPersistDebugForFailureEvent(String nodeConnection) { - return isFailureStrategy() && TbNodeConnectionType.FAILURE.equals(nodeConnection); - } - - private boolean isFailureStrategy() { - return DebugStrategy.ONLY_FAILURE_EVENTS.equals(this) || DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS.equals(this); - } - - private boolean isAllEventsStrategyAndMsgTsWithinDebugDuration(long lastUpdateTs, long msgTs, int debugModeDurationMinutes) { - return this.hasDuration && isMsgTsWithinDebugDuration(lastUpdateTs, msgTs, debugModeDurationMinutes); - } - - private boolean isMsgTsWithinDebugDuration(long lastUpdateTs, long msgCreationTs, int debugModeDurationMinutes) { - if (debugModeDurationMinutes <= 0) { - return true; - } - return msgCreationTs < lastUpdateTs + TimeUnit.MINUTES.toMillis(debugModeDurationMinutes); - } - -} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java b/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java index 35601afe02..bbc244676c 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java @@ -47,10 +47,12 @@ public class RuleNode extends BaseDataWithAdditionalInfo implements @Length(fieldName = "name") @Schema(description = "User defined name of the rule node. Used on UI and for logging. ", example = "Process sensor reading") private String name; - @Schema(description = "Timestamp of the last rule node update.") - private long lastUpdateTs; - @Schema(description = "Debug strategy. ", example = "ALL_EVENTS") - private DebugStrategy debugStrategy; + @Schema(description = "Debug failures. ", example = "false") + private boolean debugFailures; + @Schema(description = "Debug All. Used as a trigger for updating debugAllUntil.", example = "false") + private boolean debugAll; + @Schema(description = "Timestamp of the end time for the processing debug events.") + private long debugAllUntil; @Schema(description = "Enable/disable singleton mode. ", example = "false") private boolean singletonMode; @Schema(description = "Queue name. ", example = "Main") @@ -77,8 +79,9 @@ public class RuleNode extends BaseDataWithAdditionalInfo implements this.ruleChainId = ruleNode.getRuleChainId(); this.type = ruleNode.getType(); this.name = ruleNode.getName(); - this.lastUpdateTs = ruleNode.getLastUpdateTs(); - this.debugStrategy = ruleNode.getDebugStrategy(); + this.debugFailures = ruleNode.isDebugFailures(); + this.debugAll = ruleNode.isDebugAll(); + this.debugAllUntil = ruleNode.getDebugAllUntil(); this.singletonMode = ruleNode.isSingletonMode(); this.setConfiguration(ruleNode.getConfiguration()); this.externalId = ruleNode.getExternalId(); @@ -89,10 +92,6 @@ public class RuleNode extends BaseDataWithAdditionalInfo implements return name; } - public DebugStrategy getDebugStrategy() { - return debugStrategy == null ? DebugStrategy.DISABLED : debugStrategy; - } - public JsonNode getConfiguration() { return BaseDataWithAdditionalInfo.getJson(() -> configuration, () -> configurationBytes); } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNodeDebugUtil.java b/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNodeDebugUtil.java new file mode 100644 index 0000000000..c6a3b495c8 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNodeDebugUtil.java @@ -0,0 +1,45 @@ +/** + * Copyright © 2016-2024 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. + */ +package org.thingsboard.server.common.data.rule; + +import org.thingsboard.server.common.data.msg.TbNodeConnectionType; + +import java.util.Set; + +public final class RuleNodeDebugUtil { + private RuleNodeDebugUtil() {} + + public static boolean isDebugAllAvailable(RuleNode ruleNode) { + return ruleNode.getDebugAllUntil() > System.currentTimeMillis(); + } + + public static boolean isDebugAvailable(RuleNode ruleNode, String nodeConnection) { + return isDebugAllAvailable(ruleNode) || ruleNode.isDebugFailures() && TbNodeConnectionType.FAILURE.equals(nodeConnection); + } + + public static boolean isDebugFailuresAvailable(RuleNode ruleNode, Set nodeConnections) { + return isDebugFailuresAvailable(ruleNode) && nodeConnections.contains(TbNodeConnectionType.FAILURE); + } + + public static boolean isDebugFailuresAvailable(RuleNode ruleNode, String nodeConnection) { + return isDebugFailuresAvailable(ruleNode) && TbNodeConnectionType.FAILURE.equals(nodeConnection); + } + + public static boolean isDebugFailuresAvailable(RuleNode ruleNode) { + return ruleNode.isDebugFailures() || isDebugAllAvailable(ruleNode); + } + +} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/tenant/profile/DefaultTenantProfileConfiguration.java b/common/data/src/main/java/org/thingsboard/server/common/data/tenant/profile/DefaultTenantProfileConfiguration.java index ff5c0ea426..1b36578a77 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/tenant/profile/DefaultTenantProfileConfiguration.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/tenant/profile/DefaultTenantProfileConfiguration.java @@ -205,6 +205,9 @@ public class DefaultTenantProfileConfiguration implements TenantProfileConfigura @Override public int getMaxRuleNodeDebugModeDurationMinutes(int systemMaxRuleNodeDebugModeDurationMinutes) { - return Math.min(systemMaxRuleNodeDebugModeDurationMinutes, maxRuleNodeDebugDurationMinutes); + if (maxRuleNodeDebugDurationMinutes > 0) { + return Math.min(systemMaxRuleNodeDebugModeDurationMinutes, maxRuleNodeDebugDurationMinutes); + } + return systemMaxRuleNodeDebugModeDurationMinutes; } } diff --git a/common/edge-api/src/main/proto/edge.proto b/common/edge-api/src/main/proto/edge.proto index 167e160648..379ea3b77b 100644 --- a/common/edge-api/src/main/proto/edge.proto +++ b/common/edge-api/src/main/proto/edge.proto @@ -162,13 +162,6 @@ message RuleChainMetadataUpdateMsg { string entity = 8; } -enum DebugStrategy { - DISABLED = 0; - ALL_EVENTS = 1; - ALL_THEN_ONLY_FAILURE_EVENTS = 2; - ONLY_FAILURE_EVENTS = 3; -} - message RuleNodeProto { option deprecated = true; int64 idMSB = 1; @@ -180,8 +173,8 @@ message RuleNodeProto { string additionalInfo = 7; bool singletonMode = 8; int32 configurationVersion = 9; - int64 lastUpdateTs = 10; - DebugStrategy debugStrategy = 11; + bool debugFailures = 10; + int64 debugAllUntil = 11; } message NodeConnectionInfoProto { diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java b/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java index 4311bfa11d..fc987218cf 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java @@ -398,7 +398,8 @@ public class ModelConstants { public static final String EVENT_MESSAGE_COLUMN_NAME = "e_message"; public static final String DEBUG_MODE = "debug_mode"; - public static final String DEBUG_STRATEGY = "debug_strategy"; + public static final String DEBUG_FAILURES = "debug_failures"; + public static final String DEBUG__ALL_UNTIL = "debug_all_until"; public static final String SINGLETON_MODE = "singleton_mode"; public static final String QUEUE_NAME = "queue_name"; diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/RuleNodeEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/RuleNodeEntity.java index 278fde0d6d..d1efb19061 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/RuleNodeEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/RuleNodeEntity.java @@ -19,14 +19,11 @@ import com.fasterxml.jackson.databind.JsonNode; import jakarta.persistence.Column; import jakarta.persistence.Convert; import jakarta.persistence.Entity; -import jakarta.persistence.EnumType; -import jakarta.persistence.Enumerated; import jakarta.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; import org.thingsboard.server.common.data.id.RuleChainId; import org.thingsboard.server.common.data.id.RuleNodeId; -import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.RuleNode; import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.model.BaseSqlEntity; @@ -61,12 +58,11 @@ public class RuleNodeEntity extends BaseSqlEntity { @Column(name = ModelConstants.ADDITIONAL_INFO_PROPERTY) private JsonNode additionalInfo; - @Column(name = ModelConstants.LAST_UPDATE_TS_COLUMN) - private long lastUpdateTs; + @Column(name = ModelConstants.DEBUG_FAILURES) + private boolean debugFailures; - @Enumerated(EnumType.STRING) - @Column(name = ModelConstants.DEBUG_STRATEGY) - private DebugStrategy debugStrategy; + @Column(name = ModelConstants.DEBUG__ALL_UNTIL) + private long debugAllUntil; @Column(name = ModelConstants.SINGLETON_MODE) private boolean singletonMode; @@ -90,8 +86,8 @@ public class RuleNodeEntity extends BaseSqlEntity { } this.type = ruleNode.getType(); this.name = ruleNode.getName(); - this.lastUpdateTs = ruleNode.getLastUpdateTs(); - this.debugStrategy = ruleNode.getDebugStrategy(); + this.debugFailures = ruleNode.isDebugFailures(); + this.debugAllUntil = ruleNode.getDebugAllUntil(); this.singletonMode = ruleNode.isSingletonMode(); this.queueName = ruleNode.getQueueName(); this.configurationVersion = ruleNode.getConfigurationVersion(); @@ -111,8 +107,8 @@ public class RuleNodeEntity extends BaseSqlEntity { } ruleNode.setType(type); ruleNode.setName(name); - ruleNode.setLastUpdateTs(lastUpdateTs); - ruleNode.setDebugStrategy(debugStrategy); + ruleNode.setDebugFailures(debugFailures); + ruleNode.setDebugAllUntil(debugAllUntil); ruleNode.setSingletonMode(singletonMode); ruleNode.setQueueName(queueName); ruleNode.setConfigurationVersion(configurationVersion); diff --git a/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java b/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java index f141ef627e..74375b1c78 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java @@ -24,6 +24,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.hibernate.exception.ConstraintViolationException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.thingsboard.common.util.JacksonUtil; @@ -44,7 +45,6 @@ import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.plugin.ComponentClusteringMode; import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.RelationTypeGroup; -import org.thingsboard.server.common.data.rule.DebugStrategy; import org.thingsboard.server.common.data.rule.NodeConnectionInfo; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainConnectionInfo; @@ -66,6 +66,8 @@ import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.PaginatedRemover; import org.thingsboard.server.dao.service.Validator; import org.thingsboard.server.dao.service.validator.RuleChainDataValidator; +import org.thingsboard.server.dao.tenant.TbTenantProfileCache; +import org.thingsboard.server.dao.util.TimeUtils; import java.util.ArrayList; import java.util.Collection; @@ -77,6 +79,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; @@ -110,6 +113,12 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC @Autowired private DataValidator ruleChainValidator; + @Autowired + private TbTenantProfileCache tbTenantProfileCache; + + @Value("${actors.rule.node.max_debug_mode_duration:60}") + private int maxRuleNodeDebugModeDurationMinutes; + @Override @Transactional public RuleChain saveRuleChain(RuleChain ruleChain) { @@ -215,11 +224,16 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC } RuleChainId ruleChainId = ruleChain.getId(); if (nodes != null) { - long lastUpdateTs = System.currentTimeMillis(); + long now = System.currentTimeMillis(); for (RuleNode node : toAddOrUpdate) { node.setRuleChainId(ruleChainId); node = ruleNodeUpdater.apply(node); - node.setLastUpdateTs(lastUpdateTs); + + if (node.isDebugAll()) { + int debugDuration = tbTenantProfileCache.get(tenantId).getDefaultProfileConfiguration().getMaxRuleNodeDebugModeDurationMinutes(maxRuleNodeDebugModeDurationMinutes); + node.setDebugAllUntil(now + TimeUnit.MINUTES.toMillis(debugDuration)); + } + RuleChainDataValidator.validateRuleNode(node); RuleNode savedNode = ruleNodeDao.save(tenantId, node); relations.add(new EntityRelation(ruleChainMetaData.getRuleChainId(), savedNode.getId(), @@ -264,7 +278,6 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC layout.remove("description"); layout.remove("ruleChainNodeId"); targetNode.setAdditionalInfo(layout); - targetNode.setDebugStrategy(DebugStrategy.DISABLED); targetNode = ruleNodeDao.save(tenantId, targetNode); EntityRelation sourceRuleChainToRuleNode = new EntityRelation(); diff --git a/dao/src/main/resources/sql/schema-entities.sql b/dao/src/main/resources/sql/schema-entities.sql index c5f459773a..9e766392fc 100644 --- a/dao/src/main/resources/sql/schema-entities.sql +++ b/dao/src/main/resources/sql/schema-entities.sql @@ -181,8 +181,8 @@ CREATE TABLE IF NOT EXISTS rule_node ( configuration varchar(10000000), type varchar(255), name varchar(255), - last_update_ts bigint NOT NULL, - debug_strategy varchar(32) DEFAULT 'DISABLED', + debug_failures boolean, + debug_all_until bigint NOT NULL, singleton_mode boolean, queue_name varchar(255), external_id uuid diff --git a/monitoring/src/main/resources/root_rule_chain.json b/monitoring/src/main/resources/root_rule_chain.json index 1da16c9b09..9e057490aa 100644 --- a/monitoring/src/main/resources/root_rule_chain.json +++ b/monitoring/src/main/resources/root_rule_chain.json @@ -20,7 +20,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries", - "debugStrategy": "ALL_EVENTS", + "debugFailures": false, + "debugAll": true, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -35,7 +36,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", "name": "Save Attributes", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 1, "configuration": { @@ -53,7 +55,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "Message Type Switch", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -68,7 +71,8 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log RPC from Device", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -85,7 +89,8 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log Other", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -102,7 +107,8 @@ }, "type": "org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode", "name": "RPC Call Request", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -117,7 +123,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbOriginatorTypeFilterNode", "name": "Is Entity Group", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -134,7 +141,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeFilterNode", "name": "Post attributes or RPC request", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -152,7 +160,8 @@ }, "type": "org.thingsboard.rule.engine.transform.TbDuplicateMsgToGroupNode", "name": "Duplicate To Group Entities", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -169,7 +178,8 @@ }, "type": "org.thingsboard.rule.engine.profile.TbDeviceProfileNode", "name": "Device Profile Node", - "debugStrategy": "ALL_EVENTS", + "debugFailures": false, + "debugAll": true, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -186,7 +196,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbJsFilterNode", "name": "Test JS script", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -204,7 +215,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbJsFilterNode", "name": "Test TBEL script", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -222,7 +234,8 @@ }, "type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode", "name": "Add arrival timestamp", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -240,7 +253,8 @@ }, "type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode", "name": "Calculate additional latencies", - "debugStrategy": "ALL_EVENTS", + "debugFailures": false, + "debugAll": true, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -258,7 +272,8 @@ }, "type": "org.thingsboard.rule.engine.transform.TbChangeOriginatorNode", "name": "To latencies asset", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -287,7 +302,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries", - "debugStrategy": "ALL_EVENTS", + "debugFailures": false, + "debugAll": true, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -303,7 +319,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbCheckMessageNode", "name": "Has testData", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -323,7 +340,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries with TTL", - "debugStrategy": "ALL_EVENTS", + "debugFailures": false, + "debugAll": true, "singletonMode": false, "configurationVersion": 0, "configuration": { diff --git a/msa/black-box-tests/src/test/resources/MqttRuleNodeTestMetadata.json b/msa/black-box-tests/src/test/resources/MqttRuleNodeTestMetadata.json index dc6a394a39..e6c93cffe1 100644 --- a/msa/black-box-tests/src/test/resources/MqttRuleNodeTestMetadata.json +++ b/msa/black-box-tests/src/test/resources/MqttRuleNodeTestMetadata.json @@ -9,7 +9,8 @@ }, "type": "org.thingsboard.rule.engine.mqtt.TbMqttNode", "name": "test mqtt", - "debugStrategy": "ALL_EVENTS", + "debugFailures": false, + "debugAll": true, "singletonMode": true, "queueName": "HighPriority", "configurationVersion": 0, @@ -36,7 +37,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "save timeseries", - "debugStrategy": "ALL_EVENTS", + "debugFailures": false, + "debugAll": true, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -54,7 +56,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "switch", - "debugStrategy": "DISABLED", + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { diff --git a/msa/black-box-tests/src/test/resources/RpcResponseRuleChainMetadata.json b/msa/black-box-tests/src/test/resources/RpcResponseRuleChainMetadata.json index 31eb3149f3..ddb9f8bec1 100644 --- a/msa/black-box-tests/src/test/resources/RpcResponseRuleChainMetadata.json +++ b/msa/black-box-tests/src/test/resources/RpcResponseRuleChainMetadata.json @@ -8,7 +8,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "msgTypeSwitch", - "debugStrategy": "ALL_EVENTS", + "debugFailures": false, + "debugAll": true, "configuration": { "version": 0 } @@ -20,7 +21,8 @@ }, "type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode", "name": "formResponse", - "debugStrategy": "ALL_EVENTS", + "debugFailures": false, + "debugAll": true, "configuration": { "jsScript": "if (msg.method == \"getResponse\") {\n return {msg: {\"response\": \"requestReceived\"}, metadata: metadata, msgType: msgType};\n}\n\nreturn {msg: msg, metadata: metadata, msgType: msgType};" } @@ -32,7 +34,8 @@ }, "type": "org.thingsboard.rule.engine.rpc.TbSendRPCReplyNode", "name": "rpcReply", - "debugStrategy": "ALL_EVENTS", + "debugFailures": false, + "debugAll": true, "configuration": { "requestIdMetaDataAttribute": "requestId" } From 7495861080350353d6bc66c6c7cde4f982d0b2a3 Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Tue, 12 Nov 2024 09:46:20 +0100 Subject: [PATCH 16/37] added validation for the debugAllUntil --- .../thingsboard/server/dao/rule/BaseRuleChainService.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java b/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java index 74375b1c78..94eb1ad283 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java @@ -229,9 +229,13 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC node.setRuleChainId(ruleChainId); node = ruleNodeUpdater.apply(node); + int debugDuration = tbTenantProfileCache.get(tenantId).getDefaultProfileConfiguration().getMaxRuleNodeDebugModeDurationMinutes(maxRuleNodeDebugModeDurationMinutes); + long debugUntil = now + TimeUnit.MINUTES.toMillis(debugDuration); + if (node.isDebugAll()) { - int debugDuration = tbTenantProfileCache.get(tenantId).getDefaultProfileConfiguration().getMaxRuleNodeDebugModeDurationMinutes(maxRuleNodeDebugModeDurationMinutes); - node.setDebugAllUntil(now + TimeUnit.MINUTES.toMillis(debugDuration)); + node.setDebugAllUntil(debugUntil); + } else if (node.getDebugAllUntil() > debugUntil) { + throw new DataValidationException("Unable to update 'debugAllUntil' property. To reset the debug duration, please modify the 'debugAll' property instead."); } RuleChainDataValidator.validateRuleNode(node); From 78decec8da3893e3320cf529768508b821fa149b Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Tue, 12 Nov 2024 10:35:34 +0100 Subject: [PATCH 17/37] fixed default rule-chains creation --- .../org/thingsboard/server/service/install/InstallScripts.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/thingsboard/server/service/install/InstallScripts.java b/application/src/main/java/org/thingsboard/server/service/install/InstallScripts.java index 115208bc47..75c0c0873c 100644 --- a/application/src/main/java/org/thingsboard/server/service/install/InstallScripts.java +++ b/application/src/main/java/org/thingsboard/server/service/install/InstallScripts.java @@ -202,7 +202,7 @@ public class InstallScripts { ruleChain = ruleChainService.saveRuleChain(ruleChain, false); ruleChainMetaData.setRuleChainId(ruleChain.getId()); - ruleChainService.saveRuleChainMetaData(TenantId.SYS_TENANT_ID, ruleChainMetaData, Function.identity(), false); + ruleChainService.saveRuleChainMetaData(tenantId, ruleChainMetaData, Function.identity(), false); return ruleChain; } From cf377b00830f485506416ee333da430796e7373d Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Tue, 12 Nov 2024 11:04:42 +0100 Subject: [PATCH 18/37] fixed circular references --- .../org/thingsboard/server/dao/rule/BaseRuleChainService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java b/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java index 94eb1ad283..52f28db5db 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java @@ -25,6 +25,7 @@ import org.apache.commons.lang3.exception.ExceptionUtils; import org.hibernate.exception.ConstraintViolationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.thingsboard.common.util.JacksonUtil; @@ -114,6 +115,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC private DataValidator ruleChainValidator; @Autowired + @Lazy private TbTenantProfileCache tbTenantProfileCache; @Value("${actors.rule.node.max_debug_mode_duration:60}") From 9a1c92007d93e12a65f57ede8e9e9de96940ba55 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Tue, 12 Nov 2024 17:13:58 +0200 Subject: [PATCH 19/37] Changed aproach to debug config --- ...tml => debug-config-button.component.html} | 12 +- ...nt.ts => debug-config-button.component.ts} | 72 +++++------- .../debug-config-panel.component.html | 70 ++++++++++++ .../rulechain/debug-config-panel.component.ts | 105 ++++++++++++++++++ .../rulechain/debug-duration-left.pipe.ts | 11 +- .../debug-strategy-panel.component.html | 55 --------- .../debug-strategy-panel.component.ts | 97 ---------------- .../rule-node-details.component.html | 8 +- .../rulechain/rule-node-details.component.ts | 13 ++- .../rulechain/rulechain-page.component.ts | 24 ++-- .../home/pages/rulechain/rulechain.module.ts | 4 +- .../import-export/import-export.service.ts | 6 +- .../src/app/shared/models/rule-node.models.ts | 19 ++-- .../assets/locale/locale.constant-en_US.json | 4 +- 14 files changed, 258 insertions(+), 242 deletions(-) rename ui-ngx/src/app/modules/home/pages/rulechain/{debug-strategy-button.component.html => debug-config-button.component.html} (60%) rename ui-ngx/src/app/modules/home/pages/rulechain/{debug-strategy-button.component.ts => debug-config-button.component.ts} (53%) create mode 100644 ui-ngx/src/app/modules/home/pages/rulechain/debug-config-panel.component.html create mode 100644 ui-ngx/src/app/modules/home/pages/rulechain/debug-config-panel.component.ts delete mode 100644 ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.html delete mode 100644 ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.ts diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-button.component.html similarity index 60% rename from ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.html rename to ui-ngx/src/app/modules/home/pages/rulechain/debug-config-button.component.html index ea94a99c74..d5518b71f1 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.html +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-button.component.html @@ -19,14 +19,12 @@ class="tb-rounded-btn flex-1 w-36" color="primary" #matButton - [class.active]="debugStrategyFormControl.value !== DebugStrategy.DISABLED && !disabled" + [class.active]="(isDebugAllActive() || debugFailures) && !disabled" [disabled]="disabled" (click)="openDebugStrategyPanel($event, matButton)"> bug_report - - common.disabled - debug-strategy.all - {{ lastUpdateTs | debugDurationLeft : maxRuleNodeDebugDurationMinutes }} - debug-strategy.failures - + common.disabled + debug-config.all + {{ debugAllUntil | debugDurationLeft }} + debug-config.failures diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-button.component.ts similarity index 53% rename from ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.ts rename to ui-ngx/src/app/modules/home/pages/rulechain/debug-config-button.component.ts index 3422949b5a..ff05942df4 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-button.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-button.component.ts @@ -17,38 +17,30 @@ import { Component, Input, - forwardRef, Renderer2, ViewContainerRef, DestroyRef, ChangeDetectionStrategy, - ChangeDetectorRef + ChangeDetectorRef, + EventEmitter, + Output } from '@angular/core'; -import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR, UntypedFormBuilder } from '@angular/forms'; import { CommonModule } from '@angular/common'; import { SharedModule } from '@shared/shared.module'; -import { DebugStrategy } from '@shared/models/rule-node.models'; import { DebugDurationLeftPipe } from '@home/pages/rulechain/debug-duration-left.pipe'; -import { getCurrentAuthState } from '@core/auth/auth.selectors'; import { Store } from '@ngrx/store'; import { AppState } from '@core/core.state'; import { TbPopoverService } from '@shared/components/popover.service'; import { MatButton } from '@angular/material/button'; -import { DebugStrategyPanelComponent } from '@home/pages/rulechain/debug-strategy-panel.component'; +import { DebugConfigPanelComponent } from '@home/pages/rulechain/debug-config-panel.component'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { interval } from 'rxjs'; -import { MINUTE } from '@shared/models/time/time.models'; +import { SECOND } from '@shared/models/time/time.models'; +import { RuleNodeDebugConfig } from '@shared/models/rule-node.models'; @Component({ - selector: 'tb-debug-strategy-button', - templateUrl: './debug-strategy-button.component.html', - providers: [ - { - provide: NG_VALUE_ACCESSOR, - useExisting: forwardRef(() => DebugStrategyButtonComponent), - multi: true - } - ], + selector: 'tb-debug-config-button', + templateUrl: './debug-config-button.component.html', standalone: true, imports: [ CommonModule, @@ -57,29 +49,23 @@ import { MINUTE } from '@shared/models/time/time.models'; ], changeDetection: ChangeDetectionStrategy.OnPush }) -export class DebugStrategyButtonComponent implements ControlValueAccessor { - - @Input() lastUpdateTs: number; +export class DebugConfigButtonComponent { - debugStrategyFormControl: FormControl; - disabled = false; + @Input() debugFailures = false; + @Input() debugAll = false; + @Input() debugAllUntil = 0; + @Input() disabled = false; - private onChange: (debugStrategy: DebugStrategy) => void; - - readonly maxRuleNodeDebugDurationMinutes = getCurrentAuthState(this.store).maxRuleNodeDebugDurationMinutes; - readonly DebugStrategy = DebugStrategy; + @Output() onDebugConfigChanged = new EventEmitter() constructor(protected store: Store, - private fb: UntypedFormBuilder, private popoverService: TbPopoverService, private renderer: Renderer2, private viewContainerRef: ViewContainerRef, private destroyRef: DestroyRef, private cdr: ChangeDetectorRef ) { - this.debugStrategyFormControl = this.fb.control(DebugStrategy.DISABLED); - this.debugStrategyFormControl.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(value => this.onChange(value)); - interval(0.5 * MINUTE) + interval(SECOND) .pipe(takeUntilDestroyed(this.destroyRef)) .subscribe(() => this.cdr.markForCheck()); } @@ -89,36 +75,28 @@ export class DebugStrategyButtonComponent implements ControlValueAccessor { $event.stopPropagation(); } const trigger = matButton._elementRef.nativeElement; - const debugStrategy = this.debugStrategyFormControl.value; if (this.popoverService.hasPopover(trigger)) { this.popoverService.hidePopover(trigger); } else { const debugStrategyPopover = this.popoverService.displayPopover(trigger, this.renderer, - this.viewContainerRef, DebugStrategyPanelComponent, 'bottom', true, null, - { debugStrategy }, + this.viewContainerRef, DebugConfigPanelComponent, 'bottom', true, null, + { + debugFailures: this.debugFailures, + debugAll: this.debugAll, + debugAllUntil: this.debugAllUntil, + }, {}, {}, {}, true); debugStrategyPopover.tbComponentRef.instance.popover = debugStrategyPopover; - debugStrategyPopover.tbComponentRef.instance.onStrategyApplied.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((strategy: DebugStrategy) => { - this.debugStrategyFormControl.patchValue(strategy); + debugStrategyPopover.tbComponentRef.instance.onConfigApplied.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((config: RuleNodeDebugConfig) => { + this.onDebugConfigChanged.emit(config); this.cdr.markForCheck(); debugStrategyPopover.hide(); }); } } - registerOnChange(onChange: (debugStrategy: DebugStrategy) => void): void { - this.onChange = onChange; - } - - registerOnTouched(_: () => {}): void {} - - setDisabledState(isDisabled: boolean): void { - this.disabled = isDisabled; - } - - writeValue(value: DebugStrategy): void { - this.debugStrategyFormControl.patchValue(value, { emitEvent: false }); - this.cdr.markForCheck(); + isDebugAllActive(): boolean { + return this.debugAllUntil > new Date().getTime(); } } diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-panel.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-panel.component.html new file mode 100644 index 0000000000..b7d2f44d8d --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-panel.component.html @@ -0,0 +1,70 @@ + +
+
debug-config.label
+
+
+ + {{ 'debug-config.hint.main-limited' | translate: { msg: maxMessagesCount, sec: maxTimeFrameSec } }} + + {{ 'debug-config.hint.main' | translate }} +
+
+
+ + + {{ 'debug-config.on-failure' | translate }} + + +
+ + + {{ 'debug-config.all-messages' | translate }} {{ '( ' }} + {{ debugAllUntil | debugDurationLeft }} + + {{ maxRuleNodeDebugDurationMinutes + ' ' }} {{ ('debug-config.min' | translate) }} + + {{ ' )' }} + + + +
+
+
+ + +
+
+ diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-panel.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-panel.component.ts new file mode 100644 index 0000000000..a2f8091cfe --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-panel.component.ts @@ -0,0 +1,105 @@ +/// +/// Copyright © 2016-2024 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. +/// + +import { ChangeDetectorRef, Component, DestroyRef, EventEmitter, Input, OnInit } from '@angular/core'; +import { PageComponent } from '@shared/components/page.component'; +import { TbPopoverComponent } from '@shared/components/popover.component'; +import { FormControl, UntypedFormBuilder } from '@angular/forms'; +import { Store } from '@ngrx/store'; +import { AppState } from '@core/core.state'; +import { CommonModule } from '@angular/common'; +import { SharedModule } from '@shared/shared.module'; +import { getCurrentAuthState } from '@core/auth/auth.selectors'; +import { RuleNodeDebugConfig } from '@shared/models/rule-node.models'; +import { MINUTE, SECOND } from '@shared/models/time/time.models'; +import { DebugDurationLeftPipe } from '@home/pages/rulechain/debug-duration-left.pipe'; +import { interval } from 'rxjs'; +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; + +@Component({ + selector: 'tb-debug-config-panel', + templateUrl: './debug-config-panel.component.html', + standalone: true, + imports: [ + SharedModule, + CommonModule, + DebugDurationLeftPipe + ] +}) +export class DebugConfigPanelComponent extends PageComponent implements OnInit { + + @Input() popover: TbPopoverComponent; + @Input() debugFailures = false; + @Input() debugAll = false; + @Input() debugAllUntil = 0; + + onFailuresControl: FormControl; + debugAllControl: FormControl; + + onConfigApplied = new EventEmitter() + + readonly maxRuleNodeDebugDurationMinutes = getCurrentAuthState(this.store).maxRuleNodeDebugDurationMinutes; + readonly ruleChainDebugPerTenantLimitsConfiguration = getCurrentAuthState(this.store).ruleChainDebugPerTenantLimitsConfiguration; + readonly maxMessagesCount = this.ruleChainDebugPerTenantLimitsConfiguration?.split(':')[0]; + readonly maxTimeFrameSec = this.ruleChainDebugPerTenantLimitsConfiguration?.split(':')[1]; + + constructor(private fb: UntypedFormBuilder, + private destroyRef: DestroyRef, + private cdr: ChangeDetectorRef, + protected store: Store) { + super(store); + + this.onFailuresControl = this.fb.control(false); + this.debugAllControl = this.fb.control(false); + interval(SECOND) + .pipe(takeUntilDestroyed(this.destroyRef)) + .subscribe(() => { + this.debugAllControl.patchValue(this.isDebugAllOn(), { emitEvent: false }); + this.cdr.markForCheck(); + }); + this.debugAllControl.valueChanges.pipe(takeUntilDestroyed()).subscribe(value => { + this.debugAllUntil = value? new Date().getTime() + this.maxRuleNodeDebugDurationMinutes * MINUTE : 0; + this.debugAll = value; + this.cdr.markForCheck(); + }); + } + + ngOnInit(): void { + this.debugAllControl.patchValue(this.isDebugAllOn(), { emitEvent: false }); + this.onFailuresControl.patchValue(this.debugFailures); + } + + onCancel() { + this.popover?.hide(); + } + + onApply(): void { + this.onConfigApplied.emit({ + debugAll: this.debugAll, + debugFailures: this.onFailuresControl.value, + debugAllUntil: this.debugAllUntil + }); + } + + onReset(): void { + this.debugAll = true; + this.debugAllUntil = new Date().getTime() + this.maxRuleNodeDebugDurationMinutes * MINUTE; + } + + isDebugAllOn(): boolean { + return this.debugAllUntil > new Date().getTime(); + } +} diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts b/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts index d6615da476..bb23b20959 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/debug-duration-left.pipe.ts @@ -29,15 +29,8 @@ export class DebugDurationLeftPipe implements PipeTransform { constructor(private translate: TranslateService, private millisecondsToTimeString: MillisecondsToTimeStringPipe) { } - transform(lastUpdateTs: number, maxRuleNodeDebugDurationMinutes: number): string { - const time = this.millisecondsToTimeString.transform(this.getDebugTime(lastUpdateTs, maxRuleNodeDebugDurationMinutes), true, true); + transform(debugAllUntil: number): string { + const time = this.millisecondsToTimeString.transform((debugAllUntil - new Date().getTime()), true, true); return `${time} ` + this.translate.instant('common.left'); } - - private getDebugTime(lastUpdateTs: number, maxRuleNodeDebugDurationMinutes: number): number { - const maxDuration = maxRuleNodeDebugDurationMinutes * MINUTE; - const updateDuration = lastUpdateTs ? new Date().getTime() - lastUpdateTs : 0; - const leftTime = maxDuration - updateDuration; - return leftTime > 0 ? leftTime : 0; - } } diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.html deleted file mode 100644 index 0edf83096a..0000000000 --- a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.html +++ /dev/null @@ -1,55 +0,0 @@ - -
-
debug-strategy.label
-
-
- - {{ 'debug-strategy.hint.main-limited' | translate: { msg: maxMessagesCount, sec: maxTimeFrameSec } }} - - {{ 'debug-strategy.hint.main' | translate }} -
-
-
- - - {{ 'debug-strategy.on-failure' | translate }} - - - - - {{ 'debug-strategy.all-messages' | translate }} {{ ' (' + maxRuleNodeDebugDurationMinutes + ' ' }} {{ ('debug-strategy.min' | translate) + ')'}} - - -
-
- - -
-
- diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.ts deleted file mode 100644 index c24e8331d7..0000000000 --- a/ui-ngx/src/app/modules/home/pages/rulechain/debug-strategy-panel.component.ts +++ /dev/null @@ -1,97 +0,0 @@ -/// -/// Copyright © 2016-2024 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. -/// - -import { Component, EventEmitter, Input, OnInit } from '@angular/core'; -import { PageComponent } from '@shared/components/page.component'; -import { TbPopoverComponent } from '@shared/components/popover.component'; -import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; -import { Store } from '@ngrx/store'; -import { AppState } from '@core/core.state'; -import { CommonModule } from '@angular/common'; -import { SharedModule } from '@shared/shared.module'; -import { getCurrentAuthState } from '@core/auth/auth.selectors'; -import { DebugStrategy } from '@shared/models/rule-node.models'; - -@Component({ - selector: 'tb-debug-strategy-panel', - templateUrl: './debug-strategy-panel.component.html', - standalone: true, - imports: [ - SharedModule, - CommonModule - ] -}) -export class DebugStrategyPanelComponent extends PageComponent implements OnInit { - - @Input() popover: TbPopoverComponent; - @Input() debugStrategy: DebugStrategy; - - debugStrategyFormGroup: UntypedFormGroup; - - onStrategyApplied = new EventEmitter() - - readonly maxRuleNodeDebugDurationMinutes = getCurrentAuthState(this.store).maxRuleNodeDebugDurationMinutes; - readonly ruleChainDebugPerTenantLimitsConfiguration = getCurrentAuthState(this.store).ruleChainDebugPerTenantLimitsConfiguration; - readonly maxMessagesCount = this.ruleChainDebugPerTenantLimitsConfiguration?.split(':')[0]; - readonly maxTimeFrameSec = this.ruleChainDebugPerTenantLimitsConfiguration?.split(':')[1]; - - constructor(private fb: UntypedFormBuilder, - protected store: Store) { - super(store); - - this.debugStrategyFormGroup = this.fb.group({ - allMessages: [false], - onFailure: [false] - }); - } - - ngOnInit(): void { - this.updatePanelStrategy(); - } - - onCancel() { - this.popover?.hide(); - } - - onApply(): void { - const allMessages = this.debugStrategyFormGroup.get('allMessages').value; - const onFailure = this.debugStrategyFormGroup.get('onFailure').value; - if (allMessages && onFailure) { - this.onStrategyApplied.emit(DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS); - } else if (allMessages) { - this.onStrategyApplied.emit(DebugStrategy.ALL_EVENTS); - } else if (onFailure) { - this.onStrategyApplied.emit(DebugStrategy.ONLY_FAILURE_EVENTS); - } else { - this.onStrategyApplied.emit(DebugStrategy.DISABLED); - } - } - - private updatePanelStrategy(): void { - switch (this.debugStrategy) { - case DebugStrategy.ALL_THEN_ONLY_FAILURE_EVENTS: - this.debugStrategyFormGroup.get('allMessages').patchValue(true, { emitEvent: false }); - this.debugStrategyFormGroup.get('onFailure').patchValue(true, { emitEvent: false }); - break; - case DebugStrategy.ONLY_FAILURE_EVENTS: - this.debugStrategyFormGroup.get('onFailure').patchValue(true, { emitEvent: false }); - break; - case DebugStrategy.ALL_EVENTS: - this.debugStrategyFormGroup.get('allMessages').patchValue(true, { emitEvent: false }); - break; - } - } -} diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html index c21cec6dd4..35415dec57 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html @@ -35,7 +35,13 @@
- + diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-button.component.ts b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.ts similarity index 74% rename from ui-ngx/src/app/modules/home/pages/rulechain/debug-config-button.component.ts rename to ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.ts index ff05942df4..ad64833ace 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-button.component.ts +++ b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.ts @@ -21,22 +21,20 @@ import { ViewContainerRef, DestroyRef, ChangeDetectionStrategy, - ChangeDetectorRef, EventEmitter, Output } from '@angular/core'; import { CommonModule } from '@angular/common'; import { SharedModule } from '@shared/shared.module'; -import { DebugDurationLeftPipe } from '@home/pages/rulechain/debug-duration-left.pipe'; -import { Store } from '@ngrx/store'; -import { AppState } from '@core/core.state'; +import { DurationLeftPipe } from '@shared/pipe/duration-left.pipe'; import { TbPopoverService } from '@shared/components/popover.service'; import { MatButton } from '@angular/material/button'; -import { DebugConfigPanelComponent } from '@home/pages/rulechain/debug-config-panel.component'; +import { DebugConfigPanelComponent } from './debug-config-panel.component'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; -import { interval } from 'rxjs'; +import { timer } from 'rxjs'; import { SECOND } from '@shared/models/time/time.models'; -import { RuleNodeDebugConfig } from '@shared/models/rule-node.models'; +import { HasDebugConfig } from '@shared/models/entity.models'; +import { map } from 'rxjs/operators'; @Component({ selector: 'tb-debug-config-button', @@ -45,7 +43,7 @@ import { RuleNodeDebugConfig } from '@shared/models/rule-node.models'; imports: [ CommonModule, SharedModule, - DebugDurationLeftPipe, + DurationLeftPipe, ], changeDetection: ChangeDetectionStrategy.OnPush }) @@ -55,20 +53,18 @@ export class DebugConfigButtonComponent { @Input() debugAll = false; @Input() debugAllUntil = 0; @Input() disabled = false; + @Input() maxRuleNodeDebugDurationMinutes: number; + @Input() ruleChainDebugPerTenantLimitsConfiguration: string; - @Output() onDebugConfigChanged = new EventEmitter() + @Output() onDebugConfigChanged = new EventEmitter(); - constructor(protected store: Store, - private popoverService: TbPopoverService, + isDebugAllActive$ = timer(0, SECOND).pipe(map(() => this.debugAllUntil > new Date().getTime())); + + constructor(private popoverService: TbPopoverService, private renderer: Renderer2, private viewContainerRef: ViewContainerRef, private destroyRef: DestroyRef, - private cdr: ChangeDetectorRef - ) { - interval(SECOND) - .pipe(takeUntilDestroyed(this.destroyRef)) - .subscribe(() => this.cdr.markForCheck()); - } + ) {} openDebugStrategyPanel($event: Event, matButton: MatButton): void { if ($event) { @@ -84,19 +80,16 @@ export class DebugConfigButtonComponent { debugFailures: this.debugFailures, debugAll: this.debugAll, debugAllUntil: this.debugAllUntil, + maxRuleNodeDebugDurationMinutes: this.maxRuleNodeDebugDurationMinutes, + ruleChainDebugPerTenantLimitsConfiguration: this.ruleChainDebugPerTenantLimitsConfiguration }, {}, {}, {}, true); debugStrategyPopover.tbComponentRef.instance.popover = debugStrategyPopover; - debugStrategyPopover.tbComponentRef.instance.onConfigApplied.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((config: RuleNodeDebugConfig) => { + debugStrategyPopover.tbComponentRef.instance.onConfigApplied.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((config: HasDebugConfig) => { this.onDebugConfigChanged.emit(config); - this.cdr.markForCheck(); debugStrategyPopover.hide(); }); } } - - isDebugAllActive(): boolean { - return this.debugAllUntil > new Date().getTime(); - } } diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-panel.component.html b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-panel.component.html similarity index 75% rename from ui-ngx/src/app/modules/home/pages/rulechain/debug-config-panel.component.html rename to ui-ngx/src/app/modules/home/components/debug-config/debug-config-panel.component.html index b7d2f44d8d..b463882110 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/debug-config-panel.component.html +++ b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-panel.component.html @@ -27,23 +27,18 @@
- +
{{ 'debug-config.on-failure' | translate }} - +
- - {{ 'debug-config.all-messages' | translate }} {{ '( ' }} - {{ debugAllUntil | debugDurationLeft }} - - {{ maxRuleNodeDebugDurationMinutes + ' ' }} {{ ('debug-config.min' | translate) }} - - {{ ' )' }} - +
+ {{ 'debug-config.all-messages' | translate: { time: (isDebugAllActive$ | async) ? (debugAllUntil | durationLeft) : ('debug-config.min' | translate: { number: maxRuleNodeDebugDurationMinutes }) } }} +
-
+
+ + tenant-profile.maximum-debug-duration-min + + + {{ 'tenant-profile.maximum-debug-duration-min-range' | translate}} + + + {{ 'tenant-profile.maximum-debug-duration-min-required' | translate}} + + + +
+
@@ -147,22 +163,6 @@
-
- - tenant-profile.maximum-rule-node-debug-duration-min - - - {{ 'tenant-profile.maximum-rule-node-debug-duration-min-range' | translate}} - - - {{ 'tenant-profile.maximum-rule-node-debug-duration-min-required' | translate}} - - - -
-
diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index 7aca5ed0e2..f9f6fc190e 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -4308,9 +4308,9 @@ "maximum-ota-packages-sum-data-size": "Maximum total size of OTA package files (bytes)", "maximum-ota-package-sum-data-size-required": "Maximum total size of OTA package files is required.", "maximum-ota-package-sum-data-size-range": "Maximum total size of OTA package files can't be negative", - "maximum-rule-node-debug-duration-min": "Maximum rule node debug duration (min)", - "maximum-rule-node-debug-duration-min-required": "Maximum rule node debug duration is required.", - "maximum-rule-node-debug-duration-min-range": "Maximum rule node debug duration can't be negative", + "maximum-debug-duration-min": "Maximum debug duration (min)", + "maximum-debug-duration-min-required": "Maximum debug duration is required.", + "maximum-debug-duration-min-range": "Maximum debug duration can't be negative", "rest-requests-for-tenant": "REST requests for tenant", "transport-tenant-telemetry-msg-rate-limit": "Transport tenant telemetry messages", "transport-tenant-telemetry-data-points-rate-limit": "Transport tenant telemetry data points", From d1e08bf64bd0d77409dfc424c5ed6efe6d822c0a Mon Sep 17 00:00:00 2001 From: mpetrov Date: Wed, 13 Nov 2024 15:47:07 +0200 Subject: [PATCH 29/37] refactoring --- ui-ngx/src/app/shared/models/rule-node.models.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui-ngx/src/app/shared/models/rule-node.models.ts b/ui-ngx/src/app/shared/models/rule-node.models.ts index db993664a8..1a6aa40e86 100644 --- a/ui-ngx/src/app/shared/models/rule-node.models.ts +++ b/ui-ngx/src/app/shared/models/rule-node.models.ts @@ -331,7 +331,7 @@ export interface RuleNodeComponentDescriptor extends ComponentDescriptor { configurationDescriptor?: RuleNodeConfigurationDescriptor; } -export interface FcRuleNodeType extends FcNode { +export interface FcRuleNodeType extends FcNode, HasDebugConfig { component?: RuleNodeComponentDescriptor; singletonMode?: boolean; queueName?: string; From a70d40534746b0750440113071f206acffeac516 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Wed, 13 Nov 2024 15:56:53 +0200 Subject: [PATCH 30/37] refactoring --- .../default-tenant-profile-configuration.component.html | 4 ++-- ui-ngx/src/app/shared/models/entity.models.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/profile/tenant/default-tenant-profile-configuration.component.html b/ui-ngx/src/app/modules/home/components/profile/tenant/default-tenant-profile-configuration.component.html index 21d2fe49af..dd095117d7 100644 --- a/ui-ngx/src/app/modules/home/components/profile/tenant/default-tenant-profile-configuration.component.html +++ b/ui-ngx/src/app/modules/home/components/profile/tenant/default-tenant-profile-configuration.component.html @@ -83,10 +83,10 @@ formControlName="maxDebugModeDurationMinutes" type="number"> - {{ 'tenant-profile.maximum-debug-duration-min-range' | translate}} + {{ 'tenant-profile.maximum-debug-duration-min-range' | translate }} - {{ 'tenant-profile.maximum-debug-duration-min-required' | translate}} + {{ 'tenant-profile.maximum-debug-duration-min-required' | translate }} diff --git a/ui-ngx/src/app/shared/models/entity.models.ts b/ui-ngx/src/app/shared/models/entity.models.ts index 62270a0240..cb209c3352 100644 --- a/ui-ngx/src/app/shared/models/entity.models.ts +++ b/ui-ngx/src/app/shared/models/entity.models.ts @@ -193,7 +193,7 @@ export interface HasVersion { } export interface HasDebugConfig { - debugAll: boolean; - debugFailures: boolean; - debugAllUntil: number; + debugAll?: boolean; + debugFailures?: boolean; + debugAllUntil?: number; } From 075718e9563de59095008227524c47642de7fef9 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Wed, 13 Nov 2024 17:00:44 +0200 Subject: [PATCH 31/37] added mini btn mode --- ui-ngx/src/app/core/auth/auth.reducer.ts | 2 +- .../debug-config-button.component.html | 14 +++++++++++++ .../debug-config-button.component.scss | 20 +++++++++++++++++++ .../debug-config-button.component.ts | 2 ++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.scss diff --git a/ui-ngx/src/app/core/auth/auth.reducer.ts b/ui-ngx/src/app/core/auth/auth.reducer.ts index 072f3f62a5..20674adf8d 100644 --- a/ui-ngx/src/app/core/auth/auth.reducer.ts +++ b/ui-ngx/src/app/core/auth/auth.reducer.ts @@ -31,8 +31,8 @@ const emptyUserAuthState: AuthPayload = { persistDeviceStateToTelemetry: false, mobileQrEnabled: false, maxResourceSize: 0, - userSettings: initialUserSettings, maxDebugModeDurationMinutes: 0, + userSettings: initialUserSettings }; export const initialState: AuthState = { diff --git a/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.html b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.html index 59565281b3..d0905d968d 100644 --- a/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.html +++ b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.html @@ -16,6 +16,7 @@ --> + + + diff --git a/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.scss b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.scss new file mode 100644 index 0000000000..06fcba27be --- /dev/null +++ b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.scss @@ -0,0 +1,20 @@ +/** + * Copyright © 2016-2024 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. + */ +:host { + .mini-debug-btn { + color: rgba(0, 0, 0, 0.6); + } +} diff --git a/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.ts b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.ts index 154bacf69f..576f926a06 100644 --- a/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.ts +++ b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.ts @@ -42,6 +42,7 @@ import { Store } from '@ngrx/store'; @Component({ selector: 'tb-debug-config-button', templateUrl: './debug-config-button.component.html', + styleUrls: ['./debug-config-button.component.scss'], standalone: true, imports: [ CommonModule, @@ -56,6 +57,7 @@ export class DebugConfigButtonComponent { @Input() debugAll = false; @Input() debugAllUntil = 0; @Input() disabled = false; + @Input() minifyMode = false; @Input() debugLimitsConfiguration: string; @Output() onDebugConfigChanged = new EventEmitter(); From ed6e3f215408b9a40fc3a01e6f86722dfb8624e8 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Fri, 15 Nov 2024 12:19:49 +0200 Subject: [PATCH 32/37] Added static time on debugAll=true --- .../debug-config/debug-config-button.component.html | 4 +++- .../components/debug-config/debug-config-panel.component.html | 4 ++-- .../components/debug-config/debug-config-panel.component.ts | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.html b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.html index d0905d968d..bdaf2de487 100644 --- a/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.html +++ b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.html @@ -26,7 +26,9 @@ bug_report common.disabled debug-config.all - {{ debugAllUntil | durationLeft }} + + {{ !debugAll ? (debugAllUntil | durationLeft) : ('debug-config.min' | translate: { number: maxDebugModeDurationMinutes }) }} + debug-config.failures diff --git a/ui-ngx/src/app/modules/home/components/debug-config/debug-config-panel.component.html b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-panel.component.html index 2e3eb94648..379ad579eb 100644 --- a/ui-ngx/src/app/modules/home/components/debug-config/debug-config-panel.component.html +++ b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-panel.component.html @@ -34,10 +34,10 @@
- {{ 'debug-config.all-messages' | translate: { time: (isDebugAllActive$ | async) ? (debugAllUntil | durationLeft) : ('debug-config.min' | translate: { number: maxDebugModeDurationMinutes }) } }} + {{ 'debug-config.all-messages' | translate: { time: (isDebugAllActive$ | async) && !debugAll ? (debugAllUntil | durationLeft) : ('debug-config.min' | translate: { number: maxDebugModeDurationMinutes }) } }}
-