diff --git a/app/controllers/good_job/cleaner_controller.rb b/app/controllers/good_job/cleaner_controller.rb new file mode 100644 index 00000000..dfc48ef9 --- /dev/null +++ b/app/controllers/good_job/cleaner_controller.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module GoodJob + class CleanerController < ApplicationController + def index + @filter = JobsFilter.new(params) + + @discarded_jobs_grouped_by_exception = + GoodJob::Job.discarded + .select(<<-SQL.squish) + SPLIT_PART(error, ': ', 1) AS exception_class, + count(id) AS failed, + COUNT(id) FILTER (WHERE "finished_at" > NOW() - INTERVAL '1 HOUR') AS last_1_hour, + COUNT(id) FILTER (WHERE "finished_at" > NOW() - INTERVAL '3 HOURS') AS last_3_hours, + COUNT(id) FILTER (WHERE "finished_at" > NOW() - INTERVAL '24 HOURS') AS last_24_hours, + COUNT(id) FILTER (WHERE "finished_at" > NOW() - INTERVAL '3 DAYS') AS last_3_days, + COUNT(id) FILTER (WHERE "finished_at" > NOW() - INTERVAL '7 DAYS') AS last_7_days + SQL + .order(:exception_class) + .group(:exception_class) + + @discarded_jobs_grouped_by_class = + GoodJob::Job.discarded + .select(<<-SQL.squish) + job_class, + count(id) AS failed, + COUNT(*) FILTER (WHERE "finished_at" > NOW() - INTERVAL '1 HOUR') AS last_1_hour, + COUNT(*) FILTER (WHERE "finished_at" > NOW() - INTERVAL '3 HOURS') AS last_3_hours, + COUNT(*) FILTER (WHERE "finished_at" > NOW() - INTERVAL '24 HOURS') AS last_24_hours, + COUNT(*) FILTER (WHERE "finished_at" > NOW() - INTERVAL '3 DAYS') AS last_3_days, + COUNT(*) FILTER (WHERE "finished_at" > NOW() - INTERVAL '7 DAYS') AS last_7_days + SQL + .order(:job_class) + .group(:job_class) + end + end +end diff --git a/app/controllers/good_job/metrics_controller.rb b/app/controllers/good_job/metrics_controller.rb index 8096aa85..01d7543e 100644 --- a/app/controllers/good_job/metrics_controller.rb +++ b/app/controllers/good_job/metrics_controller.rb @@ -7,12 +7,14 @@ def primary_nav batches_count = GoodJob::BatchRecord.all.size cron_entries_count = GoodJob::CronEntry.all.size processes_count = GoodJob::Process.active.count + discarded_count = GoodJob::Job.discarded.count render json: { jobs_count: helpers.number_to_human(jobs_count), batches_count: helpers.number_to_human(batches_count), cron_entries_count: helpers.number_to_human(cron_entries_count), processes_count: helpers.number_to_human(processes_count), + discarded_count: helpers.number_to_human(discarded_count), } end diff --git a/app/filters/good_job/base_filter.rb b/app/filters/good_job/base_filter.rb index e6a35e0a..effffe86 100644 --- a/app/filters/good_job/base_filter.rb +++ b/app/filters/good_job/base_filter.rb @@ -54,6 +54,7 @@ def to_params(override = {}) query: params[:query], state: params[:state], cron_key: params[:cron_key], + finished_since: params[:finished_since], }.merge(override).delete_if { |_, v| v.blank? } end diff --git a/app/filters/good_job/jobs_filter.rb b/app/filters/good_job/jobs_filter.rb index dc366d6a..c4b5ff07 100644 --- a/app/filters/good_job/jobs_filter.rb +++ b/app/filters/good_job/jobs_filter.rb @@ -27,6 +27,7 @@ def filtered_query(filter_params = params) query = query.where(queue_name: filter_params[:queue_name]) if filter_params[:queue_name].present? query = query.search_text(filter_params[:query]) if filter_params[:query].present? query = query.where(cron_key: filter_params[:cron_key]) if filter_params[:cron_key].present? + query = query.where(finished_at: finished_since(filter_params[:finished_since])..) if filter_params[:finished_since].present? if filter_params[:state] case filter_params[:state] @@ -61,5 +62,20 @@ def query_for_records def default_base_query GoodJob::Job.all end + + def finished_since(finished_since) + case finished_since + when '1_hour_ago' + 1.hour.ago + when '3_hours_ago' + 3.hours.ago + when '24_hours_ago' + 24.hours.ago + when '3_days_ago' + 3.days.ago + when '7_days_ago' + 7.days.ago + end + end end end diff --git a/app/views/good_job/cleaner/index.html.erb b/app/views/good_job/cleaner/index.html.erb new file mode 100644 index 00000000..cfe19ec6 --- /dev/null +++ b/app/views/good_job/cleaner/index.html.erb @@ -0,0 +1,85 @@ +
+

<%= t ".title" %>

+
+ +
+

<%= t ".grouped_by_class" %>

