All files / app/codeCharta/util/algorithm/treeMapLayout treeMapHelper.ts

98.23% Statements 111/113
93.82% Branches 76/81
100% Functions 16/16
98.21% Lines 110/112

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 27859x 59x 59x     59x 59x 59x         59x   59x 59x 59x     9x 9x 4x 5x     9x         26x   26x 1x 3x     26x       6x   6x 4x     6x       3x 3x 3x 3x   3x                                                                 380x 380x 380x 380x 380x 380x 380x 380x 380x 380x 380x 380x   380x                                                     59x 391x   391x 4x     387x 387x   387x 387x 387x   387x 1x     1x   386x 133x   253x     59x 294x 294x     59x 394x 5x     389x 30x     359x     59x 405x 34x   371x     59x 383x 34x   349x     59x 394x 2x     392x   392x 2x     390x 1x     389x       1x   1x         1x 1x     59x             394x   394x 15x   379x   379x 306x   73x 1x     72x   72x       72x 5x 1x   4x 2x   2x     67x 8x     59x 7x     52x     59x                          
import { getMapResolutionScaleFactor, getMarkingColor, isLeaf } from "../../codeMapHelper"
import { CcState, CodeMapNode, ColorMode, Node } from "../../../codeCharta.model"
import { Vector3 } from "three"
import { CodeMapBuilding } from "../../../ui/codeMap/rendering/codeMapBuilding"
import { HierarchyRectangularNode } from "d3-hierarchy"
import { searchedNodePathsSelector } from "../../../state/selectors/searchedNodes/searchedNodePaths.selector"
import { gradientCalculator } from "../../color/gradientCalculator"
import {
    MetricMinMax,
    selectedColorMetricDataSelector
} from "../../../state/selectors/accumulatedData/metricData/selectedColorMetricData.selector"
 
export const treeMapSize = 250
 
const FOLDER_HEIGHT = 2
const MIN_BUILDING_HEIGHT = 2
const HEIGHT_VALUE_WHEN_METRIC_NOT_FOUND = 0
 
function countNodes(node: { children?: CodeMapNode[] }) {
    let count = 1
    if (node.children) {
        for (const child of node.children) {
            count += countNodes(child)
        }
    }
    return count
}
 
// TODO this function exists twice in the code - please refactor it.
function calculateSize(node: CodeMapNode, metricName: string) {
    let totalSize = node.attributes[metricName] || 0
 
    if (totalSize === 0 && node.children && node.children.length > 0) {
        for (const child of node.children) {
            totalSize += calculateSize(child, metricName)
        }
    }
    return totalSize
}
 
function buildingArrayToMap(highlighted: CodeMapBuilding[]) {
    const geomMap: Map<number, CodeMapBuilding> = new Map()
 
    for (const building of highlighted) {
        geomMap.set(building.id, building)
    }
 
    return geomMap
}
 
function buildRootFolderForFixedFolders(map: CodeMapNode, heightScale: number, state: CcState, isDeltaState: boolean) {
    const flattened = isNodeFlat(map, state)
    const height = FOLDER_HEIGHT
    const width = 100
    const length = 100
 
    return {
        name: map.name,
        id: 0,
        width,
        height,
        length,
        depth: 0,
        x0: 0,
        z0: 0,
        y0: 0,
        isLeaf: false,
        attributes: map.attributes,
        edgeAttributes: map.edgeAttributes,
        deltas: map.deltas,
        heightDelta: (map.deltas?.[state.dynamicSettings.heightMetric] ?? 0) * heightScale,
        visible: isVisible(map, false, state, flattened),
        path: map.path,
        link: map.link,
        markingColor: getMarkingColor(map, state.fileSettings.markedPackages),
        flat: false,
        color: getBuildingColor(map, state, selectedColorMetricDataSelector(state), isDeltaState, flattened),
        incomingEdgePoint: getIncomingEdgePoint(width, height, length, new Vector3(0, 0, 0), treeMapSize),
        outgoingEdgePoint: getOutgoingEdgePoint(width, height, length, new Vector3(0, 0, 0), treeMapSize)
    } as Node
}
 
