2021-12-07 19:29:37 +00:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collects information on what entries were saved and restored during the action.
|
|
|
|
* This information is used to generate a summary of the cache usage.
|
|
|
|
*/
|
|
|
|
export class CacheListener {
|
|
|
|
cacheEntries: CacheEntryListener[] = []
|
2022-06-03 04:44:08 +00:00
|
|
|
isCacheReadOnly = false
|
|
|
|
isCacheWriteOnly = false
|
2022-06-04 15:36:41 +00:00
|
|
|
isCacheDisabled = false
|
2021-12-07 19:29:37 +00:00
|
|
|
|
|
|
|
get fullyRestored(): boolean {
|
|
|
|
return this.cacheEntries.every(x => !x.wasRequestedButNotRestored())
|
|
|
|
}
|
|
|
|
|
2022-06-04 15:36:41 +00:00
|
|
|
get cacheStatus(): string {
|
|
|
|
if (this.isCacheDisabled) return 'disabled'
|
|
|
|
if (this.isCacheWriteOnly) return 'write-only'
|
|
|
|
if (this.isCacheReadOnly) return 'read-only'
|
|
|
|
return 'enabled'
|
|
|
|
}
|
|
|
|
|
2021-12-07 19:29:37 +00:00
|
|
|
entry(name: string): CacheEntryListener {
|
|
|
|
for (const entry of this.cacheEntries) {
|
|
|
|
if (entry.entryName === name) {
|
|
|
|
return entry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const newEntry = new CacheEntryListener(name)
|
|
|
|
this.cacheEntries.push(newEntry)
|
|
|
|
return newEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
stringify(): string {
|
|
|
|
return JSON.stringify(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
static rehydrate(stringRep: string): CacheListener {
|
2022-01-20 16:36:57 +00:00
|
|
|
if (stringRep === '') {
|
|
|
|
return new CacheListener()
|
|
|
|
}
|
2021-12-07 19:29:37 +00:00
|
|
|
const rehydrated: CacheListener = Object.assign(new CacheListener(), JSON.parse(stringRep))
|
|
|
|
const entries = rehydrated.cacheEntries
|
|
|
|
for (let index = 0; index < entries.length; index++) {
|
|
|
|
const rawEntry = entries[index]
|
|
|
|
entries[index] = Object.assign(new CacheEntryListener(rawEntry.entryName), rawEntry)
|
|
|
|
}
|
|
|
|
return rehydrated
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collects information on the state of a single cache entry.
|
|
|
|
*/
|
|
|
|
export class CacheEntryListener {
|
|
|
|
entryName: string
|
|
|
|
requestedKey: string | undefined
|
|
|
|
requestedRestoreKeys: string[] | undefined
|
|
|
|
restoredKey: string | undefined
|
|
|
|
restoredSize: number | undefined
|
|
|
|
|
|
|
|
savedKey: string | undefined
|
|
|
|
savedSize: number | undefined
|
|
|
|
|
2022-06-06 20:57:03 +00:00
|
|
|
unsaved: string | undefined
|
2022-06-03 04:39:55 +00:00
|
|
|
|
2021-12-07 19:29:37 +00:00
|
|
|
constructor(entryName: string) {
|
|
|
|
this.entryName = entryName
|
|
|
|
}
|
|
|
|
|
|
|
|
wasRequestedButNotRestored(): boolean {
|
|
|
|
return this.requestedKey !== undefined && this.restoredKey === undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
markRequested(key: string, restoreKeys: string[] = []): CacheEntryListener {
|
|
|
|
this.requestedKey = key
|
|
|
|
this.requestedRestoreKeys = restoreKeys
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
markRestored(key: string, size: number | undefined): CacheEntryListener {
|
|
|
|
this.restoredKey = key
|
|
|
|
this.restoredSize = size
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
markSaved(key: string, size: number | undefined): CacheEntryListener {
|
|
|
|
this.savedKey = key
|
|
|
|
this.savedSize = size
|
|
|
|
return this
|
|
|
|
}
|
2022-01-19 19:11:51 +00:00
|
|
|
|
|
|
|
markAlreadyExists(key: string): CacheEntryListener {
|
|
|
|
this.savedKey = key
|
|
|
|
this.savedSize = 0
|
|
|
|
return this
|
|
|
|
}
|
2022-06-03 04:39:55 +00:00
|
|
|
|
2022-06-06 20:57:03 +00:00
|
|
|
markUnsaved(message: string): CacheEntryListener {
|
|
|
|
this.unsaved = message
|
2022-06-03 04:39:55 +00:00
|
|
|
return this
|
|
|
|
}
|
2021-12-07 19:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function logCachingReport(listener: CacheListener): void {
|
2022-06-04 15:36:41 +00:00
|
|
|
const entries = listener.cacheEntries
|
2021-12-07 19:29:37 +00:00
|
|
|
|
2022-06-04 15:36:41 +00:00
|
|
|
core.summary.addRaw(
|
|
|
|
`\n<details><summary><h4>Caching for gradle-build-action was ${listener.cacheStatus} - expand for details</h4></summary>\n`
|
|
|
|
)
|
2022-06-02 20:21:10 +00:00
|
|
|
|
2022-06-04 15:36:41 +00:00
|
|
|
core.summary.addTable([
|
|
|
|
[
|
|
|
|
{data: '', header: true},
|
|
|
|
{data: 'Count', header: true},
|
|
|
|
{data: 'Total Size (Mb)', header: true}
|
|
|
|
],
|
|
|
|
['Entries Restored', `${getCount(entries, e => e.restoredSize)}`, `${getSize(entries, e => e.restoredSize)}`],
|
|
|
|
['Entries Saved', `${getCount(entries, e => e.savedSize)}`, `${getSize(entries, e => e.savedSize)}`]
|
|
|
|
])
|
|
|
|
|
|
|
|
core.summary.addHeading('Cache Entry Details', 5)
|
|
|
|
|
|
|
|
const entryDetails = listener.cacheEntries
|
2022-06-02 20:21:10 +00:00
|
|
|
.map(
|
|
|
|
entry =>
|
|
|
|
`Entry: ${entry.entryName}
|
2021-12-07 19:29:37 +00:00
|
|
|
Requested Key : ${entry.requestedKey ?? ''}
|
|
|
|
Restored Key : ${entry.restoredKey ?? ''}
|
|
|
|
Size: ${formatSize(entry.restoredSize)}
|
2022-06-03 04:44:08 +00:00
|
|
|
${getRestoredMessage(entry, listener.isCacheWriteOnly)}
|
2021-12-07 19:29:37 +00:00
|
|
|
Saved Key : ${entry.savedKey ?? ''}
|
2022-06-03 04:39:55 +00:00
|
|
|
Size: ${formatSize(entry.savedSize)}
|
2022-06-03 04:44:08 +00:00
|
|
|
${getSavedMessage(entry, listener.isCacheReadOnly)}
|
2022-06-02 20:21:10 +00:00
|
|
|
`
|
2022-06-04 15:36:41 +00:00
|
|
|
)
|
|
|
|
.join('---\n')
|
2022-06-03 04:44:08 +00:00
|
|
|
|
2022-06-04 15:36:41 +00:00
|
|
|
core.summary.addRaw(`<pre>
|
|
|
|
${entryDetails}
|
2022-06-02 20:21:10 +00:00
|
|
|
</pre>
|
2022-06-04 15:36:41 +00:00
|
|
|
</details>
|
|
|
|
`)
|
2021-12-07 19:29:37 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 04:44:08 +00:00
|
|
|
function getRestoredMessage(entry: CacheEntryListener, isCacheWriteOnly: boolean): string {
|
|
|
|
if (isCacheWriteOnly) {
|
|
|
|
return '(Entry not restored: cache is write-only)'
|
|
|
|
}
|
2022-06-03 04:39:55 +00:00
|
|
|
if (entry.restoredKey === undefined) {
|
2022-06-03 04:44:08 +00:00
|
|
|
return '(Entry not restored: no match found)'
|
2022-06-03 04:39:55 +00:00
|
|
|
}
|
|
|
|
if (entry.restoredKey === entry.requestedKey) {
|
2022-06-03 04:44:08 +00:00
|
|
|
return '(Entry restored: exact match found)'
|
2022-06-03 04:39:55 +00:00
|
|
|
}
|
2022-06-03 04:44:08 +00:00
|
|
|
return '(Entry restored: partial match found)'
|
2022-06-03 04:39:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 04:44:08 +00:00
|
|
|
function getSavedMessage(entry: CacheEntryListener, isCacheReadOnly: boolean): string {
|
2022-06-06 20:57:03 +00:00
|
|
|
if (entry.unsaved) {
|
|
|
|
return `(Entry not saved: ${entry.unsaved})`
|
2022-06-03 04:39:55 +00:00
|
|
|
}
|
|
|
|
if (entry.savedKey === undefined) {
|
2022-06-03 04:44:08 +00:00
|
|
|
if (isCacheReadOnly) {
|
|
|
|
return '(Entry not saved: cache is read-only)'
|
|
|
|
}
|
|
|
|
return '(Entry not saved: reason unknown)'
|
2022-06-03 04:39:55 +00:00
|
|
|
}
|
|
|
|
if (entry.savedSize === 0) {
|
2022-06-03 04:44:08 +00:00
|
|
|
return '(Entry not saved: entry with key already exists)'
|
2022-06-03 04:39:55 +00:00
|
|
|
}
|
|
|
|
return '(Entry saved)'
|
|
|
|
}
|
|
|
|
|
2021-12-07 19:29:37 +00:00
|
|
|
function getCount(
|
|
|
|
cacheEntries: CacheEntryListener[],
|
|
|
|
predicate: (value: CacheEntryListener) => number | undefined
|
|
|
|
): number {
|
|
|
|
return cacheEntries.filter(e => predicate(e) !== undefined).length
|
|
|
|
}
|
|
|
|
|
2022-06-04 15:36:41 +00:00
|
|
|
function getSize(
|
2022-06-02 20:21:10 +00:00
|
|
|
cacheEntries: CacheEntryListener[],
|
|
|
|
predicate: (value: CacheEntryListener) => number | undefined
|
|
|
|
): number {
|
2022-06-04 15:36:41 +00:00
|
|
|
const bytes = cacheEntries.map(e => predicate(e) ?? 0).reduce((p, v) => p + v, 0)
|
2022-06-02 20:21:10 +00:00
|
|
|
return Math.round(bytes / (1024 * 1024))
|
2021-12-07 19:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatSize(bytes: number | undefined): string {
|
2022-06-06 20:57:03 +00:00
|
|
|
if (bytes === undefined || bytes === 0) {
|
2021-12-07 19:29:37 +00:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return `${Math.round(bytes / (1024 * 1024))} MB (${bytes} B)`
|
|
|
|
}
|