Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Launcher save fixed attributes #3784

Merged
merged 19 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions apps/dashboard/app/javascript/launcher_edit.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you submit the changes to this file in another pull request? I can't just glance at this to see what it is, so I'll need more time to look it over.

Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,37 @@ function addInProgressField(event) {
}

function updateAutoEnvironmentVariable(event) {
var aev_name = event.target.value;
const aev_name = event.target.value;
const labelString = event.target.dataset.labelString;
var input_field = event.target.parentElement.children[2].children[1];
const idString = `launcher_auto_environment_variable_${aev_name}`;
const nameString = `launcher[auto_environment_variable_${aev_name}]`;
const input_field = event.target.parentElement.children[2].children[1];
const oldId = input_field.id;

input_field.removeAttribute('readonly');
input_field.id = `launcher_auto_environment_variable_${aev_name}`;
input_field.name = `launcher[auto_environment_variable_${aev_name}]`;
input_field.id = idString;
input_field.name = nameString;

if (labelString.match(/Environment( |\s)Variable/)) {
var label_field = event.target.parentElement.children[2].children[0];
const label_field = event.target.parentElement.children[2].children[0];
label_field.innerHTML = `Environment Variable: ${aev_name}`;
}

// Update the checkbox so that environment variables can be fixed when created
const fixedBoxGroup = event.target.parentElement.querySelector('.list-group-item');
const checkbox = fixedBoxGroup.querySelector("input[type='checkbox']");
checkbox.id = `${idString}_fixed`;
checkbox.name = `launcher[auto_environment_variable_${aev_name}_fixed]`;
checkbox.setAttribute('data-fixed-toggler', idString);

// Update hidden field if attribute is already fixed, otherwise just update label
if(fixedBoxGroup.children.length == 3) {
const hiddenField = fixedBoxGroup.querySelector("input[type='hidden']");
hiddenField.name = nameString;
}

const fixedLabel = fixedBoxGroup.querySelector('label');
fixedLabel.setAttribute('for', `${idString}_fixed`);
}

function fixExcludeBasedOnSelect(selectElement) {
Expand Down
19 changes: 17 additions & 2 deletions apps/dashboard/app/lib/smart_attributes/attributes/auto_scripts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def self.build_auto_scripts(opts = {})
options = script_options_from_directory(dir)

static_opts = {
options: options
}.merge(opts.without(:options).to_h)
options: options,
value: default_script_value(opts, options)
}.merge(opts.without(:options, :value).to_h)

Attributes::AutoScripts.new('auto_scripts', static_opts)
end
Expand All @@ -26,6 +27,20 @@ def self.script_options_from_directory(dir)
[File.basename(file), file]
end.sort_by(&:first)
end

def self.default_script_value(initial_opts, script_opts)
return nil if !initial_opts[:value] || script_opts.empty?

# Replace directory if the script is present in correct directory, otherwise delete value
valid_opt = script_opts.select { |opt| opt.first == File.basename(initial_opts[:value]) }.first
if valid_opt
initial_opts[:value] = valid_opt.last
elsif script_opts&.none? { |opt| opt.include?(initial_opts[:value]) }
initial_opts.delete(:value)
end

(initial_opts[:value] || script_opts.first.last).to_s
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling to understand this. I think because it does 2 things - potentially modifies initial_opts and has a return value.

Can we restructure this so it just has the return value with no modification to the things passed into it?

When I was investigating the tests, I came up with this - I also think a simple name change (and type change of the first argument) helped me figure out what it's attempting to do.

# called through  default_value(opts[:default], options)

    def self.default_value(default, scripts)
      if scripts.empty?
        nil
      elsif default.nil? || default.to_s.empty?
        scripts.first[1]
      elsif scripts.map { |s| s[1] }.include?(default)
        default
      else
        scripts.first[1]
      end
    end

end

module Attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ def self.build_bc_num_slots(opts = {})

module Attributes
class BcNumSlots < Attribute
# Hash of options used to define this attribute
# @return [Hash] attribute options
def opts
@opts.reverse_merge(min: 1, step: 1)
def initialize(id, opts)
super(id, opts)
@opts = @opts.reverse_merge(min: 1, step: 1)
end

# Value of attribute
Expand Down
9 changes: 6 additions & 3 deletions apps/dashboard/app/models/launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,12 @@ def add_default_fields(form: [], **_args)
def add_script_to_form(form: [], attributes: {})
form << 'auto_scripts' unless form.include?('auto_scripts')

attributes[:auto_scripts] = {
directory: project_dir
}
dir = { directory: project_dir }
attributes[:auto_scripts] = if attributes[:auto_scripts]
attributes[:auto_scripts].merge(dir)
else
dir
end
end

def add_cluster_to_form(form: [], attributes: {})
Expand Down
10 changes: 9 additions & 1 deletion apps/dashboard/test/lib/smart_attributes/auto_scripts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def fixture_dir
end

test 'can correctly set the value when scripts directory is configured' do
value = '/test/script/executable.sh'
value = "#{fixture_dir}/script4.slurm"
attribute = SmartAttributes::AttributeFactory.build('auto_scripts', { directory: fixture_dir, value: value })

assert_instance_of SmartAttributes::Attributes::AutoScripts, attribute
Expand Down Expand Up @@ -57,4 +57,12 @@ def fixture_dir
assert_instance_of SmartAttributes::Attributes::AutoScripts, attribute
assert_equal('select', File.basename(attribute.widget))
end

test 'correctly sets value when previous value is invalid' do
value = '/test/invalid/script.sh'
attribute = SmartAttributes::AttributeFactory.build('auto_scripts', { directory: fixture_dir, value: value })

assert_instance_of SmartAttributes::Attributes::AutoScripts, attribute
assert_equal(attribute.select_choices[0].last, attribute.value)
end
end
5 changes: 3 additions & 2 deletions apps/dashboard/test/system/project_manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,8 @@ def add_auto_environment_variable(project_id, script_id, save: true)
- "#{dir}/projects/#{project_id}/my_cool_script.sh"
- - my_cooler_script.bash
- "#{dir}/projects/#{project_id}/my_cooler_script.bash"
directory: "#{dir}/projects/#{project_id}"
value: "#{dir}/projects/#{project_id}/my_cool_script.sh"
directory: "#{dir}/projects/#{project_id}"
label: Script
help: ''
required: false
Expand Down Expand Up @@ -688,8 +688,8 @@ def add_auto_environment_variable(project_id, script_id, save: true)
- "#{dir}/projects/#{project_id}/my_cool_script.sh"
- - my_cooler_script.bash
- "#{dir}/projects/#{project_id}/my_cooler_script.bash"
directory: "#{dir}/projects/#{project_id}"
value: "#{dir}/projects/#{project_id}/my_cool_script.sh"
directory: "#{dir}/projects/#{project_id}"
label: Script
help: ''
required: false
Expand Down Expand Up @@ -971,6 +971,7 @@ def add_auto_environment_variable(project_id, script_id, save: true)
.stubs(:info).returns(OodCore::Job::Info.new(id: 'job-id-123', status: :running))

find("#launch_8woi7ghd").click

assert_selector('.alert-success', text: 'job-id-123')

# sleep here because this test can error with Errno::ENOTEMPTY: Directory not empty @ dir_s_rmdir
Expand Down
Loading