# Suggestions on generating dynamic form data

**URL:** <https://discourse.openondemand.org/t/suggestions-on-generating-dynamic-form-data/1670>\
**Category:** Get Help\
**Tags:** question\
**Created:** [September 16, 2021, 7:23pm UTC](https://discourse.openondemand.org/t/suggestions-on-generating-dynamic-form-data/1670 "2021-09-16T19:23:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![blixuga](https://sea1.discourse-cdn.com/flex015/user_avatar/discourse.openondemand.org/blixuga/32/716_2.png) [@blixuga](https://discourse.openondemand.org/u/blixuga)\
**Post date:** [September 16, 2021, 7:24pm UTC](https://discourse.openondemand.org/t/suggestions-on-generating-dynamic-form-data/1670/1 "2021-09-16T19:24:00Z")

</div>

An example of this would be to generate a list of partitions(queues) that the user accessing the form has access to, and populating the drop-down options list. I have all the commands/code to generate the data, but this is causing major issues due to [https://github.com/OSC/ondemand/issues/210](https://github.com/OSC/ondemand/issues/210)

So this post is looking for suggestions on how to generate data for interactive applications `form.yml` without causing extra load on the system.

Reference topic:  
[https://discourse.openondemand.org/t/ood-parses-and-executes-all-form-yml-erb-for-all-interactive-applications-no-matter-what-action/1668/6](https://discourse.openondemand.org/t/ood-parses-and-executes-all-form-yml-erb-for-all-interactive-applications-no-matter-what-action/1668/6)

Hopeful for help,

Bruce

---

<div class="post-metadata">

**Author:** ![jeff.ohrstrom](https://sea1.discourse-cdn.com/flex015/user_avatar/discourse.openondemand.org/jeff.ohrstrom/32/136_2.png) [@jeff.ohrstrom](https://discourse.openondemand.org/u/jeff.ohrstrom)\
**Post date:** [September 16, 2021, 9:36pm UTC](https://discourse.openondemand.org/t/suggestions-on-generating-dynamic-form-data/1670/2 "2021-09-16T21:36:09Z")

</div>

Maybe you can try an initializer like how we build short cuts to the files menu for project & storage space. This way the queues are build once during the app startup. Of course, you still need a `form.yml.erb` to access them, but they’ll already be computed.

[https://osc.github.io/ood-documentation/latest/customization.html?highlight=initializer#add-shortcuts-to-files-menu](https://osc.github.io/ood-documentation/latest/customization.html?highlight=initializer#add-shortcuts-to-files-menu)

```ruby
class CustomQueues
  def self.queues
    @queues ||= begin
        # here's the logic to return an array of strings
    end
  end
end

# call it once during the initiazlier so that it'll be cached for later.
CustomQueues.queues

```

---

<div class="post-metadata">

**Author:** ![blixuga](https://sea1.discourse-cdn.com/flex015/user_avatar/discourse.openondemand.org/blixuga/32/716_2.png) [@blixuga](https://discourse.openondemand.org/u/blixuga)\
**Post date:** [September 20, 2021, 2:19pm UTC](https://discourse.openondemand.org/t/suggestions-on-generating-dynamic-form-data/1670/3 "2021-09-20T14:19:31Z")

</div>

> [@jeff.ohrstrom](#):
>
> Maybe you can try an initializer like how we build short cuts to the files menu for project & storage space. This way the queues are build once during the app startup. Of course, you still need a `form.yml.erb` to access them, but they’ll already be computed.

Jeff,

Thanks for this suggestion, yes this will cut the computation down tremendously!

Bruce

---

<div class="post-metadata">

**Author:** ![blixuga](https://sea1.discourse-cdn.com/flex015/user_avatar/discourse.openondemand.org/blixuga/32/716_2.png) [@blixuga](https://discourse.openondemand.org/u/blixuga)\
**Post date:** [September 20, 2021, 7:07pm UTC](https://discourse.openondemand.org/t/suggestions-on-generating-dynamic-form-data/1670/4 "2021-09-20T19:07:01Z")

</div>

Follow-up for anyone interested, as I was able to eliminate virtually all the processing delays I had due to the individual apps issue with this work-around:

In our ood dashboard initializer `/etc/ood/config/apps/dashboard/initializers/ood.rb` I have added code for some static classes as suggested by Jeff. In case anyone is curious on our implementation; here is the first one gets all the users visible Slurm partitions:

```auto
require 'open3' # Required for capture3 command line call

class CustomQueues ### GET PARTITIONS FOR THIS USER ###
  def self.queues
    @queues ||= begin
        # here's the logic to return an array of strings
        sinfo_cmd = "/opt/apps/slurm/prod/bin/sinfo"
        args = ["--noheader","-o=\"%R\""]
        @partitions_available = []
        o, e, s = Open3.capture3(sinfo_cmd , *args) 
        o.each_line do |v|
          #v.gsub(/=\"(\S+)\"|=\"(\S+_p)\"/, '\1')
          @partitions_available.append(v.gsub(/=\"(\S+)\"|=\"(\S+_p)\"/, '\1'))
        end
      @partitions_available
    end
  end
end

# call these once during the initiazlier so that it'll be cached for later.
CustomQueues.queues

```

This data is then consumed in our apps form.yml.erb as like so:

```auto
queue:
    label: "Partition"
    widget: select
    options:
	  <%- CustomQueues.queues.each do |g| %>
      - "<%= g %>"
    <%- end %>
    cacheable: false

```

I’ve done another to populate application versions from LMOD Spider json data. If anyone is interested in it I can post it as well.

Thanks again to @jeff.ohrstrom for the help!

---

<div class="post-metadata">

**Author:** ![mjbludwig](https://sea1.discourse-cdn.com/flex015/user_avatar/discourse.openondemand.org/mjbludwig/32/684_2.png) [@mjbludwig](https://discourse.openondemand.org/u/mjbludwig)\
**Post date:** [October 1, 2021, 5:09pm UTC](https://discourse.openondemand.org/t/suggestions-on-generating-dynamic-form-data/1670/5 "2021-10-01T17:09:04Z")

</div>

This is absolute gold, thank you very much.

Just a note, you may need to strip white space from g. I had to:

```auto
 <%- CustomQueues.queues.each do |g| %>
      - "<%= g.strip %>"
    <%- end %>

```

Otherwise my (very old) slurm complained. My guess is that this is because in ooooolllldddeeee slurms, it did not strip white space.

---

<div class="post-metadata">

**Author:** ![Micket](https://sea1.discourse-cdn.com/flex015/user_avatar/discourse.openondemand.org/micket/32/829_2.png) [@Micket](https://discourse.openondemand.org/u/Micket)\
**Post date:** [October 21, 2021, 2:39pm UTC](https://discourse.openondemand.org/t/suggestions-on-generating-dynamic-form-data/1670/6 "2021-10-21T14:39:10Z")

</div>

Really helpful thread! I managed to do the same for the slurm accounts;

```ruby
class SlurmData
  def self.accounts
    username = Etc.getlogin
    accounts = []
    IO.popen 'sacctmgr -np show accounts withassoc format=account,user' do |io|
      io.each do |line|
        acc = line.split('|')
        if acc[1] == username
          accounts.append(acc[0])
        end
      end
    end
    return accounts
  end
end

```

combined with

```auto
  bc_account:
    widget: "select"
    label: "Project"
    options: [<%= SlurmData.accounts.join(', ') %>]

```

(Note: I had to modify bc\_account to allow it to specify a custom widget type, as it was hardcoded to text\_field)

Edit: The use of the bracket list yml syntax here was to avoid errors if for some reason the list happened to be empty. An empty

```auto
   options:

```

causes errors.

---

<div class="post-metadata">

**Author:** ![blixuga](https://sea1.discourse-cdn.com/flex015/user_avatar/discourse.openondemand.org/blixuga/32/716_2.png) [@blixuga](https://discourse.openondemand.org/u/blixuga)\
**Post date:** [December 14, 2021, 5:50pm UTC](https://discourse.openondemand.org/t/suggestions-on-generating-dynamic-form-data/1670/7 "2021-12-14T17:50:20Z")

</div>

For Robert Settlage from OOD Open Office Hours:

```auto
require 'json'
require 'open3' # Required for capture3 command line call

class CustomQueues ### GET PARTITIONS FOR THIS USER ###
  def self.queues
    @queues ||= begin
        #puts("Grabbing partitions for user.")
        sinfo_cmd = "/opt/apps/slurm/prod/bin/sinfo"
        args = ["--noheader","-o=\"%R\""]
        @partitions_available = []
        o, e, s = Open3.capture3(sinfo_cmd , *args) 
        o.each_line do |v|
          #v.gsub(/=\"(\S+)\"|=\"(\S+_p)\"/, '\1')
          @partitions_available.append(v.gsub(/=\"(\S+)\"|=\"(\S+_p)\"/, '\1'))
        end
      @partitions_available
    end
  end
end

class LmodSpiderData ### GET LMOD Spider info for interactive apps ###
    def self.spider_data
      puts("Grabbing Spider Data.")
        @spider_data ||= begin
            spider_data_file = File.open "/opt/ood_data/daily-lmod.json"
            @sd = JSON.load spider_data_file
            spider_data_file.close
            @sd
        end
    end
    def self.returnAppVersions(app_name)
      puts("Grabbed Versions for: " + app_name)
      begin
        _app_hash = spider_data[app_name]
        _app_hash.map {|k,v| v["fullName"]}
      end
    end
end

# call these once during the initiazlier so that it'll be cached for later.
CustomQueues.queues
LmodSpiderData.spider_data

```

And here is the script that I run as a cronjob to generate the json data:

```auto
#!/bin/bash

# Runs LMOD Spider and generates the json file OOD needs to display modules in interactive apps.
/apps/lmod/lmod/libexec/spider -o spider-json /apps/mf/gb:/apps/mf/eb/all:/apps/easybuild/modules/all | python -mjson.tool > /opt/ood_data/daily-lmod.json

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/osc/original/2X/b/bae70bd0ed39a3ae769c2108155f4cb3e9da8385.png) [@system](https://discourse.openondemand.org/u/system)\
**Post date:** [June 12, 2022, 5:51pm UTC](https://discourse.openondemand.org/t/suggestions-on-generating-dynamic-form-data/1670/8 "2022-06-12T17:51:04Z")

</div>

Closed on June 12, 2022, 5:51pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
