All files / app/codeCharta/ui/ribbonBar/artificialIntelligence/selectors/util riskProfileHelper.ts

100% Statements 22/22
100% Branches 6/6
100% Functions 3/3
100% Lines 21/21

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  5x 5x                 5x 5x 5x   5x 56x 24x 24x 24x 24x   24x 10x 14x 5x 9x 7x   2x       5x 4x             4x    
import { CodeMapNode } from "../../../../../codeCharta.model"
import percentRound from "percent-round"
import { getAssociatedMetricThresholds } from "./getMetricThresholds"
 
export interface RiskProfile {
    lowRisk: number
    moderateRisk: number
    highRisk: number
    veryHighRisk: number
}
 
export const HEIGHT_METRICS = ["complexity", "sonar_complexity", "mcc"]
export const AREA_METRIC = "rloc"
export const EXCLUDED_FILE_EXTENSION = new Set(["html", "sass", "css", "scss", "txt", "md", "json", undefined])
 
export function aggregateRiskProfile(node: CodeMapNode, rlocRisk: RiskProfile, fileExtension: string) {
    const riskMetric = HEIGHT_METRICS.find(metric => node.attributes[metric] !== undefined)
    const languageSpecificThresholds = getAssociatedMetricThresholds(fileExtension)
    const thresholds = languageSpecificThresholds[riskMetric]
    const nodeMetricValue = node.attributes[riskMetric]
    const nodeRlocValue = node.attributes[AREA_METRIC]
 
    if (nodeMetricValue <= thresholds.percentile70) {
        rlocRisk.lowRisk += nodeRlocValue
    } else if (nodeMetricValue <= thresholds.percentile80) {
        rlocRisk.moderateRisk += nodeRlocValue
    } else if (nodeMetricValue <= thresholds.percentile90) {
        rlocRisk.highRisk += nodeRlocValue
    } else {
        rlocRisk.veryHighRisk += nodeRlocValue
    }
}
 
export function getPercentagesOfRiskProfile(rlocRisk: RiskProfile): RiskProfile {
    const [lowRisk, moderateRisk, highRisk, veryHighRisk] = percentRound([
        rlocRisk.lowRisk,
        rlocRisk.moderateRisk,
        rlocRisk.highRisk,
        rlocRisk.veryHighRisk
    ])
 
    return { lowRisk, moderateRisk, highRisk, veryHighRisk }
}