-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/Dashboard Chart with auto update
- Loading branch information
Felipe
committed
Nov 10, 2024
1 parent
bb63fab
commit 839c0a4
Showing
15 changed files
with
402 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class MissionControl::Jobs::DashboardController < MissionControl::Jobs::ApplicationController | ||
def index | ||
end | ||
end |
39 changes: 39 additions & 0 deletions
39
app/controllers/mission_control/jobs/internal_api/dashboard_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class MissionControl::Jobs::InternalApi::DashboardController < MissionControl::Jobs.base_controller_class.constantize | ||
include ActionView::Helpers::NumberHelper | ||
|
||
def index | ||
render json: { | ||
uptime: { | ||
label: Time.now.strftime("%H:%M:%S"), | ||
pending: MissionControl::SolidQueueJob.pendings.where.not(id: MissionControl::SolidQueueFailedExecution.select(:job_id)).size, | ||
failed: MissionControl::SolidQueueFailedExecution.where("created_at >= ?", time_to_consult.seconds.ago).size, | ||
finished: MissionControl::SolidQueueJob.finisheds.where("finished_at >= ?", time_to_consult.seconds.ago).size, | ||
}, | ||
total: { | ||
failed: number_with_delimiter(ActiveJob.jobs.failed.count), | ||
pending: number_with_delimiter(ActiveJob.jobs.pending.count), | ||
scheduled: number_with_delimiter(ActiveJob.jobs.scheduled.count), | ||
in_progress: number_with_delimiter(ActiveJob.jobs.in_progress.count), | ||
finished: number_with_delimiter(ActiveJob.jobs.finished.count) | ||
} | ||
}, | ||
status: :ok | ||
end | ||
|
||
private | ||
def time_to_consult | ||
params[:uptime] || 5 | ||
end | ||
|
||
def humanize(count) | ||
number_to_human(count, | ||
format: "%n%u", | ||
units: { | ||
thousand: "K", | ||
million: "M", | ||
billion: "B", | ||
trillion: "T", | ||
quadrillion: "Q" | ||
}) | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
app/controllers/mission_control/jobs/internal_api/navigation_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class MissionControl::Jobs::InternalApi::NavigationController < MissionControl::Jobs::ApplicationController | ||
def index | ||
render partial: "layouts/mission_control/jobs/navigation_update", locals: { | ||
section: params[:section].to_sym | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class MissionControl::SolidQueueFailedExecution < MissionControl::SolidQueueRecord | ||
self.table_name = 'solid_queue_failed_executions' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class MissionControl::SolidQueueJob < MissionControl::SolidQueueRecord | ||
self.table_name = 'solid_queue_jobs' | ||
|
||
scope :pendings, -> { where(finished_at: nil) } | ||
scope :finisheds, -> { where.not(finished_at: nil) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class MissionControl::SolidQueueRecord < ApplicationRecord | ||
self.abstract_class = true | ||
|
||
if !ActiveRecord::Base.connection.data_source_exists?('solid_queue_jobs') | ||
connects_to database: { writing: :queue, reading: :queue } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
app/views/layouts/mission_control/jobs/_navigation_update.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<ul> | ||
<% navigation_sections.each do |key, (label, url)| %> | ||
<li class="<%= "is-active" if key == section %>"> | ||
<%= link_to label, url %> | ||
</li> | ||
<% end %> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
app/views/mission_control/jobs/dashboard/index.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
<% navigation(title: "Dashboard", section: :dashboard) %> | ||
|
||
<div class="columns"> | ||
<div class="column"> | ||
<div class="notification"> | ||
<h6 class="title is-6">Pending</h6> | ||
<span id="pending">--</span> | ||
</div> | ||
</div> | ||
<div class="column"> | ||
<div class="notification"> | ||
<h6 class="title is-6">Scheduled</h6> | ||
<span id="scheduled">--</span> | ||
</div> | ||
</div> | ||
<div class="column"> | ||
<div class="notification"> | ||
<h6 class="title is-6">In Progress</h6> | ||
<span id="in-progress">--</span> | ||
</div> | ||
</div> | ||
<div class="column"> | ||
<div class="notification"> | ||
<h6 class="title is-6">Finished</h6> | ||
<span id="finished">--</span> | ||
</div> | ||
</div> | ||
<div class="column"> | ||
<div class="notification"> | ||
<h6 class="title is-6">Failed</h6> | ||
<span id="failed">--</span> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="columns"> | ||
<div class="column"> | ||
General Overview | ||
</div> | ||
<div class="column has-text-right"> | ||
<div class="select is-small"> | ||
<select id="change-uptime" value="5" onchange="handleSelectUptime(this.value)"> | ||
<option value="10">10 seconds</option> | ||
<option value="5">5 seconds</option> | ||
<option value="3">3 seconds</option> | ||
<option value="1">1 second</option> | ||
</select> | ||
</div> | ||
</div> | ||
</div> | ||
<hr/> | ||
|
||
<div> | ||
<canvas id="general-overview-chart" style="max-height: 320px"></canvas> | ||
</div> | ||
|
||
<script> | ||
|
||
if (typeof uptimeInterval === "undefined") { | ||
var uptimeInterval = null; | ||
} | ||
if (typeof chart === "undefined") { | ||
var chart = null; | ||
} | ||
|
||
document.addEventListener("turbo:load", () => { | ||
const canvas = document.getElementById('general-overview-chart'); | ||
|
||
if (!canvas) | ||
return; | ||
|
||
const ctx = canvas.getContext('2d'); | ||
|
||
if (chart) { | ||
chart.destroy(); | ||
chart = null; | ||
} | ||
|
||
const finished = document.getElementById('finished'); | ||
const scheduled = document.getElementById('scheduled'); | ||
const pending = document.getElementById('pending'); | ||
const inProgress = document.getElementById('in-progress'); | ||
const failed = document.getElementById('failed'); | ||
|
||
let uptime = 5; | ||
|
||
const labels = []; | ||
const data = { | ||
labels: labels, | ||
datasets: [{ | ||
label: 'Success', | ||
data: [], | ||
fill: false, | ||
borderColor: 'rgb(0, 219, 124)', | ||
tension: 0.1 | ||
}, | ||
{ | ||
label: 'Error', | ||
data: [], | ||
fill: false, | ||
borderColor: 'rgb(226, 15, 15)', | ||
tension: 0.1 | ||
}, | ||
{ | ||
label: 'Pending', | ||
data: [], | ||
fill: false, | ||
borderColor: 'rgb(237, 209, 0)', | ||
tension: 0.1 | ||
}] | ||
}; | ||
|
||
const config = { | ||
type: 'line', | ||
data: data, | ||
}; | ||
|
||
chart = new Chart(ctx, config); | ||
|
||
function handleSelectUptime(value) { | ||
// console.log("Update Uptime to " + value); | ||
uptime = value; | ||
clearInterval(uptimeInterval); | ||
uptimeInterval = setInterval(() => updateChartData(), value * 1000); | ||
} | ||
|
||
async function updateChartData() { | ||
try { | ||
const response = await fetch(`<%= application_internal_api_dashboard_index_path %>?uptime=${uptime}`); | ||
if (!response.ok) throw new Error('Network response was not ok'); | ||
|
||
const data = await response.json(); | ||
|
||
if (chart == null) return; | ||
|
||
chart.data.labels.push(data.uptime.label); | ||
chart.data.labels = chart.data.labels.slice(-10); | ||
|
||
AddChartData(0, data.uptime.finished); | ||
AddChartData(1, data.uptime.failed); | ||
AddChartData(2, data.uptime.pending); | ||
|
||
finished.innerHTML = data.total.finished; | ||
inProgress.innerHTML = data.total.in_progress; | ||
pending.innerHTML = data.total.pending; | ||
scheduled.innerHTML = data.total.scheduled; | ||
failed.innerHTML = data.total.failed; | ||
|
||
chart.update(); | ||
} catch (error) { | ||
console.error('Error at consult chart API:', error); | ||
} | ||
} | ||
|
||
function AddChartData(datasetIndex, quantity) { | ||
chart.data.datasets[datasetIndex].data.push(quantity); | ||
chart.data.datasets[datasetIndex].data = chart.data.datasets[datasetIndex].data.slice(-10); | ||
} | ||
|
||
if (uptimeInterval != null) | ||
clearInterval(uptimeInterval); | ||
|
||
uptimeInterval = setInterval(() => updateChartData(), 5000); | ||
updateChartData(); | ||
|
||
// Exponha a função no escopo global | ||
window.handleSelectUptime = handleSelectUptime; | ||
}); | ||
|
||
// Limpa o gráfico e o intervalo antes de sair da página | ||
document.addEventListener("turbo:before-render", () => { | ||
if (uptimeInterval != null) { | ||
clearInterval(uptimeInterval); | ||
uptimeInterval = null; | ||
} | ||
|
||
if (chart) { | ||
chart.destroy(); | ||
chart = null; | ||
} | ||
}); | ||
|
||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.