Skip to content

Commit 65798c2

Browse files
author
Felipe
committed
feat/Dashboard Chart with auto update
1 parent bb63fab commit 65798c2

File tree

14 files changed

+376
-2
lines changed

14 files changed

+376
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class MissionControl::Jobs::DashboardController < MissionControl::Jobs::ApplicationController
2+
def index
3+
end
4+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class MissionControl::Jobs::InternalApi::DashboardController < MissionControl::Jobs.base_controller_class.constantize
2+
include ActionView::Helpers::NumberHelper
3+
4+
def index
5+
render json: {
6+
uptime: {
7+
label: Time.now.strftime("%H:%M:%S"),
8+
pending: queue_job.pendings.where.not(id: failed_execution.select(:job_id)).size,
9+
failed: failed_execution.where("created_at >= ?", time_to_consult.seconds.ago).size,
10+
finished: queue_job.finisheds.where("finished_at >= ?", time_to_consult.seconds.ago).size,
11+
},
12+
total: {
13+
failed: number_with_delimiter(ActiveJob.jobs.failed.count),
14+
pending: number_with_delimiter(ActiveJob.jobs.pending.count),
15+
scheduled: number_with_delimiter(ActiveJob.jobs.scheduled.count),
16+
in_progress: number_with_delimiter(ActiveJob.jobs.in_progress.count),
17+
finished: number_with_delimiter(ActiveJob.jobs.finished.count)
18+
}
19+
},
20+
status: :ok
21+
end
22+
23+
private
24+
def time_to_consult
25+
params[:uptime].to_i || 5
26+
end
27+
28+
def failed_execution
29+
MissionControl::SolidQueueFailedExecution
30+
end
31+
32+
def queue_job
33+
MissionControl::SolidQueueJob
34+
end
35+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class MissionControl::Jobs::InternalApi::NavigationController < MissionControl::Jobs::ApplicationController
2+
def index
3+
render partial: "layouts/mission_control/jobs/navigation_update", locals: {
4+
section: params[:section].to_sym
5+
}
6+
end
7+
end

app/helpers/mission_control/jobs/navigation_helper.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module MissionControl::Jobs::NavigationHelper
22
attr_reader :page_title, :current_section
33

44
def navigation_sections
5-
{ queues: [ "Queues", application_queues_path(@application) ] }.tap do |sections|
5+
{ dashboard: [ "Dashboard", application_dashboard_index_path(@application) ] }.tap do |sections|
6+
sections[:queues] = [ "Queues", application_queues_path(@application) ]
7+
sections[:queues] = [ "Queues", application_queues_path(@application) ]
8+
69
supported_job_statuses.without(:pending).each do |status|
710
sections[navigation_section_for_status(status)] = [ "#{status.to_s.titleize} jobs (#{jobs_count_with_status(status)})", application_jobs_path(@application, status) ]
811
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class MissionControl::SolidQueueFailedExecution < MissionControl::SolidQueueRecord
2+
self.table_name = 'solid_queue_failed_executions'
3+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class MissionControl::SolidQueueJob < MissionControl::SolidQueueRecord
2+
self.table_name = 'solid_queue_jobs'
3+
4+
scope :pendings, -> { where(finished_at: nil) }
5+
scope :finisheds, -> { where.not(finished_at: nil) }
6+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class MissionControl::SolidQueueRecord < ApplicationRecord
2+
self.abstract_class = true
3+
4+
if !ActiveRecord::Base.connection.data_source_exists?('solid_queue_jobs')
5+
connects_to database: { writing: :queue, reading: :queue }
6+
end
7+
end
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="tabs is-boxed">
1+
<div class="tabs is-boxed" id="navigation-sections">
22
<ul>
33
<% navigation_sections.each do |key, (label, url)| %>
44
<li class="<%= "is-active" if key == current_section %>">
@@ -7,3 +7,31 @@
77
<% end %>
88
</ul>
99
</div>
10+
11+
<script>
12+
document.addEventListener("turbo:load", () => {
13+
if (!window.Navigation || typeof window.Navigation.currentSection === 'undefined') {
14+
window.Navigation = {
15+
currentSection: "<%= @current_section %>",
16+
17+
changeSection(section) {
18+
this.currentSection = section;
19+
}
20+
};
21+
22+
setInterval(() => {
23+
fetch(`/jobs/applications/solidqueueusage/internal_api/navigation?section=${window.Navigation.currentSection}`)
24+
.then(response => response.text())
25+
.then(html => {
26+
const navigationSections = document.querySelector('#navigation-sections');
27+
if (navigationSections) {
28+
navigationSections.innerHTML = html;
29+
}
30+
})
31+
.catch(error => console.error("Error fetching navigation update:", error));
32+
}, 5000);
33+
} else {
34+
window.Navigation.currentSection = "<%= @current_section %>";
35+
}
36+
});
37+
</script>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<ul>
2+
<% navigation_sections.each do |key, (label, url)| %>
3+
<li class="<%= "is-active" if key == section %>">
4+
<%= link_to label, url %>
5+
</li>
6+
<% end %>
7+
</ul>

app/views/layouts/mission_control/jobs/application.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<meta name="viewport" content="width=device-width,initial-scale=1">
99
<meta name="turbo-cache-control" content="no-cache">
1010
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
11+
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
12+
1113
<%= stylesheet_link_tag "mission_control/jobs/application", "data-turbo-track": "reload" %>
1214
<%= javascript_importmap_tags "application", importmap: MissionControl::Jobs.importmap %>
1315
</head>

0 commit comments

Comments
 (0)