mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-12 22:37:27 -08:00
Improve humanizeDuration() code and output
Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
parent
00d2a49f4d
commit
2f95bbe570
|
@ -83,30 +83,33 @@ export function parseTime(timeText: string): number {
|
||||||
export const now = (): number => dayjs().valueOf();
|
export const now = (): number => dayjs().valueOf();
|
||||||
|
|
||||||
export const humanizeDuration = (milliseconds: number): string => {
|
export const humanizeDuration = (milliseconds: number): string => {
|
||||||
const sign = milliseconds < 0 ? "-" : "";
|
if (milliseconds === 0) {
|
||||||
const unsignedMillis = milliseconds < 0 ? -1 * milliseconds : milliseconds;
|
return "0s";
|
||||||
const duration = dayjs.duration(unsignedMillis, "ms");
|
}
|
||||||
|
|
||||||
|
const duration = dayjs.duration(Math.abs(milliseconds), "ms");
|
||||||
const ms = Math.floor(duration.milliseconds());
|
const ms = Math.floor(duration.milliseconds());
|
||||||
const s = Math.floor(duration.seconds());
|
const s = Math.floor(duration.seconds());
|
||||||
const m = Math.floor(duration.minutes());
|
const m = Math.floor(duration.minutes());
|
||||||
const h = Math.floor(duration.hours());
|
const h = Math.floor(duration.hours());
|
||||||
const d = Math.floor(duration.asDays());
|
const d = Math.floor(duration.asDays());
|
||||||
|
const parts: string[] = [];
|
||||||
if (d !== 0) {
|
if (d !== 0) {
|
||||||
return `${sign}${d}d ${h}h ${m}m ${s}s`;
|
parts.push(`${d}d`);
|
||||||
}
|
}
|
||||||
if (h !== 0) {
|
if (h !== 0) {
|
||||||
return `${sign}${h}h ${m}m ${s}s`;
|
parts.push(`${h}h`);
|
||||||
}
|
}
|
||||||
if (m !== 0) {
|
if (m !== 0) {
|
||||||
return `${sign}${m}m ${s}s`;
|
parts.push(`${m}m`);
|
||||||
}
|
}
|
||||||
if (s !== 0) {
|
if (s !== 0) {
|
||||||
return `${sign}${s}.${ms}s`;
|
parts.push(`${s}s`);
|
||||||
}
|
}
|
||||||
if (unsignedMillis > 0) {
|
if (ms !== 0) {
|
||||||
return `${sign}${unsignedMillis.toFixed(3)}ms`;
|
parts.push(`${ms}ms`);
|
||||||
}
|
}
|
||||||
return "0s";
|
return (milliseconds < 0 ? "-" : "") + parts.join(" ");
|
||||||
};
|
};
|
||||||
|
|
||||||
export const formatRelative = (
|
export const formatRelative = (
|
||||||
|
|
Loading…
Reference in a new issue