Skip to content

Commit

Permalink
Update demo to use whisk actions and -whiskserver mode.
Browse files Browse the repository at this point in the history
Some problems remain.  See issue #67
  • Loading branch information
joshuaauerbachwatson committed Sep 27, 2017
1 parent e9017fd commit 6e67380
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 179 deletions.
2 changes: 1 addition & 1 deletion doc/demo/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
qcertJS.js
qcert-runtime.js
demo.js
qcertPreCompiler.js
qcertWhiskDispatch.js
*.js.map
node_modules/
2 changes: 1 addition & 1 deletion doc/demo/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ <h2>Please select an input language by placing a language block on top of the st
<script src="../assets/js/docs.min.js"></script>
<!-- Q*cert -->
<script src="./qcertJS.js"></script>
<script src="./qcertPreCompiler.js"></script>
<script src="./qcertWhiskDispatch.js"></script>
<script src="./demo.js"></script>

<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
Expand Down
39 changes: 3 additions & 36 deletions doc/demo/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ interface PuzzleSides {
theTextArea.value = resultPack.result;
}

qcertPreCompile({
qcertWhiskDispatch({
source:path[0],
target:path[path.length-1],
path:middlePath,
Expand Down Expand Up @@ -2768,12 +2768,7 @@ function completeCSVs(readFiles) {
return;
}
let delimiter = (<HTMLTextAreaElement>document.getElementById("delimiter")).value;
let schema = JSON.parse(schemaText);
let toSend = JSON.stringify({schema: schema, delimiter: delimiter, data: readFiles});
let process = function(result) {
getExecInputArea().value = result;
}
preProcess(toSend, "csv2JSON", process);
getExecInputArea().value = JSON.stringify({delimiter: delimiter, data: readFiles});
}

function handleOptimFile(files:FileList) {
Expand Down Expand Up @@ -2828,41 +2823,13 @@ function handleFile(output:string, isSchema:boolean, files:FileList) {
} else {
reader.onload = function(event) {
const contents:string = (<any>event.target).result;
if (isSchema && isSQLSchema(contents))
convertSQLSchema(contents, outputElem);
else
outputElem.value = contents;
outputElem.value = contents;
}
reader.readAsText(file);
}
}
}

// Determine if a String contains a SQL schema. Not intended to be foolproof but just to discriminate the two supported schema
// notations (SQL and JSON) when the input is at least mostly valid.
function isSQLSchema(schemaText:string) : boolean {
/* A SQL schema should have the word "create" in it but SQL is case insensitive */
var create = schemaText.search(/create/i);
if (create < 0)
return false;
var brace = schemaText.indexOf('{');
if (brace >= 0 && brace < create)
/* Word create is coincidentally appearing inside what is probably a JSON schema */
return false;
/* Looking more like SQL. Drop any blanks that follow 'create' */
var balance = schemaText.substring(create + 6).trim();
/* The next word must be 'table' (case insensitive) */
var table = balance.search(/table/i);
return table == 0;
}

function convertSQLSchema(toConvert:string, outputElem:HTMLTextAreaElement) {
var process = function(result:string) {
outputElem.value = result;
}
var result = preProcess(toConvert, "sqlSchema2JSON", process);
}

function handleFileDrop(output:string, event:DragEvent) {
event.stopPropagation();
event.preventDefault();
Expand Down
4 changes: 2 additions & 2 deletions doc/demo/evalWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// is going on too long.

// Get dependencies. We put our merged runtime functions at global scope so they are available to the query
importScripts("qcert-runtime.js", "qcertPreCompiler.js", "qcertJS.js");
importScripts("qcert-runtime.js", "qcertWhiskDispatch.js", "qcertJS.js");

// Here upon receiving the activation message from the main thread. This has two possible forms.
// 1. An array of Strings containing inputText, schemaText, and compiledQuery (requesting Javascript execution).
Expand All @@ -47,7 +47,7 @@ function qcertEval(inputConfig) {
// Each spawned worker is designed to be used once
close();
}
qcertPreCompile(inputConfig, handler);
qcertWhiskDispatch(inputConfig, handler);
}

// Javascript evaluation
Expand Down
2 changes: 1 addition & 1 deletion doc/demo/qcert.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>Q*cert online form</title>
<meta charset = "UTF-8" />
<script src="./qcertJS.js"></script>
<script src="./qcertPreCompiler.js"></script>
<script src="./qcertWhiskDispatch.js"></script>
<script src="./qcertScripts.js"></script>
</head>
<body>
Expand Down
135 changes: 0 additions & 135 deletions doc/demo/qcertPreCompiler.ts

This file was deleted.

4 changes: 2 additions & 2 deletions doc/demo/qcertScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function compileButton() {
document.getElementById("result").innerHTML = escapeHtml(compiledQuery);
displayAllResults(compilationResult.emitall);
}
qcertPreCompile(input, handler);
qcertWhiskDispatch(input, handler);
}
function verify(result, expected) {
result = result[0]; // TODO is this always right?
Expand Down Expand Up @@ -76,7 +76,7 @@ function compileForEval() {
evalQuery = compilationResult.eval;
document.getElementById("execresult").innerHTML = escapeHtml(evalQuery);
}
qcertPreCompile(input, handler);
qcertWhiskDispatch(input, handler);
}
function performJsEvaluation() {
// Processing is delegated to a web-worker
Expand Down
71 changes: 71 additions & 0 deletions doc/demo/qcertWhiskDispatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (C) 2016-2017 Joshua Auerbach
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// List URLs for the "whisk" qcert service in the order they should be attempted. It is a good idea (but not required) to put a
// localhost URL first so that new functionality can be tested without perturbing publically deployed whisk actions. Note that
// our long-running server (what you would run on localhost) is not actually a whisk runtime; it just emulates enough of its behavior
// to run our two actions.

var serverHosts = [
"http://localhost:9879",
"https://openwhisk.ng.bluemix.net/api/v1/web/JoshAuerbachThoughts_test/qcert/qcert.json"
];

function whiskDispatch(input: QcertCompilerConfig, callback: (result: QcertResult) => any) {
var next = function() {
console.log("No server found to process request, calling qcertJS.js directly");
callback(qcertCompile(input));
}
for (var i = serverHosts.length - 1; i >=0; i--)
next = makeHandler(input, serverHosts[i], callback, next);
next();
}

function makeHandler(input: QcertCompilerConfig, url: string, success: (result: QcertResult) => any, failure: () => any) {
return function() {
console.log("Handler invoked on URL " + url);
var request = new XMLHttpRequest();
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/json");
request.onloadend = function() {
if (request.status == 200) {
console.log("Success at url " + url);
var response = JSON.parse(request.responseText);
success(response);
} else {
console.log("Failure at url " + url);
failure();
}
}
try {
console.log("Posting request on url " + url);
request.send(JSON.stringify(input));
} catch (e) {
}
}
}

function qcertWhiskDispatch(input: QcertCompilerConfig, callback: (result: QcertResult) => any) {
var handler = function(result: QcertResult) {
if (result.result.substring(0, 6) == "ERROR:")
console.log("Calling back with error: " + result.result);
else
console.log("Calling back after success");
callback(result);
}
console.log("Dispatching whiskDispatch");
whiskDispatch(input, handler);
}
2 changes: 1 addition & 1 deletion doc/demo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"demo.ts",
"typings.d.ts",
"qcertJS.d.ts",
"qcertPreCompiler.ts"
"qcertWhiskDispatch.ts"
]
}

0 comments on commit 6e67380

Please sign in to comment.