fix(web): properly format sub-millisecond durations in target status page

Previously, scrapes durations that are very short (e.g., connection refused)
could show as empty (durations under 1 millisecond).

This commit ensures that sub-millisecond durations are correctly
displayed as "0ms" or "1ms" when necessary.

- Adjusted `humanizeDuration` to round sub-millisecond durations to the
  nearest millisecond.
- Updated unit tests to verify the correct handling of sub-millisecond
  values.

Signed-off-by: Julien <roidelapluie@o11y.eu>
This commit is contained in:
Julien 2024-09-20 11:59:12 +02:00
parent 005bd33fe2
commit 7ebda924b8
2 changed files with 9 additions and 0 deletions

View file

@ -57,6 +57,12 @@ describe("humanizeDuration", () => {
expect(humanizeDuration(0)).toBe("0s");
});
test("formats submilliseconds correctly", () => {
expect(humanizeDuration(0.1)).toBe("0ms");
expect(humanizeDuration(0.6)).toBe("1ms");
expect(humanizeDuration(0.000001)).toBe("0ms");
});
test("formats milliseconds correctly", () => {
expect(humanizeDuration(1)).toBe("1ms");
expect(humanizeDuration(999)).toBe("999ms");

View file

@ -86,6 +86,9 @@ const formatDuration = (
r.push(`${v}${unit}`);
}
}
if (r.length == 0 && unit == "ms") {
r.push(`${Math.round(ms)}ms`)
}
}
return sign + r.join(componentSeparator || "");