Skip to content

Commit

Permalink
Merge pull request #547 from jpmorganchase/remote-arrow
Browse files Browse the repository at this point in the history
Remote arrow
  • Loading branch information
texodus authored Apr 19, 2019
2 parents 529e9b5 + 965363e commit 52d134e
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 14 deletions.
19 changes: 14 additions & 5 deletions docs/md/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,21 +432,30 @@ const fs = require("fs");
// module's directory.
const host = new WebSocketHost({ assets: [__dirname], port: 8080 });

// Read an arrow file from the file system and load it as a named data source.
// Read an arrow file from the file system and load it as a named table.
const arr = fs.readFileSync(__dirname + "/superstore.arrow");
host.open("data_source_one", table(arr));
const tbl = table(arr);
host.host_table("table_one", tbl);

// Or host a view
const view = tbl.view({filter: [["State", "==", "Texas"]]});
host.host_view("view_one", view);
```

In the browser:

```javascript
var elem = document.getElementsByTagName("perspective-viewer")[0];
const elem = document.getElementsByTagName("perspective-viewer")[0];

// Bind to the server's worker instead of instantiating a Web Worker.
var worker = perspective.worker(window.location.origin.replace("http", "ws"));
const worker = perspective.worker(window.location.origin.replace("http", "ws"));

// Bind the viewer to the preloaded data source.
elem.load(worker.open("data_source_one"));
elem.load(worker.open_table("table_one"));

// Or load data from a view
const arrow = await worker.open_view("view_one").to_arrow();
elem.load(arrow);
```

`<perspective-viewer>` instances bound in this way are otherwise no different
Expand Down
52 changes: 52 additions & 0 deletions examples/git_history/chained.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!--
Copyright (c) 2017, the Perspective Authors.
This file is part of the Perspective library, distributed under the terms of
the Apache License 2.0. The full license can be found in the LICENSE file.
-->

<!DOCTYPE html>
<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">

<script src="perspective.view.js"></script>
<script src="hypergrid.plugin.js"></script>
<script src="highcharts.plugin.js"></script>

<script src="perspective.js"></script>

<link rel='stylesheet' href="index.css">
<link rel='stylesheet' href="material.css">

</head>

<body>

<perspective-viewer
id="view1"
view="y_area"
row-pivots='["week_bucket(Date)"]'
column-pivots='["Email"]'
columns='["Hash"]'
computed-columns='[{"name":"week_bucket(Date)","inputs":["Date"],"func":"week_bucket"}]'>

</perspective-viewer>

<script>
window.addEventListener('WebComponentsReady', async function() {
var elem = document.getElementById('view1');
var worker = perspective.worker(window.location.origin.replace('http', 'ws'));
let view = worker.open_view('data_source_one');
let arrow = await view.to_arrow();
elem.load(perspective.worker().table(arrow));
});
</script>

</body>

</html>
3 changes: 2 additions & 1 deletion examples/git_history/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ function execute(command, callback) {

execute(`git log --date=iso --pretty=format:'"%h","%an","%aD","%s","%ae"'`, log => {
const host = new WebSocketHost({assets: [__dirname]});
host.open("data_source_one", table("Hash,Name,Date,Message,Email\n" + log));
const tbl = table("Hash,Name,Date,Message,Email\n" + log);
host.host_view("data_source_one", tbl.view());
});
2 changes: 1 addition & 1 deletion packages/perspective-cli/src/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
window.addEventListener('WebComponentsReady', function() {
var elem = document.getElementById('view1');
var worker = perspective.worker(window.location.origin.replace('http', 'ws'));
elem.load(worker.open('data_source_one'));
elem.load(worker.open_table('data_source_one'));
});
</script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion packages/perspective-cli/src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async function host(filename, options) {
} else {
file = await read_stdin();
}
server.open("data_source_one", table(file));
server.host_table("data_source_one", table(file));
if (options.open) {
open_browser(options.port);
}
Expand Down
13 changes: 12 additions & 1 deletion packages/perspective/src/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ function view(worker, table_name, config) {
bindall(this);
}

function proxy_view(worker, name) {
this._worker = worker;
this._name = name;
}

proxy_view.prototype = view.prototype;

view.prototype.get_config = async_queue("get_config");

view.prototype.to_json = async_queue("to_json");
Expand Down Expand Up @@ -251,10 +258,14 @@ worker.prototype.send = function() {
throw new Error("post() not implemented");
};

worker.prototype.open = function(name) {
worker.prototype.open_table = function(name) {
return new proxy_table(this, name);
};

worker.prototype.open_view = function(name) {
return new proxy_view(this, name);
};

let _initialized = false;

worker.prototype._handle = function(e) {
Expand Down
18 changes: 14 additions & 4 deletions packages/perspective/src/js/perspective.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,24 @@ class WebSocketHost extends module.exports.Host {
});
}

post(msg) {
this.REQS[msg.id].send(JSON.stringify(msg));
post(msg, transferable) {
if (transferable) {
msg.is_transferable = true;
this.REQS[msg.id].send(JSON.stringify(msg));
this.REQS[msg.id].send(transferable[0]);
} else {
this.REQS[msg.id].send(JSON.stringify(msg));
}
delete this.REQS[msg.id];
}

open(name, table) {
host_table(name, table) {
this._tables[name] = table;
table.view({aggregate: []});
table.view({columns: []});
}

host_view(name, view) {
this._views[name] = view;
}

close() {
Expand Down
14 changes: 13 additions & 1 deletion packages/perspective/src/js/perspective.parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,26 @@ class WebSocketWorker extends worker {
pingTimeout: 15000,
pingMsg: "heartbeat"
});
this._ws.ws.binaryType = "arraybuffer";
this._ws.onopen = () => {
this.send({id: -1, cmd: "init"});
};
this._ws.onmessage = msg => {
if (msg.data === "heartbeat") {
return;
}
this._handle({data: JSON.parse(msg.data)});
if (this._pending_arrow) {
this._handle({data: {id: this._pending_arrow, data: msg.data}});
delete this._pending_arrow;
} else {
msg = JSON.parse(msg.data);
if (msg.is_transferable) {
console.warn("Arrow transfer detected!");
this._pending_arrow = msg.id;
} else {
this._handle({data: msg});
}
}
};
}

Expand Down

0 comments on commit 52d134e

Please sign in to comment.