JavaScript side of multi-session support in Arbiter UI

Signed-off-by: Tamás Fenyvesi <tamas.fenyvesi@doknet.hu>
master
Tamás Fenyvesi 2020-05-05 18:54:42 +02:00
parent 64c9008ea7
commit 8f55214d1b
1 changed files with 143 additions and 86 deletions

View File

@ -197,110 +197,167 @@
var resultsTableContent; var resultsTableContent;
var selectedCandidateIdx = null; var selectedCandidateIdx = null;
//Multi-session mode
var multiSession = null;
//Session selection
var currSession = "";
function getSessionIdFromUrl() {
// path is like /arbiter/:sessionId/overview
var sessionIdRegexp = /\/arbiter\/([^\/]+)/g;
var match = sessionIdRegexp.exec(window.location.pathname)
return match[1];
}
function getCurrSession(callback) {
if (multiSession) {
if (currSession == "") {
// get only once
currSession = getSessionIdFromUrl();
}
//we don't show session selector in multi-session mode (one can list sessions at /arbiter)
callback();
} else {
$.ajax({
url: "/arbiter/sessions/current",
async: true,
error: function (query, status, error) {
console.log("Error getting data: " + error);
},
success: function (data) {
currSession = data;
console.log("Current session: " + currSession);
//Update available sessions in session selector
$.get("/arbiter/sessions/all", function(data){
var keys = data; // JSON.stringify(data);
if(keys.length > 1){
$("#sessionSelectDiv").show();
var elem = $("#sessionSelect");
elem.empty();
var currSelectedIdx = 0;
for (var i = 0; i < keys.length; i++) {
if(keys[i] == currSession){
currSelectedIdx = i;
}
elem.append("<option value='" + keys[i] + "'>" + keys[i] + "</option>");
}
$("#sessionSelect option[value='" + keys[currSelectedIdx] +"']").attr("selected", "selected");
$("#sessionSelectDiv").show();
}
// console.log("Got sessions: " + keys + ", current: " + currSession);
callback();
});
}
});
}
}
function getSessionSettings(callback) {
// load only once
if (multiSession != null) {
getCurrSession(callback);
} else {
$.ajax({
url: "/arbiter/multisession",
async: true,
error: function (query, status, error) {
console.log("Error getting data: " + error);
},
success: function (data) {
multiSession = data == "true";
getCurrSession(callback);
}
});
}
}
//Initial update
doUpdate();
//Set basic interval function to do updates //Set basic interval function to do updates
setInterval(doUpdate,5000); //Loop every 5 seconds setInterval(doUpdate,5000); //Loop every 5 seconds
function doUpdate(){ function doUpdate(){
//Get the update status, and do something with it: //Get the update status, and do something with it:
$.get("/arbiter/lastUpdate",function(data){ getSessionSettings(function(){
//Encoding: matches names in UpdateStatus class var sessionUpdateUrl = multiSession ? "/arbiter/" + currSession + "/lastUpdate" : "/arbiter/lastUpdate";
var jsonObj = JSON.parse(JSON.stringify(data)); $.get(sessionUpdateUrl,function(data){
var statusTime = jsonObj['statusUpdateTime']; //Encoding: matches names in UpdateStatus class
var settingsTime = jsonObj['settingsUpdateTime']; var jsonObj = JSON.parse(JSON.stringify(data));
var resultsTime = jsonObj['resultsUpdateTime']; var statusTime = jsonObj['statusUpdateTime'];
//console.log("Last update times: " + statusTime + ", " + settingsTime + ", " + resultsTime); var settingsTime = jsonObj['settingsUpdateTime'];
var resultsTime = jsonObj['resultsUpdateTime'];
//console.log("Last update times: " + statusTime + ", " + settingsTime + ", " + resultsTime);
//Update available sessions: //Check last update times for each part of document, and update as necessary
var currSession; //First section: summary status
$.get("/arbiter/sessions/current", function(data){ if(lastStatusUpdateTime != statusTime){
currSession = data; //JSON.stringify(data); var summaryStatusUrl = multiSession ? "/arbiter/" + currSession + "/summary" : "/arbiter/summary";
console.log("Current: " + currSession); $.get(summaryStatusUrl,function(data){
}); var summaryStatusDiv = $('#statusdiv');
summaryStatusDiv.html('');
$.get("/arbiter/sessions/all", function(data){ var str = JSON.stringify(data);
var keys = data; // JSON.stringify(data); var component = Component.getComponent(str);
component.render(summaryStatusDiv);
});
if(keys.length > 1){ lastStatusUpdateTime = statusTime;
$("#sessionSelectDiv").show();
var elem = $("#sessionSelect");
elem.empty();
var currSelectedIdx = 0;
for (var i = 0; i < keys.length; i++) {
if(keys[i] == currSession){
currSelectedIdx = i;
}
elem.append("<option value='" + keys[i] + "'>" + keys[i] + "</option>");
}
$("#sessionSelect option[value='" + keys[currSelectedIdx] +"']").attr("selected", "selected");
$("#sessionSelectDiv").show();
} }
// console.log("Got sessions: " + keys + ", current: " + currSession);
});
//Second section: Optimization settings
if(lastSettingsUpdateTime != settingsTime){
//Get JSON for components
var settingsUrl = multiSession ? "/arbiter/" + currSession + "/config" : "/arbiter/config";
$.get(settingsUrl,function(data){
var str = JSON.stringify(data);
//Check last update times for each part of document, and update as necessary var configDiv = $('#settingsdiv');
//First section: summary status configDiv.html('');
if(lastStatusUpdateTime != statusTime){
//Get JSON: address set by SummaryStatusResource
$.get("/arbiter/summary",function(data){
var summaryStatusDiv = $('#statusdiv');
summaryStatusDiv.html('');
var str = JSON.stringify(data); var component = Component.getComponent(str);
var component = Component.getComponent(str); component.render(configDiv);
component.render(summaryStatusDiv); });
});
lastStatusUpdateTime = statusTime; lastSettingsUpdateTime = settingsTime;
} }
//Second section: Optimization settings //Third section: Summary results table (summary info for each candidate)
if(lastSettingsUpdateTime != settingsTime){ if(lastResultsUpdateTime != resultsTime){
//Get JSON for components //Get JSON for results table
$.get("/arbiter/config",function(data){ var resultsUrl = multiSession ? "/arbiter/" + currSession + "/results" : "/arbiter/results";
var str = JSON.stringify(data); $.get(resultsUrl,function(data){
//Expect an array of CandidateInfo type objects here
resultsTableContent = data;
drawResultTable();
});
var configDiv = $('#settingsdiv'); lastResultsUpdateTime = resultsTime;
configDiv.html(''); }
var component = Component.getComponent(str); //Finally: Currently selected result
component.render(configDiv); if(selectedCandidateIdx != null){
}); //Get JSON for components
var candidateInfoUrl = multiSession
? "/arbiter/" + currSession + "/candidateInfo/" + selectedCandidateIdx
: "/arbiter/candidateInfo/" + selectedCandidateIdx;
$.get(candidateInfoUrl,function(data){
var str = JSON.stringify(data);
lastSettingsUpdateTime = settingsTime; var resultsViewDiv = $('#resultsviewdiv');
} resultsViewDiv.html('');
//Third section: Summary results table (summary info for each candidate) var component = Component.getComponent(str);
if(lastResultsUpdateTime != resultsTime){ component.render(resultsViewDiv);
});
//Get JSON; address set by SummaryResultsResource }
$.get("/arbiter/results",function(data){ })
//Expect an array of CandidateInfo type objects here
resultsTableContent = data;
drawResultTable();
});
lastResultsUpdateTime = resultsTime;
}
//Finally: Currently selected result
if(selectedCandidateIdx != null){
//Get JSON for components
$.get("/arbiter/candidateInfo/"+selectedCandidateIdx,function(data){
var str = JSON.stringify(data);
var resultsViewDiv = $('#resultsviewdiv');
resultsViewDiv.html('');
var component = Component.getComponent(str);
component.render(resultsViewDiv);
});
}
}) })
} }