FEATURE: allows filtering schedule history by schedule

FEATURE: allows filtering schedule history by schedule

Previously there was no easy way to see previous runs of schedules in the UI.

This adds a simply combo on the history page.

diff --git a/lib/mini_scheduler/views/history.erb b/lib/mini_scheduler/views/history.erb
index c1cc026..b1ac39c 100644
--- a/lib/mini_scheduler/views/history.erb
+++ b/lib/mini_scheduler/views/history.erb
@@ -2,7 +2,20 @@
   <div class="col-sm-12">
     <h3>Scheduler History</h3>
   </div>
+
+  <div class="col-sm-12">
+    <form>
+      <select name="filter">
+        <option value="All" <%= "selected" if !@filter %>>All</option>
+        <% @schedules.each do |schedule| %>
+          <option value="<%=schedule%>" <%= "selected" if @filter == schedule.to_s%>><%=schedule%></option>
+        <% end %>
+      </select>
+      <input type="submit" value="Filter">
+    </form>
+  </div>
 </header>
+<br>
 
 <div class="container">
   <div class="row">
diff --git a/lib/mini_scheduler/views/scheduler.erb b/lib/mini_scheduler/views/scheduler.erb
index 018f95d..c586954 100644
--- a/lib/mini_scheduler/views/scheduler.erb
+++ b/lib/mini_scheduler/views/scheduler.erb
@@ -39,7 +39,7 @@
             <td>
               <%= sane_duration @info.prev_duration %>
             </td>
-            <td>
+            <td style="word-wrap: break-word">
               <%= @info.current_owner %>
             </td>
             <td>
diff --git a/lib/mini_scheduler/web.rb b/lib/mini_scheduler/web.rb
index c755250..6ff85a8 100644
--- a/lib/mini_scheduler/web.rb
+++ b/lib/mini_scheduler/web.rb
@@ -4,6 +4,20 @@ module MiniScheduler
   module Web
     VIEWS = File.expand_path('views', File.dirname(__FILE__)) unless defined? VIEWS
 
+    def self.find_schedules_by_time
+      Manager.discover_schedules.sort do |a, b|
+        a_next = a.schedule_info.next_run
+        b_next = b.schedule_info.next_run
+        if a_next && b_next
+          a_next <=> b_next
+        elsif a_next
+          -1
+        else
+          1
+        end
+      end
+    end
+
     def self.registered(app)
 
       app.helpers do
@@ -24,23 +38,24 @@ module MiniScheduler
 
       app.get "/scheduler" do
         MiniScheduler.before_sidekiq_web_request&.call
-        @schedules = Manager.discover_schedules.sort do |a, b|
-          a_next = a.schedule_info.next_run
-          b_next = b.schedule_info.next_run
-          if a_next && b_next
-            a_next <=> b_next
-          elsif a_next
-            -1
-          else
-            1
-          end
-        end
+        @schedules = Web.find_schedules_by_time
         erb File.read(File.join(VIEWS, 'scheduler.erb')), locals: { view_path: VIEWS }
       end
 
       app.get "/scheduler/history" do
         MiniScheduler.before_sidekiq_web_request&.call
-        @scheduler_stats = Stat.order('started_at desc').limit(200)
+        @schedules = Manager.discover_schedules
+        @schedules.sort_by!(&:to_s)
+        @scheduler_stats = Stat.order('started_at desc')
+
+        @filter = params[:filter]
+        names = @schedules.map(&:to_s)
+        @filter = nil if !names.include?(@filter)
+        if @filter
+          @scheduler_stats = @scheduler_stats.where(name: @filter)
+        end
+
+        @scheduler_stats = @scheduler_stats.limit(200)
         erb File.read(File.join(VIEWS, 'history.erb')), locals: { view_path: VIEWS }
       end

GitHub sha: a9917d29

1 Like