Production package example: datemath (#16531) (#16719)

This commit is contained in:
Kim Joar Bekkelund 2018-02-13 23:58:49 +01:00 committed by GitHub
parent 358d0d82bb
commit 352966720e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 2201 additions and 69 deletions

View file

@ -19,6 +19,7 @@ module.exports = {
files: [
'.eslintrc.js',
'packages/kbn-build/**/*',
'packages/kbn-datemath/**/*.js',
'packages/kbn-plugin-generator/**/*',
],
plugins: ['prettier'],

View file

@ -75,13 +75,13 @@
"url": "https://github.com/elastic/kibana.git"
},
"dependencies": {
"@elastic/datemath": "4.0.2",
"@elastic/eui": "0.0.19",
"@elastic/filesaver": "1.1.2",
"@elastic/numeral": "2.3.1",
"@elastic/ui-ace": "0.2.3",
"@kbn/babel-preset": "link:packages/kbn-babel-preset",
"@kbn/build": "link:packages/kbn-build",
"@kbn/datemath": "link:packages/kbn-datemath",
"@kbn/test-subj-selector": "link:packages/kbn-test-subj-selector",
"accept-language-parser": "1.2.0",
"angular": "1.6.5",

View file

@ -0,0 +1,13 @@
{
"presets": [["env", {
"targets": {
"node": "current",
"browsers": [
"last 2 versions",
"> 5%",
"Safari 7",
]
}
}]],
"plugins": ["add-module-exports"]
}

View file

@ -0,0 +1,20 @@
{
"name": "@kbn/datemath",
"version": "5.0.0",
"description": "elasticsearch datemath parser, used in kibana",
"license": "Apache-2.0",
"private": true,
"main": "target/index.js",
"scripts": {
"build": "babel src --out-dir target",
"kbn:bootstrap": "yarn build"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-env": "^1.6.1"
},
"dependencies": {
"moment": "^2.13.0"
}
}

View file

@ -0,0 +1,3 @@
# datemath
Datemath string parser used in Kibana

View file

@ -0,0 +1,132 @@
import moment from 'moment';
const units = ['y', 'M', 'w', 'd', 'h', 'm', 's', 'ms'];
const unitsDesc = units;
const unitsAsc = [...unitsDesc].reverse();
const isDate = d => Object.prototype.toString.call(d) === '[object Date]';
const isValidDate = d => isDate(d) && !isNaN(d.valueOf());
/*
* This is a simplified version of elasticsearch's date parser.
* If you pass in a momentjs instance as the third parameter the calculation
* will be done using this (and its locale settings) instead of the one bundled
* with this library.
*/
function parse(
text,
{ roundUp = false, momentInstance = moment, forceNow } = {}
) {
if (!text) return undefined;
if (momentInstance.isMoment(text)) return text;
if (isDate(text)) return momentInstance(text);
if (forceNow !== undefined && !isValidDate(forceNow)) {
throw new Error('forceNow must be a valid Date');
}
let time;
let mathString = '';
let index;
let parseString;
if (text.substring(0, 3) === 'now') {
time = momentInstance(forceNow);
mathString = text.substring('now'.length);
} else {
index = text.indexOf('||');
if (index === -1) {
parseString = text;
mathString = ''; // nothing else
} else {
parseString = text.substring(0, index);
mathString = text.substring(index + 2);
}
// We're going to just require ISO8601 timestamps, k?
time = momentInstance(parseString);
}
if (!mathString.length) {
return time;
}
return parseDateMath(mathString, time, roundUp);
}
function parseDateMath(mathString, time, roundUp) {
const dateTime = time;
const len = mathString.length;
let i = 0;
while (i < len) {
const c = mathString.charAt(i++);
let type;
let num;
let unit;
if (c === '/') {
type = 0;
} else if (c === '+') {
type = 1;
} else if (c === '-') {
type = 2;
} else {
return;
}
if (isNaN(mathString.charAt(i))) {
num = 1;
} else if (mathString.length === 2) {
num = mathString.charAt(i);
} else {
const numFrom = i;
while (!isNaN(mathString.charAt(i))) {
i++;
if (i > 10) return;
}
num = parseInt(mathString.substring(numFrom, i), 10);
}
if (type === 0) {
// rounding is only allowed on whole, single, units (eg M or 1M, not 0.5M or 2M)
if (num !== 1) {
return;
}
}
unit = mathString.charAt(i++);
// append additional characters in the unit
for (let j = i; j < len; j++) {
const unitChar = mathString.charAt(i);
if (/[a-z]/i.test(unitChar)) {
unit += unitChar;
i++;
} else {
break;
}
}
if (units.indexOf(unit) === -1) {
return;
} else {
if (type === 0) {
if (roundUp) dateTime.endOf(unit);
else dateTime.startOf(unit);
} else if (type === 1) {
dateTime.add(num, unit);
} else if (type === 2) {
dateTime.subtract(num, unit);
}
}
}
return dateTime;
}
export default {
parse: parse,
units: Object.freeze(units),
unitsAsc: Object.freeze(unitsAsc),
unitsDesc: Object.freeze(unitsDesc),
};

View file

@ -0,0 +1,407 @@
import dateMath from '../src/index';
import moment from 'moment';
import sinon from 'sinon';
import expect from 'expect.js';
/**
* Require a new instance of the moment library, bypassing the require cache.
* This is needed, since we are trying to test whether or not this library works
* when passing in a different configured moment instance. If we would change
* the locales on the imported moment, it would automatically apply
* to the source code, even without passing it in to the method, since they share
* the same global state. This method avoids this, by loading a separate instance
* of moment, by deleting the require cache and require the library again.
*/
function momentClone() {
delete require.cache[require.resolve('moment')];
return require('moment');
}
describe('dateMath', function() {
// Test each of these intervals when testing relative time
const spans = ['s', 'm', 'h', 'd', 'w', 'M', 'y', 'ms'];
const anchor = '2014-01-01T06:06:06.666Z';
const anchoredDate = new Date(Date.parse(anchor));
const unix = moment(anchor).valueOf();
const format = 'YYYY-MM-DDTHH:mm:ss.SSSZ';
let clock;
describe('errors', function() {
it('should return undefined if passed something falsy', function() {
expect(dateMath.parse()).to.be(undefined);
});
it('should return undefined if I pass an operator besides [+-/]', function() {
expect(dateMath.parse('now&1d')).to.be(undefined);
});
it(
'should return undefined if I pass a unit besides' + spans.toString(),
function() {
expect(dateMath.parse('now+5f')).to.be(undefined);
}
);
it('should return undefined if rounding unit is not 1', function() {
expect(dateMath.parse('now/2y')).to.be(undefined);
expect(dateMath.parse('now/0.5y')).to.be(undefined);
});
it('should not go into an infinite loop when missing a unit', function() {
expect(dateMath.parse('now-0')).to.be(undefined);
expect(dateMath.parse('now-00')).to.be(undefined);
expect(dateMath.parse('now-000')).to.be(undefined);
});
describe('forceNow', function() {
it('should throw an Error if passed a string', function() {
const fn = () =>
dateMath.parse('now', { forceNow: '2000-01-01T00:00:00.000Z' });
expect(fn).to.throwError();
});
it('should throw an Error if passed a moment', function() {
expect(() =>
dateMath.parse('now', { forceNow: moment() })
).to.throwError();
});
it('should throw an Error if passed an invalid date', function() {
expect(() =>
dateMath.parse('now', { forceNow: new Date('foobar') })
).to.throwError();
});
});
});
describe('objects and strings', function() {
let mmnt;
let date;
let string;
let now;
beforeEach(function() {
clock = sinon.useFakeTimers(unix);
now = moment();
mmnt = moment(anchor);
date = mmnt.toDate();
string = mmnt.format(format);
});
afterEach(function() {
clock.restore();
});
it('should return the same moment if passed a moment', function() {
expect(dateMath.parse(mmnt)).to.eql(mmnt);
});
it('should return a moment if passed a date', function() {
expect(dateMath.parse(date).format(format)).to.eql(mmnt.format(format));
});
it('should return a moment if passed an ISO8601 string', function() {
expect(dateMath.parse(string).format(format)).to.eql(mmnt.format(format));
});
it('should return the current time when parsing now', function() {
expect(dateMath.parse('now').format(format)).to.eql(now.format(format));
});
it('should use the forceNow parameter when parsing now', function() {
expect(
dateMath.parse('now', { forceNow: anchoredDate }).valueOf()
).to.eql(unix);
});
});
describe('subtraction', function() {
let now;
let anchored;
beforeEach(function() {
clock = sinon.useFakeTimers(unix);
now = moment();
anchored = moment(anchor);
});
afterEach(function() {
clock.restore();
});
[5, 12, 247].forEach(len => {
spans.forEach(span => {
const nowEx = `now-${len}${span}`;
const thenEx = `${anchor}||-${len}${span}`;
it('should return ' + len + span + ' ago', function() {
const parsed = dateMath.parse(nowEx).format(format);
expect(parsed).to.eql(now.subtract(len, span).format(format));
});
it('should return ' + len + span + ' before ' + anchor, function() {
const parsed = dateMath.parse(thenEx).format(format);
expect(parsed).to.eql(anchored.subtract(len, span).format(format));
});
it('should return ' + len + span + ' before forceNow', function() {
const parsed = dateMath
.parse(nowEx, { forceNow: anchoredDate })
.valueOf();
expect(parsed).to.eql(anchored.subtract(len, span).valueOf());
});
});
});
});
describe('addition', function() {
let now;
let anchored;
beforeEach(function() {
clock = sinon.useFakeTimers(unix);
now = moment();
anchored = moment(anchor);
});
afterEach(function() {
clock.restore();
});
[5, 12, 247].forEach(len => {
spans.forEach(span => {
const nowEx = `now+${len}${span}`;
const thenEx = `${anchor}||+${len}${span}`;
it('should return ' + len + span + ' from now', function() {
expect(dateMath.parse(nowEx).format(format)).to.eql(
now.add(len, span).format(format)
);
});
it('should return ' + len + span + ' after ' + anchor, function() {
expect(dateMath.parse(thenEx).format(format)).to.eql(
anchored.add(len, span).format(format)
);
});
it('should return ' + len + span + ' after forceNow', function() {
expect(
dateMath.parse(nowEx, { forceNow: anchoredDate }).valueOf()
).to.eql(anchored.add(len, span).valueOf());
});
});
});
});
describe('rounding', function() {
let now;
let anchored;
beforeEach(function() {
clock = sinon.useFakeTimers(unix);
now = moment();
anchored = moment(anchor);
});
afterEach(function() {
clock.restore();
});
spans.forEach(span => {
it(`should round now to the beginning of the ${span}`, function() {
expect(dateMath.parse('now/' + span).format(format)).to.eql(
now.startOf(span).format(format)
);
});
it(`should round now to the beginning of forceNow's ${span}`, function() {
expect(
dateMath.parse('now/' + span, { forceNow: anchoredDate }).valueOf()
).to.eql(anchored.startOf(span).valueOf());
});
it(`should round now to the end of the ${span}`, function() {
expect(
dateMath.parse('now/' + span, { roundUp: true }).format(format)
).to.eql(now.endOf(span).format(format));
});
it(`should round now to the end of forceNow's ${span}`, function() {
expect(
dateMath
.parse('now/' + span, { roundUp: true, forceNow: anchoredDate })
.valueOf()
).to.eql(anchored.endOf(span).valueOf());
});
});
});
describe('math and rounding', function() {
let now;
let anchored;
beforeEach(function() {
clock = sinon.useFakeTimers(unix);
now = moment();
anchored = moment(anchor);
});
afterEach(function() {
clock.restore();
});
it('should round to the nearest second with 0 value', function() {
const val = dateMath.parse('now-0s/s').format(format);
expect(val).to.eql(now.startOf('s').format(format));
});
it('should subtract 17s, rounded to the nearest second', function() {
const val = dateMath.parse('now-17s/s').format(format);
expect(val).to.eql(
now
.startOf('s')
.subtract(17, 's')
.format(format)
);
});
it('should add 555ms, rounded to the nearest millisecond', function() {
const val = dateMath.parse('now+555ms/ms').format(format);
expect(val).to.eql(
now
.add(555, 'ms')
.startOf('ms')
.format(format)
);
});
it('should subtract 555ms, rounded to the nearest second', function() {
const val = dateMath.parse('now-555ms/s').format(format);
expect(val).to.eql(
now
.subtract(555, 'ms')
.startOf('s')
.format(format)
);
});
it('should round weeks to Sunday by default', function() {
const val = dateMath.parse('now-1w/w');
expect(val.isoWeekday()).to.eql(7);
});
it('should round weeks based on the passed moment locale start of week setting', function() {
const m = momentClone();
// Define a locale, that has Tuesday as beginning of the week
m.defineLocale('x-test', {
week: { dow: 2 },
});
const val = dateMath.parse('now-1w/w', { momentInstance: m });
expect(val.isoWeekday()).to.eql(2);
});
it('should round up weeks based on the passed moment locale start of week setting', function() {
const m = momentClone();
// Define a locale, that has Tuesday as beginning of the week
m.defineLocale('x-test', {
week: { dow: 3 },
});
const val = dateMath.parse('now-1w/w', {
roundUp: true,
momentInstance: m,
});
// The end of the range (rounding up) should be the last day of the week (so one day before)
// our start of the week, that's why 3 - 1
expect(val.isoWeekday()).to.eql(3 - 1);
});
it('should round relative to forceNow', function() {
const val = dateMath
.parse('now-0s/s', { forceNow: anchoredDate })
.valueOf();
expect(val).to.eql(anchored.startOf('s').valueOf());
});
});
describe('used momentjs instance', function() {
it('should use the default moment instance if parameter not specified', function() {
const momentSpy = sinon.spy(moment, 'isMoment');
dateMath.parse('now');
expect(momentSpy.called).to.be(true);
momentSpy.restore();
});
it('should not use default moment instance if parameter is specified', function() {
const m = momentClone();
const momentSpy = sinon.spy(moment, 'isMoment');
const cloneSpy = sinon.spy(m, 'isMoment');
dateMath.parse('now', { momentInstance: m });
expect(momentSpy.called).to.be(false);
expect(cloneSpy.called).to.be(true);
momentSpy.restore();
cloneSpy.restore();
});
it('should work with multiple different instances', function() {
const m1 = momentClone();
const m2 = momentClone();
const m1Spy = sinon.spy(m1, 'isMoment');
const m2Spy = sinon.spy(m2, 'isMoment');
dateMath.parse('now', { momentInstance: m1 });
expect(m1Spy.called).to.be(true);
expect(m2Spy.called).to.be(false);
m1Spy.reset();
m2Spy.reset();
dateMath.parse('now', { momentInstance: m2 });
expect(m1Spy.called).to.be(false);
expect(m2Spy.called).to.be(true);
m1Spy.restore();
m2Spy.restore();
});
it('should use global instance after passing an instance', function() {
const m = momentClone();
const momentSpy = sinon.spy(moment, 'isMoment');
const cloneSpy = sinon.spy(m, 'isMoment');
dateMath.parse('now', { momentInstance: m });
expect(momentSpy.called).to.be(false);
expect(cloneSpy.called).to.be(true);
momentSpy.reset();
cloneSpy.reset();
dateMath.parse('now');
expect(momentSpy.called).to.be(true);
expect(cloneSpy.called).to.be(false);
momentSpy.restore();
cloneSpy.restore();
});
});
describe('units', function() {
it('should have units descending for unitsDesc', function() {
expect(dateMath.unitsDesc).to.eql([
'y',
'M',
'w',
'd',
'h',
'm',
's',
'ms',
]);
});
it('should have units ascending for unitsAsc', function() {
expect(dateMath.unitsAsc).to.eql([
'ms',
's',
'm',
'h',
'd',
'w',
'M',
'y',
]);
});
});
});

File diff suppressed because it is too large Load diff

View file

@ -3,7 +3,7 @@ import angular from 'angular';
import { getSort } from 'ui/doc_table/lib/get_sort';
import * as columnActions from 'ui/doc_table/actions/columns';
import * as filterActions from 'ui/doc_table/actions/filter';
import dateMath from '@elastic/datemath';
import dateMath from '@kbn/datemath';
import 'ui/doc_table';
import 'ui/visualize';
import 'ui/fixed_scroll';

View file

@ -1,4 +1,4 @@
import dateMath from '@elastic/datemath';
import dateMath from '@kbn/datemath';
export const GTE_INTERVAL_RE = new RegExp(`^>=([\\d\\.]*\\s*(${dateMath.units.join('|')}))$`);
export const INTERVAL_STRING_RE = new RegExp('^([0-9\\.]*)\\s*(' + dateMath.units.join('|') + ')$');

View file

@ -1,6 +1,6 @@
import _ from 'lodash';
import moment from 'moment';
import dateMath from '@elastic/datemath';
import dateMath from '@kbn/datemath';
// Assume interval is in the form (value)(unit), such as "1h"
const INTERVAL_STRING_RE = new RegExp(

View file

@ -1,4 +1,4 @@
import dateMath from '@elastic/datemath';
import dateMath from '@kbn/datemath';
import { uiModules } from 'ui/modules';
uiModules.get('kibana').directive('validateDateMath', function () {

View file

@ -1,4 +1,4 @@
import dateMath from '@elastic/datemath';
import dateMath from '@kbn/datemath';
export function TimeBucketsCalcEsIntervalProvider() {

View file

@ -1,6 +1,6 @@
import _ from 'lodash';
import moment from 'moment';
import dateMath from '@elastic/datemath';
import dateMath from '@kbn/datemath';
import 'ui/state_management/global_state';
import 'ui/config';
import { EventsProvider } from 'ui/events';

View file

@ -1,4 +1,4 @@
import dateMath from '@elastic/datemath';
import dateMath from '@kbn/datemath';
import moment from 'moment';
import _ from 'lodash';
import { relativeOptions } from './relative_options';

View file

@ -1,4 +1,4 @@
import dateMath from '@elastic/datemath';
import dateMath from '@kbn/datemath';
import moment from 'moment';
import { timeUnits } from './time_units';

View file

@ -6,7 +6,7 @@ import './absolute_panel';
import _ from 'lodash';
import { relativeOptions } from './relative_options';
import { parseRelativeParts } from './parse_relative_parts';
import dateMath from '@elastic/datemath';
import dateMath from '@kbn/datemath';
import moment from 'moment';
import { Notifier } from 'ui/notify';
import 'ui/timepicker/timepicker.less';

View file

@ -1,6 +1,6 @@
import _ from 'lodash';
import moment from 'moment';
import dateMath from '@elastic/datemath';
import dateMath from '@kbn/datemath';
// Assume interval is in the form (value)(unit), such as "1h"
const INTERVAL_STRING_RE = new RegExp('^([0-9\\.]*)\\s*(' + dateMath.units.join('|') + ')$');

View file

@ -1,5 +1,5 @@
import d3 from 'd3';
import dateMath from '@elastic/datemath';
import dateMath from '@kbn/datemath';
export function VislibVisualizationsTimeMarkerProvider() {

View file

@ -1,45 +0,0 @@
module.exports = grunt => {
grunt.registerTask(
'buildPackages',
'Build all the Kibana specific packages',
async function () {
const done = this.async();
try {
await buildPackages();
done();
} catch (e) {
grunt.fail.fatal(e);
done(e);
}
}
);
function buildPackages() {
const serverCmd = {
cmd: 'yarn',
args: [
'kbn',
'run',
'build',
'--skip-kibana',
'--skip-kibana-extra'
],
opts: {
stdio: 'inherit'
}
};
return new Promise((resolve, reject) => {
grunt.util.spawn(serverCmd, (error, result, code) => {
if (error || code !== 0) {
const error = new Error(`'yarn kbn run' exited with code ${code}`);
reject(error);
return;
}
resolve();
});
});
}
};

View file

@ -15,6 +15,7 @@ export default {
'test/**/__tests__/**/*.js',
'src/**/__tests__/**/*.js',
'packages/kbn-build/**/__tests__/**/*.js',
'packages/kbn-datemath/test/**/*.js',
'tasks/**/__tests__/**/*.js',
'test/fixtures/__tests__/*.js',
'!**/__tests__/fixtures/**/*',

View file

@ -29,12 +29,6 @@ module.exports = function (grunt) {
'jenkins:env',
'rejectRejFiles',
// We need to build all the Kibana packages so ESLint is able to verify that
// e.g. imports resolve properly. If we don't build the packages here, the
// `main` field in their `package.json` would link to a location that
// doesn't exist yet.
'buildPackages',
'run:eslint',
'licenses',
'test:server',

View file

@ -63,12 +63,6 @@
lodash "^4.2.0"
to-fast-properties "^2.0.0"
"@elastic/datemath@4.0.2":
version "4.0.2"
resolved "https://registry.yarnpkg.com/@elastic/datemath/-/datemath-4.0.2.tgz#91417763fa4ec93ad1426cb69aaf2de2e9914a68"
dependencies:
moment "^2.13.0"
"@elastic/eslint-config-kibana@link:packages/eslint-config-kibana":
version "0.0.0"
uid ""
@ -128,6 +122,10 @@
version "0.0.0"
uid ""
"@kbn/datemath@link:packages/kbn-datemath":
version "0.0.0"
uid ""
"@kbn/plugin-generator@link:packages/kbn-plugin-generator":
version "0.0.0"
uid ""