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

Prefill templates #3147

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# 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
set_app
set_render_format
set_session_context
#set_user_settings
set_prefill_templates

Rails.logger.debug("params: #{params}")

if @app.valid?
begin
@app.update_session_with_cache(@session_context, cache_file)
Expand Down Expand Up @@ -62,6 +66,13 @@ def set_app
@app = BatchConnect::App.from_token params[:token]
end

# Compute app name
def app_name
#BatchConnect::App.from_token(params[:token]).gsub(/(sys|dev|usr)\//, '')
app = BatchConnect::App.from_token(params[:token])
app.router.name
end

# Set list of app lists for navigation
def set_app_groups
@sys_app_groups = bc_sys_app_groups
Expand All @@ -80,29 +91,43 @@ def set_render_format
@render_format = @app.clusters.first.job_config[:adapter] unless @app.clusters.empty?
end

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

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
def template_name
params[:template_name]
end

def template_key
template_name.gsub(/[\x00\/\\:\*\?\"<>\| ]/, '_').to_sym
end

# where the data is handed back to the user settings
def save_template
return unless params[:save_template].present? && params[:save_template] == "on" && params[:template_name].present?
return unless template_saveable?

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

def template_saveable?
params[:save_template].present? && params[:save_template] == "on" && params[:template_name].present?
end

def set_user_settings
@user_settings = user_settings
end

def set_prefill_templates
@prefill_templates = prefill_templates_apps(app_name)
end

# Only permit certian parameters
def session_contexts_param
params.require(:batch_connect_session_context).permit(@session_context.attributes.keys) if params[:batch_connect_session_context].present?
#params.require(:batch_connect_session_context).permit(@session_context.attributes.keys + [:template_name]) if params[:batch_connect_session_context].present?
return unless params[:batch_connect_session_context].present?

params.require(:batch_connect_session_context).permit(@session_context.attributes.keys)
end

# Store session context into a cache file
Expand Down
48 changes: 25 additions & 23 deletions apps/dashboard/app/models/concerns/user_setting_store.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
module UserSettingStore

def user_settings
@user_settings = read_user_settings if @user_settings.nil?
@user_settings.clone
end

def update_user_settings(new_user_settings)
# Ensure @user_settings is initialized
user_settings
@user_settings.deep_merge!(new_user_settings.deep_symbolize_keys)
save_user_settings
end
def update_user_settings(app_name, template_key, json_session_context)
apps = find_app_templates(app_name)

private
if apps
apps[app_name] << { template_key => json_session_context}
else
prefill_templates_apps << { app_name => { template_key => json_session_context } }
end

def read_user_settings
user_settings = {}
return user_settings unless user_settings_path.exist?
begin
File.write(user_settings_path, Psych.dump(user_settings))
rescue => e
# Log the error or handle it accordingly
Rails.log.error("Error writing to file: #{e.message}")
end
end

def user_settings
begin
yml = YAML.safe_load(user_settings_path.read) || {}
user_settings = yml.deep_symbolize_keys
Psych.load_file(user_settings_path)
rescue => e
Rails.logger.error("Can't read or parse settings file: #{user_settings_path} because of error #{e}")
Rails.logger.error("Cannot find user settings file at #{user_settings_path}: #{e.message}")
end
end

user_settings
def prefill_templates_apps(app)
Rails.logger.debug("user settings['prefill_templates']['apps']: #{user_settings['prefill_templates']['apps']}")
user_settings['prefill_templates'].nil? ? [] : user_settings['prefill_templates']['apps']
end

def save_user_settings
# Ensure there is a directory to write the user settings file
user_settings_path.dirname.tap { |p| p.mkpath unless p.exist? }
File.open(user_settings_path.to_s, "w") { |file| file.write(@user_settings.deep_stringify_keys.to_yaml) }
private

def find_app_templates(app_name)
prefill_templates_apps.find { |app| app.key?(app_name) }
end

def user_settings_path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<%- unless @prefill_templates.empty? -%>
<%- unless @prefill_templates.nil? -%>
<div class="form-group">
<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>
<option value="<%= template %>"><%= id %></option>
<% end %>
</select>
</div>
Expand Down Expand Up @@ -33,9 +33,11 @@
<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>
<% end %>
<%- unless @prefill_templates.nil? -%>
<% @prefill_templates.each do |id, _| %>
<option value="<%= id %>"><%= id %></option>
<% end %>
<%- end -%>
</select>
<input type="text" class="form-control" id="modal_input_template_new_name" placeholder="<%= t('dashboard.batch_connect_form_type_new_name') %>">
</div>
Expand Down
Loading