Search in sources :

Example 6 with HierarchyNode

use of com.linkedin.thirdeye.client.diffsummary.HierarchyNode in project pinot by linkedin.

the class Summary method rollbackInsertions.

private static void rollbackInsertions(HierarchyNode node, Set<HierarchyNode> answer, List<HierarchyNode> removedNodes) {
    // Rollback from top to bottom nodes
    removedNodes.sort(NODE_COMPARATOR.reversed());
    Set<HierarchyNode> targetSet = new HashSet<>(answer);
    targetSet.addAll(removedNodes);
    for (HierarchyNode removedNode : removedNodes) {
        HierarchyNode parents = findAncestor(removedNode, node, targetSet);
        if (parents != null)
            parents.removeNodeValues(removedNode);
    }
    node.resetValues();
}
Also used : HashSet(java.util.HashSet) HierarchyNode(com.linkedin.thirdeye.client.diffsummary.HierarchyNode)

Example 7 with HierarchyNode

use of com.linkedin.thirdeye.client.diffsummary.HierarchyNode in project pinot by linkedin.

the class Summary method mergeDPArray.

/**
   * Merge the answers of the two given DPArrays. The merged answer is put in the DPArray at the left hand side.
   * After merging, the baseline and current values of the removed nodes (rows) will be add back to those of their
   * parent node.
   */
private Set<HierarchyNode> mergeDPArray(HierarchyNode parentNode, DPArray parentArray, DPArray childArray) {
    Set<HierarchyNode> removedNodes = new HashSet<>(parentArray.getAnswer());
    removedNodes.addAll(childArray.getAnswer());
    // Compute the merged answer
    double targetRatio = (parentArray.targetRatio + childArray.targetRatio) / 2.;
    recomputeCostAndRemoveSmallNodes(parentNode, parentArray, targetRatio);
    List<HierarchyNode> childNodeList = new ArrayList<>(childArray.getAnswer());
    childNodeList.sort(NODE_COMPARATOR);
    for (HierarchyNode childNode : childNodeList) {
        insertRowWithAdaptiveRatio(parentArray, childNode, targetRatio);
    }
    // Update an internal node's baseline and current value if any of its child is removed due to the merge
    removedNodes.removeAll(parentArray.getAnswer());
    updateWowValuesDueToRemoval(parentNode, parentArray.getAnswer(), removedNodes);
    return removedNodes;
}
Also used : ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HierarchyNode(com.linkedin.thirdeye.client.diffsummary.HierarchyNode)

Example 8 with HierarchyNode

use of com.linkedin.thirdeye.client.diffsummary.HierarchyNode in project pinot by linkedin.

the class SummaryResponse method build.

public void build(List<HierarchyNode> nodes, int targetLevelCount, List<DimNameValueCostEntry> costSet) {
    // Compute the total baseline and current value
    for (HierarchyNode node : nodes) {
        totalBaselineValue += node.getBaselineValue();
        totalCurrentValue += node.getCurrentValue();
    }
    this.buildGainerLoserGroup(costSet);
    // If all nodes have a lower level count than targetLevelCount, then it is not necessary to print the summary with
    // height higher than the available level.
    int maxNodeLevelCount = 0;
    for (HierarchyNode node : nodes) {
        maxNodeLevelCount = Math.max(maxNodeLevelCount, node.getLevel());
    }
    targetLevelCount = Math.min(maxNodeLevelCount, targetLevelCount);
    // Build the header
    Dimensions dimensions = nodes.get(0).getDimensions();
    for (int i = 0; i < targetLevelCount; ++i) {
        this.dimensions.add(dimensions.get(i));
    }
    // Build the response
    nodes = SummaryResponseTree.sortResponseTree(nodes, targetLevelCount);
    //   Build name tag for each row of responses
    Map<HierarchyNode, NameTag> nameTags = new HashMap<>();
    Map<HierarchyNode, List<String>> otherDimensionValues = new HashMap<>();
    for (HierarchyNode node : nodes) {
        NameTag tag = new NameTag(targetLevelCount);
        nameTags.put(node, tag);
        tag.copyNames(node.getDimensionValues());
        otherDimensionValues.put(node, new ArrayList<>());
    }
    //   pre-condition: parent node is processed before its children nodes
    for (HierarchyNode node : nodes) {
        HierarchyNode parent = node;
        int levelDiff = 1;
        while ((parent = parent.getParent()) != null) {
            NameTag parentNameTag = nameTags.get(parent);
            if (parentNameTag != null) {
                // Set tag from ALL to NOT_ALL String.
                int notAllLevel = node.getLevel() - levelDiff;
                parentNameTag.setNotAll(notAllLevel);
                // For users' ease of understanding, we append what dimension values are excluded from NOT_ALL
                StringBuilder sb = new StringBuilder();
                String separator = "";
                for (int i = notAllLevel; i < node.getDimensionValues().size(); ++i) {
                    sb.append(separator).append(node.getDimensionValues().get(i));
                    separator = ".";
                }
                otherDimensionValues.get(parent).add(sb.toString());
                break;
            }
            ++levelDiff;
        }
    }
    //    Fill in the information of each response row
    for (HierarchyNode node : nodes) {
        SummaryResponseRow row = new SummaryResponseRow();
        row.names = nameTags.get(node).names;
        row.baselineValue = node.getBaselineValue();
        row.currentValue = node.getCurrentValue();
        row.percentageChange = computePercentageChange(row.baselineValue, row.currentValue);
        row.contributionChange = computeContributionChange(row.baselineValue, row.currentValue, totalBaselineValue, totalCurrentValue);
        row.contributionToOverallChange = computeContributionToOverallChange(row.baselineValue, row.currentValue, totalBaselineValue);
        StringBuilder sb = new StringBuilder();
        String separator = "";
        for (String s : otherDimensionValues.get(node)) {
            sb.append(separator).append(s);
            separator = ", ";
        }
        row.otherDimensionValues = sb.toString();
        this.responseRows.add(row);
    }
}
Also used : ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) HashMap(java.util.HashMap) Dimensions(com.linkedin.thirdeye.client.diffsummary.Dimensions) ArrayList(java.util.ArrayList) List(java.util.List) HierarchyNode(com.linkedin.thirdeye.client.diffsummary.HierarchyNode)

