chpc-uofu / ood-systemstatus

system status app for Open OnDemand
MIT License
0 stars 0 forks source link

Sort clusters #1

Open ericfranz opened 3 years ago

ericfranz commented 3 years ago

We can modify https://github.com/CHPC-UofU/ood-systemstatus/blob/583fbabffd59139907715b52f0ac5d359f38bfb7/app.rb#L17-L20

like this:

 CLUSTERS = OodCore::Clusters.new(OodCore::Clusters.load_file(ENV['OOD_CLUSTERS'] || '/etc/ood/config/clusters.d').select(&:job_allow?) 
   .select { |c| c.custom_config[:moab] || c.job_config[:adapter] == "slurm" } 
   .reject { |c| c.metadata.hidden } 
   .sort_by {|cluster| [- cluster.metadata.priority.to_i, cluster.id]}
 ) 

Then in the cluster config for Kingspeak and Notchspeak if you add priority to the metadata like

---
v2:
  metadata:
    title: "Kingspeak"
    priority: 1

This will put Kingspeak and Notchpeak on top, then sort the rest alphabetically. The cluster config filenames == the id, and since that is lowercase and starts with the same letter as the title, it works pretty well.

screen 2020-09-29 at 9 33 43 AM

ericfranz commented 3 years ago

The reason this didn't work: https://github.com/CHPC-UofU/ood-systemstatus/blob/583fbabffd59139907715b52f0ac5d359f38bfb7/app.rb#L23

  1. CLUSTERS.sort_by will return a new array, not modify the exiting CLUSTERS object
  2. CLUSTERS itself is an OodCore::Clusers object that includes the Enumerable module. But unfortunately the methods on Enumerable like sort_by return an array so in order to end up with another OodCore::Clusers object you have to do OodCore::Clusers.new(your_clusters_array) which is why this is what happens when defining CLUSTERS. So Enumerable is cool cause it makes it really easy to create custom collection objects but the drawback is that the methods do not return new custom collections but arrays.
ericfranz commented 3 years ago

Finally, above, the

.sort_by {|cluster| [- cluster.metadata.priority.to_i, cluster.id]}

is the trick for doing primary and secondary sorting in Ruby. If you only need one sort, such as priority then you do not need the array:

.sort_by {|cluster| cluster.metadata.priority.to_i }