Skip to content

Commit

Permalink
Merge pull request #173 from zealot128/main
Browse files Browse the repository at this point in the history
UI: Admin UI with filters and space efficient layout
  • Loading branch information
bensheldon authored Dec 1, 2020
2 parents 2bb4782 + b80009a commit 2840e07
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
53 changes: 51 additions & 2 deletions engine/app/controllers/good_job/dashboards_controller.rb
Original file line number Diff line number Diff line change
@@ -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 *
Expand Down
29 changes: 26 additions & 3 deletions engine/app/views/good_job/dashboards/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@
<%= render 'shared/chart', chart_data: @chart %>
</div>

<% if @jobs.present? %>
<%= render 'shared/jobs_table', jobs: @jobs %>
<div class='card mb-2'>
<div class='card-body d-flex flex-wrap'>
<div class='mr-4'>
<small>Filter by job class</small>
<br>
<% @filter.job_classes.each do |name, count| %>
<a href='<%= request.path + "?#{@filter.to_query(job_class: name)}" %>' class='btn btn-sm btn-outline-secondary <%= "active" if params[:job_class] == name %>'>
<%= name %> (<%= count %>)
</a>
<% end %>
</div>
<div>
<small>Filter by state</small>
<br>
<% @filter.states.each do |name, count| %>
<a href='<%= request.path + "?#{@filter.to_query(state: name)}" %>' class='btn btn-sm btn-outline-secondary <%= "active" if params[:state] == name %>'>
<%= name %> (<%= count %>)
</a>
<% end %>
</div>
</div>
</div>

<% if @filter.jobs.present? %>
<%= render 'shared/jobs_table', jobs: @filter.jobs %>

<nav aria-label="Job pagination">
<ul class="pagination">
<li class="page-item">
<%= link_to({ after_scheduled_at: (@jobs.last.scheduled_at || @jobs.last.created_at), after_id: @jobs.last.id }, class: "page-link") do %>
<%= link_to({ after_scheduled_at: (@filter.last.scheduled_at || @filter.last.created_at), after_id: @filter.last.id }, class: "page-link") do %>
Next jobs <span aria-hidden="true">&raquo;</span>
<% end %>
</li>
Expand Down
4 changes: 2 additions & 2 deletions engine/app/views/layouts/good_job/base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<div class="container-fluid">
<%= link_to "GoodJob 👍", root_path, class: 'navbar-brand mb-0 h1' %>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
Expand Down Expand Up @@ -48,7 +48,7 @@
</div>
</nav>

<div class="container">
<div class="container-fluid">
<div class="card border-warning text-dark my-3">
<div class="card-body">
<p class="card-text">🚧 GoodJob's dashboard is a work in progress. Please contribute ideas and code on <a href="https://github.com/bensheldon/good_job/issues" target="_blank" rel="nofollow noopener noreferrer">Github</a>.</p>
Expand Down
2 changes: 1 addition & 1 deletion engine/app/views/shared/_jobs_table.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="table-responsive">
<table class="table table-bordered table-hover">
<table class="table table-bordered table-hover table-sm">
<thead>
<th>GoodJob ID</th>
<th>ActiveJob ID</th>
Expand Down

0 comments on commit 2840e07

Please sign in to comment.