Setting the file. One moment.
Cost Coverage · Vercel Optimize · vercel-labs/agent-skills · Skills Docs
ContentsBack to the top of the page lib/cost-coverage.mjs
lib/ cost-coverage.mjs
JavaScript · 143 lines · 8 KB
Fluid Active CPU
$
/
i
, dim:
'function-duration'
},
8 { match: / ^ Fluid Provisioned Memory $ / i , dim: 'function-duration' },
9 { match: / ^ Edge Requests $ / i , dim: 'edge-requests' },
10 { match: / ^ Edge Requests . * Additional CPU Duration/ i , dim: 'edge-requests' },
11 { match: / ^ Edge Function Execution Units $ / i , dim: 'edge-requests' },
12 { match: / ^ Edge Middleware Invocations $ / i , dim: 'edge-requests' },
13 { match: / ^ ISR (Reads | Writes) $ / i , dim: 'isr' },
14 { match: / ^ Speed Insights( Data Points) ?$ / i , dim: 'speed-insights' },
15 { match: / ^ Image Optimization/ i , dim: 'image-optimization' },
16 // Indirect: bot-protection gate addresses bandwidth/edge spend.
17 { match: / ^ Fast Data Transfer $ / i , dim: 'edge-requests' },
18 { match: / ^ Fast Origin Transfer $ / i , dim: 'edge-requests' },
19
20 // Uncovered.
21 { match: / ^ Sandbox/ i , dim: null , family: 'sandbox' },
22 { match: / ^ AI Gateway $ / i , dim: null , family: 'ai-gateway' },
23 { match: / ^ Build Minutes $ / i , dim: 'build' , family: 'build' },
24 { match: / ^ Build CPU Minutes $ / i , dim: 'build' , family: 'build' },
25 { match: / ^ Private Data Transfer $ / i , dim: null , family: 'private-network' },
26 { match: / ^ Secure Compute Network $ / i , dim: null , family: 'private-network' },
27 { match: / ^ Drains Volume $ / i , dim: null , family: 'drains' },
28 { match: / ^ Observability Events $ / i , dim: 'observability-events' , family: 'observability-events' },
29 { match: / ^ Blob/ i , dim: null , family: 'blob' },
30 { match: / ^ Edge Config (Reads | Writes) $ / i , dim: null , family: 'edge-config' },
31 { match: / ^ Runtime Cache/ i , dim: null , family: 'runtime-cache' },
32 { match: / ^ Microfrontends/ i , dim: null , family: 'microfrontends' },
33 { match: / ^ Workflow/ i , dim: null , family: 'workflow' },
34 { match: / ^ Queue/ i , dim: null , family: 'queues' },
35 { match: / ^ Flag Requests $ / i , dim: null , family: 'flags' },
36 { match: / ^ Flags Explorer/ i , dim: null , family: 'flags' },
37 { match: / ^ BotID/ i , dim: null , family: 'botid' },
38 { match: / ^ Firewall/ i , dim: null , family: 'firewall' },
39 { match: / ^ Vercel Agent $ / i , dim: null , family: 'vercel-agent' },
40
41 // Fixed costs (seats, contracts) — not actionable.
42 { match: / ^ v0 / i , dim: null , family: 'fixed' , actionable: false },
43 { match: / ^ Additional Team Seats $ / i , dim: null , family: 'fixed' , actionable: false },
44 { match: / ^ SAML $ / i , dim: null , family: 'fixed' , actionable: false },
45 { match: / ^ HIPAA BAA $ / i , dim: null , family: 'fixed' , actionable: false },
46 { match: / ^ SIEM Integration $ / i , dim: null , family: 'fixed' , actionable: false },
47 { match: / ^ Web Analytics/ i , dim: null , family: 'fixed' , actionable: false },
48 { match: / ^ Static IPs $ / i , dim: null , family: 'fixed' , actionable: false },
49 { match: / ^ Bulk Redirects $ / i , dim: null , family: 'fixed' , actionable: false },
50 { match: / ^ Preview Deployment Suffix $ / i , dim: null , family: 'fixed' , actionable: false },
51 { match: / ^ Rolling Releases $ / i , dim: null , family: 'fixed' , actionable: false },
52 { match: / ^ Observability Plus $ / i , dim: null , family: 'fixed' , actionable: false },
53 { match: / ^ Platform Customer Usage $ / i , dim: null , family: 'fixed' , actionable: false },
54 { match: / ^ Advanced Deployment Protection $ / i , dim: null , family: 'fixed' , actionable: false },
55 ];
56
57 export function classifyService ( serviceName , activeDims ) {
58 if ( ! serviceName) return { covered: false , family: 'unknown' };
59 for ( const e of SERVICE_DIMENSION ) {
60 if (e.match. test (serviceName)) {
61 if (e.dim && activeDims. has (e.dim)) return { covered: true , dim: e.dim };
62 return { covered: false , family: e.family ?? 'unknown' , actionable: e.actionable ?? true };
63 }
64 }
65 return { covered: false , family: 'unknown' , actionable: true };
66 }
67
68 export function computeCostCoverage ( usage , gates ) {
69 const services = Array. isArray (usage?.services) ? usage.services : [];
70 const activeDims = new Set (
71 (gates ?? [])
72 . map (( g ) => g?.metadata?.billingDimension)
73 . filter (( d ) => typeof d === 'string' && d !== 'mixed' )
74 );
75 let total = 0 ;
76 let covered = 0 ;
77 let uncovered = 0 ;
78 const byFamily = new Map ();
79
80 for ( const s of services) {
81 const billed = Number (s.billedCost ?? 0 );
82 if ( ! Number. isFinite (billed) || billed <= 0 ) continue ;
83 total += billed;
84 const c = classifyService (s.name, activeDims);
85 if (c.covered) {
86 covered += billed;
87 continue ;
88 }
89 uncovered += billed;
90 const key = c.family;
91 const prev = byFamily. get (key) ?? { family: key, billed: 0 , services: [], actionable: c.actionable !== false };
92 prev.billed += billed;
93 prev.services. push ({ name: s.name, billed });
94 prev.actionable = prev.actionable && (c.actionable !== false );
95 byFamily. set (key, prev);
96 }
97
98 const uncoveredByFamily = [ ... byFamily. values ()]
99 . sort (( a , b ) => b.billed - a.billed)
100 . map (( f ) => ({ ... f, services: f.services. sort (( a , b ) => b.billed - a.billed) }));
101
102 // Pick top gaps globally so multiple families surface (Sandbox + AI Gateway + Build, not 5 Sandbox sub-services). Exclude fixed costs — seats aren't actionable workload.
103 const allActionableServices = [];
104 for ( const family of uncoveredByFamily) {
105 if ( ! family.actionable) continue ;
106 for ( const s of family.services) {
107 allActionableServices. push ({ name: s.name, billed: s.billed, family: family.family });
108 }
109 }
110 allActionableServices. sort (( a , b ) => b.billed - a.billed);
111 const topGaps = allActionableServices. slice ( 0 , 5 ). map (( s ) => ({
112 ... s,
113 share: total > 0 ? s.billed / total : 0 ,
114 }));
115 return { totalBilled: total, coveredBilled: covered, uncoveredBilled: uncovered, uncoveredByFamily, topGaps };
116 }
117
118 export function renderCostCoverageMarkdown ( coverage ) {
119 if ( ! coverage || ! Number. isFinite (coverage.totalBilled) || coverage.totalBilled <= 0 ) return [];
120 const { totalBilled , coveredBilled , uncoveredBilled , topGaps } = coverage;
121 const actionableGaps = topGaps. filter (( g ) => g.share >= 0.01 ); // 1%+ share
122 if (actionableGaps. length === 0 ) return [];
123 const lines = [];
124 lines. push ( '' );
125 lines. push ( '### Coverage gaps' );
126 lines. push ( '' );
127 const coveredPct = totalBilled > 0 ? (coveredBilled / totalBilled) * 100 : 0 ;
128 const uncoveredPct = totalBilled > 0 ? (uncoveredBilled / totalBilled) * 100 : 0 ;
129 lines. push ( `This audit has metric coverage for **$${ coveredBilled . toFixed ( 0 ) } (${ coveredPct . toFixed ( 0 ) }%)** of this bill via function-duration, edge-requests, ISR, middleware, and image-optimization dimensions. **$${ uncoveredBilled . toFixed ( 0 ) } (${ uncoveredPct . toFixed ( 0 ) }%)** sits in billed areas this run cannot analyze safely, including the top actionable items below:` );
130 lines. push ( '' );
131 lines. push ( '| Service | Billed | Share | Family | Coverage |' );
132 lines. push ( '|---|---|---|---|---|' );
133 for ( const g of actionableGaps) {
134 lines. push ( `| ${ escapeCell ( g . name ) } | $${ g . billed . toFixed ( 2 ) } | ${ ( g . share * 100 ). toFixed ( 1 ) }% | ${ g . family } | _not analyzed in this run_ |` );
135 }
136 lines. push ( '' );
137 lines. push ( '_Recommendations in this report address the covered dimensions. The uncovered rows are not ignored; they need a separate investigation before we can make safe recommendations._' );
138 return lines;
139 }
140
141 function escapeCell ( s ) {
142 return String (s ?? '' ). replace ( / \| / g , ' \\ |' ). replace ( / \n / g , ' ' );
143 }