Skip to content

Commit

Permalink
Updated project and script model to use random alphanumeric ids
Browse files Browse the repository at this point in the history
  • Loading branch information
abujeda committed Sep 1, 2023
1 parent 6259e46 commit 8b3bc71
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 116 deletions.
7 changes: 2 additions & 5 deletions apps/dashboard/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ def lookup_table
end

def next_id
lookup_table.map { |id, _directory| id }
.map(&:to_i)
.concat([0]) # could be the first project
.max + 1
SecureRandom.alphanumeric(8).downcase
end

def all
Expand All @@ -35,7 +32,7 @@ def all

def find(id)
opts = lookup_table.select do |lookup_id, _directory|
lookup_id == id.to_i
lookup_id == id.to_s
end.map do |lookup_id, directory|
{ id: lookup_id, directory: directory }
end.first
Expand Down
26 changes: 18 additions & 8 deletions apps/dashboard/app/models/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Script

class ClusterNotFound < StandardError; end

attr_reader :title, :id, :project_dir, :smart_attributes
attr_reader :title, :id, :created_at, :project_dir, :smart_attributes

class << self
def scripts_dir(project_dir)
Expand All @@ -14,6 +14,10 @@ def scripts_dir(project_dir)
end
end

def creation_marker_path(script_dir)
File.join(script_dir, ".created.json")
end

def find(id, project_dir)
script_path = Script.script_path(project_dir, id)
file = script_form_file(script_path)
Expand All @@ -23,7 +27,9 @@ def find(id, project_dir)
def all(project_dir)
Dir.glob("#{scripts_dir(project_dir).to_s}/*/form.yml").map do |file|
Script.from_yaml(file, project_dir)
end.compact
end.compact.sort_by do |s|
s.created_at
end
end

def from_yaml(file, project_dir)
Expand All @@ -41,11 +47,7 @@ def from_yaml(file, project_dir)
end

def next_id(project_dir)
all(project_dir)
.map(&:id)
.map(&:to_i)
.prepend(0)
.max + 1
SecureRandom.alphanumeric(8).downcase
end
end

Expand All @@ -55,6 +57,7 @@ def initialize(opts = {})
@project_dir = opts[:project_dir] || raise(StandardError, 'You must set the project directory')
@id = opts[:id]
@title = opts[:title].to_s
@created_at = opts[:created_at]
sm_opts = {
form: opts[:form] || [],
attributes: opts[:attributes] || {}
Expand All @@ -78,7 +81,7 @@ def to_yaml
hash[sm.id.to_s] = sm.options_to_serialize
end.deep_stringify_keys

hsh = { 'title' => title }
hsh = { 'title' => title, 'created_at' => created_at }
hsh.merge!({ 'form' => smart_attributes.map { |sm| sm.id.to_s } })
hsh.merge!({ 'attributes' => attributes })
hsh.to_yaml
Expand Down Expand Up @@ -129,9 +132,11 @@ def []=(_id, value)

def save
@id = Script.next_id(project_dir) if @id.nil?
@created_at = Time.now.to_i if @created_at.nil?
script_path = Script.script_path(project_dir, id)
script_path.mkpath unless script_path.exist?
File.write(Script.script_form_file(script_path), to_yaml)
add_creation_marker(script_path)

true
rescue StandardError => e
Expand Down Expand Up @@ -251,6 +256,11 @@ def cache_file_exists?
cache_file_path.exist?
end

def add_creation_marker(script_dir)
creation_marker = Pathname.new(Script.creation_marker_path(script_dir))
File.write(creation_marker, { }.to_json) unless creation_marker.exist?
end

def cached_values
@cached_values ||= begin
cache_file_path = OodAppkit.dataroot.join(Script.scripts_dir("#{project_dir}"), "#{id}_opts.json")
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/views/projects/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="col-md-9">
<div class="row">
<% @projects.each do |project| %>
<div class="col-md-3 project-icon">
<div id="<%= project.id %>" class="col-md-3 project-icon project-card">
<div class="text-center d-flex justify-content-center">
<%= link_to(project_path(project.id), class: 'text-dark') do %>
<%= icon_tag(URI.parse(project.icon)) %>
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/views/projects/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</thead>
<tbody>
<% @scripts.each do |script| %>
<tr>
<tr class="script-card" id="<%= script.id %>">
<td
class='text-center col-md-4'
data-job-id="<%= script.most_recent_job_id %>"
Expand Down
Loading

0 comments on commit 8b3bc71

Please sign in to comment.