Skip to content

Commit

Permalink
Added session relaunch feature
Browse files Browse the repository at this point in the history
  • Loading branch information
abujeda committed Jan 31, 2023
1 parent 9230065 commit ab6b4ac
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 5 deletions.
4 changes: 2 additions & 2 deletions apps/dashboard/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def help_custom_url
ENV['OOD_DASHBOARD_HELP_CUSTOM_URL']
end

def fa_icon(icon, fa_style: 'fas', id: '', classes: 'app-icon')
def fa_icon(icon, fa_style: 'fas', id: '', classes: 'app-icon', title: "FontAwesome icon specified: #{icon}")
content_tag(:i, '', id: id, class: [fa_style, "fa-#{icon}", 'fa-fw'].concat(Array(classes)),
title: "FontAwesome icon specified: #{icon}", "aria-hidden": true)
title: title, "aria-hidden": true)
end

def app_icon_tag(app)
Expand Down
26 changes: 24 additions & 2 deletions apps/dashboard/app/helpers/batch_connect/sessions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def session_panel(session)
status << content_tag(:span, pluralize(num_cores, "core"), class: "badge badge-#{status_context(session)} badge-pill") unless num_cores.zero?
end
status << "#{status session}"
relaunch(status, session)
tag.span(status.join(" | ").html_safe, class: "card-text")
end
)
Expand Down Expand Up @@ -195,6 +196,27 @@ def status_context(session)
end
end

def relaunch(status_array, session)
return unless Configuration.relaunch_session_enabled && session.completed?

batch_connect_app = session.app
return unless batch_connect_app.valid?

user_context = session.user_context
params = batch_connect_app.attributes.map{|attribute| ["batch_connect_session_context[#{attribute.id}]", user_context.fetch(attribute.id, '')]}.to_h
title = "#{t('dashboard.batch_connect_sessions_relaunch_title')} #{session.title} #{t('dashboard.batch_connect_sessions_word')}"
status_array << button_to(
batch_connect_session_contexts_path(token: batch_connect_app.token),
method: :post,
class: %w[btn px-1 py-0 btn-outline-dark relaunch],
form_class: %w[d-inline relaunch],
title: title,
data: { toggle: "tooltip", placement: "left" },
params: params
) do
"#{fa_icon('sync', classes: nil, title: '')}".html_safe
end
end

def cancel_or_delete(session)
if Configuration.cancel_session_enabled && !session.completed?
Expand All @@ -210,7 +232,7 @@ def delete(session)
method: :delete,
class: "btn btn-danger float-right btn-delete",
title: "#{t('dashboard.batch_connect_sessions_delete_title')} #{session.title} #{t('dashboard.batch_connect_sessions_word')}",
data: { confirm: t('dashboard.batch_connect_sessions_delete_confirm'), toggle: "tooltip", placement: "bottom", selector: true }
data: { confirm: t('dashboard.batch_connect_sessions_delete_confirm'), toggle: "tooltip", placement: "bottom", selector: 'button' }
) do
"#{fa_icon('times-circle', classes: nil)} <span aria-hidden='true'>#{t('dashboard.batch_connect_sessions_delete_title')}</span>".html_safe
end
Expand All @@ -222,7 +244,7 @@ def cancel(session)
method: :post,
class: "btn btn-danger float-right btn-cancel",
title: "#{t('dashboard.batch_connect_sessions_cancel_title')} #{session.title} #{t('dashboard.batch_connect_sessions_word')}",
data: { confirm: t('dashboard.batch_connect_sessions_cancel_confirm'), toggle: "tooltip", placement: "bottom", selector: true }
data: { confirm: t('dashboard.batch_connect_sessions_cancel_confirm'), toggle: "tooltip", placement: "bottom", selector: 'button' }
) do
"#{fa_icon('times-circle', classes: nil)} <span aria-hidden='true'>#{t('dashboard.batch_connect_sessions_cancel_title')}</span>".html_safe
end
Expand Down
11 changes: 11 additions & 0 deletions apps/dashboard/app/javascript/packs/batch_connect_sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ function installSettingHandlers(name) {

window.installSettingHandlers = installSettingHandlers;
window.tryUpdateSetting = tryUpdateSetting;

jQuery(function (){
function showSpinner() {
$('body').addClass('modal-open');
$('#full-page-spinner').removeClass('d-none');
}

$('button.relaunch').each((index, element) => {
$(element).on('click', showSpinner);
});
});
2 changes: 2 additions & 0 deletions apps/dashboard/app/models/user_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def nav_categories

# The current user profile. Used to select the configuration properties.
def profile
return CurrentUser.user_settings[:profile_override].to_sym if CurrentUser.user_settings[:profile_override]

if Configuration.host_based_profiles
request_hostname
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ locals: {
</div>
</div>
<%- end -%>
</div>
<div id="full-page-spinner" class="d-none">
<div class="spinner-border" role="status"></div>
</div>
1 change: 1 addition & 0 deletions apps/dashboard/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<%= yield :head %>

<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="profile" content="<%= @user_configuration.profile %>">

<!-- configuration options exposed to javascript -->
<meta id="ood_config"
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/config/configuration_singleton.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def boolean_configs
:disable_bc_shell => false,
:cancel_session_enabled => false,
:hide_app_version => false,
:relaunch_session_enabled => false,
}.freeze
end

Expand Down
2 changes: 2 additions & 0 deletions apps/dashboard/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ en:
batch_connect_sessions_delete_title: "Delete"
batch_connect_sessions_cancel_confirm: "Are you sure?"
batch_connect_sessions_cancel_title: "Cancel"
batch_connect_sessions_relaunch_confirm: "Are you sure?"
batch_connect_sessions_relaunch_title: "Relaunch"
batch_connect_sessions_errors_staging: "Failed to stage the template with the following error:"
batch_connect_sessions_errors_submission: "Failed to submit session with the following error:"
batch_connect_sessions_novnc_launch: "Launch %{app_title}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ disable_bc_shell: true
cancel_session_enabled: true
bc_clean_old_dirs: true
hide_app_version: true
relaunch_session_enabled: true

Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,57 @@ class BatchConnect::SessionsHelperTest < ActionView::TestCase
assert_equal delete_session_title, button['title']
end

def create_session(state = :running)
test 'relaunch should ignore sessions when relaunch_session_enabled is false' do
Configuration.stubs(:relaunch_session_enabled).returns(false)
status_array = []
relaunch(status_array, create_session(:completed))
assert_equal [], status_array
end

test 'relaunch should ignore sessions when session is not completed' do
Configuration.stubs(:relaunch_session_enabled).returns(true)
OodCore::Job::Status.states.each do |state|
next if state == :completed

status_array = []
relaunch(status_array, create_session(state))
assert_equal [], status_array
end
end

test 'relaunch should ignore sessions when session application is not valid' do
Configuration.stubs(:relaunch_session_enabled).returns(true)
status_array = []
relaunch(status_array, create_session(:completed, valid: false))
assert_equal [], status_array
end

test 'relaunch should add relaunch form when session is completed and relaunch_session_enabled is true' do
Configuration.stubs(:relaunch_session_enabled).returns(true)
status_array = []
relaunch(status_array, create_session(:completed))

assert_equal 1, status_array.size
html = Nokogiri::HTML(status_array[0])
form = html.at_css('form')
assert_equal batch_connect_session_contexts_path(token: 'sys/token'), form['action']
assert_equal true, form['class'].include?('relaunch')

button = html.at_css('button')
assert_equal 'Relaunch AppName Session', button['title']
assert_equal true, button['class'].include?('relaunch')
end

def create_session(state = :running, valid: true)
value = '{"id":"1234","job_id":"1","created_at":1669139262,"token":"sys/token","title":"AppName","cache_completed":false}'
BatchConnect::Session.new.from_json(value).tap do |session|
session.stubs(:status).returns(OodCore::Job::Status.new(state: state))
OpenStruct.new.tap do |sys_app|
sys_app.send('valid?=', valid)
sys_app.attributes = []
sys_app.token = 'sys/token'
session.stubs(:app).returns(sys_app)
end
end
end

Expand Down
16 changes: 16 additions & 0 deletions apps/dashboard/test/integration/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,20 @@ def setup
assert_equal batch_connect_cancel_session_path('1234'), form.first['action']
end
end

test 'should render session panel with relaunch button when relaunch_session_enabled is true' do
Configuration.stubs(:relaunch_session_enabled).returns(true)
value = '{"id":"1234","job_id":"1","created_at":1669139262,"token":"sys/token","title":"session title","cache_completed":true}'
session = BatchConnect::Session.new.from_json(value)
session.stubs(:status).returns(OodCore::Job::Status.new(state: :completed))
session.stubs(:app).returns(stub(valid?: true, token: 'sys/token', attributes: [], session_info_view: nil))
BatchConnect::Session.stubs(:all).returns([session])

get batch_connect_sessions_path
assert_response :success

assert_select 'div#id_1234 div.card-heading div.float-right form.relaunch' do |form|
assert_equal batch_connect_session_contexts_path(token: 'sys/token'), form.first['action']
end
end
end

0 comments on commit ab6b4ac

Please sign in to comment.