From b80009a1cad9ab31001c18ad11166c8b5b854648 Mon Sep 17 00:00:00 2001 From: Stefan Wienert Date: Sun, 22 Nov 2020 20:11:23 +0100 Subject: [PATCH] UI: Admin UI with filters and space efficient layout --- .../good_job/dashboards_controller.rb | 53 ++++++++++++++++++- .../views/good_job/dashboards/index.html.erb | 29 ++++++++-- .../app/views/layouts/good_job/base.html.erb | 4 +- engine/app/views/shared/_jobs_table.erb | 2 +- 4 files changed, 80 insertions(+), 8 deletions(-) diff --git a/engine/app/controllers/good_job/dashboards_controller.rb b/engine/app/controllers/good_job/dashboards_controller.rb index 561f440b7..8e707065a 100644 --- a/engine/app/controllers/good_job/dashboards_controller.rb +++ b/engine/app/controllers/good_job/dashboards_controller.rb @@ -1,8 +1,57 @@ module GoodJob class DashboardsController < GoodJob::BaseController - def index - @jobs = GoodJob::Job.display_all(after_scheduled_at: params[:after_scheduled_at], after_id: params[:after_id]) + class JobFilter + attr_accessor :params + + def initialize(params) + @params = params + end + + def last + @_last ||= jobs.last + end + + def jobs + sql = GoodJob::Job.display_all(after_scheduled_at: params[:after_scheduled_at], after_id: params[:after_id]) .limit(params.fetch(:limit, 10)) + if params[:job_class] # rubocop:disable Style/IfUnlessModifier + sql = sql.where("serialized_params->>'job_class' = ?", params[:job_class]) + end + if params[:state] + case params[:state] + when 'finished' + sql = sql.finished + when 'unfinished' + sql = sql.unfinished + when 'errors' + sql = sql.where.not(error: nil) + end + end + sql + end + + def states + { + 'finished' => GoodJob::Job.finished.count, + 'unfinished' => GoodJob::Job.unfinished.count, + 'errors' => GoodJob::Job.where.not(error: nil).count, + } + end + + def job_classes + GoodJob::Job.group("serialized_params->>'job_class'").count + end + + def to_query(override) + { + state: params[:state], + job_class: params[:job_class], + }.merge(override).delete_if { |_, v| v.nil? }.to_query + end + end + + def index + @filter = JobFilter.new(params) job_data = GoodJob::Job.connection.exec_query Arel.sql(<<~SQL.squish) SELECT * diff --git a/engine/app/views/good_job/dashboards/index.html.erb b/engine/app/views/good_job/dashboards/index.html.erb index 666b34854..225822392 100644 --- a/engine/app/views/good_job/dashboards/index.html.erb +++ b/engine/app/views/good_job/dashboards/index.html.erb @@ -2,13 +2,36 @@ <%= render 'shared/chart', chart_data: @chart %> -<% if @jobs.present? %> - <%= render 'shared/jobs_table', jobs: @jobs %> +
+
+
+ Filter by job class +
+ <% @filter.job_classes.each do |name, count| %> + + <%= name %> (<%= count %>) + + <% end %> +
+
+ Filter by state +
+ <% @filter.states.each do |name, count| %> + + <%= name %> (<%= count %>) + + <% end %> +
+
+
+ +<% if @filter.jobs.present? %> + <%= render 'shared/jobs_table', jobs: @filter.jobs %>