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

Added Jupyter API for incremental updates #230

Merged
merged 2 commits into from
Sep 11, 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
working with perspective@7fe4c296c0243801f49637cb8bf78a4184c78a19
  • Loading branch information
timkpaine committed Sep 8, 2018
commit 24950031f4fb74130d69eda3db9c3d4c2369db18
133 changes: 2 additions & 131 deletions packages/perspective-jupyterlab/src/ts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*
*/

import * as io from 'socket.io-client';
import {Clipboard} from '@jupyterlab/apputils';

/* defines */
Expand All @@ -23,140 +22,12 @@ const PSP_CONTAINER_CLASS = 'jp-PSPContainer';
export
const PSP_CONTAINER_CLASS_DARK = 'jp-PSPContainer-dark';


export interface PSPHelper {
//FIXME should enforce perspective viewer type
start(psp: any): void;
getUrl(): string;
}

export function datasourceToSource(source: string){
if(source.indexOf('sio://') !== -1){
return 'sio';
} else if(source.indexOf('ws://') !== -1){
return 'ws';
} else if(source.indexOf('wss://') !== -1){
return 'wss';
} else if(source.indexOf('http://') !== -1){
return 'http';
} else if(source.indexOf('https://') !== -1){
return 'http';
} else if(source.indexOf('comm://') !== -1){
if(source.indexOf('comm://') !== -1){
return 'comm';
} else{
throw new Error('Unrecognized source');
}
}

export class PSPWebsocketHelper implements PSPHelper {
constructor(url: string, send: string, records: boolean) {
this.url = url;
this.send = send;
}

start(psp: any): void {
let socket = new WebSocket(this.url);
let to_send = this.send;
let as_record = this.records;
socket.onopen = function (event: any) {
if (to_send) {
socket.send(to_send);
}
};

socket.onmessage = function (event: any) {
if (as_record) {
psp.update([event.data]);
} else {
psp.update(event.data);
}
};

}

getUrl(): string {
return this.url;
}

private url:string;
private send:string;
private records:boolean;
}

export class PSPSocketIOHelper implements PSPHelper {
constructor(url: string, channel: string, records: boolean) {
this.url = url;
this.channel = channel;
this.records = records;
}

start(psp: any): void {
let socket = io.connect(this.url);
let as_record = this.records;
socket.on(this.channel, function (msg: any) {
if (as_record){
psp.update([msg.msg]);
} else {
psp.update(msg.msg);
}
})
return 'static';
}

getUrl(): string {
return this.url;
}

private url:string;
private channel:string;
private records:boolean;
}

export class PSPHttpHelper implements PSPHelper {
constructor(url:string, field: string, records: boolean, repeat: number) {
this.url = url;
this.field = field;
this.records = records;
this.repeat = repeat;
}


sendAndLoad(psp: any) {
let xhr = new XMLHttpRequest();
let field = this.field;
let as_record = this.records;

xhr.open('GET', this.url, true);
xhr.onload = function () {
let data = JSON.parse(xhr.response);
if (field !== ''){
data = data[field];
}

if (as_record){
psp.update([data]);
} else {
psp.update(data);
}
}
xhr.send(null);
}

start(psp: any): void {
if (this.repeat){
setInterval( () => this.sendAndLoad(psp), this.repeat);
} else {
this.sendAndLoad(psp);
}
}

getUrl(): string {
return this.url;
}

private url:string;
private field:string;
private records:boolean;
private repeat:number;
}

export function convertToCSV(objArray: Array<Object>): string {
Expand Down
30 changes: 2 additions & 28 deletions packages/perspective-jupyterlab/src/ts/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {PERSPECTIVE_VERSION} from './version.ts';

/* Helper methods */
import {
PSPHelper, PSPWebsocketHelper, PSPSocketIOHelper, PSPHttpHelper, datasourceToSource, createCopyDl
datasourceToSource, createCopyDl
} from './utils.ts';

/* perspective components */
Expand All @@ -46,7 +46,6 @@ class PerspectiveModel extends DOMWidgetModel {
_view_module_version: PerspectiveModel.view_module_version,
_data: null,

datasrc: 'static',
schema: {},
view: 'hypergrid',
columns: [],
Expand All @@ -55,8 +54,7 @@ class PerspectiveModel extends DOMWidgetModel {
aggregates: [],
sort: [],
settings: false,
dark: false,
helper_config: {}
dark: false
};
}

Expand All @@ -77,15 +75,13 @@ class PerspectiveModel extends DOMWidgetModel {
export
class PerspectiveView extends DOMWidgetView {
private psp: any;
private helper: PSPHelper;

render() {
this.psp = Private.createNode(this.el);
let observer = new MutationObserver(this.psp.notifyResize.bind(this.psp));
observer.observe(this.el, {attributes: true});

this.model.on('change:_data', this.data_changed, this);
this.model.on('change:datasrc', this.datasrc_changed, this);
this.model.on('change:schema', this.schema_changed, this);
this.model.on('change:view', this.view_changed, this);
this.model.on('change:columns', this.columns_changed, this);
Expand Down Expand Up @@ -153,28 +149,6 @@ class PerspectiveView extends DOMWidgetView {

if (type === 'static') {
this.data_changed();
} else if (type === 'ws' || type === 'wss') {
let config = this.model.get('helper_config');
let send = config.send || '';
let records = config.records || false;
this.helper = new PSPWebsocketHelper(this.model.get('datasrc'), send, records);
this.helper.start(this.psp);
} else if (type === 'http' || type === 'https') {
let config = this.model.get('helper_config');
let field = config.field || '';
let records = config.records || false;
let repeat = config.repeat || 1;
this.helper = new PSPHttpHelper(this.model.get('datasrc'), field, records, repeat);
this.helper.start(this.psp);

} else if (type === 'sio') {
let config = this.model.get('helper_config');
let channel = config.channel || '';
let records = config.records || false;
let addr = this.model.get('datasrc').replace('sio://', '');
this.helper = new PSPSocketIOHelper(addr, channel, records);
this.helper.start(this.psp);

} else if (type === 'comm') {
//grab session id
let els = this.model.get('datasrc').replace('comm://', '').split('/');
Expand Down