Example 9 with HierarchyNode

use of com.linkedin.thirdeye.client.diffsummary.HierarchyNode in project pinot by linkedin.

the class SummaryResponseTree method sortResponseTree.

public static List<HierarchyNode> sortResponseTree(List<HierarchyNode> nodes, int levelCount) {
    SummaryResponseTree responseTree = new SummaryResponseTree();
    // Build the header
    Dimensions dimensions = nodes.get(0).getDimensions();
    double totalValue = nodes.get(0).getOriginalCurrentValue() + nodes.get(0).getOriginalBaselineValue();
    for (int i = 0; i < levelCount; ++i) {
        responseTree.dimensions.add(dimensions.get(i));
    }
    List<SummaryResponseTreeNode> treeNodes = new ArrayList<>();
    // Build the response tree
    // pre-order traversal
    nodes.sort(Summary.NODE_COMPARATOR.reversed());
    for (HierarchyNode node : nodes) {
        SummaryResponseTreeNode treeNode = new SummaryResponseTreeNode();
        treeNode.hierarchyNode = node;
        treeNode.level = node.getLevel();
        treeNodes.add(treeNode);
    }
    //    Connecting child and parent response tree node. Note: response tree is not a perfect tree like the tree
    //    of HierarchyNodes, because in response tree a node's direct parent may be missing.
    //    In that case, we have to bootstrap the search until a higher level parent, which also exists in the response
    //    tree, is found.
    //    Pre-condition: treeNodes are sorted in the pre-order fashion when projecting the nodes back to the tree of
    //                   HierarchyNode.
    SummaryResponseTreeNode preTreeNode = null;
    for (SummaryResponseTreeNode treeNode : treeNodes) {
        if (preTreeNode != null) {
            SummaryResponseTreeNode parent = preTreeNode.getCommonParent(treeNode.getDimensionValues());
            treeNode.parent = parent;
            parent.children.add(treeNode);
        }
        preTreeNode = treeNode;
    }
    // Sort the children of each node by their cost
    sortChildNodes(treeNodes.get(0), totalValue);
    // Put the nodes to a flattened array
    insertChildNodes(treeNodes.get(0), responseTree.hierarchicalNodes);
    return responseTree.hierarchicalNodes;
}
Also used : ArrayList(java.util.ArrayList) Dimensions(com.linkedin.thirdeye.client.diffsummary.Dimensions) HierarchyNode(com.linkedin.thirdeye.client.diffsummary.HierarchyNode)

Aggregations

HierarchyNode (com.linkedin.thirdeye.client.diffsummary.HierarchyNode)9 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)4 Dimensions (com.linkedin.thirdeye.client.diffsummary.Dimensions)2 HashMap (java.util.HashMap)1 List (java.util.List)1 ToStringBuilder (org.apache.commons.lang3.builder.ToStringBuilder)1