Add telemetry for TRAP caching

This commit is contained in:
Edoardo Pirovano 2022-08-15 14:44:43 +01:00
parent ca10a6d552
commit 4139682b64
No known key found for this signature in database
GPG key ID: 047556B5D93FFE28
63 changed files with 1195 additions and 126 deletions

View file

@ -2,6 +2,7 @@ import * as fs from "fs";
import * as path from "path";
import * as cache from "@actions/cache";
import getFolderSize from "get-folder-size";
import * as actionsUtil from "./actions-util";
import { CodeQL, CODEQL_VERSION_BETTER_RESOLVE_LANGUAGES } from "./codeql";
@ -118,12 +119,19 @@ export async function downloadTrapCaches(
return result;
}
/**
* Possibly upload TRAP caches to the Actions cache.
* @param codeql The CodeQL instance to use.
* @param config The configuration for this workflow.
* @param logger A logger to record some informational messages to.
* @returns Whether the TRAP caches were uploaded.
*/
export async function uploadTrapCaches(
codeql: CodeQL,
config: Config,
logger: Logger
): Promise<void> {
if (!(await actionsUtil.isAnalyzingDefaultBranch())) return; // Only upload caches from the default branch
): Promise<boolean> {
if (!(await actionsUtil.isAnalyzingDefaultBranch())) return false; // Only upload caches from the default branch
const toAwait: Array<Promise<number>> = [];
for (const language of config.languages) {
@ -138,6 +146,7 @@ export async function uploadTrapCaches(
toAwait.push(cache.saveCache([cacheDir], key));
}
await Promise.all(toAwait);
return true;
}
export async function getLanguagesSupportingCaching(
@ -175,6 +184,23 @@ export async function getLanguagesSupportingCaching(
return result;
}
export async function getTotalCacheSize(
trapCaches: Partial<Record<Language, string>>
): Promise<number> {
const sizes = await Promise.all(
Object.values(trapCaches).map(async (cacheDir) => {
return new Promise<number>((resolve) => {
getFolderSize(cacheDir, (err, size) => {
// Ignore file system errors when getting the size. It's only used for telemetry anyway.
if (err) resolve(0);
resolve(size);
});
});
})
);
return sizes.reduce((a, b) => a + b, 0);
}
async function cacheKey(
codeql: CodeQL,
language: Language,