@@ -84,20 +84,44 @@ async function makeGlobber(patterns: string[]): Promise<glob.Globber> {
8484 return glob . create ( patterns . join ( "\n" ) ) ;
8585}
8686
87+ /** Enumerates possible outcomes for cache hits. */
88+ export enum CacheHitResult {
89+ /** We were unable to calculate a hash for the key. */
90+ NoHash = "no-hash" ,
91+ /** No cache was found. */
92+ Miss = "miss" ,
93+ /** The primary cache key matched. */
94+ Exact = "exact" ,
95+ /** A restore key matched. */
96+ Partial = "partial" ,
97+ }
98+
99+ /** Represents results of trying to restore a dependency cache for a language. */
100+ export interface DependencyCacheRestoreStatus {
101+ hit : CacheHitResult ;
102+ download_size_bytes ?: number ;
103+ download_duration_ms ?: number ;
104+ }
105+
106+ /** A partial mapping from languages to the results of restoring dependency caches for them. */
107+ export type DependencyCacheRestoreStatusReport = Partial <
108+ Record < Language , DependencyCacheRestoreStatus >
109+ > ;
110+
87111/**
88112 * Attempts to restore dependency caches for the languages being analyzed.
89113 *
90114 * @param languages The languages being analyzed.
91115 * @param logger A logger to record some informational messages to.
92116 * @param minimizeJavaJars Whether the Java extractor should rewrite downloaded JARs to minimize their size.
93- * @returns A list of languages for which dependency caches were restored .
117+ * @returns A partial mapping of languages to results of restoring dependency caches for them .
94118 */
95119export async function downloadDependencyCaches (
96120 languages : Language [ ] ,
97121 logger : Logger ,
98122 minimizeJavaJars : boolean ,
99- ) : Promise < Language [ ] > {
100- const restoredCaches : Language [ ] = [ ] ;
123+ ) : Promise < DependencyCacheRestoreStatusReport > {
124+ const status : DependencyCacheRestoreStatusReport = { } ;
101125
102126 for ( const language of languages ) {
103127 const cacheConfig = getDefaultCacheConfig ( ) [ language ] ;
@@ -114,6 +138,7 @@ export async function downloadDependencyCaches(
114138 const globber = await makeGlobber ( cacheConfig . hash ) ;
115139
116140 if ( ( await globber . glob ( ) ) . length === 0 ) {
141+ status [ language ] = { hit : CacheHitResult . NoHash } ;
117142 logger . info (
118143 `Skipping download of dependency cache for ${ language } as we cannot calculate a hash for the cache key.` ,
119144 ) ;
@@ -131,21 +156,29 @@ export async function downloadDependencyCaches(
131156 ) } `,
132157 ) ;
133158
159+ const start = performance . now ( ) ;
134160 const hitKey = await actionsCache . restoreCache (
135161 cacheConfig . paths ,
136162 primaryKey ,
137163 restoreKeys ,
138164 ) ;
165+ const download_duration_ms = Math . round ( performance . now ( ) - start ) ;
166+ const download_size_bytes = Math . round (
167+ await getTotalCacheSize ( cacheConfig . paths , logger ) ,
168+ ) ;
139169
140170 if ( hitKey !== undefined ) {
141171 logger . info ( `Cache hit on key ${ hitKey } for ${ language } .` ) ;
142- restoredCaches . push ( language ) ;
172+ const hit =
173+ hitKey === primaryKey ? CacheHitResult . Exact : CacheHitResult . Partial ;
174+ status [ language ] = { hit, download_duration_ms, download_size_bytes } ;
143175 } else {
176+ status [ language ] = { hit : CacheHitResult . Miss } ;
144177 logger . info ( `No suitable cache found for ${ language } .` ) ;
145178 }
146179 }
147180
148- return restoredCaches ;
181+ return status ;
149182}
150183
151184/**
0 commit comments