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
28 lines (23 loc) • 1.11 kB
JavaScript
;
var _ = require('lodash');
var moment = require('moment');
// map of moment's short/long unit ids and elasticsearch's long unit ids
// to their value in milliseconds
var vals = _.transform([['ms', 'milliseconds', 'millisecond'], ['s', 'seconds', 'second', 'sec'], ['m', 'minutes', 'minute', 'min'], ['h', 'hours', 'hour'], ['d', 'days', 'day'], ['w', 'weeks', 'week'], ['M', 'months', 'month'], ['quarter'], ['y', 'years', 'year']], function (vals, units) {
var normal = moment.normalizeUnits(units[0]);
var val = moment.duration(1, normal).asMilliseconds();
[].concat(normal, units).forEach(function (unit) {
vals[unit] = val;
});
}, {});
// match any key from the vals object prececed by an optional number
var parseRE = new RegExp('^(\\d+(?:\\.\\d*)?)?\\s*(' + _.keys(vals).join('|') + ')$');
module.exports = function (expr) {
var match = expr.match(parseRE);
if (match) {
if (match[2] === 'M' && match[1] !== '1') {
throw new Error('Invalid interval. 1M is only valid monthly interval.');
}
return parseFloat(match[1] || 1) * vals[match[2]];
}
};