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

#7326 improve spark status widget #7423

Merged
merged 11 commits into from
May 24, 2018
60 changes: 60 additions & 0 deletions js/notebook/src/SparkUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class SparkUIModel extends widgets.VBoxModel {

class SparkUIView extends widgets.VBoxView {
private sparkStats: Widget;
private sparkAppId: string;
private apiCallIntervalId: number;
private connectionLabelActive: HTMLElement;
private connectionLabelMemory: HTMLElement;
private connectionLabelDead: HTMLElement;
Expand All @@ -50,6 +52,7 @@ class SparkUIView extends widgets.VBoxView {
public update() {
super.update();

this.connectToApi();
this.processConnectionWidget();
this.updateLabels();
}
Expand Down Expand Up @@ -82,6 +85,10 @@ class SparkUIView extends widgets.VBoxView {
}

private appendSparkStats(): void {
if (this.sparkStats) {
return;
}

this.sparkStats = new Widget();
this.sparkStats.node.classList.add('bx-stats');
this.sparkStats.node.innerHTML = `
Expand All @@ -97,6 +104,54 @@ class SparkUIView extends widgets.VBoxView {
this.el.querySelector('.bx-connection-status').insertAdjacentElement('afterend', this.sparkStats.node);
}

private connectToApi() {
let baseUrl;
let api;

this.sparkAppId = this.model.get('sparkAppId');

if (!this.sparkAppId) {
return;
}

try {
const coreutils = require('@jupyterlab/coreutils');
coreutils.PageConfig.getOption('pageUrl');
baseUrl = coreutils.PageConfig.getBaseUrl();
} catch(e) {
baseUrl = `${window.location.origin}/`;
}

api = new BeakerXApi(baseUrl);
this.setApiCallInterval(api);
}

private setApiCallInterval(api: BeakerXApi): void {
const sparkUrl = `${api.getApiUrl('sparkmetrics/executors')}/${this.sparkAppId}`;
const getMetrict = async () => {
try {
const response = await fetch(sparkUrl);

if (!response.ok) {
console.log(response);

return;
}

const data = await response.json();
this.updateMetrics(data);
} catch(e) {
clearInterval(this.apiCallIntervalId)
}
};

this.apiCallIntervalId = setInterval(getMetrict, 1000);
}

private updateMetrics(data: object) {
console.log(data);
}

private processConnectionWidget() {
this.children_views.update(this.model.get('children')).then((views) => {
views.forEach((view) => {
Expand All @@ -110,6 +165,11 @@ class SparkUIView extends widgets.VBoxView {
});
});
}

despose() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks #7425

super.dispose();
clearInterval(this.apiCallIntervalId);
}
}

export default {
Expand Down