-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from nbgallery/execution_tracking
WIP to do cell execution tracking. Lots of cleanup to do but don't w…
- Loading branch information
Showing
8 changed files
with
243 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{ | ||
"name": "@jupyterlab-nbgallery/instrumentation", | ||
"version": "0.3.0", | ||
"description": "Track cell execution Metrics", | ||
"keywords": [ | ||
"jupyter", | ||
"jupyterlab", | ||
"jupyterlab-extension" | ||
], | ||
"homepage": "https://github.com/nbgallery/lab-extensions", | ||
"bugs": { | ||
"url": "https://github.com/nbgallery/lab-extensions/issues" | ||
}, | ||
"license": "MIT", | ||
"author": "Team@NBG", | ||
"files": [ | ||
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}", | ||
"style/**/*.{css,eot,gif,html,jpg,json,png,svg,woff2,ttf}", | ||
"schema/**/*.json" | ||
], | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/nbgallery/lab-extensions" | ||
}, | ||
"scripts": { | ||
"build": "jlpm run build:lib", | ||
"build:labextension": "mkdir -p ../labextension && mkdir -p labextension && cd labextension && npm pack .. && cp *.tgz ../../labextension/", | ||
"build:lib": "tsc", | ||
"build:all": "jlpm run build:labextension", | ||
"clean": "jlpm run clean:lib", | ||
"clean:lib": "rimraf lib tsconfig.tsbuildinfo", | ||
"clean:labextension": "rimraf labextension", | ||
"install-ext": "jupyter labextension install . --no-build", | ||
"prepare": "jlpm run clean && jlpm run build", | ||
"eslint": "eslint . --ext .ts,.tsx --fix", | ||
"eslint:check": "eslint . --ext .ts,.tsx", | ||
"watch": "tsc -w" | ||
}, | ||
"dependencies": { | ||
"@jupyterlab/notebook": "^3.1.0", | ||
"@jupyterlab/settingregistry": "^3.1.0", | ||
"@types/jquery": "^3.5.0", | ||
"jquery": "^3.5.0", | ||
"ts-md5": "^1.2.9" | ||
}, | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^2.21.0", | ||
"@typescript-eslint/parser": "^2.21.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-prettier": "^6.10.0", | ||
"eslint-plugin-jsdoc": "^22.0.0", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"eslint-plugin-react": "^7.18.3", | ||
"rimraf": "^3.0.0", | ||
"typescript": "~3.7.5", | ||
"glob": "latest" | ||
}, | ||
"jupyterlab": { | ||
"extension": true, | ||
"schemaDir": "schema" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"title": "Instrumentation Settings", | ||
"description": "Should Jupyter report cell execution to NBGallery?", | ||
"type": "object", | ||
"properties":{ | ||
"enabled": { | ||
"type": "boolean", | ||
"title": "Is instrumentation enabled?", | ||
"description": "Should Jupyter send execution data to NBGallery?", | ||
"default": false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { | ||
JupyterFrontEnd, | ||
JupyterFrontEndPlugin | ||
} from '@jupyterlab/application'; | ||
|
||
import { | ||
ISettingRegistry | ||
} from '@jupyterlab/settingregistry'; | ||
|
||
import { NotebookActions, Notebook } from '@jupyterlab/notebook'; | ||
import { Cell, CodeCell } from '@jupyterlab/cells'; | ||
import {Md5} from 'ts-md5/dist/md5' | ||
import $ from 'jquery'; | ||
|
||
interface executionTracking{ | ||
startTime: number; | ||
cellIndex: number; | ||
} | ||
interface CellTracking { | ||
[cellid: string]: executionTracking; | ||
} | ||
|
||
interface executionRecord{ | ||
uuid: string; | ||
md5: string; | ||
success: boolean; | ||
runtime: number; | ||
} | ||
|
||
|
||
function transmit_execution( notebook: Notebook, cell: Cell, success: boolean, runtime: number){ | ||
let gallery_metadata :any; | ||
gallery_metadata = notebook.model.metadata.toJSON()["gallery"]; | ||
if (gallery_metadata){ | ||
let log = new Object() as executionRecord; | ||
log["success"] = success; | ||
log["md5"] = Md5.hashStr(cell.model.value.text); | ||
log["runtime"] = runtime; | ||
log["uuid"] = gallery_metadata["uuid"] || gallery_metadata["link"] || gallery_metadata["clone"]; | ||
let url = gallery_metadata["gallery_url"]; | ||
console.log(url); | ||
if(url.length>0 && log["uuid"].length>0){ | ||
$.ajax({ | ||
method: "POST", | ||
headers: {Accept: "application/json"}, | ||
url: url + "/executions", | ||
data: log, | ||
xhrFields: { withCredentials: true } | ||
}); | ||
} | ||
console.log("Made it here" + notebook + cell + success + runtime); | ||
console.log(gallery_metadata["uuid"]); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Initialization data for the hello-world extension. | ||
*/ | ||
const extension: JupyterFrontEndPlugin<void> = { | ||
id: 'instrumentation', | ||
autoStart: true, | ||
requires: [ISettingRegistry], | ||
activate: async (app: JupyterFrontEnd, | ||
settings: ISettingRegistry | ||
) => { | ||
|
||
let tracker: CellTracking = {}; | ||
let enabled = false; | ||
|
||
function get_url(){ | ||
return window.location.href.replace(/\/lab.*$/g,"/"); | ||
} | ||
|
||
function instrumentation(setting: ISettingRegistry.ISettings){ | ||
$.ajax({ | ||
method: 'GET', | ||
headers: { Accept: 'application/json' }, | ||
url: get_url() + 'jupyterlab_nbgallery/instrumentation', | ||
cache: false, | ||
xhrFields: {withCredentials: true}, | ||
success: function(environment) { | ||
if (environment['NBGALLERY_ENABLE_INSTRUMENTATION'] == 1 || (setting.get('enabled').composite as boolean)){ | ||
setting.set("enabled",true); | ||
enabled = true; | ||
}else{ | ||
enabled = false; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
NotebookActions.executionScheduled.connect((_, args) => { | ||
if(enabled){ | ||
let cell: Cell; | ||
let notebook: Notebook; | ||
notebook = args["notebook"]; | ||
cell = args ["cell"]; | ||
const started = new Date(); | ||
tracker[cell.id] = new Object() as executionTracking; | ||
tracker[cell.id].startTime = started.getTime(); | ||
tracker[cell.id].cellIndex = notebook.activeCellIndex; | ||
console.log(cell); | ||
} | ||
}); | ||
|
||
NotebookActions.executed.connect((_, args) => { | ||
const { cell, notebook, success } = args; | ||
if (enabled && cell instanceof CodeCell) { | ||
const finished = new Date(); | ||
console.log("Post execution"); | ||
transmit_execution(notebook, cell, success, (finished.getTime() - tracker[cell.id].startTime) ); | ||
} | ||
}); | ||
Promise.all([app.restored, settings.load('@jupyterlab-nbgallery/instrumentation:instrumentation')]) | ||
.then(([, setting]) => { | ||
try { | ||
instrumentation(setting); | ||
} catch(reason) { | ||
console.error(`Problem initializing instrumentation \n ${reason}`); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
|
||
export default extension; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowSyntheticDefaultImports": true, | ||
"composite": true, | ||
"declaration": true, | ||
"esModuleInterop": true, | ||
"incremental": true, | ||
"jsx": "react", | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"noEmitOnError": true, | ||
"noImplicitAny": true, | ||
"noUnusedLocals": true, | ||
"preserveWatchOutput": true, | ||
"resolveJsonModule": true, | ||
"outDir": "lib", | ||
"rootDir": "src", | ||
"strict": true, | ||
"strictNullChecks": false, | ||
"target": "es2017", | ||
"types": [] | ||
}, | ||
"include": ["src/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
version_info = (0, 2, 1) | ||
version_info = (0, 3, 0) | ||
__version__ = ".".join(map(str, version_info)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters