<commit_before>import {SquarifiedValuedCodeMapNode, TreeMapSettings} from "./treemap.service";
import {node} from "../../ui/codeMap/rendering/node";

export class TreeMapUtils {

    public static countNodes(node: { children?: any }): number {
        let count = 1;
        if (node.children && node.children.length > 0) {
            for (let i = 0; i < node.children.length; i++) {
                count += TreeMapUtils.countNodes(node.children[i]);
            }
        }
        return count;
    }

    public static isNodeLeaf(node: { children?: any }): boolean {
        return !(node.children && node.children.length > 0);
    }

    public static buildNodeFrom(squaredNode: SquarifiedValuedCodeMapNode,
                                heightScale: number,
                                heightValue: number,
                                maxHeight: number,
                                depth: number,
                                parent: node,
                                s: TreeMapSettings,
                                minHeight: number,
                                folderHeight: number): node {

        if (s.invertHeight) {
            heightValue = (maxHeight - heightValue);
        }

        const flattened = TreeMapUtils.isNodeToBeFlat(squaredNode, s);
        if (flattened) {
            heightValue = minHeight;
        }

        return {
            name: squaredNode.data.name,
            width: squaredNode.x1 - squaredNode.x0,
            height: Math.abs(TreeMapUtils.isNodeLeaf(squaredNode) ? Math.max(heightScale * heightValue, minHeight) : folderHeight),
            length: squaredNode.y1 - squaredNode.y0,
            depth: depth,
            x0: squaredNode.x0,
            z0: depth * folderHeight,
            y0: squaredNode.y0,
            isLeaf: TreeMapUtils.isNodeLeaf(squaredNode),
            attributes: squaredNode.data.attributes,
            deltas: squaredNode.data.deltas,
            parent: parent,
            heightDelta: squaredNode.data.deltas && squaredNode.data.deltas[s.heightKey] ? heightScale * squaredNode.data.deltas[s.heightKey] : 0,
            visible: squaredNode.data.visible && !(TreeMapUtils.isNodeLeaf(squaredNode) && s.hideFlatBuildings && flattened),
            path: squaredNode.data.path,
            origin: squaredNode.data.origin,
            link: squaredNode.data.link,
            children: [],
            markingColor: parseInt(squaredNode.data.markingColor),
            flat: flattened,
        };

    }

    private static isNodeToBeFlat(squaredNode: SquarifiedValuedCodeMapNode, s: TreeMapSettings): boolean {

        let flattened = false;
        if (s.visibleEdges && s.visibleEdges.length > 0) {
            flattened = this.nodeHasNoVisibleEdges(squaredNode, s);
        }

        if (s.searchedNodePaths && s.searchPattern && s.searchPattern.length > 0) {
            flattened = (s.searchedNodePaths.length == 0) ? true : this.isNodeNonSearched(squaredNode, s);
        }
        return flattened;
    }

    private static nodeHasNoVisibleEdges(squaredNode: SquarifiedValuedCodeMapNode, s: TreeMapSettings): boolean {
        return s.visibleEdges.filter(edge =>
            squaredNode.data.path === edge.fromNodeName ||
            squaredNode.data.path === edge.toNodeName).length == 0;
    }

    private static isNodeNonSearched(squaredNode: SquarifiedValuedCodeMapNode, s: TreeMapSettings): boolean {
        return s.searchedNodePaths.filter(path =>
            path == squaredNode.data.path).length == 0;
    }
}<commit_msg>WIP: Set markingColor in treeMap.utils node #337<commit_after>import {SquarifiedValuedCodeMapNode, TreeMapSettings} from "./treemap.service";
import {node} from "../../ui/codeMap/rendering/node";
import {MarkedPackage} from "../settings/settings.service";

export class TreeMapUtils {

    public static countNodes(node: { children?: any }): number {
        let count = 1;
        if (node.children && node.children.length > 0) {
            for (let i = 0; i < node.children.length; i++) {
                count += TreeMapUtils.countNodes(node.children[i]);
            }
        }
        return count;
    }

