Skip to content

Commit

Permalink
Add more details to cache summary report
Browse files Browse the repository at this point in the history
  • Loading branch information
bigdaz committed Jun 3, 2022
1 parent a9a5bcf commit 1437742
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/cache-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ export class GradleStateCache {
async save(listener: CacheListener): Promise<void> {
const cacheKey = generateCacheKey(this.cacheName).key
const restoredCacheKey = core.getState(RESTORED_CACHE_KEY_KEY)
const entryListener = listener.entry(this.cacheDescription)

if (restoredCacheKey && cacheKey === restoredCacheKey) {
core.info(`Cache hit occurred on the cache key ${cacheKey}, not saving cache.`)
entryListener.markUnchanged('cache key not changed')
return
}

Expand All @@ -110,7 +112,6 @@ export class GradleStateCache {

core.info(`Caching ${this.cacheDescription} with cache key: ${cacheKey}`)
const cachePath = this.getCachePath()
const entryListener = listener.entry(this.cacheDescription)
await saveCache(cachePath, cacheKey, entryListener)

return
Expand Down
1 change: 1 addition & 0 deletions src/cache-extract-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ abstract class AbstractEntryExtractor {

if (previouslyRestoredKey === cacheKey) {
cacheDebug(`No change to previously restored ${artifactType}. Not saving.`)
entryListener.markUnchanged('entry contents unchanged')
} else {
core.info(`Caching ${artifactType} with path '${pattern}' and cache key: ${cacheKey}`)
await saveCache([pattern], cacheKey, entryListener)
Expand Down
37 changes: 35 additions & 2 deletions src/cache-reporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export class CacheEntryListener {
savedKey: string | undefined
savedSize: number | undefined

unchanged: string | undefined

constructor(entryName: string) {
this.entryName = entryName
}
Expand Down Expand Up @@ -85,14 +87,19 @@ export class CacheEntryListener {
this.savedSize = 0
return this
}

markUnchanged(message: string): CacheEntryListener {
this.unchanged = message
return this
}
}

export function logCachingReport(listener: CacheListener): void {
if (listener.cacheEntries.length === 0) {
return
}

core.summary.addHeading('Caching Summary', 3)
core.summary.addHeading('Gradle Home Caching Summary', 3)

const entries = listener.cacheEntries
.map(
Expand All @@ -101,8 +108,11 @@ export function logCachingReport(listener: CacheListener): void {
Requested Key : ${entry.requestedKey ?? ''}
Restored Key : ${entry.restoredKey ?? ''}
Size: ${formatSize(entry.restoredSize)}
${getRestoredMessage(entry)}
Saved Key : ${entry.savedKey ?? ''}
Size: ${formatSize(entry.savedSize)}`
Size: ${formatSize(entry.savedSize)}
${getSavedMessage(entry)}
---`
)
.join('\n')

Expand Down Expand Up @@ -134,6 +144,29 @@ ${entries}
)
}

function getRestoredMessage(entry: CacheEntryListener): string {
if (entry.restoredKey === undefined) {
return '(No match found)'
}
if (entry.restoredKey === entry.requestedKey) {
return '(Exact match found)'
}
return '(Fuzzy match found)'
}

function getSavedMessage(entry: CacheEntryListener): string {
if (entry.unchanged) {
return `(Entry not saved: ${entry.unchanged})`
}
if (entry.savedKey === undefined) {
return '(Entry not saved: ???'
}
if (entry.savedSize === 0) {
return '(Could not save: entry exists)'
}
return '(Entry saved)'
}

function getCount(
cacheEntries: CacheEntryListener[],
predicate: (value: CacheEntryListener) => number | undefined
Expand Down

0 comments on commit 1437742

Please sign in to comment.