Socket
Socket
Sign inDemoInstall

@cubejs-client/core

Package Overview
Dependencies
4
Maintainers
1
Versions
223
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 0.1.1

595

dist/cubejs-client-core.js

@@ -1,169 +0,514 @@

import { groupBy, pipe, toPairs, uniq, flatten, map } from 'ramda';
import 'core-js/modules/es6.number.constructor';
import 'core-js/modules/es6.number.parse-float';
import 'core-js/modules/es6.object.assign';
import 'core-js/modules/es6.array.reduce';
import 'core-js/modules/es6.array.find';
import 'core-js/modules/es6.array.filter';
import 'core-js/modules/es6.array.map';
import { groupBy, pipe, toPairs, uniq, map, unnest, dropLast } from 'ramda';
import 'regenerator-runtime/runtime';
import { fetch } from 'whatwg-fetch';
class ResultSet {
constructor(loadResponse) {
this.loadResponse = loadResponse;
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
series(pivotConfig) {
return this.seriesNames(pivotConfig).map(({ title, key}) => ({
title,
series: this.pivotedRows(pivotConfig).map(({ category, ...obj }) => ({ value: obj[key], category }))
}));
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
axisValues(axis) {
const query = this.loadResponse.query;
return row => {
const value = (measure) =>
axis.filter(d => d !== 'measures')
.map(d => row[d]).concat(measure ? [measure] : [])
.map(v => v != null ? v : '∅').join(', ');
if (axis.find(d => d === 'measures') && (query.measures || []).length) {
return query.measures.map(value);
function _asyncToGenerator(fn) {
return function () {
var self = this,
args = arguments;
return new Promise(function (resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
return [value()];
};
}
axisKeys(axis) {
const query = this.loadResponse.query;
if (axis.find(d => d === 'measures') && (query.measures || []).length) {
let withoutMeasures = axis.filter(d => d !== 'measures');
return query.measures.map(measure => withoutMeasures.concat(measure).join(', '));
} else {
return [axis.join(', ')];
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
normalizePivotConfig(pivotConfig) {
const query = this.loadResponse.query;
let timeDimensions = (query.timeDimensions || []).filter(td => !!td.granularity);
pivotConfig = pivotConfig || timeDimensions.length ? {
x: timeDimensions.map(td => td.dimension),
y: query.dimensions || []
} : {
x: query.dimensions || [],
y: []
};
if (!pivotConfig.x.concat(pivotConfig.y).find(d => d === 'measures')) {
pivotConfig.y = pivotConfig.y.concat(['measures']);
}
return pivotConfig;
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
static measureFromAxis(axis) {
const axisValues = axis.split(', ');
return axisValues[axisValues.length - 1];
};
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
pivotedRows(pivotConfig) {
// TODO missing date filling
pivotConfig = this.normalizePivotConfig(pivotConfig);
return pipe(groupBy(this.axisValues(pivotConfig.x)), toPairs)(this.loadResponse.data).map(([category, rows]) => ({
rows,
category,
...(rows.map(row => this.axisValues(pivotConfig.y)(row).map(series => {
let measure = pivotConfig.x.find(d => d === 'measures') ?
ResultSet.measureFromAxis(category) :
ResultSet.measureFromAxis(series);
return {
[series]: row[measure]
}
}).reduce((a, b) => Object.assign(a, b), {})
)).reduce((a, b) => Object.assign(a, b), {})
})
);
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
categories(pivotConfig) { //TODO
return this.pivotedRows(pivotConfig);
}
return obj;
}
seriesNames(pivotConfig) {
pivotConfig = this.normalizePivotConfig(pivotConfig);
return pipe(map(this.axisValues(pivotConfig.y)), uniq, flatten)(this.loadResponse.data).map(axis => ({
title: pivotConfig.y.find(d => d === 'measures') ? axis.replace(
ResultSet.measureFromAxis(axis),
this.loadResponse.annotation.measures[ResultSet.measureFromAxis(axis)].title
) : axis,
key: axis
}))
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function (key) {
_defineProperty(target, key, source[key]);
});
}
query() {
return this.loadResponse.query;
return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {};
var target = {};
var sourceKeys = Object.keys(source);
var key, i;
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) continue;
target[key] = source[key];
}
rawData() {
return this.loadResponse.data;
}
return target;
}
class ProgressResult {
constructor(progressResponse) {
this.progressResponse = progressResponse;
function _objectWithoutProperties(source, excluded) {
if (source == null) return {};
var target = _objectWithoutPropertiesLoose(source, excluded);
var key, i;
if (Object.getOwnPropertySymbols) {
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
for (i = 0; i < sourceSymbolKeys.length; i++) {
key = sourceSymbolKeys[i];
if (excluded.indexOf(key) >= 0) continue;
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
target[key] = source[key];
}
}
stage() {
return this.progressResponse.stage;
return target;
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
timeElapsed() {
return this.progressResponse.timeElapsed;
}
return _arr;
}
const API_URL = "https://statsbot.co/cubejs-api/v1";
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
class CubejsApi {
constructor(apiToken) {
this.apiToken = apiToken;
var ResultSet =
/*#__PURE__*/
function () {
function ResultSet(loadResponse) {
_classCallCheck(this, ResultSet);
this.loadResponse = loadResponse;
}
request(url, config) {
return fetch(
`${API_URL}${url}`,
Object.assign({ headers: { Authorization: this.apiToken, 'Content-Type': 'application/json' }}, config || {})
)
_createClass(ResultSet, [{
key: "series",
value: function series(pivotConfig) {
var _this = this;
return this.seriesNames(pivotConfig).map(function (_ref) {
var title = _ref.title,
key = _ref.key;
return {
title: title,
series: _this.pivotedRows(pivotConfig).map(function (_ref2) {
var category = _ref2.category,
obj = _objectWithoutProperties(_ref2, ["category"]);
return {
value: obj[key],
category: category
};
})
};
});
}
}, {
key: "axisValues",
value: function axisValues(axis) {
var query = this.loadResponse.query;
return function (row) {
var value = function value(measure) {
return axis.filter(function (d) {
return d !== 'measures';
}).map(function (d) {
return row[d];
}).concat(measure ? [measure] : []);
};
if (axis.find(function (d) {
return d === 'measures';
}) && (query.measures || []).length) {
return query.measures.map(value);
}
return [value()];
};
}
}, {
key: "axisValuesString",
value: function axisValuesString(axisValues, delimiter) {
return axisValues.map(function (v) {
return v != null ? v : '∅';
}).join(delimiter || ':');
}
}, {
key: "normalizePivotConfig",
value: function normalizePivotConfig(pivotConfig) {
var query = this.loadResponse.query;
var timeDimensions = (query.timeDimensions || []).filter(function (td) {
return !!td.granularity;
});
pivotConfig = pivotConfig || timeDimensions.length ? {
x: timeDimensions.map(function (td) {
return td.dimension;
}),
y: query.dimensions || []
} : {
x: query.dimensions || [],
y: []
};
if (!pivotConfig.x.concat(pivotConfig.y).find(function (d) {
return d === 'measures';
})) {
pivotConfig.y = pivotConfig.y.concat(['measures']);
}
return pivotConfig;
}
}, {
key: "pivot",
value: function pivot(pivotConfig) {
var _this2 = this;
// TODO missing date filling
pivotConfig = this.normalizePivotConfig(pivotConfig);
return pipe(map(function (row) {
return _this2.axisValues(pivotConfig.x)(row).map(function (xValues) {
return {
xValues: xValues,
row: row
};
});
}), unnest, groupBy(function (_ref3) {
var xValues = _ref3.xValues;
return _this2.axisValuesString(xValues);
}), toPairs)(this.loadResponse.data).map(function (_ref4) {
var _ref5 = _slicedToArray(_ref4, 2),
xValuesString = _ref5[0],
rows = _ref5[1];
var xValues = rows[0].xValues;
return _objectSpread({
xValues: xValues
}, rows.map(function (r) {
return r.row;
}).map(function (row) {
return _this2.axisValues(pivotConfig.y)(row).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);
}, {}));
});
}
}, {
key: "pivotedRows",
value: function pivotedRows(pivotConfig) {
// TODO
return this.chartPivot(pivotConfig);
}
}, {
key: "chartPivot",
value: function chartPivot(pivotConfig) {
var _this3 = this;
return this.pivot(pivotConfig).map(function (_ref7) {
var xValues = _ref7.xValues,
measures = _objectWithoutProperties(_ref7, ["xValues"]);
return _objectSpread({
category: _this3.axisValuesString(xValues, ', ')
}, map(function (m) {
return m && Number.parseFloat(m);
}, measures));
});
}
}, {
key: "totalRow",
value: function totalRow() {
return this.pivotedRows()[0];
}
}, {
key: "categories",
value: function categories(pivotConfig) {
//TODO
return this.pivotedRows(pivotConfig);
}
}, {
key: "seriesNames",
value: function seriesNames(pivotConfig) {
var _this4 = this;
pivotConfig = this.normalizePivotConfig(pivotConfig);
return pipe(map(this.axisValues(pivotConfig.y)), unnest, uniq)(this.loadResponse.data).map(function (axisValues) {
return {
title: _this4.axisValuesString(pivotConfig.y.find(function (d) {
return d === 'measures';
}) ? dropLast(1, axisValues).concat(_this4.loadResponse.annotation.measures[ResultSet.measureFromAxis(axisValues)].title) : axisValues, ', '),
key: _this4.axisValuesString(axisValues)
};
});
}
}, {
key: "query",
value: function query() {
return this.loadResponse.query;
}
}, {
key: "rawData",
value: function rawData() {
return this.loadResponse.data;
}
}], [{
key: "measureFromAxis",
value: function measureFromAxis(axisValues) {
return axisValues[axisValues.length - 1];
}
}]);
return ResultSet;
}();
var ProgressResult =
/*#__PURE__*/
function () {
function ProgressResult(progressResponse) {
_classCallCheck(this, ProgressResult);
this.progressResponse = progressResponse;
}
load(query, options, callback) {
if (typeof options === 'function' && !callback) {
callback = options;
options = undefined;
_createClass(ProgressResult, [{
key: "stage",
value: function stage() {
return this.progressResponse.stage;
}
}, {
key: "timeElapsed",
value: function timeElapsed() {
return this.progressResponse.timeElapsed;
}
}]);
options = options || {};
return ProgressResult;
}();
const loadImpl = async () => {
const response = await this.request(`/load?query=${JSON.stringify(query)}`);
if (response.status === 502) {
return loadImpl(); // TODO backoff wait
var API_URL = "https://statsbot.co/cubejs-api/v1";
var CubejsApi =
/*#__PURE__*/
function () {
function CubejsApi(apiToken) {
_classCallCheck(this, CubejsApi);
this.apiToken = apiToken;
}
_createClass(CubejsApi, [{
key: "request",
value: function request(url, config) {
return fetch("".concat(API_URL).concat(url), Object.assign({
headers: {
Authorization: this.apiToken,
'Content-Type': 'application/json'
}
}, config || {}));
}
}, {
key: "load",
value: function load(query, options, callback) {
var _this = this;
if (typeof options === 'function' && !callback) {
callback = options;
options = undefined;
}
const body = await response.json();
if (body.error === 'Continue wait') {
if (options.progressCallback) {
options.progressCallback(new ProgressResult(body));
}
options = options || {};
var loadImpl =
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee() {
var response, body;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return _this.request("/load?query=".concat(JSON.stringify(query)));
case 2:
response = _context.sent;
if (!(response.status === 502)) {
_context.next = 5;
break;
}
return _context.abrupt("return", loadImpl());
case 5:
_context.next = 7;
return response.json();
case 7:
body = _context.sent;
if (!(body.error === 'Continue wait')) {
_context.next = 11;
break;
}
if (options.progressCallback) {
options.progressCallback(new ProgressResult(body));
}
return _context.abrupt("return", loadImpl());
case 11:
if (!(response.status !== 200)) {
_context.next = 13;
break;
}
throw new Error(body.error);
case 13:
return _context.abrupt("return", new ResultSet(body));
case 14:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
return function loadImpl() {
return _ref.apply(this, arguments);
};
}();
if (callback) {
loadImpl().then(function (r) {
return callback(null, r);
}, function (e) {
return callback(e);
});
} else {
return loadImpl();
}
if (response.status !== 200) {
throw new Error(body.error); // TODO error class
}
return new ResultSet(body);
};
if (callback) {
loadImpl().then(r => callback(null, r), e => callback(e));
} else {
return loadImpl();
}
}
}
}]);
var index = (apiToken) => {
return CubejsApi;
}();
var index = (function (apiToken) {
return new CubejsApi(apiToken);
};
});
export default index;
{
"name": "@cubejs-client/core",
"version": "0.1.0",
"version": "0.1.1",
"description": "cube.js client",

@@ -8,3 +8,5 @@ "main": "dist/cubejs-client-core.js",

"dependencies": {
"core-js": "^2.5.3",
"ramda": "^0.25.0",
"regenerator-runtime": "^0.12.1",
"whatwg-fetch": "^3.0.0"

@@ -11,0 +13,0 @@ },

@@ -1,2 +0,2 @@

import { groupBy, pipe, toPairs, uniq, flatten, map } from 'ramda';
import { groupBy, pipe, toPairs, uniq, flatten, map, unnest, dropLast } from 'ramda';

@@ -9,3 +9,3 @@ export default class ResultSet {

series(pivotConfig) {
return this.seriesNames(pivotConfig).map(({ title, key}) => ({
return this.seriesNames(pivotConfig).map(({ title, key }) => ({
title,

@@ -21,4 +21,3 @@ series: this.pivotedRows(pivotConfig).map(({ category, ...obj }) => ({ value: obj[key], category }))

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

@@ -31,10 +30,4 @@ return query.measures.map(value);

axisKeys(axis) {
const query = this.loadResponse.query;
if (axis.find(d => d === 'measures') && (query.measures || []).length) {
let withoutMeasures = axis.filter(d => d !== 'measures');
return query.measures.map(measure => withoutMeasures.concat(measure).join(', '));
} else {
return [axis.join(', ')];
}
axisValuesString(axisValues, delimiter) {
return axisValues.map(v => v != null ? v : '∅').join(delimiter || ':');
}

@@ -58,26 +51,46 @@

static measureFromAxis(axis) {
const axisValues = axis.split(', ');
static measureFromAxis(axisValues) {
return axisValues[axisValues.length - 1];
};
pivotedRows(pivotConfig) {
pivot(pivotConfig) {
// TODO missing date filling
pivotConfig = this.normalizePivotConfig(pivotConfig);
return pipe(groupBy(this.axisValues(pivotConfig.x)), toPairs)(this.loadResponse.data).map(([category, rows]) => ({
rows,
category,
...(rows.map(row => this.axisValues(pivotConfig.y)(row).map(series => {
return pipe(
map(row => this.axisValues(pivotConfig.x)(row).map(xValues => ({ xValues, row }))),
unnest,
groupBy(({ xValues }) => this.axisValuesString(xValues)),
toPairs
)(this.loadResponse.data).map(([xValuesString, rows]) => {
const xValues = rows[0].xValues;
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(category) :
ResultSet.measureFromAxis(series);
ResultSet.measureFromAxis(xValues) :
ResultSet.measureFromAxis(yValues);
return {
[series]: row[measure]
[this.axisValuesString(yValues)]: row[measure]
}
}).reduce((a, b) => Object.assign(a, b), {})
)).reduce((a, b) => Object.assign(a, b), {})
})
);
};
});
}
pivotedRows(pivotConfig) { // TODO
return this.chartPivot(pivotConfig);
}
chartPivot(pivotConfig) {
return this.pivot(pivotConfig).map(({ xValues, ...measures }) => ({
category: this.axisValuesString(xValues, ', '),
...(map(m => m && Number.parseFloat(m), measures))
}));
}
totalRow() {
return this.pivotedRows()[0];
}
categories(pivotConfig) { //TODO

@@ -89,8 +102,8 @@ return this.pivotedRows(pivotConfig);

pivotConfig = this.normalizePivotConfig(pivotConfig);
return pipe(map(this.axisValues(pivotConfig.y)), uniq, flatten)(this.loadResponse.data).map(axis => ({
title: pivotConfig.y.find(d => d === 'measures') ? axis.replace(
ResultSet.measureFromAxis(axis),
this.loadResponse.annotation.measures[ResultSet.measureFromAxis(axis)].title
) : axis,
key: axis
return pipe(map(this.axisValues(pivotConfig.y)), unnest, uniq)(this.loadResponse.data).map(axisValues => ({
title: this.axisValuesString(pivotConfig.y.find(d => d === 'measures') ?
dropLast(1, axisValues)
.concat(this.loadResponse.annotation.measures[ResultSet.measureFromAxis(axisValues)].title) :
axisValues, ', '),
key: this.axisValuesString(axisValues)
}))

@@ -97,0 +110,0 @@ }

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

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc