Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote performance #190

Merged
merged 5 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added column-oriented JSON output method
  • Loading branch information
texodus committed Aug 14, 2018
commit 9ed5a6af61b93e783e00ba46dd5839aaf65e379e
2 changes: 2 additions & 0 deletions packages/perspective/src/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ function view(worker, table_name, config) {

view.prototype.to_json = async_queue('to_json');

view.prototype.to_columns = async_queue('to_columns');

view.prototype.to_csv = async_queue('to_csv');

view.prototype.schema = async_queue('schema');
Expand Down
34 changes: 31 additions & 3 deletions packages/perspective/src/js/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ const to_format = async function (options, formatter) {
if (this.config.row_pivot[0] !== 'psp_okey') {
let col_name = "__ROW_PATH__";
let row_path = this.ctx.unity_get_row_path(start_row + ridx);
formatter.initColumnValue(row, col_name)
formatter.initColumnValue(data, row, col_name)
for (let i = 0; i < row_path.size(); i++) {
const value = __MODULE__.scalar_vec_to_val(row_path, i);
formatter.addColumnValue(data, row, col_name, value);
Expand All @@ -586,8 +586,36 @@ const to_format = async function (options, formatter) {

return formatter.formatData(data, options.config)
}

/**
* Serializes this view to JSON data in a column-oriented format.
*
* @async
*
* @param {Object} [options] An optional configuration object.
* @param {number} options.start_row The starting row index from which
* to serialize.
* @param {number} options.end_row The ending row index from which
* to serialize.
* @param {number} options.start_col The starting column index from which
* to serialize.
* @param {number} options.end_col The ending column index from which
* to serialize.
*
* @returns {Promise<Array>} A Promise resolving to An array of Objects
* representing the rows of this {@link view}. If this {@link view} had a
* "row_pivots" config parameter supplied when constructed, each row Object
* will have a "__ROW_PATH__" key, whose value specifies this row's
* aggregated path. If this {@link view} had a "column_pivots" config
* parameter supplied, the keys of this object will be comma-prepended with
* their comma-separated column paths.
*/
view.prototype.to_columns = async function (options) {
return to_format.call(this, options, formatters.jsonTableFormatter);
}

/**
* Serializes this view to JSON data in a standard format.
* Serializes this view to JSON data in a row-oriented format.
*
* @async
*
Expand All @@ -609,7 +637,7 @@ const to_format = async function (options, formatter) {
* parameter supplied, the keys of this object will be comma-prepended with
* their comma-separated column paths.
*/
view.prototype.to_json = async function(options) {
view.prototype.to_json = async function (options) {
return to_format.call(this, options, formatters.jsonFormatter);
}

Expand Down
22 changes: 21 additions & 1 deletion packages/perspective/src/js/view_formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import papaparse from "papaparse";
const jsonFormatter = {
initDataValue: () => [],
initRowValue: () => ({}),
initColumnValue: (row, colName) => row[colName] = [],
initColumnValue: (data, row, colName) => row[colName] = [],
setColumnValue: (data, row, colName, value) => row[colName] = value,
addColumnValue: (data, row, colName, value) => row[colName].unshift(value),
addRow: (data, row) => data.push(row),
Expand All @@ -23,7 +23,27 @@ const csvFormatter = Object.assign({}, jsonFormatter, {
formatData: (data, config) => papaparse.unparse(data, config)
});

const jsonTableFormatter = {
initDataValue: () => new Object(),
initRowValue: () => {},
setColumnValue: (data, row, colName, value) => {
data[colName] = data[colName] || [];
data[colName].push(value)
},
addColumnValue: (data, row, colName, value) => {
data[colName] = data[colName] || [];
data[colName][data[colName].length - 1].unshift(value);
},
initColumnValue: (data, row, colName) => {
data[colName] = data[colName] || [];
data[colName].push([]);
},
addRow: (data, row) => {},
formatData: data => data
}

export default {
jsonFormatter,
jsonTableFormatter,
csvFormatter
};
5 changes: 5 additions & 0 deletions packages/perspective/test/config/test_node.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ module.exports = Object.assign({}, common(), {
path: path.resolve(__dirname, '../../build'),
libraryTarget: 'umd'
}
});

module.exports.module.rules.push({
test: /\.wasm$/,
loader: "arraybuffer-loader"
});
14 changes: 14 additions & 0 deletions packages/perspective/test/js/constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ var data_3 = [
{'w': 4.5, 'x': 4, 'y':'d', 'z': false}
];

var data_7 = {
'w': [1.5, 2.5, 3.5, 4.5],
'x': [1, 2, 3, 4],
'y': ['a', 'b', 'c', 'd'],
'z': [true, false, true, false]
};

var meta_3 = {
'w': 'float',
'x': "integer",
Expand Down Expand Up @@ -176,6 +183,13 @@ module.exports = (perspective) => {
expect(answer).toEqual(result2);
});

it("Serializes a simple view to column-oriented JSON", async function () {
var table = perspective.table(data_3);
var view = table.view({});
let result2 = await view.to_columns();
expect(data_7).toEqual(result2);
});

});

describe("Constructors", function() {
Expand Down