    public static isNodeLeaf(node: { children?: any }): boolean {
        return !(node.children && node.children.length > 0);
    }

    public static buildNodeFrom(squaredNode: SquarifiedValuedCodeMapNode,
                                heightScale: number,
                                heightValue: number,
                                maxHeight: number,
                                depth: number,
                                parent: node,
                                s: TreeMapSettings,
                                minHeight: number,
                                folderHeight: number): node {

        if (s.invertHeight) {
            heightValue = (maxHeight - heightValue);
        }

        const flattened = TreeMapUtils.isNodeToBeFlat(squaredNode, s);
        if (flattened) {
            heightValue = minHeight;
        }

        return {
            name: squaredNode.data.name,
            width: squaredNode.x1 - squaredNode.x0,
            height: Math.abs(TreeMapUtils.isNodeLeaf(squaredNode) ? Math.max(heightScale * heightValue, minHeight) : folderHeight),
            length: squaredNode.y1 - squaredNode.y0,
            depth: depth,
            x0: squaredNode.x0,
            z0: depth * folderHeight,
            y0: squaredNode.y0,
            isLeaf: TreeMapUtils.isNodeLeaf(squaredNode),
            attributes: squaredNode.data.attributes,
            deltas: squaredNode.data.deltas,
            parent: parent,
            heightDelta: squaredNode.data.deltas && squaredNode.data.deltas[s.heightKey] ? heightScale * squaredNode.data.deltas[s.heightKey] : 0,
            visible: squaredNode.data.visible && !(TreeMapUtils.isNodeLeaf(squaredNode) && s.hideFlatBuildings && flattened),
            path: squaredNode.data.path,
            origin: squaredNode.data.origin,
            link: squaredNode.data.link,
            children: [],
            markingColor: parseInt(this.getMarkingColor(squaredNode, s)),
            flat: flattened,
        };

    }

    private static isNodeToBeFlat(squaredNode: SquarifiedValuedCodeMapNode, s: TreeMapSettings): boolean {

        let flattened = false;
        if (s.visibleEdges && s.visibleEdges.length > 0) {
            flattened = this.nodeHasNoVisibleEdges(squaredNode, s);
        }

        if (s.searchedNodePaths && s.searchPattern && s.searchPattern.length > 0) {
            flattened = (s.searchedNodePaths.length == 0) ? true : this.isNodeNonSearched(squaredNode, s);
        }
        return flattened;
    }

    private static nodeHasNoVisibleEdges(squaredNode: SquarifiedValuedCodeMapNode, s: TreeMapSettings): boolean {
        return s.visibleEdges.filter(edge =>
            squaredNode.data.path === edge.fromNodeName ||
            squaredNode.data.path === edge.toNodeName).length == 0;
    }

    private static isNodeNonSearched(squaredNode: SquarifiedValuedCodeMapNode, s: TreeMapSettings): boolean {
        return s.searchedNodePaths.filter(path =>
            path == squaredNode.data.path).length == 0;
    }

    private static getMarkingColor(squaredNode: SquarifiedValuedCodeMapNode, s: TreeMapSettings): string {
        let markingColor = "";

        if (!this.isNodeLeaf(squaredNode)) {
            let markedParentPackages = s.markedPackages.filter(mp => squaredNode.data.path.includes(mp.path));

            if (markedParentPackages.length > 0) {
                let sortedMarkedParentPackages = markedParentPackages.sort((a, b) => a.path.length > b.path.length ? 0 : 1);
                console.log(
                    squaredNode.data.path,
                    s.markedPackages,
                    markedParentPackages,
                    sortedMarkedParentPackages,
                    sortedMarkedParentPackages[0],
                );
                markingColor = sortedMarkedParentPackages[0].color;
            }
        }
        return markingColor;
    }
}
{'fraction_non_alphanumeric': 0.10096510764662213, 'fraction_numerical': 0.0056916604800791885, 'mean_word_length': 1.9695077149155034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2649898', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2189, 'n_words': 427}