All files / src/highlights severity-utils.ts

100% Statements 91/91
100% Branches 43/43
100% Functions 8/8
100% Lines 91/91

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                                                                    1x 141x 30x 30x 141x 23x 23x 88x 88x         1x 62x 62x 62x   62x 133x 133x 133x   62x 62x           1x 20x 20x 20x 6x 6x 20x 2x 2x 12x 12x     1x 28x 28x 3x 3x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x                 1x 144x 144x 144x 144x 144x   60x 60x 60x 60x 60x 60x 60x 53x 7x   144x 144x 60x 93x   60x 60x     1x 48x 48x 48x 48x 48x 48x 48x     1x 48x 48x     1x 12x 12x  
/**
 * Shared severity classification utilities for DataFlint highlights.
 *
 * This module centralizes the logic for classifying highlights by severity,
 * ensuring consistent behavior across file decorations, tree views, and diagnostics.
 */
 
import { HighlightCodeLineData } from "./highlights-models";
 
/**
 * Severity levels for DataFlint highlights.
 * - hotspot: Critical performance issues (🔥)
 * - warning: Potential problems with alerts (⚠️)
 * - info: Optimization suggestions and findings (📊)
 */
export type Severity = "hotspot" | "warning" | "info";
 
/**
 * Aggregated severity counts for a collection of highlights.
 */
export interface SeverityCounts {
    hotspot: number;
    warning: number;
    info: number;
}
 
/**
 * Classify a single highlight by its severity level.
 *
 * Classification rules (in priority order):
 * 1. If isHotspot is true → "hotspot"
 * 2. If alertCount > 0 → "warning"
 * 3. Otherwise → "info"
 */
export function classifyHighlight(highlight: HighlightCodeLineData): Severity {
    if (highlight.isHotspot) {
        return "hotspot";
    }
    if ((highlight.alertCount ?? 0) > 0) {
        return "warning";
    }
    return "info";
}
 
/**
 * Calculate severity counts from a list of highlights.
 */
export function calculateSeverityCounts(
    highlights: HighlightCodeLineData[]
): SeverityCounts {
    const counts: SeverityCounts = { hotspot: 0, warning: 0, info: 0 };
 
    for (const highlight of highlights) {
        const severity = classifyHighlight(highlight);
        counts[severity]++;
    }
 
    return counts;
}
 
/**
 * Get the highest severity from a list of highlights.
 * Priority: hotspot > warning > info
 */
export function getHighestSeverity(
    highlights: HighlightCodeLineData[]
): Severity {
    if (highlights.some((h) => h.isHotspot)) {
        return "hotspot";
    }
    if (highlights.some((h) => (h.alertCount ?? 0) > 0)) {
        return "warning";
    }
    return "info";
}
 
/** Get highest severity from severity counts */
export function getHighestSeverityFromCounts(counts: SeverityCounts): Severity {
    if (counts.hotspot > 0) return "hotspot";
    if (counts.warning > 0) return "warning";
    return "info";
}
 
/** VS Code ThemeColor identifiers for each severity */
export const SEVERITY_THEME_COLORS: Record<Severity, string> = {
    hotspot: "charts.red",
    warning: "charts.orange",
    info: "charts.green",
};
 
/** Emoji icons for each severity type */
export const SEVERITY_EMOJIS: Record<Severity, string> = {
    hotspot: "🔥",
    warning: "⚠️",
    info: "📊",
};
 
/** Labels for each severity type */
export const SEVERITY_LABELS: Record<
    Severity,
    { singular: string; plural: string }
> = {
    hotspot: { singular: "hotspot", plural: "hotspots" },
    warning: { singular: "alert", plural: "alerts" },
    info: { singular: "finding", plural: "findings" },
};
 
/** Descriptions for severity types */
export const SEVERITY_DESCRIPTIONS: Record<Severity, string> = {
    hotspot: "critical performance issues",
    warning: "potential problems",
    info: "optimization suggestions",
};
 
export interface SeverityBreakdownOptions {
    includeEmoji?: boolean;
    includeDescription?: boolean;
    prefix?: string;
}
 
/** Format a single severity line */
export function formatSeverityLine(
    severity: Severity,
    count: number,
    options: SeverityBreakdownOptions = {}
): string | null {
    if (count === 0) return null;
 
    const {
        includeEmoji = false,
        includeDescription = false,
        prefix = "",
    } = options;
    const label =
        count === 1
            ? SEVERITY_LABELS[severity].singular
            : SEVERITY_LABELS[severity].plural;
 
    let line = prefix;
    if (includeEmoji) line += `${SEVERITY_EMOJIS[severity]} `;
    line += `${count} ${label}`;
    if (includeDescription) line += ` - ${SEVERITY_DESCRIPTIONS[severity]}`;
 
    return line;
}
 
/** Build severity breakdown lines from counts */
export function buildSeverityBreakdown(
    counts: SeverityCounts,
    options: SeverityBreakdownOptions = {}
): string[] {
    return (["hotspot", "warning", "info"] as Severity[])
        .map((s) => formatSeverityLine(s, counts[s], options))
        .filter((line): line is string => line !== null);
}
 
/** Pluralize "issue" based on count */
export function pluralizeIssue(count: number): string {
    return count === 1 ? "issue" : "issues";
}
 
/** Pluralize "file" based on count */
export function pluralizeFile(count: number): string {
    return count === 1 ? "file" : "files";
}