|
| 1 | +import { |
| 2 | + FindingsSeverityOverviewResponse, |
| 3 | + ProviderOverview, |
| 4 | + ProvidersOverviewResponse, |
| 5 | +} from "./types"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Sankey chart node structure |
| 9 | + */ |
| 10 | +export interface SankeyNode { |
| 11 | + name: string; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Sankey chart link structure |
| 16 | + */ |
| 17 | +export interface SankeyLink { |
| 18 | + source: number; |
| 19 | + target: number; |
| 20 | + value: number; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Sankey chart data structure |
| 25 | + */ |
| 26 | +export interface SankeyData { |
| 27 | + nodes: SankeyNode[]; |
| 28 | + links: SankeyLink[]; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Provider display name mapping |
| 33 | + * Maps provider IDs to user-friendly display names |
| 34 | + * These names must match the COLOR_MAP keys in sankey-chart.tsx |
| 35 | + */ |
| 36 | +const PROVIDER_DISPLAY_NAMES: Record<string, string> = { |
| 37 | + aws: "AWS", |
| 38 | + azure: "Azure", |
| 39 | + gcp: "Google Cloud", |
| 40 | + kubernetes: "Kubernetes", |
| 41 | + github: "GitHub", |
| 42 | + m365: "Microsoft 365", |
| 43 | + iac: "Infrastructure as Code", |
| 44 | + oraclecloud: "Oracle Cloud Infrastructure", |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * Aggregated provider data after grouping by provider type |
| 49 | + */ |
| 50 | +interface AggregatedProvider { |
| 51 | + id: string; |
| 52 | + displayName: string; |
| 53 | + pass: number; |
| 54 | + fail: number; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Provider types to exclude from the Sankey chart |
| 59 | + */ |
| 60 | +const EXCLUDED_PROVIDERS = new Set(["mongo", "mongodb", "mongodbatlas"]); |
| 61 | + |
| 62 | +/** |
| 63 | + * Aggregates multiple provider entries by provider type (id) |
| 64 | + * Since the API can return multiple entries for the same provider type, |
| 65 | + * we need to sum up their findings |
| 66 | + * |
| 67 | + * @param providers - Raw provider overview data from API |
| 68 | + * @returns Aggregated providers with summed findings |
| 69 | + */ |
| 70 | +function aggregateProvidersByType( |
| 71 | + providers: ProviderOverview[], |
| 72 | +): AggregatedProvider[] { |
| 73 | + const aggregated = new Map<string, AggregatedProvider>(); |
| 74 | + |
| 75 | + for (const provider of providers) { |
| 76 | + const { id, attributes } = provider; |
| 77 | + |
| 78 | + // Skip excluded providers |
| 79 | + if (EXCLUDED_PROVIDERS.has(id)) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + const existing = aggregated.get(id); |
| 84 | + |
| 85 | + if (existing) { |
| 86 | + existing.pass += attributes.findings.pass; |
| 87 | + existing.fail += attributes.findings.fail; |
| 88 | + } else { |
| 89 | + aggregated.set(id, { |
| 90 | + id, |
| 91 | + displayName: PROVIDER_DISPLAY_NAMES[id] || id, |
| 92 | + pass: attributes.findings.pass, |
| 93 | + fail: attributes.findings.fail, |
| 94 | + }); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return Array.from(aggregated.values()); |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Severity display names in order |
| 103 | + */ |
| 104 | +const SEVERITY_ORDER = [ |
| 105 | + "Critical", |
| 106 | + "High", |
| 107 | + "Medium", |
| 108 | + "Low", |
| 109 | + "Informational", |
| 110 | +] as const; |
| 111 | + |
| 112 | +/** |
| 113 | + * Adapts providers overview and findings severity API responses to Sankey chart format |
| 114 | + * |
| 115 | + * Creates a 2-level flow visualization: |
| 116 | + * - Level 1: Cloud providers (AWS, Azure, GCP, etc.) |
| 117 | + * - Level 2: Severity breakdown (Critical, High, Medium, Low, Informational) |
| 118 | + * |
| 119 | + * The severity distribution is calculated proportionally based on each provider's |
| 120 | + * fail count relative to the total fails across all providers. |
| 121 | + * |
| 122 | + * @param providersResponse - Raw API response from /overviews/providers |
| 123 | + * @param severityResponse - Raw API response from /overviews/findings_severity |
| 124 | + * @returns Sankey chart data with nodes and links |
| 125 | + */ |
| 126 | +export function adaptProvidersOverviewToSankey( |
| 127 | + providersResponse: ProvidersOverviewResponse | undefined, |
| 128 | + severityResponse?: FindingsSeverityOverviewResponse | undefined, |
| 129 | +): SankeyData { |
| 130 | + if (!providersResponse?.data || providersResponse.data.length === 0) { |
| 131 | + return { nodes: [], links: [] }; |
| 132 | + } |
| 133 | + |
| 134 | + // Aggregate providers by type |
| 135 | + const aggregatedProviders = aggregateProvidersByType(providersResponse.data); |
| 136 | + |
| 137 | + // Filter out providers with no findings (only need fail > 0 for severity view) |
| 138 | + const providersWithFailures = aggregatedProviders.filter((p) => p.fail > 0); |
| 139 | + |
| 140 | + if (providersWithFailures.length === 0) { |
| 141 | + return { nodes: [], links: [] }; |
| 142 | + } |
| 143 | + |
| 144 | + // Build nodes array: providers first, then severities |
| 145 | + const providerNodes: SankeyNode[] = providersWithFailures.map((p) => ({ |
| 146 | + name: p.displayName, |
| 147 | + })); |
| 148 | + |
| 149 | + const severityNodes: SankeyNode[] = SEVERITY_ORDER.map((severity) => ({ |
| 150 | + name: severity, |
| 151 | + })); |
| 152 | + |
| 153 | + const nodes = [...providerNodes, ...severityNodes]; |
| 154 | + |
| 155 | + // Calculate severity start index (after provider nodes) |
| 156 | + const severityStartIndex = providerNodes.length; |
| 157 | + |
| 158 | + // Build links from each provider to severities |
| 159 | + const links: SankeyLink[] = []; |
| 160 | + |
| 161 | + // If we have severity data, distribute proportionally |
| 162 | + if (severityResponse?.data?.attributes) { |
| 163 | + const { critical, high, medium, low, informational } = |
| 164 | + severityResponse.data.attributes; |
| 165 | + |
| 166 | + const severityValues = [critical, high, medium, low, informational]; |
| 167 | + const totalSeverity = severityValues.reduce((sum, v) => sum + v, 0); |
| 168 | + |
| 169 | + if (totalSeverity > 0) { |
| 170 | + // Calculate total fails across all providers |
| 171 | + const totalFails = providersWithFailures.reduce( |
| 172 | + (sum, p) => sum + p.fail, |
| 173 | + 0, |
| 174 | + ); |
| 175 | + |
| 176 | + providersWithFailures.forEach((provider, sourceIndex) => { |
| 177 | + // Calculate this provider's proportion of total fails |
| 178 | + const providerRatio = provider.fail / totalFails; |
| 179 | + |
| 180 | + severityValues.forEach((severityValue, severityIndex) => { |
| 181 | + // Distribute severity proportionally to this provider |
| 182 | + const value = Math.round(severityValue * providerRatio); |
| 183 | + |
| 184 | + if (value > 0) { |
| 185 | + links.push({ |
| 186 | + source: sourceIndex, |
| 187 | + target: severityStartIndex + severityIndex, |
| 188 | + value, |
| 189 | + }); |
| 190 | + } |
| 191 | + }); |
| 192 | + }); |
| 193 | + } |
| 194 | + } else { |
| 195 | + // Fallback: if no severity data, just show fail counts to a generic "Fail" node |
| 196 | + const failNode: SankeyNode = { name: "Fail" }; |
| 197 | + nodes.push(failNode); |
| 198 | + const failIndex = nodes.length - 1; |
| 199 | + |
| 200 | + providersWithFailures.forEach((provider, sourceIndex) => { |
| 201 | + links.push({ |
| 202 | + source: sourceIndex, |
| 203 | + target: failIndex, |
| 204 | + value: provider.fail, |
| 205 | + }); |
| 206 | + }); |
| 207 | + } |
| 208 | + |
| 209 | + return { nodes, links }; |
| 210 | +} |
0 commit comments