Socket
Socket
Sign inDemoInstall

@cubejs-client/core

Package Overview
Dependencies
Maintainers
1
Versions
224
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cubejs-client/core - npm Package Compare versions

Comparing version 0.1.4 to 0.2.0

220

dist/cubejs-client-core.js

@@ -7,14 +7,18 @@ 'use strict';

require('core-js/modules/es6.number.parse-float');
var _objectSpread = _interopDefault(require('@babel/runtime/helpers/objectSpread'));
var _slicedToArray = _interopDefault(require('@babel/runtime/helpers/slicedToArray'));
require('core-js/modules/es6.object.assign');
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty'));
require('core-js/modules/es6.array.reduce');
var _objectSpread = _interopDefault(require('@babel/runtime/helpers/objectSpread'));
var _slicedToArray = _interopDefault(require('@babel/runtime/helpers/slicedToArray'));
require('core-js/modules/es6.array.find');
require('core-js/modules/es6.array.filter');
var _objectWithoutProperties = _interopDefault(require('@babel/runtime/helpers/objectWithoutProperties'));
require('core-js/modules/es6.array.map');
var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck'));
var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));
require('core-js/modules/es6.string.iterator');
require('core-js/modules/es6.array.from');
require('core-js/modules/es6.array.map');
var ramda = require('ramda');
var Moment = require('moment');
var momentRange = require('moment-range');
var _regeneratorRuntime = _interopDefault(require('@babel/runtime/regenerator'));

@@ -25,2 +29,26 @@ require('regenerator-runtime/runtime');

var moment = momentRange.extendMoment(Moment);
var TIME_SERIES = {
day: function day(range) {
return Array.from(range.by('day')).map(function (d) {
return d.format('YYYY-MM-DDT00:00:00.000');
});
},
month: function month(range) {
return Array.from(range.snapTo('month').by('month')).map(function (d) {
return d.format('YYYY-MM-01T00:00:00.000');
});
},
hour: function hour(range) {
return Array.from(range.by('hour')).map(function (d) {
return d.format('YYYY-MM-DDTHH:00:00.000');
});
},
week: function week(range) {
return Array.from(range.snapTo('isoweek').by('week')).map(function (d) {
return d.startOf('isoweek').format('YYYY-MM-DDT00:00:00.000');
});
}
};
var ResultSet =

@@ -45,9 +73,11 @@ /*#__PURE__*/

title: title,
series: _this.pivotedRows(pivotConfig).map(function (_ref2) {
series: _this.chartPivot(pivotConfig).map(function (_ref2) {
var category = _ref2.category,
obj = _objectWithoutProperties(_ref2, ["category"]);
x = _ref2.x,
obj = _objectWithoutProperties(_ref2, ["category", "x"]);
return {
value: obj[key],
category: category
category: category,
x: x
};

@@ -67,3 +97,3 @@ })

}).map(function (d) {
return row[d];
return row[d] != null ? row[d] : null;
}).concat(measure ? [measure] : []);

@@ -84,5 +114,13 @@ };

value: function axisValuesString(axisValues, delimiter) {
return axisValues.map(function (v) {
return v != null ? v : '∅';
}).join(delimiter || ':');
var formatValue = function formatValue(v) {
if (v == null) {
return '∅';
} else if (v === '') {
return '[Empty string]';
} else {
return v;
}
};
return axisValues.map(formatValue).join(delimiter || ':');
}

@@ -112,5 +150,43 @@ }, {

if (pivotConfig.fillMissingDates == null) {
pivotConfig.fillMissingDates = true;
}
return pivotConfig;
}
}, {
key: "timeSeries",
value: function timeSeries(timeDimension) {
if (!timeDimension.granularity) {
return null;
}
var dateRange = timeDimension.dateRange;
if (!dateRange) {
var dates = ramda.pipe(ramda.map(function (row) {
return row[timeDimension.dimension] && moment(row[timeDimension.dimension]);
}), ramda.filter(function (r) {
return !!r;
}))(this.loadResponse.data);
dateRange = dates.length && [ramda.reduce(ramda.minBy(function (d) {
return d.toDate();
}), dates[0], dates), ramda.reduce(ramda.maxBy(function (d) {
return d.toDate();
}), dates[0], dates)] || null;
}
if (!dateRange) {
return null;
}
var range = moment.range(dateRange[0], dateRange[1]);
if (!TIME_SERIES[timeDimension.granularity]) {
throw new Error("Unsupported time granularity: ".concat(timeDimension.granularity));
}
return TIME_SERIES[timeDimension.granularity](range);
}
}, {
key: "pivot",

@@ -120,5 +196,42 @@ value: function pivot(pivotConfig) {

// TODO missing date filling
pivotConfig = this.normalizePivotConfig(pivotConfig);
return ramda.pipe(ramda.map(function (row) {
var groupByXAxis = ramda.groupBy(function (_ref3) {
var xValues = _ref3.xValues;
return _this2.axisValuesString(xValues);
});
var measureValue = function measureValue(row, measure, xValues) {
return row[measure];
};
if (pivotConfig.fillMissingDates && pivotConfig.x.length === 1 && ramda.equals(pivotConfig.x, (this.loadResponse.query.timeDimensions || []).filter(function (td) {
return !!td.granularity;
}).map(function (td) {
return td.dimension;
}))) {
var series = this.timeSeries(this.loadResponse.query.timeDimensions[0]);
if (series) {
groupByXAxis = function groupByXAxis(rows) {
var byXValues = ramda.groupBy(function (_ref4) {
var xValues = _ref4.xValues;
return moment(xValues[0]).format(moment.HTML5_FMT.DATETIME_LOCAL_MS);
}, rows);
return series.map(function (d) {
return _defineProperty({}, d, byXValues[d] || [{
xValues: [d],
row: {}
}]);
}).reduce(function (a, b) {
return Object.assign(a, b);
}, {});
};
measureValue = function measureValue(row, measure, xValues) {
return row[measure] || 0;
};
}
}
var xGrouped = ramda.pipe(ramda.map(function (row) {
return _this2.axisValues(pivotConfig.x)(row).map(function (xValues) {

@@ -130,27 +243,45 @@ return {

});
}), ramda.unnest, ramda.groupBy(function (_ref3) {
var xValues = _ref3.xValues;
return _this2.axisValuesString(xValues);
}), ramda.toPairs)(this.loadResponse.data).map(function (_ref4) {
var _ref5 = _slicedToArray(_ref4, 2),
xValuesString = _ref5[0],
rows = _ref5[1];
}), ramda.unnest, groupByXAxis, ramda.toPairs)(this.loadResponse.data);
var allYValues = ramda.pipe(ramda.map(function (_ref6) {
var _ref7 = _slicedToArray(_ref6, 2),
xValuesString = _ref7[0],
rows = _ref7[1];
return ramda.unnest(rows.map(function (_ref8) {
var row = _ref8.row;
return _this2.axisValues(pivotConfig.y)(row);
}));
}), ramda.unnest, ramda.uniq)(xGrouped);
return xGrouped.map(function (_ref9) {
var _ref10 = _slicedToArray(_ref9, 2),
xValuesString = _ref10[0],
rows = _ref10[1];
var xValues = rows[0].xValues;
return _objectSpread({
xValues: xValues
}, rows.map(function (r) {
return r.row;
}).map(function (row) {
var yGrouped = ramda.pipe(ramda.map(function (_ref11) {
var row = _ref11.row;
return _this2.axisValues(pivotConfig.y)(row).map(function (yValues) {
return {
yValues: yValues,
row: row
};
});
}), ramda.unnest, ramda.groupBy(function (_ref12) {
var yValues = _ref12.yValues;
return _this2.axisValuesString(yValues);
}))(rows);
return {
xValues: xValues,
yValuesArray: ramda.unnest(allYValues.map(function (yValues) {
var measure = pivotConfig.x.find(function (d) {
return d === 'measures';
}) ? ResultSet.measureFromAxis(xValues) : ResultSet.measureFromAxis(yValues);
return _defineProperty({}, _this2.axisValuesString(yValues), row[measure]);
}).reduce(function (a, b) {
return Object.assign(a, b);
}, {});
}).reduce(function (a, b) {
return Object.assign(a, b);
}, {}));
return (yGrouped[_this2.axisValuesString(yValues)] || [{
row: {}
}]).map(function (_ref13) {
var row = _ref13.row;
return [yValues, measureValue(row, measure, xValues)];
});
}))
};
});

@@ -169,11 +300,18 @@ }

return this.pivot(pivotConfig).map(function (_ref7) {
var xValues = _ref7.xValues,
measures = _objectWithoutProperties(_ref7, ["xValues"]);
return this.pivot(pivotConfig).map(function (_ref14) {
var xValues = _ref14.xValues,
yValuesArray = _ref14.yValuesArray;
return _objectSpread({
category: _this3.axisValuesString(xValues, ', '),
//TODO deprecated
x: _this3.axisValuesString(xValues, ', ')
}, yValuesArray.map(function (_ref15) {
var _ref16 = _slicedToArray(_ref15, 2),
yValues = _ref16[0],
m = _ref16[1];
return _objectSpread({
category: _this3.axisValuesString(xValues, ', ')
}, ramda.map(function (m) {
return m && Number.parseFloat(m);
}, measures));
return _defineProperty({}, _this3.axisValuesString(yValues, ', '), m && Number.parseFloat(m));
}).reduce(function (a, b) {
return Object.assign(a, b);
}, {}));
});

@@ -184,3 +322,3 @@ }

value: function totalRow() {
return this.pivotedRows()[0];
return this.chartPivot()[0];
}

@@ -191,3 +329,3 @@ }, {

//TODO
return this.pivotedRows(pivotConfig);
return this.chartPivot(pivotConfig);
}

@@ -194,0 +332,0 @@ }, {

4

package.json
{
"name": "@cubejs-client/core",
"version": "0.1.4",
"version": "0.2.0",
"description": "cube.js client",

@@ -10,2 +10,4 @@ "main": "dist/cubejs-client-core.js",

"core-js": "^2.5.3",
"moment": "^2.22.2",
"moment-range": "^4.0.1",
"ramda": "^0.25.0",

@@ -12,0 +14,0 @@ "whatwg-fetch": "^3.0.0"

@@ -1,3 +0,22 @@

import { groupBy, pipe, toPairs, uniq, flatten, map, unnest, dropLast } from 'ramda';
import { groupBy, pipe, toPairs, uniq, filter, map, unnest, dropLast, equals, reduce, minBy, maxBy } from 'ramda';
import * as Moment from 'moment';
import * as momentRange from 'moment-range';
const moment = momentRange.extendMoment(Moment);
const TIME_SERIES = {
day: (range) =>
Array.from(range.by('day'))
.map(d => d.format('YYYY-MM-DDT00:00:00.000')),
month: (range) =>
Array.from(range.snapTo('month').by('month'))
.map(d => d.format('YYYY-MM-01T00:00:00.000')),
hour: (range) =>
Array.from(range.by('hour'))
.map(d => d.format('YYYY-MM-DDTHH:00:00.000')),
week: (range) =>
Array.from(range.snapTo('isoweek').by('week'))
.map(d => d.startOf('isoweek').format('YYYY-MM-DDT00:00:00.000'))
};
export default class ResultSet {

@@ -11,3 +30,3 @@ constructor(loadResponse) {

title,
series: this.pivotedRows(pivotConfig).map(({ category, ...obj }) => ({ value: obj[key], category }))
series: this.chartPivot(pivotConfig).map(({ category, x, ...obj }) => ({ value: obj[key], category, x }))
}));

@@ -21,3 +40,3 @@ }

axis.filter(d => d !== 'measures')
.map(d => row[d]).concat(measure ? [measure] : []);
.map(d => row[d] != null ? row[d] : null).concat(measure ? [measure] : []);
if (axis.find(d => d === 'measures') && (query.measures || []).length) {

@@ -31,3 +50,12 @@ return query.measures.map(value);

axisValuesString(axisValues, delimiter) {
return axisValues.map(v => v != null ? v : '∅').join(delimiter || ':');
const formatValue = (v) => {
if (v == null) {
return '∅';
} else if (v === '') {
return '[Empty string]';
} else {
return v;
}
};
return axisValues.map(formatValue).join(delimiter || ':');
}

@@ -48,2 +76,5 @@

}
if (pivotConfig.fillMissingDates == null) {
pivotConfig.fillMissingDates = true;
}
return pivotConfig;

@@ -56,23 +87,84 @@ }

