All files / app/codeCharta/state/effects/resetChosenMetrics/utils metricHelper.ts

100% Statements 37/37
100% Branches 4/4
100% Functions 8/8
100% Lines 33/33

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    6x 6x                   6x 9x     6x 2x 2x 2x 6x     6x 4x   4x 8x 4x   4x 4x 4x 1x       3x 1x     2x 2x   2x     6x 4x   4x 8x 4x 12x 32x 10x 10x       4x    
import { NodeMetricData, RecursivePartial, Settings } from "../../../../codeCharta.model"
 
const sizeMetrics = ["rloc", "real_lines_of_code", "loc", "lines_of_code", "lines", "statements", "functions"]
const complexityMetrics = [
    "complexity",
    "mcc",
    "cognitive_complexity",
    "sonar_complexity",
    "sonar_cognitive_complexity",
    "max_nesting_level",
    "indentation_level"
]
 
export function isAnyMetricAvailable<T extends Pick<NodeMetricData, "maxValue">[]>(metricData: T) {
    return metricData.some(x => x.maxValue > 0)
}
 
export function areScenarioSettingsApplicable(scenario: RecursivePartial<Settings>, nodeMetricData: Pick<NodeMetricData, "name">[]) {
    const { areaMetric, heightMetric, colorMetric } = scenario.dynamicSettings
    const relevantMetrics = [areaMetric, heightMetric, colorMetric]
    const existingMetrics = new Set(nodeMetricData.map(x => x.name))
    return relevantMetrics.every(relevantMetric => existingMetrics.has(relevantMetric))
}
 
export function defaultNMetrics<T extends Pick<NodeMetricData, "maxValue" | "name">>(metricData: T[], n: number) {
    const defaultedMetrics: string[] = []
    let lastMetricNameWithValue: string
    for (const metric of metricData) {
        if (!metric.maxValue) {
            continue
        }
        defaultedMetrics.push(metric.name)
        lastMetricNameWithValue = metric.name
        if (--n === 0) {
            return defaultedMetrics
        }
    }
 
    if (!lastMetricNameWithValue) {
        throw new Error("there are no metrics available")
    }
 
    while (n-- > 0) {
        defaultedMetrics.push(lastMetricNameWithValue)
    }
    return defaultedMetrics
}
 
export function preselectCombination(nodeMetricData: Pick<NodeMetricData, "name">[]) {
    const preselectCombinationMetrics: string[] = []
    // combinations might be a parameter for this function for different scenarios
    const combination = { AreaMetric: sizeMetrics, HeightMetric: complexityMetrics, ColorMetric: complexityMetrics }
    const nodeMetricSet = new Set(nodeMetricData.map(data => data.name))
    for (const key in combination) {
        for (const metric of combination[key]) {
            if (nodeMetricSet.has(metric)) {
                preselectCombinationMetrics.push(metric)
                break
            }
        }
    }
    return preselectCombinationMetrics
}