Skip to content

Commit

Permalink
fix(client-core): Update normalizePivotConfig method to not to fail i…
Browse files Browse the repository at this point in the history
…f x or y are missing

Fixes #10
  • Loading branch information
paveltiunov committed Jun 2, 2019
1 parent 91ca994 commit ee20863
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 46 deletions.
13 changes: 12 additions & 1 deletion packages/cubejs-client-core/dist/cubejs-client-core.esm.js
Expand Up @@ -15,6 +15,7 @@ import 'core-js/modules/es6.object.keys';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import 'core-js/modules/es6.array.reduce';
import 'core-js/modules/es6.array.index-of';
import 'core-js/modules/es6.array.find';
import 'core-js/modules/es6.array.filter';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
Expand Down Expand Up @@ -143,6 +144,15 @@ function () {
x: query.dimensions || [],
y: []
});
pivotConfig.x = pivotConfig.x || [];
pivotConfig.y = pivotConfig.y || [];
var allIncludedDimensions = pivotConfig.x.concat(pivotConfig.y);
var allDimensions = timeDimensions.map(function (td) {
return td.dimension;
}).concat(query.dimensions);
pivotConfig.x = pivotConfig.x.concat(allDimensions.filter(function (d) {
return allIncludedDimensions.indexOf(d) === -1;
}));

