From ca0a4af455559f6c48ab4d722ff84b0fa11fa2ce Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 30 Nov 2022 17:55:39 -0600 Subject: [PATCH] jobs: include job duration and queue delay on /jobs page. --- app/helpers/application_helper.rb | 13 +++++++++++++ app/models/background_job.rb | 8 ++++++++ app/policies/background_job_policy.rb | 2 +- app/views/jobs/index.html.erb | 8 ++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9fa51d45a..d5c8a1499 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -133,6 +133,19 @@ module ApplicationHelper end end + def duration_to_hhmmssms(seconds) + hh = seconds.div(1.hour).to_s + mm = seconds.div(1.minute).to_s + ss = "%.2d" % (seconds % 1.minute) + ms = ("%.3f" % (seconds % 1.second)).delete_prefix("0.") + + if seconds >= 1.hour + "#{hh}:#{mm}:#{ss}.#{ms}" + else + "#{mm}:#{ss}.#{ms}" + end + end + def humanized_number(number, million: "M", thousand: "k") if number >= 1_000_000 format("%.1f#{million}", number / 1_000_000.0) diff --git a/app/models/background_job.rb b/app/models/background_job.rb index fd267cd69..268db554e 100644 --- a/app/models/background_job.rb +++ b/app/models/background_job.rb @@ -62,5 +62,13 @@ class BackgroundJob < GoodJob::Job def pretty_name job_class.titleize.delete_suffix(" Job") end + + def job_duration + finished_at - performed_at if finished_at + end + + def queue_delay + performed_at - created_at if performed_at + end end end diff --git a/app/policies/background_job_policy.rb b/app/policies/background_job_policy.rb index aaa61440d..808b30fae 100644 --- a/app/policies/background_job_policy.rb +++ b/app/policies/background_job_policy.rb @@ -21,6 +21,6 @@ class BackgroundJobPolicy < ApplicationPolicy def api_attributes attributes = super attributes -= [:serialized_params] unless can_see_params? - attributes + attributes + [:job_duration, :queue_delay] end end diff --git a/app/views/jobs/index.html.erb b/app/views/jobs/index.html.erb index c38655b84..41d51224e 100644 --- a/app/views/jobs/index.html.erb +++ b/app/views/jobs/index.html.erb @@ -32,6 +32,14 @@ <% t.column :status %> + <% t.column "Duration" do |job| %> + <%= duration_to_hhmmssms(job.job_duration) if job.job_duration %> + <% end %> + + <% t.column "Queue Delay" do |job| %> + <%= duration_to_hhmmssms(job.queue_delay) if job.queue_delay %> + <% end %> + <% t.column "Created" do |job| %> <%= time_ago_in_words_tagged(job.created_at) %> <% end %>