+ + + + + + + + + + + + + + <% @discarded_jobs_grouped_by_class.each do |discard_job| %> + + + + + + + + + + <% end %> + + + + + + + + + + + + +
<%= t ".class" %><%= t ".all" %><%= t ".last_1_hour" %><%= t ".last_3_hours" %><%= t ".last_24_hours" %><%= t ".last_3_days" %><%= t ".last_7_days" %>
<%= discard_job.job_class %><%= link_to discard_job.failed, jobs_path(@filter.to_params(job_class: discard_job.job_class, state: 'discarded')) %><%= link_to discard_job.last_1_hour, jobs_path(@filter.to_params(job_class: discard_job.job_class, state: 'discarded', finished_since: '1_hour_ago')) %><%= link_to discard_job.last_3_hours, jobs_path(@filter.to_params(job_class: discard_job.job_class, state: 'discarded', finished_since: '3_hours_ago')) %><%= link_to discard_job.last_24_hours, jobs_path(@filter.to_params(job_class: discard_job.job_class, state: 'discarded', finished_since: '24_hours_ago')) %><%= link_to discard_job.last_3_days, jobs_path(@filter.to_params(job_class: discard_job.job_class, state: 'discarded', finished_since: '3_days_ago')) %><%= link_to discard_job.last_7_days, jobs_path(@filter.to_params(job_class: discard_job.job_class, state: 'discarded', finished_since: '7_days_ago')) %>
<%= t ".total" %><%= link_to @discarded_jobs_grouped_by_class.sum(&:failed), jobs_path(@filter.to_params(state: 'discarded')) %><%= link_to @discarded_jobs_grouped_by_class.sum(&:last_1_hour), jobs_path(@filter.to_params(state: 'discarded', finished_since: '1_hour_ago')) %><%= link_to @discarded_jobs_grouped_by_class.sum(&:last_3_hours), jobs_path(@filter.to_params(state: 'discarded', finished_since: '3_hours_ago')) %><%= link_to @discarded_jobs_grouped_by_class.sum(&:last_24_hours), jobs_path(@filter.to_params(state: 'discarded', finished_since: '24_hours_ago')) %><%= link_to @discarded_jobs_grouped_by_class.sum(&:last_3_days), jobs_path(@filter.to_params(state: 'discarded', finished_since: '3_days_ago')) %><%= link_to @discarded_jobs_grouped_by_class.sum(&:last_7_days), jobs_path(@filter.to_params(state: 'discarded', finished_since: '7_days_ago')) %>
+
+ +
+

<%= t ".grouped_by_exception" %>

+ + + + + + + + + + + + + + <% @discarded_jobs_grouped_by_exception.each do |discard_job| %> + + + + + + + + + + <% end %> + + + + + + + + + + + + +
<%= t ".exception" %><%= t ".all" %><%= t ".last_1_hour" %><%= t ".last_3_hours" %><%= t ".last_24_hours" %><%= t ".last_3_days" %><%= t ".last_7_days" %>
<%= discard_job.exception_class %><%= link_to discard_job.failed, jobs_path(@filter.to_params(state: 'discarded', query: discard_job.exception_class)) %><%= link_to discard_job.last_1_hour, jobs_path(@filter.to_params(state: 'discarded', query: discard_job.exception_class, finished_since: '1_hour_ago')) %><%= link_to discard_job.last_3_hours, jobs_path(@filter.to_params(state: 'discarded', query: discard_job.exception_class, finished_since: '3_hours_ago')) %><%= link_to discard_job.last_24_hours, jobs_path(@filter.to_params(state: 'discarded', query: discard_job.exception_class, finished_since: '24_hours_ago')) %><%= link_to discard_job.last_3_days, jobs_path(@filter.to_params(state: 'discarded', query: discard_job.exception_class, finished_since: '3_days_ago')) %><%= link_to discard_job.last_7_days, jobs_path(@filter.to_params(state: 'discarded', query: discard_job.exception_class, finished_since: '7_days_ago')) %>
<%= t ".total" %><%= link_to @discarded_jobs_grouped_by_exception.sum(&:failed), jobs_path(@filter.to_params(state: 'discarded')) %><%= link_to @discarded_jobs_grouped_by_exception.sum(&:last_1_hour), jobs_path(@filter.to_params(state: 'discarded', finished_since: '1_hour_ago')) %><%= link_to @discarded_jobs_grouped_by_exception.sum(&:last_3_hours), jobs_path(@filter.to_params(state: 'discarded', finished_since: '3_hours_ago')) %><%= link_to @discarded_jobs_grouped_by_exception.sum(&:last_24_hours), jobs_path(@filter.to_params(state: 'discarded', finished_since: '24_hours_ago')) %><%= link_to @discarded_jobs_grouped_by_exception.sum(&:last_3_days), jobs_path(@filter.to_params(state: 'discarded', finished_since: '3_days_ago')) %><%= link_to @discarded_jobs_grouped_by_exception.sum(&:last_7_days), jobs_path(@filter.to_params(state: 'discarded', finished_since: '7_days_ago')) %>
+
diff --git a/app/views/good_job/shared/_navbar.erb b/app/views/good_job/shared/_navbar.erb index 375abd48..29b7f743 100644 --- a/app/views/good_job/shared/_navbar.erb +++ b/app/views/good_job/shared/_navbar.erb @@ -44,6 +44,12 @@ <%= t(".performance") %> <% end %> +