Skip to content

Commit

Permalink
Bc templates (#3280)
Browse files Browse the repository at this point in the history
Refactor batch connect templates to store this data in
the UserStore.
  • Loading branch information
johrstrom authored Jan 12, 2024
1 parent 55a7c66 commit d11eb0b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# The controller for creating batch connect sessions.
class BatchConnect::SessionContextsController < ApplicationController
include BatchConnectConcern
include UserSettingStore

# GET /batch_connect/<app_token>/session_contexts/new
def new
Expand Down Expand Up @@ -57,6 +58,7 @@ def create
end

private

# Set the app from the token
def set_app
@app = BatchConnect::App.from_token params[:token]
Expand All @@ -82,22 +84,13 @@ def set_render_format

# Set the rendering format for displaying attributes
def set_prefill_templates
@prefill_templates ||= begin
return {} unless @app.valid?

json_path = prefill_templates_root.join("*.json")
Dir.glob(json_path).map do |path|
[File.basename(path, '.json'), File.read(path)]
end.to_h
end
@prefill_templates ||= bc_templates(@app.token)
end

def save_template
return unless params[:save_template].present? && params[:save_template] == "on" && params[:template_name].present?

safe_name = params[:template_name].gsub(/[\x00\/\\:\*\?\"<>\| ]/, '_')
path = prefill_templates_root.join(safe_name.to_s + '.json')
path.write(@session_context.to_json)
save_bc_template(@app.token, params[:template_name], @session_context.to_h)
end

# Only permit certian parameters
Expand All @@ -111,15 +104,4 @@ def cache_file
p.mkpath unless p.exist?
end.join(@app.cache_file)
end

# Root path to the prefill templates
# @return [Pathname] root directory of prefill templates
def prefill_templates_root
cluster = ::Configuration.per_cluster_dataroot? ? cluster_id : nil

base = BatchConnect::Session.dataroot(@app.token, cluster: cluster)
base.join('prefill_templates').tap do |p|
p.mkpath unless p.exist?
end
end
end
6 changes: 5 additions & 1 deletion apps/dashboard/app/models/batch_connect/session_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ def update_with_cache(cache)
self.attributes = cache.select { |k,v| self[k.to_sym] && self[k.to_sym].cacheable?(app_specific_cache_enabled?) }
end

def to_h
Hash[*map { |a| [a.id.to_sym, a.value] }.flatten]
end

def to_openstruct(addons: {})
context_attrs = Hash[*map { |a| [a.id.to_sym, a.value] }.flatten]
context_attrs = to_h
illegal_attrs = OpenStruct.new.methods & context_attrs.keys

raise ArgumentError, "#{illegal_attrs.inspect} are keywords that cannot be used as names for form items" unless illegal_attrs.empty?
Expand Down
23 changes: 23 additions & 0 deletions apps/dashboard/app/models/concerns/user_setting_store.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module UserSettingStore

BC_TEMPLATES = :batch_connect_templates

def user_settings
@user_settings = read_user_settings if @user_settings.nil?
@user_settings.clone
Expand Down Expand Up @@ -37,4 +39,25 @@ def save_user_settings
def user_settings_path
Pathname.new(::Configuration.dataroot).join(::Configuration.user_settings_file)
end

def bc_templates(app_token)
templates = user_settings[BC_TEMPLATES]
return {} if templates.nil? || templates.empty?

user_settings[BC_TEMPLATES][app_token.to_sym].to_h
end

def save_bc_template(app_token, name, key_values)
current_templates = user_settings[BC_TEMPLATES] || {}
current_app_templates = current_templates[app_token.to_sym] || {}

new_template = {}
new_template[name.to_sym] = key_values

new_settings = {}
new_settings[BC_TEMPLATES] = {}
new_settings[BC_TEMPLATES][app_token.to_sym] = current_app_templates.merge(new_template)

update_user_settings(new_settings)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<label class="control-label"><%= t('dashboard.batch_connect_form_prefill') %></label>
<select class="form-control selectpicker" id="batch_connect_session_prefill_template">
<option selected value> -- <%= t('dashboard.batch_connect_form_select_template') %> -- </option>
<% @prefill_templates.each do |id, template| %>
<option value="<%= template %>"><%= id.humanize %></option>
<% @prefill_templates.each do |name, template_data| %>
<option value="<%= template_data.to_json %>"><%= name %></option>
<% end %>
</select>
</div>
Expand Down Expand Up @@ -33,8 +33,8 @@
<div class="modal-body">
<select class="form-control selectpicker" id="modal_input_template_name">
<option selected value> -- <%= t('dashboard.batch_connect_form_save_new_template') %> -- </option>
<% @prefill_templates.each do |id, _| %>
<option value="<%= id %>"><%= id %></option>
<% @prefill_templates.each do |name, template_data| %>
<option value="<%= name %>"><%= name %></option>
<% end %>
</select>
<input type="text" class="form-control" id="modal_input_template_new_name" placeholder="<%= t('dashboard.batch_connect_form_type_new_name') %>">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
batch_connect_templates:
sys/bc_paraview:
test template:
cluster: quick
bc_num_hours: '5'
bc_account: abc123
bc_vnc_resolution: 500x600
32 changes: 32 additions & 0 deletions apps/dashboard/test/system/batch_connect_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'application_system_test_case'
require 'ood_core/job/adapters/slurm'

class BatchConnectTest < ApplicationSystemTestCase
def setup
Expand Down Expand Up @@ -1293,4 +1294,35 @@ def make_bc_app(dir, form)
assert_equal("#{Rails.root}/Gemfile", text_field.value)
end
end

test 'saves settings as a template' do
with_modified_env({ ENABLE_NATIVE_VNC: 'true' }) do
Dir.mktmpdir do |dir|
Configuration.stubs(:dataroot).returns(Pathname.new(dir))
BatchConnect::Session.any_instance.stubs(:save).returns(true)
BatchConnect::Session.any_instance.stubs(:job_id).returns('job-id-123')
OodCore::Job::Adapters::Slurm.any_instance
.stubs(:info)
.returns(OodCore::Job::Info.new(id: 'job-id-123', status: :running))

visit new_batch_connect_session_context_url('sys/bc_paraview')

fill_in(bc_ele_id('bc_num_hours'), with: 5)
fill_in(bc_ele_id('bc_account'), with: 'abc123')
fill_in('bc_vnc_resolution_x_field', with: '500')
fill_in('bc_vnc_resolution_y_field', with: '600')

check('batch_connect_session_save_template')
fill_in('modal_input_template_new_name', with: 'test template')
sleep 5 # modal needs to sleep?
click_on('Save')

click_on('Launch', wait: 30)
expected = output_fixture('user_settings/simple_bc_test.yml')
actual = File.read("#{dir}/.ood")

assert_equal(expected, actual)
end
end
end
end
4 changes: 4 additions & 0 deletions apps/dashboard/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def stub_sacctmgr
.with({}, 'sacctmgr', '-nP', 'show', 'users', 'withassoc', 'format=account,cluster,partition,qos', 'where', 'user=me', stdin_data: '')
.returns([File.read('test/fixtures/cmd_output/sacctmgr_show_accts.txt'), '', exit_success])
end

def output_fixture(file)
File.read("#{Rails.root}/test/fixtures/file_output/#{file}")
end
end
end

Expand Down

0 comments on commit d11eb0b

Please sign in to comment.