Skip to content

Commit

Permalink
Merge pull request #25 from hjeeves/cadc11882
Browse files Browse the repository at this point in the history
CADC-11882: launch for reset working correctly now
  • Loading branch information
at88mph authored Nov 18, 2022
2 parents 1c5473d + 5af3b25 commit 0f94dfa
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 120 deletions.
105 changes: 45 additions & 60 deletions src/main/webapp/js/science_portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,7 @@
}
})


portalCore.subscribe(portalSessions, cadc.web.science.portal.session.events.onLoadSessionListError, function (e, request){
// This should be triggered if the user doesn't have access to Skaha resources, as well as
// other error conditions. Without access to Skaha, the user should be blocked from using the page,
// but be directed to some place they can ask for what they need (a resource allocation.)

if (request.status == 403) {
portalCore.setInfoModal('Skaha authorization issue', portalCore.getRcDisplayText(request), true, false, false)
} else {
// There some other problem contacting the session service. Report the error
portalCore.setAjaxFail(request)
}
})
portalCore.subscribe(portalSessions, cadc.web.science.portal.session.events.onLoadSessionListError, handleServiceError)

portalCore.subscribe(portalSessions, cadc.web.science.portal.session.events.onPollingContinue, function (e) {
// Rebuild session list on top of page
Expand All @@ -180,29 +168,30 @@
checkForSessions()
})


// TODO: make sure this works somehow - try triggering it in
// a situation where the context call works?
// If this method does work, change the sesion list error to use it as well
// This should only happen once during a page load
// Portal Form listeners
portalCore.subscribe(portalForm, cadc.web.science.portal.form.events.onLoadFormDataDone, initForm)
portalCore.subscribe(portalSessions, cadc.web.science.portal.form.events.onLoadContextDataError, handleServiceError)
portalCore.subscribe(portalSessions, cadc.web.science.portal.form.events.onLoadImageDataError, handleServiceError)
portalCore.subscribe(portalForm, cadc.web.science.portal.form.events.onLoadContextDataError, handleServiceError)
portalCore.subscribe(portalForm, cadc.web.science.portal.form.events.onLoadImageDataError, handleServiceError)

} // end attachListeners()


function handleServiceError(e, request) {

// Stop any outstanding ajax calls from being processed. If one has failed,
// it's assumed the skaha service is unreachable or has something else wrong,
// so the page becomes unavailable
portalForm.interruptAjaxProcessing()

// This should be triggered if the user doesn't have access to Skaha resources, as well as
// other error conditions. Without access to Skaha, the user should be blocked from using the page,
// but be directed to some place they can ask for what they need (a resource allocation.)

if (request.status == 403) {
portalCore.setInfoModal('Skaha authorization issue', portalCore.getRcDisplayText(request), true, false, false)
} else {
// There some other problem contacting the session service. Report the error
portalCore.setAjaxFail(request)
var msgHeader = 'Service Error'
var msgBody = portalCore.getRcDisplayText(request)
if (request.status === 403 || request.status === 401) {
msgHeader = 'Skaha authorization issue'
msgBody = 'Your userid is not authorized to use Skaha resources. Contact CANFAR admin for assistance.'
}
portalCore.setInfoModal(msgHeader, msgBody, true, false, false)
}

// ------------ Data display functions
Expand Down Expand Up @@ -381,6 +370,10 @@
+ 'Reload this page to try again. ', true, false, false)
}

// set dropdown defaults for context
// image default is handled when it's populated elsewhere
_resetFormDropdown("sp_cores", portalForm.getCoresDefault())
_resetFormDropdown("sp_memory", portalForm.getRAMDefault())
}

function populateSelect(selectID, optionData, placeholderText, defaultOptionID) {
Expand All @@ -399,6 +392,7 @@
var curOption = optionData[i]
var option = $('<option />')
option.val(curOption)
option.attr("val", curOption)
if (curOption === defaultOptionID ) {
option.prop('selected', true)
}
Expand All @@ -407,9 +401,8 @@
}
}


function checkForSessions() {
portalCore.setInfoModal('Session Check', 'Grabbing session list', false, false)
portalCore.setInfoModal('Session Check', 'Grabbing session list', false, true)
// This is an ajax function that will fire either onFindSessionOK or onFindSessionFail
// and listeners to those will respond accordingly
portalCore.clearAjaxAlert()
Expand Down Expand Up @@ -560,69 +553,61 @@

// This can only happen after the portalForm has grabbed all the data
function initForm() {

// notebook is default
// Start with default value
_curSessionType = portalForm.getSessionTypeDefault();

// put values into the 'type' dropdown on the launch form
// This part of the form will not change throughout the session
// These 3 dropdowns won't change content over the lifetime of a session:
// type, RAM, cores. Populate them here.
populateSelect('sp_session_type', portalForm.getSessionTypeList(), 'select type', _curSessionType)

// For now the RAM and # or cores selections do not change, other than their display
var ramList = portalForm.getRAMArray()
populateSelect('sp_memory', ramList, 'select RAM', ramList[0])
populateSelect('sp_memory', ramList, 'select RAM', portalForm.getRAMDefault())

var coresList = portalForm.getCoresArray()
populateSelect('sp_cores', coresList, "select # cores", coresList[0])
populateSelect('sp_cores', coresList, "select # cores", portalForm.getCoresDefault())

// Set the parts of the launch form that may change per type selection
setLaunchFormForType(_curSessionType, true)

// Enable the Launch button
// Enable the Launch button as the form is now ready
$('.sp-add-session').removeAttr('disabled')
$('.sp-add-session').removeClass('sp-button-disable')
}

function setLaunchFormForType(sessionType, isReset) {
// TODO: these functions will still be called, but will
// need to get the appropriate values from the PortalForm object.
//loadContainerImages(sessionType)
// the getImageListForType() function will return an array
// of IDs only.
//loadContainerImages(portalForm.getImageListForType(sessionType))

function setLaunchFormForType(sessionType) {
var tempImageList = portalForm.getImageListForType(sessionType)
populateSelect('sp_software_stack', tempImageList, 'select stack', tempImageList[0])

// Display or hide form fields as needed by the session type (ie RAM or cores...)
setFormFields(sessionType)

// Don't set the next default session name if the user
// appears to have changed the name, unless it has been cleared
var curSessionName = $('#sp_session_name').val().trim()
if (isReset === true || curSessionName === "" ||
(curSessionName === portalSessions.getDefaultSessionName(_curSessionType))) {
setDefaultSessionName(sessionType.trim())
}
setDefaultSessionName(sessionType.trim())

_curSessionType = sessionType
}

function _resetFormDropdown(selector, defaultVar) {

// Clear existing selections
$('#' + selector + ' option').each(function() {
if ($(this).attr("val") === defaultVar) {
$(this).prop('selected',true)
// break out of the loop
$(this).change()
return false
}
});
}

function handleResetFormState(event) {
event.preventDefault()
// Clear messages
portalCore.clearAjaxAlert()
portalCore.setProgressBar('okay')
var sessionTypeDefault = portalForm.getSessionTypeDefault()

// set selected back to session type default
$("#sp_session_type option").each(function() {
if ($(this).val() === sessionTypeDefault) {
$(this).attr('selected','selected')
} else {
$(this).removeAttr('selected')
}
});

_resetFormDropdown("sp_session_type", sessionTypeDefault)
// reload the form for default session type
setLaunchFormForType(sessionTypeDefault, true)
}
Expand Down
113 changes: 53 additions & 60 deletions src/main/webapp/js/science_portal_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,6 @@
function PortalForm() {
var _selfPortalForm = this


//images: [
// { type: <type>,
// imageListRaw: {
//
// },
// imageNameArray: [name, name,...]
// }
//},...
//]
//context: {
// <as it comes from skaha>
//}


this._imageData = new Array()
this._contextData = {}
this._sessionTypeList = null
Expand Down Expand Up @@ -106,7 +91,7 @@
$.getJSON(contentFileURL, function (jsonData) {
_selfPortalForm._sessionTypeMap = jsonData
})
// TODO: is default even used?
// Used to reset launch form
_selfPortalForm._sessionTypeMap.default = 'notebook'
}

Expand Down Expand Up @@ -141,46 +126,29 @@
// calls is such that curTypeURL is the same for all lists so
// there's no effective lookup.
_getImageDataForType(curTypeURL, curTypename)

//Promise.resolve(_getAjaxData(curTypeURL))
// .then(function (imageList) {
//
// _selfPortalForm._ajaxCallCount--
// _selfPortalForm._imageData.push({
// "name": curTypename,
// "imageList": imageList
// })
//
// if (_selfPortalForm._ajaxCallCount === 0) {
// trigger(_selfPortalForm, cadc.web.science.portal.form.events.onLoadFormDataDone)
// }
//
// })
// .catch(function (message) {
// trigger(_selfPortalForm, cadc.web.science.portal.form.events.onLoadImageDataError, message)
// })
}

}

function _getImageDataForType(callURL, sessionType ){
Promise.resolve(_getAjaxData(callURL))
.then(function (imageList) {

var imageIDList = new Array()
for (var i = 0; i < imageList.length; i++) {
imageIDList.push(imageList[i].id)
}
if (_selfPortalForm._ajaxCallCount !== -9) {
var imageIDList = new Array()
for (var i = 0; i < imageList.length; i++) {
imageIDList.push(imageList[i].id)
}

_selfPortalForm._ajaxCallCount--
_selfPortalForm._imageData.push({
"sessionType": sessionType,
"imageList": imageList,
"imageIDList": imageIDList
})
_selfPortalForm._ajaxCallCount--
_selfPortalForm._imageData.push({
"sessionType": sessionType,
"imageList": imageList,
"imageIDList": imageIDList
})

if (_selfPortalForm._ajaxCallCount === 0) {
trigger(_selfPortalForm, cadc.web.science.portal.form.events.onLoadFormDataDone)
if (_selfPortalForm._ajaxCallCount === 0) {
trigger(_selfPortalForm, cadc.web.science.portal.form.events.onLoadFormDataDone)
}
}

})
Expand All @@ -195,8 +163,6 @@
//}


// TODO: is it better to use a map for the type to imageIDList? Would be faster
// to get the info
function getImageListForType(sessionType) {
// return what it's asking for, in an array of IDs.
var imageList = null
Expand All @@ -215,27 +181,51 @@
return _selfPortalForm._contextData.availableRAM
}

function getRAMDefault() {
// Convert value to string. (The + "" will do just that.
// They will come out of curContext as numbers that can't be compared sanely
return _selfPortalForm._contextData.defaultRAM + ""
}

function getCoresArray() {
return _selfPortalForm._contextData.availableCores
}

function getCoresDefault() {
// Convert value to string. (The + "" will do just that.
// They will come out of curContext as numbers that can't be compared sanely
return _selfPortalForm._contextData.defaultCores + ""
}


function interruptAjaxProcessing() {
// Yes, basically a 'kill -9'
_selfPortalForm._ajaxCallCount = -9
}

/**
* Get context information from skaha
*/
function getContextData() {
Promise.resolve(_getAjaxData(_selfPortalForm.sessionURLs.context))
.then(function(curContext) {
// Not everything sent from the server is used by the front end yet.
// In future, the data can be stored as
// _selfPortalForm._contextData.rawData, but it'd be a waste of storage
// to do it currently.
// Save items that are used by the front end
_selfPortalForm._contextData.availableCores = curContext.availableCores
_selfPortalForm._contextData.availableRAM = curContext.availableRAM

_selfPortalForm._ajaxCallCount--
if (_selfPortalForm._ajaxCallCount === 0) {
trigger(_selfPortalForm, cadc.web.science.portal.form.events.onLoadFormDataDone)
// Check to see if functions have been interrupted
if (_selfPortalForm._ajaxCallCount !== -9) {

// Not everything sent from the server is used by the front end yet.
// In future, the data can be stored as
// _selfPortalForm._contextData.rawData, but it'd be a waste of storage
// to do it currently.
// Save items that are used by the front end
_selfPortalForm._contextData.availableCores = curContext.availableCores
_selfPortalForm._contextData.availableRAM = curContext.availableRAM
_selfPortalForm._contextData.defaultCores = curContext.defaultCores
_selfPortalForm._contextData.defaultRAM = curContext.defaultRAM

_selfPortalForm._ajaxCallCount--
if (_selfPortalForm._ajaxCallCount === 0) {
trigger(_selfPortalForm, cadc.web.science.portal.form.events.onLoadFormDataDone)
}
}
})
.catch(function(message) {
Expand Down Expand Up @@ -301,13 +291,16 @@
$.extend(this, {
getContextData: getContextData,
getRAMArray: getRAMArray,
getRAMDefault: getRAMDefault,
getCoresArray: getCoresArray,
getCoresDefault: getCoresDefault,
getImageData: getImageData,
getImageListForType: getImageListForType,
getSessionTypeDefault: getSessionTypeDefault,
getSessionTypeList: getSessionTypeList,
getMapEntry: getMapEntry,
getFormData: getFormData,
interruptAjaxProcessing: interruptAjaxProcessing,
loadSessionTypeMap: loadSessionTypeMap,
setServiceURLs: setServiceURLs,
})
Expand Down

0 comments on commit 0f94dfa

Please sign in to comment.