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..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", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "configuration": { "persistAlarmRulesState": false, "fetchAlarmRulesStateOnStart": false @@ -34,7 +35,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "configuration": { "defaultTTL": 0 }, @@ -47,7 +49,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", "name": "Save Client Attributes", - "debugMode": false, + "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", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "configuration": { "version": 0 }, @@ -77,7 +81,8 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log RPC from Device", - "debugMode": false, + "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", - "debugMode": false, + "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", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "configuration": { "timeoutInSeconds": 60 }, @@ -120,7 +127,8 @@ }, "type": "org.thingsboard.rule.engine.edge.TbMsgPushToCloudNode", "name": "Push to cloud", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "configuration": { "scope": "SERVER_SCOPE" }, @@ -133,7 +141,8 @@ }, "type": "org.thingsboard.rule.engine.edge.TbMsgPushToCloudNode", "name": "Push to cloud", - "debugMode": false, + "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 0256a2ccf2..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", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "configuration": { "defaultTTL": 0 } @@ -31,7 +32,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", "name": "Save Client Attributes", - "debugMode": false, + "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", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "configuration": { "version": 0 } @@ -59,7 +62,8 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log RPC from Device", - "debugMode": false, + "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", - "debugMode": false, + "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", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "configuration": { "timeoutInSeconds": 60 } @@ -100,7 +106,8 @@ }, "type": "org.thingsboard.rule.engine.profile.TbDeviceProfileNode", "name": "Device Profile Node", - "debugMode": false, + "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 0b70d087e7..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", - "debugMode": false, + "debugFailures": false, + "debugAll": false, + "debugAllUntil": 0, "configuration": { "defaultTTL": 0 } @@ -30,7 +32,9 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", "name": "Save Client Attributes", - "debugMode": false, + "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", - "debugMode": false, + "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", - "debugMode": false, + "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", - "debugMode": false, + "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", - "debugMode": false, + "debugFailures": false, + "debugAll": false, + "debugAllUntil": 0, "configuration": { "timeoutInSeconds": 60 } @@ -99,7 +111,9 @@ }, "type": "org.thingsboard.rule.engine.profile.TbDeviceProfileNode", "name": "Device Profile Node", - "debugMode": false, + "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 3aa0f08edf..3e18026c52 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 @@ -25,6 +25,23 @@ UPDATE user_credentials c SET failed_login_attempts = (SELECT (additional_info:: UPDATE tb_user SET additional_info = (additional_info::jsonb - 'lastLoginTs' - 'failedLoginAttempts' - 'userCredentialsEnabled')::text WHERE additional_info IS NOT NULL AND additional_info != 'null'; +-- UPDATE RULE NODE DEBUG MODE TO DEBUG STRATEGY START + +ALTER TABLE rule_node ADD COLUMN IF NOT EXISTS debug_failures boolean DEFAULT false; +ALTER TABLE rule_node ADD COLUMN IF NOT EXISTS debug_all_until bigint NOT NULL DEFAULT 0; +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_all_until = (extract(epoch from now()) + 3600) * 1000 WHERE debug_mode = true; + ALTER TABLE rule_node DROP COLUMN debug_mode; + END IF; + END +$$; + +-- UPDATE RULE NODE DEBUG MODE TO DEBUG STRATEGY END + -- CREATE MOBILE APP BUNDLES FROM EXISTING APPS 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 a62cb432da..ac1d49d761 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 @@ -64,6 +64,7 @@ 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.RuleNode; +import org.thingsboard.common.util.DebugModeUtil; import org.thingsboard.server.common.data.rule.RuleNodeState; import org.thingsboard.server.common.data.script.ScriptLanguage; import org.thingsboard.server.common.msg.TbActorMsg; @@ -130,7 +131,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; @@ -144,25 +145,20 @@ 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)); - } - msg.getCallback().onProcessingEnd(nodeCtx.getSelf().getId()); - nodeCtx.getChainActor().tell(new RuleNodeToRuleChainTellNextMsg(nodeCtx.getSelf().getRuleChainId(), nodeCtx.getSelf().getId(), relationTypes, msg, th != null ? th.getMessage() : null)); + RuleNode ruleNode = nodeCtx.getSelf(); + persistDebugOutput(msg, relationTypes); + msg.getCallback().onProcessingEnd(ruleNode.getId()); + nodeCtx.getChainActor().tell(new RuleNodeToRuleChainTellNextMsg(ruleNode.getRuleChainId(), ruleNode.getId(), relationTypes, msg, null)); } @Override @@ -183,9 +179,7 @@ class DefaultTbContext implements TbContext { if (item == null) { ack(msg); } else { - if (nodeCtx.getSelf().isDebugMode()) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), nodeCtx.getSelf().getId(), msg, relationType); - } + persistDebugOutput(msg, relationType); nodeCtx.getChainActor().tell(new RuleChainOutputMsg(item.getRuleChainId(), item.getRuleNodeId(), relationType, msg)); } } @@ -214,11 +208,9 @@ 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 -> { + persistDebugOutput(tbMsg, TbNodeConnectionType.TO_ROOT_RULE_CHAIN); if (onSuccess != null) { onSuccess.run(); } @@ -300,8 +292,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()) @@ -311,12 +304,9 @@ 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 -> { + persistDebugOutput(tbMsg, relationTypes, null, failureMessage); if (onSuccess != null) { onSuccess.run(); } @@ -332,10 +322,9 @@ class DefaultTbContext implements TbContext { @Override public void ack(TbMsg tbMsg) { - if (nodeCtx.getSelf().isDebugMode()) { - mainCtx.persistDebugOutput(nodeCtx.getTenantId(), nodeCtx.getSelf().getId(), tbMsg, "ACK", null); - } - tbMsg.getCallback().onProcessingEnd(nodeCtx.getSelf().getId()); + RuleNode ruleNode = nodeCtx.getSelf(); + persistDebugOutput(tbMsg, TbNodeConnectionType.ACK); + tbMsg.getCallback().onProcessingEnd(ruleNode.getId()); tbMsg.getCallback().onSuccess(); } @@ -350,12 +339,11 @@ 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(); + persistDebugOutput(msg, Set.of(TbNodeConnectionType.FAILURE), th, null); 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)); } @@ -1010,4 +998,21 @@ 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(); + if (DebugModeUtil.isDebugAllAvailable(ruleNode)) { + relationTypes.forEach(relationType -> mainCtx.persistDebugOutput(getTenantId(), ruleNode.getId(), msg, relationType, error, failureMessage)); + } else if (DebugModeUtil.isDebugFailuresAvailable(ruleNode, relationTypes)) { + mainCtx.persistDebugOutput(getTenantId(), ruleNode.getId(), msg, TbNodeConnectionType.FAILURE, error, failureMessage); + } + } + } 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..460da228c3 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.common.util.DebugModeUtil; import org.thingsboard.server.common.msg.TbMsg; import org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg; import org.thingsboard.server.common.msg.plugin.RuleNodeUpdatedMsg; @@ -43,7 +44,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 +72,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 +80,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 +175,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 +236,47 @@ 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/SystemInfoController.java b/application/src/main/java/org/thingsboard/server/controller/SystemInfoController.java index 9443e58a04..bf90c51e40 100644 --- a/application/src/main/java/org/thingsboard/server/controller/SystemInfoController.java +++ b/application/src/main/java/org/thingsboard/server/controller/SystemInfoController.java @@ -70,6 +70,15 @@ public class SystemInfoController extends BaseController { @Value("${ui.dashboard.max_datapoints_limit}") private long maxDatapointsLimit; + @Value("${debug_mode.max_duration:15}") + private int maxDebugModeDurationMinutes; + + @Value("${actors.rule.chain.debug_mode_rate_limits_per_tenant.enabled:true}") + private boolean ruleChainDebugPerTenantLimitsEnabled; + + @Value("${actors.rule.chain.debug_mode_rate_limits_per_tenant.configuration:50000:3600}") + private String ruleChainDebugPerTenantLimitsConfiguration; + @Autowired(required = false) private BuildProperties buildProperties; @@ -141,6 +150,10 @@ public class SystemInfoController extends BaseController { if (!currentUser.isSystemAdmin()) { DefaultTenantProfileConfiguration tenantProfileConfiguration = tenantProfileCache.get(tenantId).getDefaultProfileConfiguration(); systemParams.setMaxResourceSize(tenantProfileConfiguration.getMaxResourceSize()); + systemParams.setMaxDebugModeDurationMinutes(tenantProfileConfiguration.getMaxDebugModeDurationMinutes(maxDebugModeDurationMinutes)); + if (ruleChainDebugPerTenantLimitsEnabled) { + systemParams.setRuleChainDebugPerTenantLimitsConfiguration(ruleChainDebugPerTenantLimitsConfiguration); + } } systemParams.setMobileQrEnabled(Optional.ofNullable(qrCodeSettingService.findQrCodeSettings(TenantId.SYS_TENANT_ID)) .map(QrCodeSettings::getQrCodeConfig).map(QRCodeConfig::isShowOnHomePage) 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..258014d6fe 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" + + " \"maxDebugModeDurationMinutes\": 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..cdc0e3ae21 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 @@ -88,7 +88,8 @@ public abstract class BaseRuleChainMetadataConstructor implements RuleChainMetad .setIdLSB(node.getId().getId().getLeastSignificantBits()) .setType(node.getType()) .setName(node.getName()) - .setDebugMode(node.isDebugMode()) + .setDebugFailures(node.isDebugFailures()) + .setDebugAllUntil(node.getDebugAllUntil()) .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..a9accf572d 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.setMaxDebugModeDurationMinutes(0); tenantProfileData.setConfiguration(configuration); tenantProfile.setProfileData(tenantProfileData); 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 8c0ba3a17a..e93b4717a3 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; } diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml index 2a121e2626..190ec5a798 100644 --- a/application/src/main/resources/thingsboard.yml +++ b/application/src/main/resources/thingsboard.yml @@ -494,6 +494,14 @@ actors: # Actors statistic persistence frequency in milliseconds persist_frequency: "${ACTORS_STATISTICS_PERSIST_FREQUENCY:3600000}" +debug_mode: + # Maximum duration (in minutes) for debug mode. + # Min value is 1 minute. + # If set in the tenant profile and valid, it will be used. + # Otherwise, the value from this setting is used. + # If value from this setting is invalid, the default value (15 minutes) will be used. + max_duration: "${DEBUG_MODE_MAX_DURATION_MINUTES:15}" + # Cache settings parameters cache: # caffeine or redis(7.2 - latest compatible version) 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..c8e1787657 --- /dev/null +++ b/application/src/test/java/org/thingsboard/server/actors/rule/DefaultTbContextTest.java @@ -0,0 +1,974 @@ +/** + * 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.Mockito; +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.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.RuleNode; +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.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.concurrent.TimeUnit; +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.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; +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 givenDebugFailuresEvents_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.setDebugFailures(true); + ruleNode.setDebugAllUntil(0); + 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 givenDebugFailuresEventsAndSuccessConnection_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.setDebugFailures(true); + ruleNode.setDebugAllUntil(0); + 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 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.setDebugFailures(true); + ruleNode.setDebugAllUntil(0); + 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, null, null); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + checkTellNextCommonLogic(callbackMock, connections, msg); + } + + private static Stream> givenDebugFailuresEventsAndConnections_whenTellNext_thenVerifyDebugOutputPersisted() { + return Stream.of( + Collections.singleton(TbNodeConnectionType.FAILURE), + Set.of(TbNodeConnectionType.FAILURE, TbNodeConnectionType.SUCCESS) + ); + } + + @MethodSource + @ParameterizedTest + 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.setDebugFailures(false); + ruleNode.setDebugAllUntil(0); + 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> givenDebugDisabledAndConnections_whenTellNext_thenVerifyDebugOutputNotPersisted() { + return Stream.of( + Collections.singleton(TbNodeConnectionType.FAILURE), + Collections.singleton(TbNodeConnectionType.SUCCESS), + Set.of(TbNodeConnectionType.FAILURE, TbNodeConnectionType.SUCCESS) + ); + } + + @MethodSource + @ParameterizedTest + 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.setDebugFailures(false); + ruleNode.setDebugAllUntil(getUntilTime()); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + defaultTbContext.tellNext(msg, connection); + + // THEN + then(nodeCtxMock).should().getChainActor(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, connection, null, null); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + checkTellNextCommonLogic(callbackMock, connection, msg); + } + + private static Stream givenDebugAllEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted() { + return failureAndSuccessConnection(); + } + + @Test + 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.setDebugFailures(false); + ruleNode.setDebugAllUntil(getUntilTime()); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + Set connections = failureAndSuccessConnection().collect(Collectors.toSet()); + defaultTbContext.tellNext(msg, connections); + + // THEN + then(nodeCtxMock).should().getChainActor(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + 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); + } + + @MethodSource + @ParameterizedTest + 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.setDebugFailures(false); + ruleNode.setDebugAllUntil(getUntilTime()); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + defaultTbContext.tellNext(msg, connection); + + // THEN + then(nodeCtxMock).should().getChainActor(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msg, connection, null, null); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + checkTellNextCommonLogic(callbackMock, connection, msg); + } + + private static Stream givenDebugAllThenOnlyFailureEventsAndConnection_whenTellNext_thenVerifyDebugOutputPersisted() { + return failureAndSuccessConnection(); + } + + @Test + 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.setDebugFailures(true); + ruleNode.setDebugAllUntil(getUntilTime()); + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // WHEN + Set connections = failureAndSuccessConnection().collect(Collectors.toSet()); + defaultTbContext.tellNext(msg, connections); + + // THEN + then(nodeCtxMock).should().getChainActor(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + 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); + } + + @Test + public void givenDebugFailuresEventsAndFailureConnection_whenOutput_thenVerifyDebugOutputPersisted() { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + 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); + 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, null, null); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + + @Test + public void givenDebugFailuresEventsAndSuccessConnection_whenOutput_thenVerifyDebugOutputNotPersisted() { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + 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); + + // 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 givenDebugDisabled_whenOutput_thenVerifyDebugOutputNotPersisted(String nodeConnection) { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + 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); + + // WHEN + defaultTbContext.output(msgMock, nodeConnection); + + // THEN + checkOutputCommonLogic(msgMock, nodeConnection); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + + @ParameterizedTest + @ValueSource(strings = {TbNodeConnectionType.SUCCESS, TbNodeConnectionType.FAILURE}) + 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.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); + + // WHEN + defaultTbContext.output(msgMock, nodeConnection); + + // THEN + checkOutputCommonLogic(msgMock, nodeConnection); + then(mainCtxMock).should().persistDebugOutput(TENANT_ID, RULE_NODE_ID, msgMock, nodeConnection, null, null); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + + @ParameterizedTest + @ValueSource(strings = {TbNodeConnectionType.SUCCESS, TbNodeConnectionType.FAILURE}) + 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.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); + + // WHEN + defaultTbContext.output(msgMock, nodeConnection); + + // THEN + checkOutputCommonLogic(msgMock, nodeConnection); + 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 + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(0); + 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 givenEmptyStackAndDebugAllEvents_whenOutput_thenVerifyMsgAckAndDebugOutputPersisted() { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + 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); + + // 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 givenEmptyStackAndDebugAllThenOnlyFailureEvents_whenOutput_thenVerifyMsgAckAndDebugOutputPersisted() { + // GIVEN + var msgMock = mock(TbMsg.class); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + 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); + + // 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 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.setDebugFailures(true); + 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 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.setDebugFailures(false); + ruleNode.setDebugAllUntil(0); + 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 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.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); + + // 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 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 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.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); + + // 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 (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(); + assertThat(actualTbMsg).usingRecursiveComparison() + .ignoringFields("id", "ctx") + .isEqualTo(expectedTbMsg); + } + then(mainCtxMock).should().getClusterService(); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(tbClusterServiceMock).shouldHaveNoMoreInteractions(); + } + + @MethodSource + @ParameterizedTest + 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.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); + + 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 (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(); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(tbClusterServiceMock).shouldHaveNoMoreInteractions(); + } + + @Test + public void givenDebugFailuress_whenTellFailure_thenVerifyDebugOutputPersisted() { + // GIVEN + var msg = getTbMsg(); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugFailures(true); + ruleNode.setDebugAllUntil(0); + 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, null); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + + @Test + public void givenDebugDisabled_whenTellFailure_thenVerifyDebugOutputNotPersisted() { + // GIVEN + var msg = getTbMsg(); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(0); + 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 givenDebugAllEvents_whenTellFailure_thenVerifyDebugOutputPersisted() { + // GIVEN + var msg = getTbMsg(); + var ruleNode = new RuleNode(RULE_NODE_ID); + ruleNode.setRuleChainId(RULE_CHAIN_ID); + ruleNode.setDebugFailures(false); + ruleNode.setDebugAllUntil(getUntilTime()); + 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, null); + then(mainCtxMock).shouldHaveNoMoreInteractions(); + then(nodeCtxMock).shouldHaveNoMoreInteractions(); + } + + @MethodSource + @ParameterizedTest + void givenDebugFailuresAndDebugAllAndConnectionAndPersistedResultOptions_whenTellNext_thenVerifyDebugOutputPersistence(boolean debugFailures, + long debugAllUntil, + 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.setDebugFailures(debugFailures); + ruleNode.setDebugAllUntil(debugAllUntil); + if (shouldPersist) { + given(nodeCtxMock.getTenantId()).willReturn(TENANT_ID); + } + given(nodeCtxMock.getSelf()).willReturn(ruleNode); + given(nodeCtxMock.getChainActor()).willReturn(chainActorMock); + + // 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); + ruleNode.setDebugAllUntil(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); + } + + 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 givenDebugOptions_whenEnqueueForTellNext_thenVerifyDebugOutputPersistedOnlyForDebugAll() { + return Stream.of( + 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 givenDebugOptions_whenEnqueue_thenVerifyDebugOutputPersistedOnlyForDebugAll() { + return Stream.of( + Arguments.of(false, getUntilTime()), + Arguments.of(true, getUntilTime()), + Arguments.of(true, 0), + Arguments.of(false, 0) + ); + } + + private static Stream givenDebugFailuresAndDebugAllAndConnectionAndPersistedResultOptions_whenTellNext_thenVerifyDebugOutputPersistence() { + return Stream.of( + 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) + ); + } + + 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 static long getUntilTime() { + return getUntilTime(15); + } + + 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 5484c29305..3220620dfd 100644 --- a/application/src/test/java/org/thingsboard/server/edge/AbstractEdgeTest.java +++ b/application/src/test/java/org/thingsboard/server/edge/AbstractEdgeTest.java @@ -103,7 +103,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 +124,6 @@ abstract public class AbstractEdgeTest extends AbstractControllerTest { protected EdgeImitator edgeImitator; protected Edge edge; - private Random random = new Random(); - @Autowired protected EdgeEventService edgeEventService; @@ -166,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(); @@ -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.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 eaf4468156..224c21ffc5 100644 --- a/application/src/test/java/org/thingsboard/server/edge/RuleChainEdgeTest.java +++ b/application/src/test/java/org/thingsboard/server/edge/RuleChainEdgeTest.java @@ -228,7 +228,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.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 9e36fed677..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 @@ -142,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.setDebugMode(true); + ruleNode1.setDebugAll(true); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setFetchTo(TbMsgSource.METADATA); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); @@ -152,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.setDebugMode(true); + ruleNode2.setDebugAll(true); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setFetchTo(TbMsgSource.METADATA); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); @@ -248,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.setDebugMode(true); + ruleNode1.setDebugAll(true); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setFetchTo(TbMsgSource.METADATA); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); @@ -257,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.setDebugMode(true); + ruleNode12.setDebugAll(true); TbRuleChainInputNodeConfiguration configuration12 = new TbRuleChainInputNodeConfiguration(); configuration12.setRuleChainId(secondaryRuleChain.getId().getId().toString()); ruleNode12.setConfiguration(JacksonUtil.valueToTree(configuration12)); @@ -282,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.setDebugMode(true); + 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 4c4e887644..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 @@ -97,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.setDebugMode(true); + 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 c7f2e37211..3f193620ab 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 @@ -496,7 +496,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.setDebugAll(true); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); ruleNode1.setConfiguration(JacksonUtil.valueToTree(configuration1)); @@ -505,7 +505,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.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 55f15516f4..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 @@ -471,7 +471,7 @@ public class ExportImportServiceSqlTest extends AbstractControllerTest { RuleNode ruleNode1 = new RuleNode(); ruleNode1.setName("Generator 1"); ruleNode1.setType(TbMsgGeneratorNode.class.getName()); - ruleNode1.setDebugMode(true); + ruleNode1.setDebugAllUntil(System.currentTimeMillis()); TbMsgGeneratorNodeConfiguration configuration1 = new TbMsgGeneratorNodeConfiguration(); configuration1.setOriginatorType(originatorId.getEntityType()); configuration1.setOriginatorId(originatorId.getId().toString()); @@ -481,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.setDebugMode(true); + ruleNode2.setDebugAllUntil(System.currentTimeMillis()); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); ruleNode2.setConfiguration(JacksonUtil.valueToTree(configuration2)); @@ -510,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.setDebugMode(true); + ruleNode1.setDebugAllUntil(System.currentTimeMillis()); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); ruleNode1.setConfiguration(JacksonUtil.valueToTree(configuration1)); @@ -519,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.setDebugMode(true); + 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 2e01d700a9..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 @@ -870,7 +870,7 @@ public class VersionControlTest extends AbstractControllerTest { RuleNode ruleNode1 = new RuleNode(); ruleNode1.setName("Generator 1"); ruleNode1.setType(TbMsgGeneratorNode.class.getName()); - ruleNode1.setDebugMode(true); + ruleNode1.setDebugAll(true); TbMsgGeneratorNodeConfiguration configuration1 = new TbMsgGeneratorNodeConfiguration(); configuration1.setOriginatorType(originatorId.getEntityType()); configuration1.setOriginatorId(originatorId.getId().toString()); @@ -880,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.setDebugMode(true); + ruleNode2.setDebugAll(true); TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration(); configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2")); ruleNode2.setConfiguration(JacksonUtil.valueToTree(configuration2)); @@ -908,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.setDebugMode(true); + ruleNode1.setDebugAll(true); TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration(); configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1")); ruleNode1.setConfiguration(JacksonUtil.valueToTree(configuration1)); @@ -917,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.setDebugMode(true); + 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/HasDebugMode.java b/common/data/src/main/java/org/thingsboard/server/common/data/HasDebugMode.java new file mode 100644 index 0000000000..c62696758b --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/HasDebugMode.java @@ -0,0 +1,33 @@ +/** + * 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; + +import io.swagger.v3.oas.annotations.media.Schema; + +public interface HasDebugMode { + + boolean isDebugFailures(); + + void setDebugFailures(boolean debugFailures); + + boolean isDebugAll(); + + void setDebugAll(boolean debugAll); + + long getDebugAllUntil(); + + void setDebugAllUntil(long debugAllUntil); +} 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..abe1932327 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,6 @@ public class SystemParams { long maxDatapointsLimit; long maxResourceSize; boolean mobileQrEnabled; + int maxDebugModeDurationMinutes; + String ruleChainDebugPerTenantLimitsConfiguration; } 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/RuleNode.java b/common/data/src/main/java/org/thingsboard/server/common/data/rule/RuleNode.java index 542debcfdd..2938d7c6f1 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,12 +16,14 @@ 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; import lombok.EqualsAndHashCode; import lombok.extern.slf4j.Slf4j; import org.thingsboard.server.common.data.BaseDataWithAdditionalInfo; +import org.thingsboard.server.common.data.HasDebugMode; import org.thingsboard.server.common.data.HasName; import org.thingsboard.server.common.data.id.RuleChainId; import org.thingsboard.server.common.data.id.RuleNodeId; @@ -32,7 +34,8 @@ import org.thingsboard.server.common.data.validation.NoXss; @Data @EqualsAndHashCode(callSuper = true) @Slf4j -public class RuleNode extends BaseDataWithAdditionalInfo implements HasName { +@JsonIgnoreProperties(ignoreUnknown = true) +public class RuleNode extends BaseDataWithAdditionalInfo implements HasName, HasDebugMode { private static final long serialVersionUID = -5656679015121235465L; @@ -45,15 +48,19 @@ 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 = "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") 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 +80,9 @@ public class RuleNode extends BaseDataWithAdditionalInfo implements this.ruleChainId = ruleNode.getRuleChainId(); this.type = ruleNode.getType(); this.name = ruleNode.getName(); - this.debugMode = ruleNode.isDebugMode(); + this.debugFailures = ruleNode.isDebugFailures(); + this.debugAll = ruleNode.isDebugAll(); + this.debugAllUntil = ruleNode.getDebugAllUntil(); this.singletonMode = ruleNode.isSingletonMode(); this.setConfiguration(ruleNode.getConfiguration()); this.externalId = ruleNode.getExternalId(); @@ -93,9 +102,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 +116,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..4359f0f2bb 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,8 +33,11 @@ import org.thingsboard.server.common.data.TenantProfileType; @Data public class DefaultTenantProfileConfiguration implements TenantProfileConfiguration { + @Serial private static final long serialVersionUID = -7134932690332578595L; + private static final int DEBUG_MODE_DEFAULT_DURATION_MINUTES = 15; + private long maxDevices; private long maxAssets; private long maxCustomers; @@ -91,6 +96,8 @@ public class DefaultTenantProfileConfiguration implements TenantProfileConfigura private long maxDPStorageDays; @Schema(example = "50") private int maxRuleNodeExecutionsPerMessage; + @Schema(example = "15") + private int maxDebugModeDurationMinutes; @Schema(example = "0") private long maxEmails; @Schema(example = "true") @@ -197,4 +204,10 @@ public class DefaultTenantProfileConfiguration implements TenantProfileConfigura public int getMaxRuleNodeExecsPerMessage() { return maxRuleNodeExecutionsPerMessage; } + + @Override + public int getMaxDebugModeDurationMinutes(int systemMaxDebugModeDurationMinutes) { + return maxDebugModeDurationMinutes > 0 ? maxDebugModeDurationMinutes : systemMaxDebugModeDurationMinutes > 0 ? + systemMaxDebugModeDurationMinutes : DEBUG_MODE_DEFAULT_DURATION_MINUTES; + } } 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..12d66e5178 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 getMaxDebugModeDurationMinutes(int systemMaxDebugModeDurationMinutes); + } diff --git a/common/edge-api/src/main/proto/edge.proto b/common/edge-api/src/main/proto/edge.proto index 5fb51de2ab..379ea3b77b 100644 --- a/common/edge-api/src/main/proto/edge.proto +++ b/common/edge-api/src/main/proto/edge.proto @@ -168,11 +168,13 @@ message RuleNodeProto { 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; + bool debugFailures = 10; + int64 debugAllUntil = 11; } message NodeConnectionInfoProto { diff --git a/common/util/src/main/java/org/thingsboard/common/util/DebugModeUtil.java b/common/util/src/main/java/org/thingsboard/common/util/DebugModeUtil.java new file mode 100644 index 0000000000..0f4213f989 --- /dev/null +++ b/common/util/src/main/java/org/thingsboard/common/util/DebugModeUtil.java @@ -0,0 +1,43 @@ +/** + * 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.common.util; + +import org.thingsboard.server.common.data.HasDebugMode; +import org.thingsboard.server.common.data.msg.TbNodeConnectionType; + +import java.util.Set; + +public final class DebugModeUtil { + private DebugModeUtil() { + } + + public static boolean isDebugAllAvailable(HasDebugMode debugMode) { + return debugMode.getDebugAllUntil() > System.currentTimeMillis(); + } + + public static boolean isDebugAvailable(HasDebugMode debugMode, String nodeConnection) { + return isDebugAllAvailable(debugMode) || debugMode.isDebugFailures() && TbNodeConnectionType.FAILURE.equals(nodeConnection); + } + + public static boolean isDebugFailuresAvailable(HasDebugMode debugMode, Set nodeConnections) { + return isDebugFailuresAvailable(debugMode) && nodeConnections.contains(TbNodeConnectionType.FAILURE); + } + + private static boolean isDebugFailuresAvailable(HasDebugMode debugMode) { + return debugMode.isDebugFailures() || isDebugAllAvailable(debugMode); + } + +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/entity/AbstractEntityService.java b/dao/src/main/java/org/thingsboard/server/dao/entity/AbstractEntityService.java index 6b88f19699..14df35b193 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entity/AbstractEntityService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entity/AbstractEntityService.java @@ -18,9 +18,11 @@ package org.thingsboard.server.dao.entity; import lombok.extern.slf4j.Slf4j; import org.hibernate.exception.ConstraintViolationException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Lazy; import org.thingsboard.server.common.data.EntityView; +import org.thingsboard.server.common.data.HasDebugMode; import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.id.EdgeId; import org.thingsboard.server.common.data.id.EntityId; @@ -33,11 +35,13 @@ import org.thingsboard.server.dao.entityview.EntityViewService; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.housekeeper.CleanUpService; import org.thingsboard.server.dao.relation.RelationService; +import org.thingsboard.server.dao.tenant.TbTenantProfileCache; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.TimeUnit; @Slf4j public abstract class AbstractEntityService { @@ -68,6 +72,13 @@ public abstract class AbstractEntityService { @Lazy protected CleanUpService cleanUpService; + @Autowired + @Lazy + private TbTenantProfileCache tbTenantProfileCache; + + @Value("${debug_mode.max_duration:15}") + private int maxDebugModeDurationMinutes; + protected void createRelation(TenantId tenantId, EntityRelation relation) { log.debug("Creating relation: {}", relation); relationService.saveRelation(tenantId, relation); @@ -124,4 +135,15 @@ public abstract class AbstractEntityService { } } } + + protected void setDebugAllUntil(TenantId tenantId, HasDebugMode entity, long now) { + int debugDuration = tbTenantProfileCache.get(tenantId).getDefaultProfileConfiguration().getMaxDebugModeDurationMinutes(maxDebugModeDurationMinutes); + long debugUntil = now + TimeUnit.MINUTES.toMillis(debugDuration); + + if (entity.isDebugAll()) { + entity.setDebugAllUntil(debugUntil); + } else if (entity.getDebugAllUntil() > debugUntil) { + throw new DataValidationException("Unable to update 'debugAllUntil' property. To reset the debug duration, please modify the 'debugAll' property instead."); + } + } } 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 28b6e6ebc2..a5c8f46251 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,6 +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_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 bdba4549bb..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 @@ -58,8 +58,11 @@ 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.DEBUG_FAILURES) + private boolean debugFailures; + + @Column(name = ModelConstants.DEBUG__ALL_UNTIL) + private long debugAllUntil; @Column(name = ModelConstants.SINGLETON_MODE) private boolean singletonMode; @@ -83,7 +86,8 @@ public class RuleNodeEntity extends BaseSqlEntity { } this.type = ruleNode.getType(); this.name = ruleNode.getName(); - this.debugMode = ruleNode.isDebugMode(); + this.debugFailures = ruleNode.isDebugFailures(); + this.debugAllUntil = ruleNode.getDebugAllUntil(); this.singletonMode = ruleNode.isSingletonMode(); this.queueName = ruleNode.getQueueName(); this.configurationVersion = ruleNode.getConfigurationVersion(); @@ -103,7 +107,8 @@ public class RuleNodeEntity extends BaseSqlEntity { } ruleNode.setType(type); ruleNode.setName(name); - ruleNode.setDebugMode(debugMode); + 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 6f34437e0d..2b7146d48c 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 @@ -214,9 +214,13 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC } RuleChainId ruleChainId = ruleChain.getId(); if (nodes != null) { + long now = System.currentTimeMillis(); for (RuleNode node : toAddOrUpdate) { node.setRuleChainId(ruleChainId); node = ruleNodeUpdater.apply(node); + + setDebugAllUntil(tenantId, node, now); + RuleChainDataValidator.validateRuleNode(node); RuleNode savedNode = ruleNodeDao.save(tenantId, node); relations.add(new EntityRelation(ruleChainMetaData.getRuleChainId(), savedNode.getId(), @@ -261,7 +265,6 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC layout.remove("description"); layout.remove("ruleChainNodeId"); targetNode.setAdditionalInfo(layout); - targetNode.setDebugMode(false); 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 cccaf9062d..057dc44d01 100644 --- a/dao/src/main/resources/sql/schema-entities.sql +++ b/dao/src/main/resources/sql/schema-entities.sql @@ -181,7 +181,8 @@ CREATE TABLE IF NOT EXISTS rule_node ( configuration varchar(10000000), type varchar(255), name varchar(255), - debug_mode boolean, + 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 ed8a93fb63..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", - "debugMode": true, + "debugFailures": false, + "debugAll": true, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -35,7 +36,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode", "name": "Save Attributes", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 1, "configuration": { @@ -53,7 +55,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "Message Type Switch", - "debugMode": false, + "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", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -85,7 +89,8 @@ }, "type": "org.thingsboard.rule.engine.action.TbLogNode", "name": "Log Other", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -102,7 +107,8 @@ }, "type": "org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode", "name": "RPC Call Request", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -117,7 +123,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbOriginatorTypeFilterNode", "name": "Is Entity Group", - "debugMode": false, + "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", - "debugMode": false, + "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", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -169,7 +178,8 @@ }, "type": "org.thingsboard.rule.engine.profile.TbDeviceProfileNode", "name": "Device Profile Node", - "debugMode": true, + "debugFailures": false, + "debugAll": true, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -186,7 +196,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbJsFilterNode", "name": "Test JS script", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -204,7 +215,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbJsFilterNode", "name": "Test TBEL script", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -222,7 +234,8 @@ }, "type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode", "name": "Add arrival timestamp", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -240,7 +253,8 @@ }, "type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode", "name": "Calculate additional latencies", - "debugMode": true, + "debugFailures": false, + "debugAll": true, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -258,7 +272,8 @@ }, "type": "org.thingsboard.rule.engine.transform.TbChangeOriginatorNode", "name": "To latencies asset", - "debugMode": false, + "debugFailures": false, + "debugAll": false, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -287,7 +302,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "Save Timeseries", - "debugMode": true, + "debugFailures": false, + "debugAll": true, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -303,7 +319,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbCheckMessageNode", "name": "Has testData", - "debugMode": false, + "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", - "debugMode": true, + "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 7a5015add3..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", - "debugMode": true, + "debugFailures": false, + "debugAll": true, "singletonMode": true, "queueName": "HighPriority", "configurationVersion": 0, @@ -36,7 +37,8 @@ }, "type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode", "name": "save timeseries", - "debugMode": true, + "debugFailures": false, + "debugAll": true, "singletonMode": false, "configurationVersion": 0, "configuration": { @@ -54,7 +56,8 @@ }, "type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode", "name": "switch", - "debugMode": false, + "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 09178ef781..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", - "debugMode": true, + "debugFailures": false, + "debugAll": true, "configuration": { "version": 0 } @@ -20,7 +21,8 @@ }, "type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode", "name": "formResponse", - "debugMode": true, + "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", - "debugMode": true, + "debugFailures": false, + "debugAll": true, "configuration": { "requestIdMetaDataAttribute": "requestId" } diff --git a/ui-ngx/src/app/core/auth/auth.models.ts b/ui-ngx/src/app/core/auth/auth.models.ts index b73847ef83..840984b34e 100644 --- a/ui-ngx/src/app/core/auth/auth.models.ts +++ b/ui-ngx/src/app/core/auth/auth.models.ts @@ -27,6 +27,8 @@ export interface SysParamsState { mobileQrEnabled: boolean; userSettings: UserSettings; maxResourceSize: number; + maxDebugModeDurationMinutes: number; + ruleChainDebugPerTenantLimitsConfiguration?: string; } 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..20674adf8d 100644 --- a/ui-ngx/src/app/core/auth/auth.reducer.ts +++ b/ui-ngx/src/app/core/auth/auth.reducer.ts @@ -31,6 +31,7 @@ const emptyUserAuthState: AuthPayload = { persistDeviceStateToTelemetry: false, mobileQrEnabled: false, maxResourceSize: 0, + maxDebugModeDurationMinutes: 0, userSettings: initialUserSettings }; 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 new file mode 100644 index 0000000000..bdaf2de487 --- /dev/null +++ b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.html @@ -0,0 +1,46 @@ + + + + + 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 new file mode 100644 index 0000000000..c1a93d8ed9 --- /dev/null +++ b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-button.component.ts @@ -0,0 +1,102 @@ +/// +/// 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, + Renderer2, + ViewContainerRef, + DestroyRef, + ChangeDetectionStrategy, + EventEmitter, + Output +} from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { SharedModule } from '@shared/shared.module'; +import { DurationLeftPipe } from '@shared/pipe/duration-left.pipe'; +import { TbPopoverService } from '@shared/components/popover.service'; +import { MatButton } from '@angular/material/button'; +import { DebugConfigPanelComponent } from './debug-config-panel.component'; +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; +import { shareReplay, timer } from 'rxjs'; +import { SECOND } from '@shared/models/time/time.models'; +import { HasDebugConfig } from '@shared/models/entity.models'; +import { map } from 'rxjs/operators'; +import { getCurrentAuthState } from '@core/auth/auth.selectors'; +import { AppState } from '@core/core.state'; +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, + SharedModule, + DurationLeftPipe, + ], + changeDetection: ChangeDetectionStrategy.OnPush +}) +export class DebugConfigButtonComponent { + + @Input() debugFailures = false; + @Input() debugAll = false; + @Input() debugAllUntil = 0; + @Input() disabled = false; + @Input() minifyMode = false; + @Input() debugLimitsConfiguration: string; + + @Output() onDebugConfigChanged = new EventEmitter(); + + isDebugAllActive$ = timer(0, SECOND).pipe(map(() => this.debugAllUntil > new Date().getTime() || this.debugAll), shareReplay(1)); + + readonly maxDebugModeDurationMinutes = getCurrentAuthState(this.store).maxDebugModeDurationMinutes; + + constructor(private popoverService: TbPopoverService, + private renderer: Renderer2, + private store: Store, + private viewContainerRef: ViewContainerRef, + private destroyRef: DestroyRef, + ) {} + + openDebugStrategyPanel($event: Event, matButton: MatButton): void { + if ($event) { + $event.stopPropagation(); + } + const trigger = matButton._elementRef.nativeElement; + if (this.popoverService.hasPopover(trigger)) { + this.popoverService.hidePopover(trigger); + } else { + const debugStrategyPopover = this.popoverService.displayPopover(trigger, this.renderer, + this.viewContainerRef, DebugConfigPanelComponent, 'bottom', true, null, + { + debugFailures: this.debugFailures, + debugAll: this.debugAll, + debugAllUntil: this.debugAllUntil, + maxDebugModeDurationMinutes: this.maxDebugModeDurationMinutes, + debugLimitsConfiguration: this.debugLimitsConfiguration + }, + {}, + {}, {}, true); + debugStrategyPopover.tbComponentRef.instance.popover = debugStrategyPopover; + debugStrategyPopover.tbComponentRef.instance.onConfigApplied.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((config: HasDebugConfig) => { + this.onDebugConfigChanged.emit(config); + debugStrategyPopover.hide(); + }); + } + } +} 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 new file mode 100644 index 0000000000..379ad579eb --- /dev/null +++ b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-panel.component.html @@ -0,0 +1,65 @@ + +
+
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: { time: (isDebugAllActive$ | async) && !debugAll ? (debugAllUntil | durationLeft) : ('debug-config.min' | translate: { number: maxDebugModeDurationMinutes }) } }} +
+
+ +
+
+
+ + +
+
+ diff --git a/ui-ngx/src/app/modules/home/components/debug-config/debug-config-panel.component.ts b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-panel.component.ts new file mode 100644 index 0000000000..7ad88955ba --- /dev/null +++ b/ui-ngx/src/app/modules/home/components/debug-config/debug-config-panel.component.ts @@ -0,0 +1,112 @@ +/// +/// 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 { + ChangeDetectionStrategy, + ChangeDetectorRef, + Component, + EventEmitter, + Input, + OnInit +} from '@angular/core'; +import { PageComponent } from '@shared/components/page.component'; +import { TbPopoverComponent } from '@shared/components/popover.component'; +import { UntypedFormBuilder } from '@angular/forms'; +import { CommonModule } from '@angular/common'; +import { SharedModule } from '@shared/shared.module'; +import { MINUTE, SECOND } from '@shared/models/time/time.models'; +import { DurationLeftPipe } from '@shared/pipe/duration-left.pipe'; +import { shareReplay, timer } from 'rxjs'; +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; +import { HasDebugConfig } from '@shared/models/entity.models'; +import { distinctUntilChanged, map, tap } from 'rxjs/operators'; + +@Component({ + selector: 'tb-debug-config-panel', + templateUrl: './debug-config-panel.component.html', + standalone: true, + imports: [ + SharedModule, + CommonModule, + DurationLeftPipe + ], + changeDetection: ChangeDetectionStrategy.OnPush +}) +export class DebugConfigPanelComponent extends PageComponent implements OnInit { + + @Input() popover: TbPopoverComponent; + @Input() debugFailures = false; + @Input() debugAll = false; + @Input() debugAllUntil = 0; + @Input() maxDebugModeDurationMinutes: number; + @Input() debugLimitsConfiguration: string; + + onFailuresControl = this.fb.control(false); + debugAllControl = this.fb.control(false); + + maxMessagesCount: string; + maxTimeFrameSec: string; + + isDebugAllActive$ = timer(0, SECOND).pipe( + map(() => { + this.cd.markForCheck(); + return this.debugAllUntil > new Date().getTime() || this.debugAll; + }), + distinctUntilChanged(), + tap(isDebugOn => this.debugAllControl.patchValue(isDebugOn, { emitEvent: false })), + shareReplay(1), + ); + + onConfigApplied = new EventEmitter(); + + constructor(private fb: UntypedFormBuilder, private cd: ChangeDetectorRef) { + super(); + + this.observeDebugAllChange(); + } + + ngOnInit(): void { + this.maxMessagesCount = this.debugLimitsConfiguration?.split(':')[0]; + this.maxTimeFrameSec = this.debugLimitsConfiguration?.split(':')[1]; + this.onFailuresControl.patchValue(this.debugFailures); + } + + onCancel(): void { + 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.maxDebugModeDurationMinutes * MINUTE; + this.cd.markForCheck(); + } + + private observeDebugAllChange(): void { + this.debugAllControl.valueChanges.pipe(takeUntilDestroyed()).subscribe(value => { + this.debugAllUntil = value? new Date().getTime() + this.maxDebugModeDurationMinutes * MINUTE : 0; + this.debugAll = value; + this.cd.markForCheck(); + }); + } +} diff --git a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts index c8947ed55d..32244e6951 100644 --- a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts +++ b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts @@ -40,8 +40,8 @@ import { EventContentDialogData } from '@home/components/event/event-content-dialog.component'; import { isEqual, sortObjectKeys } from '@core/utils'; -import { historyInterval, MINUTE } from '@shared/models/time/time.models'; -import { ConnectedPosition, Overlay, OverlayConfig, OverlayRef } from '@angular/cdk/overlay'; +import { DAY, historyInterval, MINUTE } from '@shared/models/time/time.models'; +import { Overlay, OverlayConfig, OverlayRef } from '@angular/cdk/overlay'; import { ChangeDetectorRef, EventEmitter, Injector, StaticProvider, ViewContainerRef } from '@angular/core'; import { ComponentPortal } from '@angular/cdk/portal'; import { @@ -51,6 +51,9 @@ import { FilterEntityColumn } from '@home/components/event/event-filter-panel.component'; import { DEFAULT_OVERLAY_POSITIONS } from '@shared/models/overlay.models'; +import { getCurrentAuthState } from '@core/auth/auth.selectors'; +import { Store } from '@ngrx/store'; +import { AppState } from '@core/core.state'; export class EventTableConfig extends EntityTableConfig { @@ -59,6 +62,7 @@ export class EventTableConfig extends EntityTableConfig { private filterParams: FilterEventBody = {}; private filterColumns: FilterEntityColumn[] = []; + private readonly maxDebugModeDurationMinutes = getCurrentAuthState(this.store).maxDebugModeDurationMinutes; set eventType(eventType: EventType | DebugEventType) { if (this.eventTypeValue !== eventType) { @@ -88,13 +92,14 @@ export class EventTableConfig extends EntityTableConfig { private overlay: Overlay, private viewContainerRef: ViewContainerRef, private cd: ChangeDetectorRef, + private store: Store, public testButtonLabel?: string, private debugEventSelected?: EventEmitter) { super(); this.loadDataOnInit = false; this.tableTitle = ''; this.useTimePageLink = true; - this.defaultTimewindowInterval = historyInterval(MINUTE * 15); + this.defaultTimewindowInterval = this.maxDebugModeDurationMinutes ? historyInterval(this.maxDebugModeDurationMinutes * MINUTE) : historyInterval(DAY); this.detailsPanelEnabled = false; this.selectionEnabled = false; this.searchEnabled = false; diff --git a/ui-ngx/src/app/modules/home/components/event/event-table.component.ts b/ui-ngx/src/app/modules/home/components/event/event-table.component.ts index fbcce00581..b4c3bfc58f 100644 --- a/ui-ngx/src/app/modules/home/components/event/event-table.component.ts +++ b/ui-ngx/src/app/modules/home/components/event/event-table.component.ts @@ -36,6 +36,8 @@ import { DebugEventType, EventBody, EventType } from '@shared/models/event.model import { Overlay } from '@angular/cdk/overlay'; import { Subscription } from 'rxjs'; import { isNotEmptyStr } from '@core/utils'; +import { Store } from '@ngrx/store'; +import { AppState } from '@core/core.state'; @Component({ selector: 'tb-event-table', @@ -123,6 +125,7 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { private dialog: MatDialog, private overlay: Overlay, private viewContainerRef: ViewContainerRef, + private store: Store, private cd: ChangeDetectorRef) { } @@ -142,6 +145,7 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { this.overlay, this.viewContainerRef, this.cd, + this.store, this.functionTestButtonLabel, this.debugEventSelected ); 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..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 @@ -76,6 +76,22 @@ +
+ + tenant-profile.maximum-debug-duration-min + + + {{ 'tenant-profile.maximum-debug-duration-min-range' | translate }} + + + {{ 'tenant-profile.maximum-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..c715a978b2 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)]], + maxDebugModeDurationMinutes: [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/rule-node-details.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html index 014a73973c..5ae72a8880 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,26 @@ {{ 'rulenode.name-max-length' | translate }} -
- - {{ 'rulenode.debug-mode' | translate }} - - - {{ 'rulenode.singleton-mode' | translate }} - +
+ +
(); constructor(protected store: Store, @@ -92,7 +96,9 @@ 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)]], - debugMode: [this.ruleNode.debugMode, []], + debugAll: [this.ruleNode.debugAll], + debugFailures: [this.ruleNode.debugFailures], + debugAllUntil: [this.ruleNode.debugAllUntil], singletonMode: [this.ruleNode.singletonMode, []], configuration: [this.ruleNode.configuration, [Validators.required]], additionalInfo: this.fb.group( @@ -167,6 +173,15 @@ export class RuleNodeDetailsComponent extends PageComponent implements OnInit, O this.ruleNodeConfigComponent.validate(); } + onSingleModeChange($event: Event): void { + if ($event) { + $event.stopPropagation(); + } + const singleModeControl = this.ruleNodeFormGroup.get('singletonMode'); + singleModeControl.patchValue(!singleModeControl.value); + singleModeControl.markAsDirty(); + } + openRuleChain($event: Event) { if ($event) { $event.stopPropagation(); @@ -193,4 +208,11 @@ export class RuleNodeDetailsComponent extends PageComponent implements OnInit, O isSingletonEditAllowed() { return this.ruleNode.component.clusteringMode === ComponentClusteringMode.USER_PREFERENCE; } + + onDebugConfigChanged(config: HasDebugConfig): void { + this.ruleNodeFormGroup.get('debugAllUntil').setValue(config.debugAllUntil); + this.ruleNodeFormGroup.get('debugAll').setValue(config.debugAll); + this.ruleNodeFormGroup.get('debugFailures').setValue(config.debugFailures); + this.ruleNodeFormGroup.markAsDirty(); + } } diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts index 4425b9c039..2e0c1d95a9 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts @@ -575,7 +575,9 @@ export class RuleChainPageComponent extends PageComponent additionalInfo: ruleNode.additionalInfo, configuration: ruleNode.configuration, configurationVersion: isDefinedAndNotNull(ruleNode.configurationVersion) ? ruleNode.configurationVersion : 0, - debugMode: ruleNode.debugMode, + debugAll: ruleNode.debugAll, + debugFailures: ruleNode.debugFailures, + debugAllUntil: ruleNode.debugAllUntil, singletonMode: ruleNode.singletonMode, queueName: ruleNode.queueName, x: Math.round(ruleNode.additionalInfo.layoutX), @@ -936,7 +938,9 @@ export class RuleChainPageComponent extends PageComponent name: node.name, configuration: deepClone(node.configuration), additionalInfo: node.additionalInfo ? deepClone(node.additionalInfo) : {}, - debugMode: node.debugMode, + debugFailures: node.debugFailures, + debugAllUntil: node.debugAllUntil, + debugAll: node.debugAll, singletonMode: node.singletonMode, queueName: node.queueName }; @@ -1009,7 +1013,9 @@ export class RuleChainPageComponent extends PageComponent name: outputEdge.label, configuration: {}, additionalInfo: {}, - debugMode: false, + debugFailures: false, + debugAllUntil: 0, + debugAll: false, singletonMode: false }; outputNode.additionalInfo.layoutX = Math.round(destNode.x); @@ -1055,7 +1061,9 @@ export class RuleChainPageComponent extends PageComponent configuration: { ruleChainId: ruleChain.id.id }, - debugMode: false, + debugFailures: false, + debugAllUntil: 0, + debugAll: false, singletonMode: false, x: Math.round(ruleChainNodeX), y: Math.round(ruleChainNodeY), @@ -1475,7 +1483,9 @@ export class RuleChainPageComponent extends PageComponent : node.component.configurationVersion, configuration: node.configuration, additionalInfo: node.additionalInfo ? node.additionalInfo : {}, - debugMode: node.debugMode, + debugFailures: node.debugFailures, + debugAllUntil: node.debugAllUntil, + debugAll: node.debugAll, singletonMode: node.singletonMode, queueName: node.queueName }; diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain.module.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain.module.ts index a2a3d3995b..dc6f9149d9 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain.module.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain.module.ts @@ -32,6 +32,8 @@ import { RuleNodeDetailsComponent } from './rule-node-details.component'; import { RuleNodeLinkComponent } from './rule-node-link.component'; import { LinkLabelsComponent } from '@home/pages/rulechain/link-labels.component'; import { RuleNodeConfigComponent } from './rule-node-config.component'; +import { DurationLeftPipe } from '@shared/pipe/duration-left.pipe'; +import { DebugConfigButtonComponent } from '@home/components/debug-config/debug-config-button.component'; @NgModule({ declarations: [ @@ -59,7 +61,9 @@ import { RuleNodeConfigComponent } from './rule-node-config.component'; CommonModule, SharedModule, HomeComponentsModule, - RuleChainRoutingModule + RuleChainRoutingModule, + DurationLeftPipe, + DebugConfigButtonComponent ] }) export class RuleChainModule { } diff --git a/ui-ngx/src/app/shared/import-export/import-export.service.ts b/ui-ngx/src/app/shared/import-export/import-export.service.ts index 5ad59fc10f..7c975dec10 100644 --- a/ui-ngx/src/app/shared/import-export/import-export.service.ts +++ b/ui-ngx/src/app/shared/import-export/import-export.service.ts @@ -658,7 +658,9 @@ export class ImportExportService { if (ruleChainConnection.targetRuleChainId && ruleChainConnection.targetRuleChainId.id) { const ruleChainNode: RuleNode = { name: '', - debugMode: false, + debugFailures: false, + debugAllUntil: 0, + debugAll: false, singletonMode: false, type: 'org.thingsboard.rule.engine.flow.TbRuleChainInputNode', configuration: { diff --git a/ui-ngx/src/app/shared/models/entity.models.ts b/ui-ngx/src/app/shared/models/entity.models.ts index 51fbb8d261..50dd227b97 100644 --- a/ui-ngx/src/app/shared/models/entity.models.ts +++ b/ui-ngx/src/app/shared/models/entity.models.ts @@ -193,4 +193,10 @@ export interface HasVersion { version?: number; } -export type VersionedEntity = EntityInfoData & HasVersion | RuleChainMetaData; +export interface HasDebugConfig { + debugAll?: boolean; + debugFailures?: boolean; + debugAllUntil?: number; +} + +export type VersionedEntity = EntityInfoData & HasVersion | RuleChainMetaData; \ No newline at end of file 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 712b225743..bfc9dff775 100644 --- a/ui-ngx/src/app/shared/models/rule-node.models.ts +++ b/ui-ngx/src/app/shared/models/rule-node.models.ts @@ -27,17 +27,16 @@ import { AppState } from '@core/core.state'; import { AbstractControl, UntypedFormGroup } from '@angular/forms'; import { RuleChainType } from '@shared/models/rule-chain.models'; import { DebugRuleNodeEventBody } from '@shared/models/event.models'; -import { TranslateService } from '@ngx-translate/core'; +import { HasDebugConfig } from '@shared/models/entity.models'; export interface RuleNodeConfiguration { [key: string]: any; } -export interface RuleNode extends BaseData { +export interface RuleNode extends BaseData, HasDebugConfig { ruleChainId?: RuleChainId; type: string; name: string; - debugMode: boolean; singletonMode: boolean; queueName?: string; configurationVersion?: number; @@ -332,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; @@ -345,7 +344,6 @@ export interface FcRuleNode extends FcRuleNodeType { ruleNodeId?: RuleNodeId; additionalInfo?: any; configuration?: RuleNodeConfiguration; - debugMode?: boolean; error?: string; highlighted?: boolean; componentClazz?: string; diff --git a/ui-ngx/src/app/shared/pipe/duration-left.pipe.ts b/ui-ngx/src/app/shared/pipe/duration-left.pipe.ts new file mode 100644 index 0000000000..4aeb3842a2 --- /dev/null +++ b/ui-ngx/src/app/shared/pipe/duration-left.pipe.ts @@ -0,0 +1,35 @@ +/// +/// 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 { TranslateService } from '@ngx-translate/core'; +import { MillisecondsToTimeStringPipe } from './milliseconds-to-time-string.pipe'; + +@Pipe({ + name: 'durationLeft', + pure: false, + standalone: true, +}) +export class DurationLeftPipe implements PipeTransform { + + constructor(private translate: TranslateService, private millisecondsToTimeString: MillisecondsToTimeStringPipe) { + } + + transform(untilTimestamp: number, shortFormat = true, onlyFirstDigit = true): string { + const time = this.millisecondsToTimeString.transform((untilTimestamp - new Date().getTime()), shortFormat, onlyFirstDigit) ?? 0; + return this.translate.instant('common.time-left', { time }); + } +} 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..853890ee09 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' @@ -25,52 +26,44 @@ export class MillisecondsToTimeStringPipe implements PipeTransform { constructor(private translate: TranslateService) { } - transform(millseconds: number, shortFormat = false): string { - let seconds = Math.floor(millseconds / 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; + 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); + } + + private extractTimeUnits(milliseconds: number): { days: number; hours: number; minutes: number; seconds: number } { + 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( + 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; + 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; } - timeString += this.translate.instant('timewindow.seconds', {seconds}); } } - return timeString; + + return timeString.length > 0 ? timeString : this.translate.instant('timewindow.short.seconds', { seconds: 0 }); } } diff --git a/ui-ngx/src/app/shared/pipe/public-api.ts b/ui-ngx/src/app/shared/pipe/public-api.ts index 7c477f44c6..767ff1deee 100644 --- a/ui-ngx/src/app/shared/pipe/public-api.ts +++ b/ui-ngx/src/app/shared/pipe/public-api.ts @@ -25,3 +25,4 @@ export * from './file-size.pipe'; export * from './selectable-columns.pipe'; export * from './image.pipe'; export * from './key-value-not-empty.pipe'; +export * from './duration-left.pipe'; 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 405f4ae9d3..f19a781b8f 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -988,6 +988,20 @@ "type-timeseries-deleted": "Telemetry deleted", "type-sms-sent": "SMS sent" }, + "debug-config": { + "min": "{{number}} min", + "label": "Debug configuration", + "on-failure": "Failures only (24/7)", + "all-messages": "All messages ({{time}})", + "failures": "Failures", + "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." + } + }, "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?", @@ -1018,11 +1032,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", + "time-left": "{{time}} left" }, "content-type": { "json": "Json", @@ -4175,7 +4191,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", @@ -4453,6 +4469,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-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", 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 346941d4d1..c394a29826 100644 --- a/ui-ngx/src/styles.scss +++ b/ui-ngx/src/styles.scss @@ -1270,6 +1270,24 @@ pre.tb-highlight { .no-wrap { white-space: nowrap; } + + .tb-rounded-btn { + border-radius: 20px; + padding: 0 16px; + + &:not(.active, :disabled) { + color: rgba(0, 0, 0, 0.76); + background-color: rgba(0, 0, 0, 0.06); + } + + &.active:not(:disabled) { + --mdc-outlined-button-outline-color: $primary; + } + + &:disabled { + background: rgba(0, 0, 0, 0.06); + } + } } /***************