Skip to content

Add admin dashboard #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions app/controllers/admin/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module Admin
class DashboardController < ApplicationController
def index
@manpower_survey = [
{ name: 'Chapter Manpower', data: {}, points: false },
{ name: 'Chapter Count', data: {}, format: 'bar', points: false }
]
Chapter.active.each do |c|
c.manpower_surveys.each do |s|
survey_date = s.survey_date
manpower = s.manpower
@manpower_survey[0]['data'] ||= {}
@manpower_survey[0]['data'][survey_date] ||= 0
@manpower_survey[0]['data'][survey_date] += manpower
@manpower_survey[1]['data'] ||= {}
@manpower_survey[1]['data'][survey_date] ||= 0
@manpower_survey[1]['data'][survey_date] += 1
end
end

@active_chapters = Chapter.active.size

@current_manpower = Chapter.where(status: true).sum(:manpower)

@largest_chapters = Chapter.where(status: true)
.order('manpower DESC')
.limit(10)

@smallest_chapters = Chapter.where(status: true)
.where('chapters.manpower > 0')
.order('manpower ASC')
.limit(10)

@manpower_distribution = Chapter.where(status: true)
.order('manpower DESC')
.pluck(:name, :manpower)
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/admin/dashboard_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Admin::DashboardHelper
end
3 changes: 3 additions & 0 deletions app/views/admin/_admin_menu.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<ul class="nav">
<li class="nav-item">
<%= link_to 'Dashboard', admin_path, class: 'nav-link' %>
</li>
<li class="nav-item">
<%= link_to 'Chapters', admin_chapters_path, class: 'nav-link' %>
</li>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/chapters/_chapter.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<% unless current_page?(admin_chapters_url) %>
<div id="manpower_chart">
<%= line_chart @manpower_survey, title: 'Daily Reported Manpower' %>
<%= line_chart @manpower_survey, title: 'Daily Reported Manpower', points: false %>
</div>

<hr />
Expand Down
37 changes: 37 additions & 0 deletions app/views/admin/dashboard/_ranking_table.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<h4 class="mb-2"><%= title %></h4>
<table class="table table-striped table-sm">
<tr>
<th>Rank</th>
<th>Chapter</th>
<th>Manpower</th>
</tr>
<% rank = 1 %>
<% chapters.each_with_index do |chapter, index| %>
<%
# Check if the next row has the same manpower
if index + 1 < chapters.length && chapters[index + 1].manpower === chapter.manpower
tie = 'T'
if index > 0 && chapters[index - 1].manpower != chapter.manpower
rank = index + 1
end
# Check if the previous row has the same manpower
elsif index > 0 && chapters[index - 1].manpower === chapter.manpower
tie = 'T'
# Default to index + 1
else
rank = index + 1
tie = ''
end
%>
<tr>
<td class="text-center">
<%= tie %><%= rank %>
</td>
<td>
<%= link_to chapter.name, admin_chapter_path(chapter) %>
<div><small><%= chapter.institution_name %></small></div>
</td>
<td class="text-end"><%= chapter.manpower %></td>
</tr>
<% end %>
</table>
39 changes: 39 additions & 0 deletions app/views/admin/dashboard/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<%= render 'admin/admin_menu' %>

<div class="row my-3">
<div class="col-md-6">
<div id="manpower_chart" class="my-3">
<%= line_chart @manpower_survey, title: 'Daily Reported Manpower', thousands: ',' %>
</div>
</div>
<div class="col-md-6">
<div id="manpower_distribution" class="my-3">
<%= column_chart @manpower_distribution, title: 'Manpower Distribution', library: { scales: { x: { display: false } } } %>
</div>
</div>
</div>

<div class="row mt-5">
<div class="col-md-4 my-3 text-center">

<div class="mb-4">
<div class="display-1">
<%= @active_chapters %>
</div>
Active Chapters
</div>

<div class="mb-4">
<div class="display-1">
<%= number_with_delimiter(@current_manpower) %>
</div>
Current Manpower
</div>
</div>
<div class="col-md-4 my-3">
<%= render partial: 'admin/dashboard/ranking_table', locals: { title: 'Largest Chapters', chapters: @largest_chapters } %>
</div>
<div class="col-md-4 my-3">
<%= render partial: 'admin/dashboard/ranking_table', locals: { title: 'Smallest Chapters', chapters: @smallest_chapters } %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/admin/districts/_district.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<% unless current_page?(admin_districts_url) %>
<hr />
<div id="manpower_chart">
<%= line_chart @manpower_survey, title: 'Daily Reported Manpower' %>
<%= line_chart @manpower_survey, title: 'Daily Reported Manpower', points: false %>
</div>

<hr />
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

authenticate :user do
namespace :admin do
get '/', to: redirect('/admin/chapters')
get '/', to: 'dashboard#index'

# Import Chapters
get 'chapters/import', to: 'chapters#import'
Expand Down
7 changes: 7 additions & 0 deletions test/controllers/admin/dashboard_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class Admin::DashboardControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
Loading