function buildNodeFrom(
    squaredNode: HierarchyRectangularNode<CodeMapNode>,
    heightScale: number,
    maxHeight: number,
    state: CcState,
    isDeltaState: boolean
): Node {
    const mapSizeResolutionScaling = getMapResolutionScaleFactor(state.files)
    const { x0, x1, y0, y1, data } = squaredNode
    const isNodeLeaf = isLeaf(squaredNode)
    const flattened = isNodeFlat(data, state)
    const heightValue = getHeightValue(state, data, maxHeight, flattened)
    const depth = data.path.split("/").length - 2
    const height = isNodeLeaf ? resolveHeightValue(heightValue, heightScale, data, state) * mapSizeResolutionScaling : FOLDER_HEIGHT
    const width = x1 - x0
    const length = y1 - y0
    const z0 = squaredNode.depth * FOLDER_HEIGHT
    const heightDelta = (data.deltas?.[state.dynamicSettings.heightMetric] ?? 0) * heightScale * mapSizeResolutionScaling
    const edgePointHeight = height + (heightDelta < 0 ? Math.abs(heightDelta) : 0)
 
    return {
        name: data.name,
        id: data.id,
        width,
        height,
        length,
        depth,
        mapNodeDepth: squaredNode.depth,
        x0,
        z0,
        y0,
        isLeaf: isNodeLeaf,
        attributes: data.attributes,
        edgeAttributes: data.edgeAttributes,
        deltas: data.deltas,
        heightDelta,
        visible: isVisible(data, isNodeLeaf, state, flattened),
        path: data.path,
        link: data.link,
        markingColor: getMarkingColor(data, state.fileSettings.markedPackages),
        flat: flattened,
        color: getBuildingColor(data, state, selectedColorMetricDataSelector(state), isDeltaState, flattened),
        incomingEdgePoint: getIncomingEdgePoint(width, edgePointHeight, length, new Vector3(x0, z0, y0), treeMapSize),
        outgoingEdgePoint: getOutgoingEdgePoint(width, edgePointHeight, length, new Vector3(x0, z0, y0), treeMapSize)
    }
}
 
export function getHeightValue(state: CcState, squaredNode: CodeMapNode, maxHeight: number, flattened: boolean) {
    const mapSizeResolutionScaling = getMapResolutionScaleFactor(state.files)
 
    if (flattened) {
        return MIN_BUILDING_HEIGHT
    }
 
    let heightValue = squaredNode.attributes[state.dynamicSettings.heightMetric] || HEIGHT_VALUE_WHEN_METRIC_NOT_FOUND
    heightValue *= mapSizeResolutionScaling
 
    const heightMetric = state.dynamicSettings.heightMetric
    const attributeDescriptors = state.fileSettings.attributeDescriptors
    const isAttributeDirectionInversed = attributeDescriptors[heightMetric]?.direction === 1
 
    if (isAttributeDirectionInversed) {
        Iif (state.appSettings.invertHeight) {
            return heightValue
        }
        return maxHeight - heightValue
    }
    if (state.appSettings.invertHeight) {
        return maxHeight - heightValue
    }
    return heightValue
}
 
export function resolveHeightValue(heightValue: number, heightScale: number, data: CodeMapNode, state: CcState): number {
    const minimalHeight = data.deltas?.[state.dynamicSettings.heightMetric] ? 0 : MIN_BUILDING_HEIGHT
    return Math.max(Math.abs(heightScale * heightValue), minimalHeight)
}
 
export function isVisible(squaredNode: CodeMapNode, isNodeLeaf: boolean, state: CcState, flattened: boolean) {
    if (squaredNode.isExcluded || (isNodeLeaf && state.appSettings.hideFlatBuildings && flattened)) {
        return false
    }
 
    if (state.dynamicSettings.focusedNodePath.length > 0) {
        return squaredNode.path.startsWith(state.dynamicSettings.focusedNodePath[0])
    }
 
    return true
}
 
