|
| 1 | +import type { |
| 2 | + AnalysisDetailRow, |
| 3 | + VerdictResultRow, |
| 4 | +} from '@crowd/data-access-layer/src/packages/blastRadius' |
| 5 | + |
| 6 | +import type { BlastRadiusJobEcosystem, BlastRadiusJobStatus } from './blastRadius' |
| 7 | + |
| 8 | +export type BlastRadiusResultConfidence = 'high' | 'medium' | 'low' |
| 9 | + |
| 10 | +export interface BlastRadiusResultItem { |
| 11 | + dependent: string |
| 12 | + affected: boolean |
| 13 | + confidence: BlastRadiusResultConfidence |
| 14 | + evidence: string | null |
| 15 | + downloadsLast30Days: string | null |
| 16 | +} |
| 17 | + |
| 18 | +export interface BlastRadiusAnalysisSummary { |
| 19 | + totalDependentsInRange: number |
| 20 | + dependentsExcludedUpfront: number |
| 21 | + dependentsAnalyzed: number |
| 22 | + dependentsAffected: number |
| 23 | + affectedPercentage: number | null |
| 24 | + affectedDependents: string[] |
| 25 | +} |
| 26 | + |
| 27 | +export interface BlastRadiusAnalysis { |
| 28 | + analysisId: string |
| 29 | + status: BlastRadiusJobStatus |
| 30 | + advisoryId: string |
| 31 | + package: string | null |
| 32 | + ecosystem: BlastRadiusJobEcosystem |
| 33 | + submittedAt: string | null |
| 34 | + completedAt: string | null |
| 35 | + errorMessage: string | null |
| 36 | + summary: BlastRadiusAnalysisSummary | null |
| 37 | + results: BlastRadiusResultItem[] | null |
| 38 | +} |
| 39 | + |
| 40 | +// Crosswalk from the DB's NUMERIC(3,2) 0-1 confidence score to the contract's |
| 41 | +// enum — matches the PoC methodology's confidence bands (report.py). |
| 42 | +function toResultConfidence(confidence: number): BlastRadiusResultConfidence { |
| 43 | + if (confidence >= 0.8) return 'high' |
| 44 | + if (confidence >= 0.4) return 'medium' |
| 45 | + return 'low' |
| 46 | +} |
| 47 | + |
| 48 | +// npm purls use a literal (unencoded) `@` for the scope, matching the contract's |
| 49 | +// example response bodies — these are JSON fields, not URL query params, so the |
| 50 | +// %40-encoding normalizePurl applies elsewhere in this file group does not apply here. |
| 51 | +function toPurl(name: string): string { |
| 52 | + return `pkg:npm/${name}` |
| 53 | +} |
| 54 | + |
| 55 | +function flattenEvidence(evidence: Record<string, unknown>[] | null): string | null { |
| 56 | + if (!evidence || evidence.length === 0) return null |
| 57 | + return evidence |
| 58 | + .map((e) => { |
| 59 | + const file = e.file ? String(e.file) : null |
| 60 | + const line = e.line !== undefined && e.line !== null ? String(e.line) : null |
| 61 | + const snippet = e.snippet ? String(e.snippet) : null |
| 62 | + const location = [file, line].filter(Boolean).join(':') |
| 63 | + return [location, snippet].filter(Boolean).join(' — ') |
| 64 | + }) |
| 65 | + .filter(Boolean) |
| 66 | + .join('\n') |
| 67 | +} |
| 68 | + |
| 69 | +function toResultItem(row: VerdictResultRow): BlastRadiusResultItem { |
| 70 | + return { |
| 71 | + dependent: toPurl(row.name), |
| 72 | + affected: row.reachable_verdict === 'affected', |
| 73 | + confidence: toResultConfidence(row.confidence), |
| 74 | + evidence: flattenEvidence(row.evidence), |
| 75 | + downloadsLast30Days: row.downloads !== null ? String(row.downloads) : null, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Population-level summary, following the PoC's report.py ground truth: |
| 80 | +// totalDependentsInRange = candidates_considered (post phase-1-filter population), |
| 81 | +// dependentsAnalyzed = number of verdicts produced, dependentsExcludedUpfront = |
| 82 | +// the difference (range-excluded before reachability ran), dependentsAffected = |
| 83 | +// count with an 'affected' verdict, affectedPercentage rounded to 1 decimal |
| 84 | +// (null when nothing was analyzed, to avoid a misleading 0%). |
| 85 | +function toSummary( |
| 86 | + candidatesConsidered: number | null, |
| 87 | + results: BlastRadiusResultItem[], |
| 88 | +): BlastRadiusAnalysisSummary { |
| 89 | + const totalDependentsInRange = candidatesConsidered ?? results.length |
| 90 | + const dependentsAnalyzed = results.length |
| 91 | + const affected = results.filter((r) => r.affected) |
| 92 | + |
| 93 | + return { |
| 94 | + totalDependentsInRange, |
| 95 | + dependentsExcludedUpfront: Math.max(totalDependentsInRange - dependentsAnalyzed, 0), |
| 96 | + dependentsAnalyzed, |
| 97 | + dependentsAffected: affected.length, |
| 98 | + affectedPercentage: |
| 99 | + dependentsAnalyzed > 0 |
| 100 | + ? Math.round((affected.length / dependentsAnalyzed) * 1000) / 10 |
| 101 | + : null, |
| 102 | + affectedDependents: affected.map((r) => r.dependent), |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +export function toBlastRadiusAnalysis( |
| 107 | + analysis: AnalysisDetailRow, |
| 108 | + verdictRows: VerdictResultRow[], |
| 109 | +): BlastRadiusAnalysis { |
| 110 | + const status = analysis.status as BlastRadiusJobStatus |
| 111 | + const done = status === 'done' |
| 112 | + |
| 113 | + const results = done ? verdictRows.map(toResultItem) : null |
| 114 | + |
| 115 | + return { |
| 116 | + analysisId: analysis.id, |
| 117 | + status, |
| 118 | + advisoryId: analysis.advisory_osv_id, |
| 119 | + package: analysis.package_name, |
| 120 | + ecosystem: analysis.ecosystem as BlastRadiusJobEcosystem, |
| 121 | + submittedAt: analysis.started_at, |
| 122 | + completedAt: analysis.completed_at, |
| 123 | + errorMessage: analysis.error, |
| 124 | + summary: done && results ? toSummary(analysis.candidates_considered, results) : null, |
| 125 | + results, |
| 126 | + } |
| 127 | +} |
0 commit comments