Skip to content

Commit

Permalink
Improve duration formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Mercer committed Oct 10, 2024
1 parent edd7713 commit 222ac62
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 14 deletions.
13 changes: 13 additions & 0 deletions lib/logging.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/logging.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/setup-codeql.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/setup-codeql.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions lib/tools-download.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/tools-download.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,17 @@ export function withGroup<T>(groupName: string, f: () => T): T {
core.endGroup();
}
}

/** Format a duration for use in logs. */
export function formatDuration(durationMs: number) {
if (durationMs < 1000) {
return `${durationMs}ms`;
}

if (durationMs < 60 * 1000) {
return `${(durationMs / 1000).toFixed(1)}s`;
}
const minutes = Math.floor(durationMs / (60 * 1000));
const seconds = Math.floor((durationMs % (60 * 1000)) / 1000);
return `${minutes}m${seconds}s`;
}
6 changes: 3 additions & 3 deletions src/setup-codeql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Feature,
FeatureEnablement,
} from "./feature-flags";
import { Logger } from "./logging";
import { formatDuration, Logger } from "./logging";
import * as tar from "./tar";
import {
downloadAndExtract,
Expand Down Expand Up @@ -557,9 +557,9 @@ export const downloadCodeQL = async function (
);

logger.info(
`Added CodeQL bundle to the tool cache (${Math.round(
`Added CodeQL bundle to the tool cache (${formatDuration(
performance.now() - toolcacheStart,
)} ms).`,
)}).`,
);

// Defensive check: we expect `cacheDir` to copy the bundle to a new location.
Expand Down
14 changes: 10 additions & 4 deletions src/tools-download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { https } from "follow-redirects";
import { v4 as uuidV4 } from "uuid";

import { Feature, FeatureEnablement } from "./feature-flags";
import { Logger } from "./logging";
import { formatDuration, Logger } from "./logging";
import * as tar from "./tar";
import { cleanUpGlob } from "./util";

Expand Down Expand Up @@ -78,7 +78,9 @@ export async function downloadAndExtract(
performance.now() - toolsInstallStart,
);
logger.info(
`Finished downloading and extracting CodeQL bundle to ${extractedBundlePath} (${combinedDurationMs} ms).`,
`Finished downloading and extracting CodeQL bundle to ${extractedBundlePath} (${formatDuration(
combinedDurationMs,
)}).`,
);

return {
Expand All @@ -105,7 +107,9 @@ export async function downloadAndExtract(
const downloadDurationMs = Math.round(performance.now() - toolsDownloadStart);

logger.info(
`Finished downloading CodeQL bundle to ${archivedBundlePath} (${downloadDurationMs} ms).`,
`Finished downloading CodeQL bundle to ${archivedBundlePath} (${formatDuration(
downloadDurationMs,
)}).`,
);

let extractedBundlePath: string;
Expand All @@ -122,7 +126,9 @@ export async function downloadAndExtract(
);
extractionDurationMs = Math.round(performance.now() - extractionStart);
logger.info(
`Finished extracting CodeQL bundle to ${extractedBundlePath} (${extractionDurationMs} ms).`,
`Finished extracting CodeQL bundle to ${extractedBundlePath} (${formatDuration(
extractionDurationMs,
)}).`,
);
} finally {
await cleanUpGlob(archivedBundlePath, "CodeQL bundle archive", logger);
Expand Down

0 comments on commit 222ac62

Please sign in to comment.