export function getIncomingEdgePoint(width: number, height: number, length: number, vector: Vector3, mapSize: number) {
    if (width > length) {
        return new Vector3(vector.x - mapSize + width / 4, vector.y + height, vector.z - mapSize + length / 2)
    }
    return new Vector3(vector.x - mapSize + width / 2, vector.y + height, vector.z - mapSize + length / 4)
}
 
export function getOutgoingEdgePoint(width: number, height: number, length: number, vector: Vector3, mapSize: number) {
    if (width > length) {
        return new Vector3(vector.x - mapSize + 0.75 * width, vector.y + height, vector.z - mapSize + length / 2)
    }
    return new Vector3(vector.x - mapSize + width / 2, vector.y + height, vector.z - mapSize + 0.75 * length)
}
 
export function isNodeFlat(codeMapNode: CodeMapNode, state: CcState) {
    if (codeMapNode.isFlattened) {
        return true
    }
 
    const searchedNodePaths = searchedNodePathsSelector(state)
 
    if (searchedNodePaths && state.dynamicSettings.searchPattern?.length > 0) {
        return searchedNodePaths.size === 0 || isNodeNonSearched(codeMapNode, state)
    }
 
    if (state.appSettings.showOnlyBuildingsWithEdges && state.fileSettings.edges.some(edge => edge.visible)) {
        return nodeHasNoVisibleEdges(codeMapNode, state)
    }
 
    return false
}
 
function nodeHasNoVisibleEdges(codeMapNode: CodeMapNode, state: CcState) {
    return (
        codeMapNode.edgeAttributes[state.dynamicSettings.edgeMetric] === undefined ||
        !state.fileSettings.edges.some(edge => codeMapNode.path === edge.fromNodeName || codeMapNode.path === edge.toNodeName)
    )
}
 
function isNodeNonSearched(squaredNode: CodeMapNode, state: CcState) {
    const searchedNodePaths = searchedNodePathsSelector(state)
    return !searchedNodePaths.has(squaredNode.path)
}
 
export function getBuildingColor(
    node: CodeMapNode,
    { appSettings, dynamicSettings }: CcState,
    nodeMetricDataRange: MetricMinMax,
    isDeltaState: boolean,
    flattened: boolean
) {
    const { mapColors } = appSettings
 
    if (isDeltaState) {
        return mapColors.base
    }
    const metricValue = node.attributes[dynamicSettings.colorMetric]
 
    if (metricValue === undefined) {
        return mapColors.base
    }
    if (flattened) {
        return mapColors.flat
    }
 
    const { colorRange, colorMode } = dynamicSettings
 
    Iif (dynamicSettings["colorMetric"] === "unary") {
        return mapColors.positive
    }
 
    if (colorMode === ColorMode.absolute) {
        if (metricValue < colorRange.from || colorRange.from === nodeMetricDataRange.maxValue) {
            return mapColors.positive
        }
        if (metricValue < colorRange.to || colorRange.to === nodeMetricDataRange.maxValue) {
            return mapColors.neutral
        }
        return mapColors.negative
    }
 
    if (colorMode === ColorMode.trueGradient) {
        return gradientCalculator.getColorByTrueGradient(mapColors, colorRange, nodeMetricDataRange, metricValue)
    }
 
    if (colorMode === ColorMode.focusedGradient) {
        return gradientCalculator.getColorByFocusedGradient(mapColors, colorRange, nodeMetricDataRange, metricValue)
    }
 
    return gradientCalculator.getColorByWeightedGradient(mapColors, colorRange, nodeMetricDataRange, metricValue)
}
 
export const TreeMapHelper = {
    countNodes,
    buildingArrayToMap,
    buildRootFolderForFixedFolders,
    calculateSize,
    buildNodeFrom,
    isNodeFlat,
    resolveHeightValue,
    getHeightValue,
    FOLDER_HEIGHT,
    MIN_BUILDING_HEIGHT,
    HEIGHT_VALUE_WHEN_METRIC_NOT_FOUND
}