Skip to content

Commit 222ac62

Browse files
committed
Improve duration formatting
1 parent edd7713 commit 222ac62

9 files changed

+49
-14
lines changed

lib/logging.js

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/logging.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/setup-codeql.js

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/setup-codeql.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/tools-download.js

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/tools-download.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/logging.ts

+14
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,17 @@ export function withGroup<T>(groupName: string, f: () => T): T {
4040
core.endGroup();
4141
}
4242
}
43+
44+
/** Format a duration for use in logs. */
45+
export function formatDuration(durationMs: number) {
46+
if (durationMs < 1000) {
47+
return `${durationMs}ms`;
48+
}
49+
50+
if (durationMs < 60 * 1000) {
51+
return `${(durationMs / 1000).toFixed(1)}s`;
52+
}
53+
const minutes = Math.floor(durationMs / (60 * 1000));
54+
const seconds = Math.floor((durationMs % (60 * 1000)) / 1000);
55+
return `${minutes}m${seconds}s`;
56+
}

src/setup-codeql.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
Feature,
1616
FeatureEnablement,
1717
} from "./feature-flags";
18-
import { Logger } from "./logging";
18+
import { formatDuration, Logger } from "./logging";
1919
import * as tar from "./tar";
2020
import {
2121
downloadAndExtract,
@@ -557,9 +557,9 @@ export const downloadCodeQL = async function (
557557
);
558558

559559
logger.info(
560-
`Added CodeQL bundle to the tool cache (${Math.round(
560+
`Added CodeQL bundle to the tool cache (${formatDuration(
561561
performance.now() - toolcacheStart,
562-
)} ms).`,
562+
)}).`,
563563
);
564564

565565
// Defensive check: we expect `cacheDir` to copy the bundle to a new location.

src/tools-download.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { https } from "follow-redirects";
77
import { v4 as uuidV4 } from "uuid";
88

99
import { Feature, FeatureEnablement } from "./feature-flags";
10-
import { Logger } from "./logging";
10+
import { formatDuration, Logger } from "./logging";
1111
import * as tar from "./tar";
1212
import { cleanUpGlob } from "./util";
1313

@@ -78,7 +78,9 @@ export async function downloadAndExtract(
7878
performance.now() - toolsInstallStart,
7979
);
8080
logger.info(
81-
`Finished downloading and extracting CodeQL bundle to ${extractedBundlePath} (${combinedDurationMs} ms).`,
81+
`Finished downloading and extracting CodeQL bundle to ${extractedBundlePath} (${formatDuration(
82+
combinedDurationMs,
83+
)}).`,
8284
);
8385

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

107109
logger.info(
108-
`Finished downloading CodeQL bundle to ${archivedBundlePath} (${downloadDurationMs} ms).`,
110+
`Finished downloading CodeQL bundle to ${archivedBundlePath} (${formatDuration(
111+
downloadDurationMs,
112+
)}).`,
109113
);
110114

111115
let extractedBundlePath: string;
@@ -122,7 +126,9 @@ export async function downloadAndExtract(
122126
);
123127
extractionDurationMs = Math.round(performance.now() - extractionStart);
124128
logger.info(
125-
`Finished extracting CodeQL bundle to ${extractedBundlePath} (${extractionDurationMs} ms).`,
129+
`Finished extracting CodeQL bundle to ${extractedBundlePath} (${formatDuration(
130+
extractionDurationMs,
131+
)}).`,
126132
);
127133
} finally {
128134
await cleanUpGlob(archivedBundlePath, "CodeQL bundle archive", logger);

0 commit comments

Comments
 (0)