Skip to content

Commit

Permalink
format stats as html table
Browse files Browse the repository at this point in the history
  • Loading branch information
planetmarshall committed Nov 22, 2024
1 parent d9b4eb6 commit d340fc1
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 12 deletions.
64 changes: 62 additions & 2 deletions __tests__/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,66 @@ Copyright (C) 2009-2024 Joel Rosdahl and other contributors
See <https://ccache.dev/credits.html> for a complete list of contributors.
This program is free software; etc etc.`;
expect(common.parseCCacheVersion(ccacheOutput)).toStrictEqual([4,10,2]);
expect(common.parseCCacheVersion(ccacheOutput)).toStrictEqual([4, 10, 2]);
});
});

test('format JSON stats output as table data', () => {
const stats = `{
"autoconf_test": 0,
"bad_compiler_arguments": 0,
"bad_input_file": 0,
"bad_output_file": 0,
"cache_miss": 3965,
"cache_size_kibibyte": 3511716,
"called_for_link": 0,
"called_for_preprocessing": 0,
"cleanups_performed": 0,
"compile_failed": 0,
"compiler_check_failed": 0,
"compiler_produced_empty_output": 0,
"compiler_produced_no_output": 0,
"compiler_produced_stdout": 0,
"could_not_find_compiler": 0,
"could_not_use_modules": 0,
"could_not_use_precompiled_header": 0,
"direct_cache_hit": 254,
"direct_cache_miss": 3965,
"disabled": 0,
"error_hashing_extra_file": 0,
"files_in_cache": 16868,
"internal_error": 0,
"local_storage_hit": 254,
"local_storage_miss": 3965,
"local_storage_read_hit": 508,
"local_storage_read_miss": 7930,
"local_storage_write": 7930,
"max_cache_size_kibibyte": 5242880,
"max_files_in_cache": 0,
"missing_cache_file": 0,
"modified_input_file": 0,
"multiple_source_files": 0,
"no_input_file": 0,
"output_to_stdout": 0,
"preprocessed_cache_hit": 0,
"preprocessed_cache_miss": 3965,
"preprocessor_error": 0,
"recache": 0,
"remote_storage_error": 0,
"remote_storage_hit": 0,
"remote_storage_miss": 0,
"remote_storage_read_hit": 0,
"remote_storage_read_miss": 0,
"remote_storage_timeout": 0,
"remote_storage_write": 0,
"stats_updated_timestamp": 1732222833,
"stats_zeroed_timestamp": 1732201768,
"unsupported_code_directive": 0,
"unsupported_compiler_option": 0,
"unsupported_environment_variable": 0,
"unsupported_source_language": 0
}`
expect(common.formatStatsAsTable(stats)).toStrictEqual([[
{data: "Cache hits", header: true }, "254 / 4219", "6.02%"
]]);
})
});
18 changes: 18 additions & 0 deletions dist/restore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59570,8 +59570,13 @@ const external_process_namespaceObject = require("process");
var cache = __nccwpck_require__(7799);
;// CONCATENATED MODULE: ./src/common.ts

/**
* Parse the output of ccache --version to extract the semantic version components
* @param ccacheOutput
*/
function parseCCacheVersion(ccacheOutput) {
const firstLine = ccacheOutput.split("\n", 1)[0];
// short version of https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
const semver = /(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)/;
const result = firstLine.match(semver);
if (!result) {
Expand All @@ -59582,6 +59587,19 @@ function parseCCacheVersion(ccacheOutput) {
}
return [Number.parseInt(result[1]), Number.parseInt(result[2]), Number.parseInt(result[3])];
}
function formatStatsAsTable(statsJson) {
const stats = JSON.parse(statsJson);
if (stats === undefined) {
return null;
}
// @ts-ignore
const hits = stats["direct_cache_hit"] + stats["preprocessed_cache_hit"];
const misses = stats["cache_miss"];
const total = hits + misses;
return [
[{ data: "Cache hits", header: true }, `${hits} / ${total}`, `${((hits / total) * 100).toPrecision(3)}%`]
];
}
function cacheDir(ccacheVariant) {
const ghWorkSpace = process.env.GITHUB_WORKSPACE || "unreachable, make ncc happy";
if (ccacheVariant === "ccache") {
Expand Down
32 changes: 28 additions & 4 deletions dist/save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59557,8 +59557,13 @@ var external_path_ = __nccwpck_require__(1017);
var external_path_default = /*#__PURE__*/__nccwpck_require__.n(external_path_);
;// CONCATENATED MODULE: ./src/common.ts

/**
* Parse the output of ccache --version to extract the semantic version components
* @param ccacheOutput
*/
function parseCCacheVersion(ccacheOutput) {
const firstLine = ccacheOutput.split("\n", 1)[0];
// short version of https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
const semver = /(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)/;
const result = firstLine.match(semver);
if (!result) {
Expand All @@ -59569,6 +59574,19 @@ function parseCCacheVersion(ccacheOutput) {
}
return [Number.parseInt(result[1]), Number.parseInt(result[2]), Number.parseInt(result[3])];
}
function formatStatsAsTable(statsJson) {
const stats = JSON.parse(statsJson);
if (stats === undefined) {
return null;
}
// @ts-ignore
const hits = stats["direct_cache_hit"] + stats["preprocessed_cache_hit"];
const misses = stats["cache_miss"];
const total = hits + misses;
return [
[{ data: "Cache hits", header: true }, `${hits} / ${total}`, `${((hits / total) * 100).toPrecision(3)}%`]
];
}
function cacheDir(ccacheVariant) {
const ghWorkSpace = process.env.GITHUB_WORKSPACE || "unreachable, make ncc happy";
if (ccacheVariant === "ccache") {
Expand Down Expand Up @@ -59648,10 +59666,16 @@ async function run(earlyExit) {
core.warning("job summary requested but is not supported");
}
else {
const jsonStats = await exec.getExecOutput(ccacheVariant, ["--print-stats", "--format=json"]);
await core.summary.addHeading(jobSummaryTitle)
.addCodeBlock(jsonStats.stdout, "json")
.write();
const jsonStats = await exec.getExecOutput(ccacheVariant, ["--print-stats", "--format=json"], { silent: true });
const formattedStats = formatStatsAsTable(jsonStats.stdout);
if (formattedStats === null) {
core.warning("Could not parse json stats");
}
else {
await core.summary.addHeading(jobSummaryTitle)
.addTable(formattedStats)
.write();
}
}
}
core.endGroup();
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module.exports = {
transform: {
"^.+.tsx?$": ["ts-jest",{}],
},
};
};
20 changes: 20 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import path from "path";
import {SummaryTableRow} from "@actions/core/lib/summary";

type Version = [number,number,number];

/**
* Parse the output of ccache --version to extract the semantic version components
* @param ccacheOutput
*/
export function parseCCacheVersion(ccacheOutput: string) : Version | null {
const firstLine = ccacheOutput.split("\n", 1)[0];
// short version of https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
const semver = /(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)/;
const result = firstLine.match(semver);

Expand All @@ -18,6 +24,20 @@ export function parseCCacheVersion(ccacheOutput: string) : Version | null {
return [Number.parseInt(result[1]), Number.parseInt(result[2]), Number.parseInt(result[3])];
}

export function formatStatsAsTable(statsJson: string) : SummaryTableRow[] | null {
const stats = JSON.parse(statsJson);
if (stats === undefined) {
return null;
}
// @ts-ignore
const hits = stats["direct_cache_hit"] + stats["preprocessed_cache_hit"];
const misses = stats["cache_miss"];
const total = hits + misses;
return [
[{data: "Cache hits", header: true}, `${hits} / ${total}`, `${((hits / total) * 100).toPrecision(3)}%`]
];
}

export function cacheDir(ccacheVariant: string): string {
const ghWorkSpace = process.env.GITHUB_WORKSPACE || "unreachable, make ncc happy";
if (ccacheVariant === "ccache") {
Expand Down
16 changes: 11 additions & 5 deletions src/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,17 @@ async function run(earlyExit : boolean | undefined) : Promise<void> {
if (!await hasJsonStats(ccacheVariant)) {
core.warning("job summary requested but is not supported")
} else {
const jsonStats = await exec.getExecOutput(ccacheVariant, ["--print-stats", "--format=json"]);
await core.summary
.addHeading(jobSummaryTitle)
.addCodeBlock(jsonStats.stdout, "json")
.write()
const jsonStats =
await exec.getExecOutput(ccacheVariant, ["--print-stats", "--format=json"], {silent: true});
const formattedStats = common.formatStatsAsTable(jsonStats.stdout)
if (formattedStats === null) {
core.warning("Could not parse json stats")
} else {
await core.summary
.addHeading(jobSummaryTitle)
.addTable(formattedStats)
.write()
}
}
}

Expand Down

0 comments on commit d340fc1

Please sign in to comment.