Files
TRIMSP/TrimSPweb.js
T

118 lines
3.8 KiB
JavaScript

// This file contains function that are web app specific
let webRunDiagnostics = [];
function openWebRunDetails() {
let dialog = document.getElementById("runDetailsDialog");
if (dialog && !dialog.open) {
dialog.showModal();
}
}
function recordWebDiagnostic(filename, request, response) {
let timestamp = new Date().toLocaleTimeString();
let entry = `[${timestamp}] ${filename}\nHTTP ${request.status} ${request.statusText || ""}`;
if (response) {
entry += `\nPhase: ${response.phase || "unknown"}`;
entry += `\nStatus: ${response.ok ? "successful" : "failed"}`;
if (response.exitCode !== null && response.exitCode !== undefined) {
entry += `\nBinary exit code: ${response.exitCode}`;
}
if (response.message) {
entry += `\nCGI: ${response.message}`;
}
if (response.log) {
entry += `\n\n${response.log.trim()}`;
}
} else if (request.responseText) {
entry += `\n\n${request.responseText.trim()}`;
}
webRunDiagnostics.push(entry);
let log = document.getElementById("webRunLog");
if (log) {
log.textContent = webRunDiagnostics.join("\n\n----------------------------------------\n\n");
log.scrollTop = log.scrollHeight;
}
}
function writeAsciiFile(filename,content) {
// Write string content into ascii file filename
// Go via CGI script
//console.log(filename, content);
// Prepare CGI args use POST for long files
let cgiargs = new URLSearchParams();
cgiargs.set("fn", filename);
// Forward the GUI-selected RNG backend to the CGI wrapper, which passes it
// to the TRIM.SP-NL engine as the optional second command-line argument.
let rngType = document.getElementById("rngType");
if (rngType) {
cgiargs.set("rngType", rngType.value);
}
let lines = content.split(/\n/);
let prefix = filename.split(/\//);
for (let i=0; i<lines.length; i++) {
cgiargs.set("line" + i, lines[i].replace(/\s\s+/g, ' '));
}
var xhttp;
xhttp = new XMLHttpRequest();
let request = "/cgi-bin/singleTrimSP.cgi"; //POST
xhttp.open("POST", request, false); //POST
//Send the proper header information along with the request
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); //POST
xhttp.send(cgiargs.toString()); //POST
let response = null;
try {
response = JSON.parse(xhttp.responseText);
} catch (error) {
console.error("Invalid CGI response", error, xhttp.responseText);
}
recordWebDiagnostic(filename, xhttp, response);
if (xhttp.status < 200 || xhttp.status >= 300 || !response || !response.ok) {
updateStatusBar({ run: `Web backend failed for ${prefix[prefix.length - 1]}` });
return(0);
}
if (response.archiveUrl) {
let outputLink = document.getElementById("statusOutputLink");
outputLink.href = response.archiveUrl;
outputLink.textContent = "Outputs";
outputLink.title = `Download ${prefix[2]}.tgz`;
outputLink.hidden = false;
}
return(1);
}
function readAsciiFile(filename) {
// Read server-side simulation output through the CGI. Files remain in
// filesystem /tmp and do not need to be exposed directly by Apache.
if (filename == "" || filename == undefined || filename == null) {
return 0;
}
let request = "/cgi-bin/singleTrimSP.cgi?action=read&fn="
+ encodeURIComponent(filename) + "&_=" + Date.now();
let xhttp = new XMLHttpRequest();
xhttp.open("GET", request, false);
xhttp.overrideMimeType("text/plain");
xhttp.send();
if (xhttp.status < 200 || xhttp.status >= 300) {
recordWebDiagnostic(filename, xhttp, {
ok: false,
phase: "read",
message: `Could not read simulation output (HTTP ${xhttp.status}).`,
});
updateStatusBar({ run: `Could not read ${filename.split("/").pop()}` });
throw new Error(`Could not read ${filename}: HTTP ${xhttp.status} ${xhttp.statusText}`);
}
return(xhttp.responseText);
}