kibana-riya
Version:
Kibana is an open source (Apache Licensed), browser based analytics and search dashboard for Elasticsearch. Kibana is a snap to setup and start using. Kibana strives to be easy to get started with, while also being flexible and powerful, just like Elastic
49 lines (41 loc) • 1.1 kB
JavaScript
import moment from 'moment';
import dateMath from '@elastic/datemath';
export default function () {
let unitsDesc = dateMath.unitsDesc;
let largeMax = unitsDesc.indexOf('M');
/**
* Convert a moment.duration into an es
* compatible expression, and provide
* associated metadata
*
* @param {moment.duration} duration
* @return {object}
*/
function esDuration(duration) {
for (let i = 0; i < unitsDesc.length; i++) {
let unit = unitsDesc[i];
let val = duration.as(unit);
// find a unit that rounds neatly
if (val >= 1 && Math.floor(val) === val) {
// if the unit is "large", like years, but
// isn't set to 1 ES will puke. So keep going until
// we get out of the "large" units
if (i <= largeMax && val !== 1) {
continue;
}
return {
value: val,
unit: unit,
expression: val + unit
};
}
}
let ms = duration.as('ms');
return {
value: ms,
unit: 'ms',
expression: ms + 'ms'
};
}
return esDuration;
};