timeSeries(timeDimension) {
if (!timeDimension.granularity) {
return null;
}
let dateRange = timeDimension.dateRange;
if (!dateRange) {
const dates = pipe(
map(row => row[timeDimension.dimension] && moment(row[timeDimension.dimension])),
filter(r => !!r)
)(this.loadResponse.data);
dateRange = dates.length && [
reduce(minBy(d => d.toDate()), dates[0], dates),
reduce(maxBy(d => d.toDate()), dates[0], dates)
] || null;
}
if (!dateRange) {
return null;
}
const range = moment.range(dateRange[0], dateRange[1]);
if (!TIME_SERIES[timeDimension.granularity]) {
throw new Error(`Unsupported time granularity: ${timeDimension.granularity}`);
}
return TIME_SERIES[timeDimension.granularity](range);
}
pivot(pivotConfig) {
// TODO missing date filling
pivotConfig = this.normalizePivotConfig(pivotConfig);
return pipe(
let groupByXAxis = groupBy(({ xValues }) => this.axisValuesString(xValues));
let measureValue = (row, measure, xValues) => row[measure];
if (
pivotConfig.fillMissingDates &&
pivotConfig.x.length === 1 &&
equals(
pivotConfig.x,
(this.loadResponse.query.timeDimensions || []).filter(td => !!td.granularity).map(td => td.dimension)
)
) {
const series = this.timeSeries(this.loadResponse.query.timeDimensions[0]);
if (series) {
groupByXAxis = (rows) => {
const byXValues = groupBy(({ xValues }) => moment(xValues[0]).format(moment.HTML5_FMT.DATETIME_LOCAL_MS), rows);
return series.map(d => ({ [d]: byXValues[d] || [{ xValues: [d], row: {} }] }))
.reduce((a, b) => Object.assign(a, b), {});
};
measureValue = (row, measure, xValues) => row[measure] || 0;
}
}
const xGrouped = pipe(
map(row => this.axisValues(pivotConfig.x)(row).map(xValues => ({ xValues, row }))),
unnest,
groupBy(({ xValues }) => this.axisValuesString(xValues)),
groupByXAxis,
toPairs
)(this.loadResponse.data).map(([xValuesString, rows]) => {
)(this.loadResponse.data);
const allYValues = pipe(
map(
([xValuesString, rows]) => unnest(rows.map(({ row }) => this.axisValues(pivotConfig.y)(row)))
),
unnest,
uniq
)(xGrouped);
return xGrouped.map(([xValuesString, rows]) => {
const xValues = rows[0].xValues;
const yGrouped = pipe(
map(({ row }) => this.axisValues(pivotConfig.y)(row).map(yValues => ({ yValues, row }))),
unnest,
groupBy(({ yValues }) => this.axisValuesString(yValues))
)(rows);
return {
xValues,
...(rows.map(r => r.row).map(row => this.axisValues(pivotConfig.y)(row).map(yValues => {
let measure = pivotConfig.x.find(d => d === 'measures') ?
ResultSet.measureFromAxis(xValues) :
ResultSet.measureFromAxis(yValues);
return {
[this.axisValuesString(yValues)]: row[measure]
}
}).reduce((a, b) => Object.assign(a, b), {})
)).reduce((a, b) => Object.assign(a, b), {})
yValuesArray: unnest(allYValues.map(yValues => {
let measure = pivotConfig.x.find(d => d === 'measures') ?
ResultSet.measureFromAxis(xValues) :
ResultSet.measureFromAxis(yValues);
return (yGrouped[this.axisValuesString(yValues)] || [{ row: {} }]).map(({ row }) => [yValues, measureValue(row, measure, xValues)])
}))
};

@@ -87,5 +179,10 @@ });

chartPivot(pivotConfig) {
return this.pivot(pivotConfig).map(({ xValues, ...measures }) => ({
category: this.axisValuesString(xValues, ', '),
...(map(m => m && Number.parseFloat(m), measures))
return this.pivot(pivotConfig).map(({ xValues, yValuesArray }) => ({
category: this.axisValuesString(xValues, ', '), //TODO deprecated
x: this.axisValuesString(xValues, ', '),
...(
yValuesArray
.map(([yValues, m]) => ({ [this.axisValuesString(yValues, ', ')]: m && Number.parseFloat(m) }))
.reduce((a, b) => Object.assign(a, b), {})
)
}));

@@ -95,7 +192,7 @@ }

totalRow() {
return this.pivotedRows()[0];
return this.chartPivot()[0];
}
categories(pivotConfig) { //TODO
return this.pivotedRows(pivotConfig);
return this.chartPivot(pivotConfig);
}

@@ -102,0 +199,0 @@

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc