Skip to content

Commit

Permalink
more batch connect card refactor work (#3352)
Browse files Browse the repository at this point in the history
More batch connect card refactor work. This moves a lot of the
batch connect card rendering to partials. there are still things
being rendered programatically, but this finishes a big chunk of
moving from helper methods to partials.
  • Loading branch information
johrstrom authored Feb 26, 2024
1 parent d1715df commit 50c3514
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 129 deletions.
104 changes: 25 additions & 79 deletions apps/dashboard/app/helpers/batch_connect/sessions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,35 @@
# Note that this module programatically generates the cards for
# active batch connect sessions.
module BatchConnect::SessionsHelper
def session_panel(session)
content_tag(:div, id: "id_#{session.id}", class: "card session-panel mb-4", data: { id: session.id, hash: session.to_hash }) do
concat(
content_tag(:div, class: "card-heading") do
content_tag(:h5, class: "card-header overflow-auto alert-#{status_context(session)}") do
concat link_to(content_tag(:span, session.title, class: "card-text alert-#{status_context(session)}"), new_batch_connect_session_context_path(token: session.token))
concat tag.span(" (#{session.job_id})", class: 'card-text')
concat(
content_tag(:div, class: "float-right") do
num_nodes = session.info.allocated_nodes.size
num_cores = session.info.procs.to_i

# Generate nice status display
status = []
if session.starting? || session.running?
status << content_tag(:span, pluralize(num_nodes, "node"), class: "badge badge-#{status_context(session)} badge-pill") unless num_nodes.zero?
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
)
end
end
)
concat(
content_tag(:div, class: "card-body") do
yield
end
)
end
end

def session_view(session)
capture do
concat(
content_tag(:div) do
concat content_tag(:div, cancel_or_delete(session), class: 'float-right')
concat host(session)
concat created(session)
concat render_session_time(session)
concat id(session)
concat support_ticket(session) unless @user_configuration.support_ticket.empty?
concat display_choices(session)
safe_concat custom_info_view(session) if session.app.session_info_view
safe_concat completed_view(session) if session.app.session_completed_view && session.completed?
def render_connection(session)
if session.running?
if session.view
views = { partial: "custom", locals: { view: session.view, connect: session.connect } }
else
if session.vnc?
views = []
views << { title: "noVNC Connection", partial: "novnc", locals: { connect: session.connect, app_title: session.title } }
views << { title: "Native Instructions", partial: "native_vnc", locals: { connect: session.connect } } if ENV["ENABLE_NATIVE_VNC"]
else
views = { partial: "missing_connection" }
end
)
concat content_tag(:div) { yield }
end
elsif session.starting?
views = { partial: "starting" }
elsif session.queued?
views = { partial: "queued" }
elsif session.completed?
views = { partial: "completed", locals: { session: session } }
else
views = { partial: "bad" }
end
end

def custom_info_view(session)
render(partial: 'batch_connect/sessions/card/custom_info_view', locals: { session: session })
connection_tabs(session.id, views)
end

def completed_view(session)
render(partial: 'batch_connect/sessions/card/completed_view', locals: { session: session })
end

def created(session)
render(partial: 'batch_connect/sessions/card/created', locals: { session: session })
def render_card_partial(name, session)
render(partial: "batch_connect/sessions/card/#{name}", locals: { session: session })
end

def session_time(session)
Expand All @@ -84,26 +50,6 @@ def session_time(session)
end
end

def render_session_time(session)
render(partial: 'batch_connect/sessions/card/session_time', locals: { session: session })
end

def host(session)
render(partial: 'batch_connect/sessions/card/host', locals: { session: session })
end

def id(session)
render(partial: 'batch_connect/sessions/card/id', locals: { session: session })
end

def support_ticket(session)
render(partial: 'batch_connect/sessions/card/support_ticket', locals: { session: session })
end

def display_choices(session)
render(partial: 'batch_connect/sessions/card/display_choices', locals: { session: session })
end

def status(session)
if session.starting?
"Starting"
Expand Down Expand Up @@ -138,7 +84,7 @@ def status_context(session)
end
end

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

batch_connect_app = session.app
Expand All @@ -147,7 +93,7 @@ def relaunch(status_array, session)
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(
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],
Expand Down
33 changes: 5 additions & 28 deletions apps/dashboard/app/views/batch_connect/sessions/_panel.html.erb
Original file line number Diff line number Diff line change
@@ -1,28 +1,5 @@
<%= session_panel session do %>
<%= session_view session do %>
<%
if session.running?
if session.view
views = { partial: "custom", locals: { view: session.view, connect: session.connect } }
else
if session.vnc?
views = []
views << { title: "noVNC Connection", partial: "novnc", locals: { connect: session.connect, app_title: session.title } }
views << { title: "Native Instructions", partial: "native_vnc", locals: { connect: session.connect } } if ENV["ENABLE_NATIVE_VNC"]
else
views = { partial: "missing_connection" }
end
end
elsif session.starting?
views = { partial: "starting" }
elsif session.queued?
views = { partial: "queued" }
elsif session.completed?
views = { partial: "completed", locals: { session: session } }
else
views = { partial: "bad" }
end
%>
<%= connection_tabs(session.id, views) %>
<% end %>
<% end %>
<div id="<%= "id_#{session.id}" %>" class="card session-panel mb-4"
data-id="<%= session.id%>" data-hash="<%= session.to_hash %>" >
<%= render_card_partial('card_header', session) %>
<%= render_card_partial('card_body', session) %>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="card-body">
<div>
<div class="float-right"><%= cancel_or_delete(session) %></div>
<%= render_card_partial('host', session) %>
<%= render_card_partial('created', session) %>
<%= render_card_partial('session_time', session) %>
<%= render_card_partial('id', session) %>
<%= render_card_partial('support_ticket', session) if Configuration.support_ticket_enabled? %>
<%= render_card_partial('display_choices', session) %>
<%= render_connection(session) %>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%-
session_class = status_context(session)
badge_class = "badge-#{session_class}"
alert_class = "alert-#{session_class}"
num_nodes = session.info.allocated_nodes.size
num_cores = session.info.procs.to_i
-%>

<div class="card-heading">
<div class="h5 card-header overflow-auto <%= alert_class %>">
<a href="<%= new_batch_connect_session_context_path(token: session.token) %>">
<span class="card-text <%= alert_class %>"><%= session.title %></span>
</a>
<span class="card-text"> (<%= session.job_id %>)</span>
<div class="float-right">
<%- if session.starting? || session.running? -%>
<span class="badge <%= badge_class %> badge-pill"><%= pluralize(num_nodes, "node") %></span>
<span class="card-text"> | </span>
<span class="badge <%= badge_class %> badge-pill"><%= pluralize(num_nodes, "core") %></span>
<span class="card-text"> | </span>
<%- end -%>
<%= status(session) %>
<%- if session.completed? -%>
<span class="card-text"> | </span>
<%= relaunch(session) %>
<%- end -%>
</div>
</div>
</div>
22 changes: 2 additions & 20 deletions apps/dashboard/test/helpers/batch_connect/sessions_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,10 @@ class BatchConnect::SessionsHelperTest < ActionView::TestCase
assert_equal delete_session_title, button['title']
end

test 'relaunch should ignore sessions when session is not completed' do
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
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' do
status_array = []
relaunch(status_array, create_session(:completed))
button = relaunch(create_session(:completed))

assert_equal 1, status_array.size
html = Nokogiri::HTML(status_array[0])
html = Nokogiri::HTML(button)
form = html.at_css('form')
assert_equal batch_connect_session_contexts_path(token: 'sys/token'), form['action']
assert_equal true, form['class'].include?('relaunch')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ def setup
assert_equal true, widget_header.first.text.include?(I18n.t('dashboard.active_sessions_title')), 'Should display the widget title'
assert_equal true, widget_header.first.text.include?('(1)'), 'Should display the total number of active sessions'
end
assert_select 'div#id_1234 h5 a span', text: 'session title'
assert_select 'div#id_1234 a span', text: 'session title'
end
end
2 changes: 1 addition & 1 deletion apps/dashboard/test/system/batch_connect_sessions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def stub_scheduler(state)
card = find("#id_#{test_bc_id}")
assert_not_nil(card)

header_text = card.find('h5').text
header_text = card.find('.h5').text
assert_equal("Code Server (#{test_job_id})\nQueued", header_text)
end
end
Expand Down

0 comments on commit 50c3514

Please sign in to comment.