@atombender/hydrant-api
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -38,1 +38,5 @@ 'use strict'; | ||
_defaults(exports, _interopRequireWildcard(_schemas)); | ||
var _tables = require('./tables'); | ||
_defaults(exports, _interopRequireWildcard(_tables)); |
208
dist/json.js
@@ -11,2 +11,4 @@ 'use strict'; | ||
exports.deserializeQuery = deserializeQuery; | ||
exports.serializeTable = serializeTable; | ||
exports.deserializeTable = deserializeTable; | ||
exports.serializeResultSet = serializeResultSet; | ||
@@ -24,2 +26,6 @@ exports.serializeSchema = serializeSchema; | ||
var _immutable = require('immutable'); | ||
var _immutable2 = _interopRequireDefault(_immutable); | ||
var _queries = require('./queries'); | ||
@@ -33,2 +39,40 @@ | ||
var _tables = require('./tables'); | ||
// Serialize a basic JavaScript value object (eg., string, integer, Moment object). | ||
// TODO: Should probably go through Dimension class. | ||
function getRawValue(_x) { | ||
var _again = true; | ||
_function: while (_again) { | ||
var value = _x; | ||
_again = false; | ||
if (value instanceof _dimensions.Value) { | ||
_x = value.value; | ||
_again = true; | ||
continue _function; | ||
} else if (_moment2['default'].isMoment(value)) { | ||
return +value; | ||
} else { | ||
return value; | ||
} | ||
} | ||
} | ||
function serializeTimeRange(timeRange) { | ||
return { | ||
start: (0, _moment2['default'])(timeRange.start).unix(), | ||
end: (0, _moment2['default'])(timeRange.end).unix() | ||
}; | ||
} | ||
function deserializeTimeRange(raw) { | ||
if (raw && raw.start && raw.end) { | ||
return _moment2['default'].range(_moment2['default'].unix(raw.start), _moment2['default'].unix(raw.end)); | ||
} else { | ||
return null; | ||
} | ||
} | ||
function serializeQuery(query) { | ||
@@ -59,6 +103,3 @@ var raw = {}; | ||
if (query.timeRange) { | ||
raw.timeRange = { | ||
start: (0, _moment2['default'])(query.timeRange.start).unix(), | ||
end: (0, _moment2['default'])(query.timeRange.end).unix() | ||
}; | ||
raw.timeRange = serializeTimeRange(query.timeRange); | ||
} | ||
@@ -86,3 +127,3 @@ raw.timeZone = query.timeZone; | ||
query.setTimeframe(parseTimeframe(rawQuery.timeframe)); | ||
query.setTimeRange(parseTimeRange(rawQuery.timeRange)); | ||
query.setTimeRange(deserializeTimeRange(rawQuery.timeRange)); | ||
query.setTimeZone(rawQuery.timeZone); | ||
@@ -94,10 +135,2 @@ if (rawQuery.limit != null) { | ||
function parseTimeRange(raw) { | ||
if (raw && raw.start && raw.end) { | ||
return _moment2['default'].range(_moment2['default'].unix(raw.start), _moment2['default'].unix(raw.end)); | ||
} else { | ||
return null; | ||
} | ||
} | ||
function parseTimeInterval(raw) { | ||
@@ -184,2 +217,151 @@ if (raw) { | ||
function serializeTable(table) { | ||
return { | ||
rows: table.rows.map(serializeTableRow) | ||
}; | ||
function serializeTableRow(tableRow) { | ||
var rawRow = { | ||
valueMap: serializeValueMap(tableRow.valueMap) | ||
}; | ||
if (tableRow.childRows.size > 0) { | ||
rawRow.childRows = tableRow.childRows.map(serializeTableRow).toJS(); | ||
} | ||
return rawRow; | ||
} | ||
function serializeValueMap(valueMap) { | ||
return valueMap.entrySeq().map(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2); | ||
var column = _ref2[0]; | ||
var value = _ref2[1]; | ||
if (value instanceof _tables.TableRow) { | ||
return { | ||
row: serializeTableRow(value), | ||
column: serializeColumn(column) | ||
}; | ||
} else { | ||
return { | ||
value: getRawValue(value), | ||
column: serializeColumn(column) | ||
}; | ||
} | ||
}).toJS(); | ||
} | ||
function serializeColumn(column) { | ||
if (column instanceof _dimensions.AggregationInstance) { | ||
return { | ||
aggregation: serializeAggregationInstance(column) | ||
}; | ||
} else if (column instanceof _tables.CompositeColumn) { | ||
return { | ||
composite: column.columns.map(serializeColumn).toJS() | ||
}; | ||
} else if (column instanceof _dimensions.Dimension) { | ||
return { | ||
dimension: { id: column.id } | ||
}; | ||
} else if (column instanceof _dimensions.Value) { | ||
return { | ||
value: { | ||
value: getRawValue(column), | ||
id: column.dimension.id | ||
} | ||
}; | ||
} else { | ||
throw new Error('Don\'t know how to serialize: ' + column); | ||
} | ||
} | ||
function serializeAggregationInstance(aggregationInstance) { | ||
return { | ||
id: aggregationInstance.dimension.id, | ||
categoryValue: getRawValue(aggregationInstance.categoryValue), | ||
functionName: aggregationInstance.functionName | ||
}; | ||
} | ||
} | ||
function deserializeTable(rawTable, schema) { | ||
var availableDimensions = [].concat(schema.dimensions); | ||
// TODO: We add this aggregation here dynamically, but it probably should | ||
// be "declared" by the result somehow. | ||
var countDimension = new _dimensions.Dimension('__count__', 'count'); | ||
countDimension.description = 'Count'; | ||
countDimension.type = 'integer'; | ||
countDimension.aggregationFunctionName = 'count'; | ||
availableDimensions.push(countDimension); | ||
var tableRows = _immutable2['default'].List(rawTable.rows).map(deserializeTableRow); | ||
return new _tables.Table(schema, tableRows); | ||
function deserializeTableRow(rawTableRow) { | ||
var tableRow = new _tables.TableRow(deserializeValueMap(rawTableRow.valueMap)); | ||
if (rawTableRow.childRows && rawTableRow.childRows.length) { | ||
rawTableRow.childRows.forEach(function (rawChildRow) { | ||
tableRow = tableRow.addChildRow(deserializeTableRow(rawChildRow)); | ||
}); | ||
} | ||
return tableRow; | ||
} | ||
function deserializeValueMap(rawValueMap) { | ||
var valueMap = _immutable2['default'].OrderedMap(); | ||
rawValueMap.forEach(function (rawValueEntry) { | ||
var column = deserializeColumn(rawValueEntry.column); | ||
if (rawValueEntry.row) { | ||
valueMap = valueMap.set(column, deserializeTableRow(rawValueEntry.row)); | ||
} else { | ||
var value = rawValueEntry.value; | ||
if (typeof column.parseRawValue === 'function') { | ||
value = column.parseRawValue(value); | ||
} | ||
valueMap = valueMap.set(column, value); | ||
} | ||
}); | ||
return valueMap; | ||
} | ||
function deserializeColumn(rawColumn) { | ||
if (rawColumn.aggregation) { | ||
return deserializeAggregationInstance(rawColumn.aggregation); | ||
} else if (rawColumn.composite) { | ||
return new _tables.CompositeColumn(rawColumn.composite.map(deserializeColumn)); | ||
} else if (rawColumn.value) { | ||
var dimension = (0, _underscore.find)(availableDimensions, function (d) { | ||
return d.id === rawColumn.value.id; | ||
}); | ||
if (!dimension) { | ||
throw new Error('Could not find dimension: ' + rawColumn.value.id); | ||
} | ||
var value = dimension.parseRawValue(rawColumn.value.value); | ||
return new _dimensions.Value(dimension, value); | ||
} else if (rawColumn.dimension) { | ||
var dimension = (0, _underscore.find)(availableDimensions, function (d) { | ||
return d.id === rawColumn.dimension.id; | ||
}); | ||
if (!dimension) { | ||
throw new Error('Could not find dimension: ' + rawColumn.dimension.id); | ||
} | ||
return dimension; | ||
} else { | ||
throw new Error('Don\'t know how to deserialize this column: ' + rawColumn); | ||
} | ||
} | ||
function deserializeAggregationInstance(rawAggregationInstance) { | ||
var dimension = (0, _underscore.find)(schema.dimensions, function (d) { | ||
return d.id === rawAggregationInstance.id; | ||
}); | ||
if (!dimension) { | ||
throw new Error('Could not find dimension: ' + rawAggregationInstance.id); | ||
} | ||
return new _dimensions.AggregationInstance(dimension, dimension.parseRawValue(rawAggregationInstance.categoryValue), rawAggregationInstance.functionName); | ||
} | ||
} | ||
function serializeResultSet(results) { | ||
@@ -186,0 +368,0 @@ return { |
@@ -26,3 +26,3 @@ 'use strict'; | ||
} | ||
if (!(mode === 'row' || mode === 'pivot')) { | ||
if (!(mode === 'row' || mode === 'pivot' || mode === 'nest')) { | ||
throw new Error('Invalid mode: ' + mode); | ||
@@ -90,26 +90,30 @@ } | ||
value: function mayGroupItemBeNested(groupByItem) { | ||
return check(groupByItem, this.groupByItems); | ||
return this._mayGroupItemBeNested(groupByItem, this.groupByItems); | ||
} | ||
}, { | ||
key: '_mayGroupItemBeNested', | ||
function check(item, items) { | ||
var idx = (0, _atombenderUtils.findIndex)(items, function (g) { | ||
return g.dimension.id === item.dimension.id; | ||
}); | ||
if (idx > 0 && items.length > 1) { | ||
for (var i = idx - 1; i >= 0; i--) { | ||
if (items[i].mode === 'nest') { | ||
// A group item can only be nested if it comes after a "row" or a "nest", and | ||
// we only permit one parent. | ||
value: function _mayGroupItemBeNested(groupByItem, items) { | ||
var idx = (0, _atombenderUtils.findIndex)(items, function (g) { | ||
return g.dimension.id === groupByItem.dimension.id; | ||
}); | ||
if (idx > 0 && items.length > 1) { | ||
for (var i = idx - 1; i >= 0; i--) { | ||
if (items[i].mode === 'nest') { | ||
return true; | ||
} else if (items[i].mode === 'row') { | ||
var j = (0, _atombenderUtils.findIndex)(items, function (g) { | ||
return g.mode === 'nest'; | ||
}); | ||
if (j === -1 || j > i) { | ||
return true; | ||
} else if (items[i].mode === 'row') { | ||
var j = (0, _atombenderUtils.findIndex)(items, function (g) { | ||
return g.mode === 'nest'; | ||
}); | ||
if (j === -1 || j > i) { | ||
return true; | ||
} | ||
} else { | ||
break; | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
return false; | ||
} | ||
return false; | ||
} | ||
@@ -170,9 +174,10 @@ }, { | ||
if (idx === 0 && g.mode !== 'pivot') { | ||
return (0, _underscore.extend)({}, g, { mode: 'row' }); | ||
} else if (g.mode === 'pivot' || _this.mayGroupItemBeNested(g, groupByItems)) { | ||
return new GroupBy(g.dimension, 'row'); | ||
} else if (g.mode === 'pivot' || _this._mayGroupItemBeNested(g, groupByItems)) { | ||
return g; | ||
} else { | ||
return (0, _underscore.extend)({}, g, { mode: 'row' }); | ||
return new GroupBy(g.dimension, 'row'); | ||
} | ||
}); | ||
console.log('new groupbys', this.groupByItems); | ||
} | ||
@@ -179,0 +184,0 @@ }, { |
@@ -17,2 +17,5 @@ 'use strict'; | ||
// A result row is a map of dimensions to raw values (eg., strings, | ||
// epoch integers, etc.). | ||
var ResultRow = (function () { | ||
@@ -24,3 +27,3 @@ function ResultRow() { | ||
this.values = values || _immutable2['default'].Map(); | ||
this.values = values ? _immutable2['default'].Map(values) : _immutable2['default'].Map(); | ||
} | ||
@@ -40,2 +43,5 @@ | ||
// A result set is a very basic set of result rows (pairs of dimensions and | ||
// raw values) that is the raw output of a search. | ||
var ResultSet = (function () { | ||
@@ -47,3 +53,3 @@ function ResultSet() { | ||
this.rows = rows || _immutable2['default'].List(); | ||
this.rows = rows ? _immutable2['default'].List(rows) : _immutable2['default'].List(); | ||
} | ||
@@ -50,0 +56,0 @@ |
{ | ||
"name": "@atombender/hydrant-api", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75734
13
1996