All files / app/codeCharta/state/selectors/accumulatedData/metricData nodeMetricData.calculator.ts

100% Statements 31/31
100% Branches 12/12
100% Functions 1/1
100% Lines 30/30

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 61139x       139x 139x   139x   139x 31x 23x     8x 8x 8x   8x 8x 26x 14x 42x 42x   42x 24x   42x   42x 30x     42x 34x             8x     8x 8x   8x 31x               8x 8x    
import { hierarchy } from "d3-hierarchy"
 
import { BlacklistItem, NodeMetricData } from "../../../../codeCharta.model"
import { FileState } from "../../../../model/files/files"
import { isLeaf, isPathBlacklisted } from "../../../../util/codeMapHelper"
import { sortByMetricName } from "./sortByMetricName"
 
export const UNARY_METRIC = "unary"
 
export const calculateNodeMetricData = (visibleFileStates: FileState[], blacklist: BlacklistItem[]) => {
    if (visibleFileStates.length === 0) {
        return []
    }
 
    const metricValues: Map<string, number[]> = new Map()
    const metricMaxValues: Map<string, number> = new Map()
    const metricMinValues: Map<string, number> = new Map()
 
    for (const { file } of visibleFileStates) {
        for (const node of hierarchy(file.map)) {
            if (isLeaf(node) && node.data.path && !isPathBlacklisted(node.data.path, blacklist, "exclude")) {
                for (const metric of Object.keys(node.data.attributes)) {
                    const maxValue = metricMaxValues.get(metric)
                    const minValue = metricMinValues.get(metric)
 
                    if (!metricValues.get(metric)) {
                        metricValues.set(metric, [])
                    }
                    metricValues.get(metric).push(node.data.attributes[metric])
 
                    if (minValue === undefined || minValue >= node.data.attributes[metric]) {
                        metricMinValues.set(metric, node.data.attributes[metric])
                    }
 
                    if (maxValue === undefined || maxValue <= node.data.attributes[metric]) {
                        metricMaxValues.set(metric, node.data.attributes[metric])
                    }
                }
            }
        }
    }
 
    const metricData: NodeMetricData[] = []
 
    // TODO: Remove the unary metric.
    metricMaxValues.set(UNARY_METRIC, 1)
    metricMinValues.set(UNARY_METRIC, 1)
 
    for (const [key, value] of metricMaxValues) {
        metricData.push({
            name: key,
            values: metricValues.get(key),
            maxValue: value,
            minValue: metricMinValues.get(key)
        })
    }
 
    sortByMetricName(metricData)
    return metricData
}