Skip to content

Commit

Permalink
Merge pull request #699 from finos/leaves-only
Browse files Browse the repository at this point in the history
`leaves_only` flag for perspective
  • Loading branch information
texodus authored Sep 2, 2019
2 parents 69dde95 + 417b43d commit 42775fd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
5 changes: 4 additions & 1 deletion packages/perspective-viewer-hypergrid/src/js/hypergrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ async function grid_create(div, view, task) {
}

const dataModel = this.hypergrid.behavior.dataModel;
const columns = Object.keys(json);
let columns = Object.keys(json);

dataModel.setIsTree(rowPivots.length > 0);
dataModel.setDirty(nrows);
Expand All @@ -289,6 +289,9 @@ async function grid_create(div, view, task) {
range.end_row += this.hasAttribute("settings") ? 8 : 2;
range.end_col += rowPivots && rowPivots.length > 0 ? 1 : 0;
let next_page = await dataModel._view.to_columns(range);
if (columns.length === 0) {
columns = Object.keys(await view.to_columns(window));
}
dataModel.data = [];
const rows = page2hypergrid(next_page, rowPivots, columns);
const data = dataModel.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const TREE_COLUMN_INDEX = require("fin-hypergrid/src/behaviors/Behavior").protot
function page2hypergrid(data, row_pivots, columns) {
const data_columns = Object.keys(data);
const firstcol = data_columns.length > 0 ? data_columns[0] : undefined;
if (columns.length === 0 || data[firstcol].length === 0) {
if (columns.length === 0) {
return [];
}

Expand Down
17 changes: 12 additions & 5 deletions packages/perspective/src/js/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,21 +389,28 @@ export default function(Module) {

const viewport = this.config.viewport ? this.config.viewport : {};
const start_row = options.start_row || (viewport.top ? viewport.top : 0);
const end_row = options.end_row || (viewport.height ? start_row + viewport.height : max_rows);
const end_row = Math.min(max_rows, options.end_row || (viewport.height ? start_row + viewport.height : max_rows));
const start_col = options.start_col || (viewport.left ? viewport.left : 0);
const end_col = Math.min(max_cols, (options.end_col || (viewport.width ? start_col + viewport.width : max_cols)) * (hidden + 1));

const get_pkeys = !!options.index;

const leaves_only = !!options.leaves_only;
const num_sides = this.sides();
const has_row_path = num_sides !== 0 && !this.column_only;
const nidx = ["zero", "one", "two"][num_sides];

const slice = this.get_data_slice(start_row, end_row, start_col, end_col);
const ns = slice.get_column_names();
const col_names = extract_vector_scalar(ns).map(x => x.join(defaults.COLUMN_SEPARATOR_STRING));

let data = formatter.initDataValue();

for (let ridx = start_row; ridx < end_row; ridx++) {
let row_path = has_row_path ? slice.get_row_path(ridx) : undefined;
if (has_row_path && leaves_only && row_path.size() < this.config.row_pivots.length) {
row_path.delete();
continue;
}
let row = formatter.initRowValue();
for (let cidx = start_col; cidx < end_col; cidx++) {
const col_name = col_names[cidx];
Expand All @@ -412,13 +419,11 @@ export default function(Module) {
continue;
} else if (cidx === start_col && num_sides !== 0) {
if (!this.column_only) {
const row_path = slice.get_row_path(ridx);
formatter.initColumnValue(data, row, "__ROW_PATH__");
for (let i = 0; i < row_path.size(); i++) {
const value = __MODULE__.scalar_vec_to_val(row_path, i);
formatter.addColumnValue(data, row, "__ROW_PATH__", value);
}
row_path.delete();
}
} else {
const value = __MODULE__[`get_from_data_slice_${nidx}`](slice, ridx, cidx);
Expand All @@ -436,11 +441,13 @@ export default function(Module) {
}
}

if (row_path) {
row_path.delete();
}
formatter.addRow(data, row);
}

slice.delete();

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

Expand Down
31 changes: 31 additions & 0 deletions packages/perspective/test/js/to_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ module.exports = perspective => {
expect(json[0]).toEqual(comparator);
});

it("should respect end rows when larger than data size", async function() {
let table = perspective.table(int_float_string_data);
let view = table.view();
let json = await view.to_json({
start_row: 2,
end_row: 6
});
expect(json).toEqual(
int_float_string_data.slice(2).map(x => {
x.datetime = +x.datetime;
return x;
})
);
});

it("should respect start/end columns", async function() {
let table = perspective.table(int_float_string_data);
let view = table.view();
Expand Down Expand Up @@ -164,6 +179,22 @@ module.exports = perspective => {
});
});

describe("leaves_only flag", function() {
it("only emits leaves when leaves_only is set", async function() {
let table = perspective.table(int_float_string_data);
let view = table.view({
row_pivots: ["int"]
});
let json = await view.to_json({leaves_only: true});
expect(json).toEqual([
{__ROW_PATH__: [1], datetime: 1, float: 2.25, int: 1, string: 1},
{__ROW_PATH__: [2], datetime: 1, float: 3.5, int: 2, string: 1},
{__ROW_PATH__: [3], datetime: 1, float: 4.75, int: 3, string: 1},
{__ROW_PATH__: [4], datetime: 1, float: 5.25, int: 4, string: 1}
]);
});
});

describe("to_arrow()", function() {
it("serializes boolean arrays correctly", async function() {
// prevent regression in boolean parsing
Expand Down

0 comments on commit 42775fd

Please sign in to comment.