mirror of
https://github.com/weaveworks/scope.git
synced 2026-08-18 03:46:45 +00:00
Improved the duration parsing on frontend.
This commit is contained in:
@@ -86,6 +86,20 @@ export function ipToPaddedString(value) {
|
||||
return value.match(/\d+/g).map(padToThreeDigits).join('.');
|
||||
}
|
||||
|
||||
// Doing the manual parsing because `duration.humanize()` would sometimes round up the period,
|
||||
// while we always want a rounded down value for consistency with other values sent by backend.
|
||||
export function humanizedRoundedDownDuration(duration) {
|
||||
let humanizedDuration = 'now';
|
||||
['second', 'minute', 'hour', 'day', 'month', 'year'].forEach((period) => {
|
||||
const durationAsPeriod = Math.floor(duration.as(period));
|
||||
if (durationAsPeriod > 0) {
|
||||
const pluralEnding = ((durationAsPeriod !== 11 && (durationAsPeriod % 10) === 1) ? '' : 's');
|
||||
humanizedDuration = `${durationAsPeriod} ${period}${pluralEnding}`;
|
||||
}
|
||||
});
|
||||
return humanizedDuration;
|
||||
}
|
||||
|
||||
// Formats metadata values. Add a key to the `formatters` obj
|
||||
// that matches the `dataType` of the field. You must return an Object
|
||||
// with the keys `value` and `title` defined.
|
||||
@@ -100,8 +114,10 @@ export function formatDataType(field, referenceTimestampStr = null) {
|
||||
title: timestamp.utc().toISOString()
|
||||
};
|
||||
},
|
||||
duration(durationString) {
|
||||
const humanizedDuration = moment.duration(Number(durationString), 'seconds').humanize();
|
||||
duration(durationSecondsString) {
|
||||
const duration = moment.duration(Number(durationSecondsString), 'seconds');
|
||||
const humanizedDuration = humanizedRoundedDownDuration(duration);
|
||||
|
||||
return {
|
||||
value: humanizedDuration,
|
||||
title: humanizedDuration,
|
||||
|
||||
Reference in New Issue
Block a user