Search in sources :

Example 1 with RuleChainOutputLabelsUsage

use of org.thingsboard.server.common.data.rule.RuleChainOutputLabelsUsage in project thingsboard by thingsboard.

the class DefaultTbRuleChainService method getOutputLabelUsage.

@Override
public List<RuleChainOutputLabelsUsage> getOutputLabelUsage(TenantId tenantId, RuleChainId ruleChainId) {
    List<RuleNode> ruleNodes = ruleChainService.findRuleNodesByTenantIdAndType(tenantId, TbRuleChainInputNode.class.getName(), ruleChainId.getId().toString());
    Map<RuleChainId, String> ruleChainNamesCache = new HashMap<>();
    // Additional filter, "just in case" the structure of the JSON configuration will change.
    var filteredRuleNodes = ruleNodes.stream().filter(node -> {
        try {
            TbRuleChainInputNodeConfiguration configuration = JacksonUtil.treeToValue(node.getConfiguration(), TbRuleChainInputNodeConfiguration.class);
            return ruleChainId.getId().toString().equals(configuration.getRuleChainId());
        } catch (Exception e) {
            log.warn("[{}][{}] Failed to decode rule node configuration", tenantId, ruleChainId, e);
            return false;
        }
    }).collect(Collectors.toList());
    return filteredRuleNodes.stream().map(ruleNode -> {
        RuleChainOutputLabelsUsage usage = new RuleChainOutputLabelsUsage();
        usage.setRuleNodeId(ruleNode.getId());
        usage.setRuleNodeName(ruleNode.getName());
        usage.setRuleChainId(ruleNode.getRuleChainId());
        List<EntityRelation> relations = ruleChainService.getRuleNodeRelations(tenantId, ruleNode.getId());
        if (relations != null && !relations.isEmpty()) {
            usage.setLabels(relations.stream().map(EntityRelation::getType).collect(Collectors.toSet()));
        }
        return usage;
    }).filter(usage -> usage.getLabels() != null).peek(usage -> {
        String ruleChainName = ruleChainNamesCache.computeIfAbsent(usage.getRuleChainId(), id -> ruleChainService.findRuleChainById(tenantId, id).getName());
        usage.setRuleChainName(ruleChainName);
    }).sorted(Comparator.comparing(RuleChainOutputLabelsUsage::getRuleChainName).thenComparing(RuleChainOutputLabelsUsage::getRuleNodeName)).collect(Collectors.toList());
}
Also used : RequiredArgsConstructor(lombok.RequiredArgsConstructor) JacksonUtil(org.thingsboard.common.util.JacksonUtil) TbRuleChainInputNode(org.thingsboard.rule.engine.flow.TbRuleChainInputNode) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) HashMap(java.util.HashMap) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChainUpdateResult(org.thingsboard.server.common.data.rule.RuleChainUpdateResult) RuleChainService(org.thingsboard.server.dao.rule.RuleChainService) TreeSet(java.util.TreeSet) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RelationService(org.thingsboard.server.dao.relation.RelationService) HashSet(java.util.HashSet) RuleNodeUpdateResult(org.thingsboard.server.common.data.rule.RuleNodeUpdateResult) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) Service(org.springframework.stereotype.Service) Map(java.util.Map) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) Set(java.util.Set) Collectors(java.util.stream.Collectors) TbRuleChainOutputNode(org.thingsboard.rule.engine.flow.TbRuleChainOutputNode) RuleChainOutputLabelsUsage(org.thingsboard.server.common.data.rule.RuleChainOutputLabelsUsage) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Comparator(java.util.Comparator) Collections(java.util.Collections) RuleChainOutputLabelsUsage(org.thingsboard.server.common.data.rule.RuleChainOutputLabelsUsage) HashMap(java.util.HashMap) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) TbRuleChainInputNode(org.thingsboard.rule.engine.flow.TbRuleChainInputNode)

Example 2 with RuleChainOutputLabelsUsage

use of org.thingsboard.server.common.data.rule.RuleChainOutputLabelsUsage in project thingsboard by thingsboard.

the class DefaultTbRuleChainService method updateRelatedRuleChains.

public Set<RuleChainId> updateRelatedRuleChains(TenantId tenantId, RuleChainId ruleChainId, Map<String, String> labelsMap) {
    Set<RuleChainId> updatedRuleChains = new HashSet<>();
    List<RuleChainOutputLabelsUsage> usageList = getOutputLabelUsage(tenantId, ruleChainId);
    for (RuleChainOutputLabelsUsage usage : usageList) {
        labelsMap.forEach((oldLabel, newLabel) -> {
            if (usage.getLabels().contains(oldLabel)) {
                updatedRuleChains.add(usage.getRuleChainId());
                renameOutgoingLinks(tenantId, usage.getRuleNodeId(), oldLabel, newLabel);
            }
        });
    }
    return updatedRuleChains;
}
Also used : RuleChainOutputLabelsUsage(org.thingsboard.server.common.data.rule.RuleChainOutputLabelsUsage) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)2 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)2 RuleChainOutputLabelsUsage (org.thingsboard.server.common.data.rule.RuleChainOutputLabelsUsage)2 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Service (org.springframework.stereotype.Service)1 JacksonUtil (org.thingsboard.common.util.JacksonUtil)1 TbRuleChainInputNode (org.thingsboard.rule.engine.flow.TbRuleChainInputNode)1 TbRuleChainInputNodeConfiguration (org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration)1 TbRuleChainOutputNode (org.thingsboard.rule.engine.flow.TbRuleChainOutputNode)1 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1