mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 17:52:30 +08:00
Formatters: speed up toDuration() (#44345)
This commit is contained in:
@ -20,6 +20,17 @@ export enum Interval {
|
|||||||
Millisecond = 'millisecond',
|
Millisecond = 'millisecond',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const UNITS = [
|
||||||
|
Interval.Year,
|
||||||
|
Interval.Month,
|
||||||
|
Interval.Week,
|
||||||
|
Interval.Day,
|
||||||
|
Interval.Hour,
|
||||||
|
Interval.Minute,
|
||||||
|
Interval.Second,
|
||||||
|
Interval.Millisecond,
|
||||||
|
];
|
||||||
|
|
||||||
const INTERVALS_IN_SECONDS: IntervalsInSeconds = {
|
const INTERVALS_IN_SECONDS: IntervalsInSeconds = {
|
||||||
[Interval.Year]: 31536000,
|
[Interval.Year]: 31536000,
|
||||||
[Interval.Month]: 2592000,
|
[Interval.Month]: 2592000,
|
||||||
@ -206,17 +217,6 @@ export function toDuration(size: number, decimals: DecimalCount, timeScale: Inte
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
const units = [
|
|
||||||
{ long: Interval.Year },
|
|
||||||
{ long: Interval.Month },
|
|
||||||
{ long: Interval.Week },
|
|
||||||
{ long: Interval.Day },
|
|
||||||
{ long: Interval.Hour },
|
|
||||||
{ long: Interval.Minute },
|
|
||||||
{ long: Interval.Second },
|
|
||||||
{ long: Interval.Millisecond },
|
|
||||||
];
|
|
||||||
|
|
||||||
// convert $size to milliseconds
|
// convert $size to milliseconds
|
||||||
// intervals_in_seconds uses seconds (duh), convert them to milliseconds here to minimize floating point errors
|
// intervals_in_seconds uses seconds (duh), convert them to milliseconds here to minimize floating point errors
|
||||||
size *= INTERVALS_IN_SECONDS[timeScale] * 1000;
|
size *= INTERVALS_IN_SECONDS[timeScale] * 1000;
|
||||||
@ -231,13 +231,13 @@ export function toDuration(size: number, decimals: DecimalCount, timeScale: Inte
|
|||||||
decimalsCount = decimals as number;
|
decimalsCount = decimals as number;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < units.length && decimalsCount >= 0; i++) {
|
for (let i = 0; i < UNITS.length && decimalsCount >= 0; i++) {
|
||||||
const interval = INTERVALS_IN_SECONDS[units[i].long] * 1000;
|
const interval = INTERVALS_IN_SECONDS[UNITS[i]] * 1000;
|
||||||
const value = size / interval;
|
const value = size / interval;
|
||||||
if (value >= 1 || decrementDecimals) {
|
if (value >= 1 || decrementDecimals) {
|
||||||
decrementDecimals = true;
|
decrementDecimals = true;
|
||||||
const floor = Math.floor(value);
|
const floor = Math.floor(value);
|
||||||
const unit = units[i].long + (floor !== 1 ? 's' : '');
|
const unit = UNITS[i] + (floor !== 1 ? 's' : '');
|
||||||
strings.push(floor + ' ' + unit);
|
strings.push(floor + ' ' + unit);
|
||||||
size = size % interval;
|
size = size % interval;
|
||||||
decimalsCount--;
|
decimalsCount--;
|
||||||
|
Reference in New Issue
Block a user