if (!pivotConfig.x.concat(pivotConfig.y).find(function (d) {
return d === 'measures';
Expand Down Expand Up @@ -251,7 +261,8 @@ function () {
xValuesString = _ref7[0],
rows = _ref7[1];

return unnest(rows.filter(function (_ref8) {
return unnest( // collect Y values only from filled rows
rows.filter(function (_ref8) {
var row = _ref8.row;
return Object.keys(row).length > 0;
}).map(function (_ref9) {
Expand Down
13 changes: 12 additions & 1 deletion packages/cubejs-client-core/dist/cubejs-client-core.js
Expand Up @@ -19,6 +19,7 @@ require('core-js/modules/es6.object.keys');
var _slicedToArray = _interopDefault(require('@babel/runtime/helpers/slicedToArray'));
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty'));
require('core-js/modules/es6.array.reduce');
require('core-js/modules/es6.array.index-of');
require('core-js/modules/es6.array.find');
require('core-js/modules/es6.array.filter');
var _objectWithoutProperties = _interopDefault(require('@babel/runtime/helpers/objectWithoutProperties'));
Expand Down Expand Up @@ -147,6 +148,15 @@ function () {
x: query.dimensions || [],
y: []
});
pivotConfig.x = pivotConfig.x || [];
pivotConfig.y = pivotConfig.y || [];
var allIncludedDimensions = pivotConfig.x.concat(pivotConfig.y);
var allDimensions = timeDimensions.map(function (td) {
return td.dimension;
}).concat(query.dimensions);
pivotConfig.x = pivotConfig.x.concat(allDimensions.filter(function (d) {
return allIncludedDimensions.indexOf(d) === -1;
}));

if (!pivotConfig.x.concat(pivotConfig.y).find(function (d) {
return d === 'measures';
Expand Down Expand Up @@ -255,7 +265,8 @@ function () {
xValuesString = _ref7[0],
rows = _ref7[1];

return ramda.unnest(rows.filter(function (_ref8) {
return ramda.unnest( // collect Y values only from filled rows
rows.filter(function (_ref8) {
var row = _ref8.row;
return Object.keys(row).length > 0;
}).map(function (_ref9) {
Expand Down
12 changes: 11 additions & 1 deletion packages/cubejs-client-core/dist/cubejs-client-core.umd.js
Expand Up @@ -14117,6 +14117,15 @@
x: query.dimensions || [],
y: []
});
pivotConfig.x = pivotConfig.x || [];
pivotConfig.y = pivotConfig.y || [];
var allIncludedDimensions = pivotConfig.x.concat(pivotConfig.y);
var allDimensions = timeDimensions.map(function (td) {
return td.dimension;
}).concat(query.dimensions);
pivotConfig.x = pivotConfig.x.concat(allDimensions.filter(function (d) {
return allIncludedDimensions.indexOf(d) === -1;
}));

if (!pivotConfig.x.concat(pivotConfig.y).find(function (d) {
return d === 'measures';
Expand Down Expand Up @@ -14225,7 +14234,8 @@
xValuesString = _ref7[0],
rows = _ref7[1];

return unnest(rows.filter(function (_ref8) {
return unnest( // collect Y values only from filled rows
rows.filter(function (_ref8) {
var row = _ref8.row;
return Object.keys(row).length > 0;
}).map(function (_ref9) {
Expand Down
5 changes: 5 additions & 0 deletions packages/cubejs-client-core/src/ResultSet.js
Expand Up @@ -66,6 +66,11 @@ export default class ResultSet {
x: query.dimensions || [],
y: []
});
pivotConfig.x = pivotConfig.x || [];
pivotConfig.y = pivotConfig.y || [];
const allIncludedDimensions = pivotConfig.x.concat(pivotConfig.y);
const allDimensions = timeDimensions.map(td => td.dimension).concat(query.dimensions);
pivotConfig.x = pivotConfig.x.concat(allDimensions.filter(d => allIncludedDimensions.indexOf(d) === -1));
if (!pivotConfig.x.concat(pivotConfig.y).find(d => d === 'measures')) {
pivotConfig.y = pivotConfig.y.concat(['measures']);
}
Expand Down
108 changes: 65 additions & 43 deletions packages/cubejs-client-core/src/ResultSet.test.js
@@ -1,3 +1,4 @@
/* globals jest,describe,test,expect */
import ResultSet from './ResultSet';

jest.mock('moment-range', () => {
Expand All @@ -6,7 +7,7 @@ jest.mock('moment-range', () => {
const moment = MomentRange.extendMoment(Moment);
return {
extendMoment: () => moment
}
};
});

describe('ResultSet', () => {
Expand All @@ -17,21 +18,22 @@ describe('ResultSet', () => {
dateRange: ['2015-01-01', '2015-12-31'],
granularity: 'month',
timeDimension: 'Events.time'
}
const output =
[ '2015-01-01T00:00:00.000',
'2015-02-01T00:00:00.000',
'2015-03-01T00:00:00.000',
'2015-04-01T00:00:00.000',
'2015-05-01T00:00:00.000',
'2015-06-01T00:00:00.000',
'2015-07-01T00:00:00.000',
'2015-08-01T00:00:00.000',
'2015-09-01T00:00:00.000',
'2015-10-01T00:00:00.000',
'2015-11-01T00:00:00.000',
'2015-12-01T00:00:00.000' ]
expect(resultSet.timeSeries(timeDimension)).toEqual(output)
};
const output = [
'2015-01-01T00:00:00.000',
'2015-02-01T00:00:00.000',
'2015-03-01T00:00:00.000',
'2015-04-01T00:00:00.000',
'2015-05-01T00:00:00.000',
'2015-06-01T00:00:00.000',
'2015-07-01T00:00:00.000',
'2015-08-01T00:00:00.000',
'2015-09-01T00:00:00.000',
'2015-10-01T00:00:00.000',
'2015-11-01T00:00:00.000',
'2015-12-01T00:00:00.000'
];
expect(resultSet.timeSeries(timeDimension)).toEqual(output);
});

test('it generates array of dates - granularity hour', () => {
Expand All @@ -40,34 +42,54 @@ describe('ResultSet', () => {
dateRange: ['2015-01-01', '2015-01-01'],
granularity: 'hour',
timeDimension: 'Events.time'
}
const output =
[ '2015-01-01T00:00:00.000',
'2015-01-01T01:00:00.000',
'2015-01-01T02:00:00.000',
'2015-01-01T03:00:00.000',
'2015-01-01T04:00:00.000',
'2015-01-01T05:00:00.000',
'2015-01-01T06:00:00.000',
'2015-01-01T07:00:00.000',
'2015-01-01T08:00:00.000',
'2015-01-01T09:00:00.000',
'2015-01-01T10:00:00.000',
'2015-01-01T11:00:00.000',
'2015-01-01T12:00:00.000',
'2015-01-01T13:00:00.000',
'2015-01-01T14:00:00.000',
'2015-01-01T15:00:00.000',
'2015-01-01T16:00:00.000',
'2015-01-01T17:00:00.000',
'2015-01-01T18:00:00.000',
'2015-01-01T19:00:00.000',
'2015-01-01T20:00:00.000',
'2015-01-01T21:00:00.000',
'2015-01-01T22:00:00.000',
'2015-01-01T23:00:00.000' ]
expect(resultSet.timeSeries(timeDimension)).toEqual(output)
};
const output = [
'2015-01-01T00:00:00.000',
'2015-01-01T01:00:00.000',
'2015-01-01T02:00:00.000',
'2015-01-01T03:00:00.000',
'2015-01-01T04:00:00.000',
'2015-01-01T05:00:00.000',
'2015-01-01T06:00:00.000',
'2015-01-01T07:00:00.000',
'2015-01-01T08:00:00.000',
'2015-01-01T09:00:00.000',
'2015-01-01T10:00:00.000',
'2015-01-01T11:00:00.000',
'2015-01-01T12:00:00.000',
'2015-01-01T13:00:00.000',
'2015-01-01T14:00:00.000',
'2015-01-01T15:00:00.000',
'2015-01-01T16:00:00.000',
'2015-01-01T17:00:00.000',
'2015-01-01T18:00:00.000',
'2015-01-01T19:00:00.000',
'2015-01-01T20:00:00.000',
'2015-01-01T21:00:00.000',
'2015-01-01T22:00:00.000',
'2015-01-01T23:00:00.000'
];
expect(resultSet.timeSeries(timeDimension)).toEqual(output);
});
});

describe('normalizePivotConfig', () => {
test('fills missing x, y', () => {
const resultSet = new ResultSet({
query: {
dimensions: ['Foo.bar'],
timeDimensions: [{
granularity: 'day',
dimension: 'Foo.createdAt'
}]
}
});

expect(resultSet.normalizePivotConfig({ y: ['Foo.bar'] })).toEqual({
x: ['Foo.createdAt'],
y: ['Foo.bar', 'measures'],
fillMissingDates: true
});
});
});
});

0 comments on commit ee20863

Please sign in to comment.