Skip to content

Commit

Permalink
Merge pull request #26 from scottwillson/racersbygender
Browse files Browse the repository at this point in the history
Racers By Gender
  • Loading branch information
scotthillson authored Oct 8, 2024
2 parents 1c24003 + 61d65ff commit cd8f09b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 22 deletions.
22 changes: 16 additions & 6 deletions app/controllers/admin/stats_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
module Admin
class StatsController < Admin::AdminController
def index
@years = Stats.years
@racer_days_by_discipline = Stats.racer_days_by_discipline
@total_racer_days = Stats.total_racer_days
@racers_by_discipline = Stats.racers_by_discipline
@memberships = Stats.memberships
@total_juniors = Stats.total_juniors
@years = %w[2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024]
@racer_days_by_discipline = Stats.racer_days_by_discipline(@years)
@racers_by_discipline = Stats.racers_by_discipline(@years)
@total_racer_days = Stats.total_racer_days(@years)
@racers_by_gender = Stats.racers_by_gender(@years)
@total_juniors = Stats.total_juniors(@years)
@memberships = Stats.memberships(@years)
end

def nopandemic
@years = %w[2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2021 2022 2023 2024]
@racer_days_by_discipline = Stats.racer_days_by_discipline(@years)
@racers_by_discipline = Stats.racers_by_discipline(@years)
@total_racer_days = Stats.total_racer_days(@years)
@total_juniors = Stats.total_juniors(@years)
@memberships = Stats.memberships(@years)
end
end
end
50 changes: 34 additions & 16 deletions app/services/stats.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
# frozen_string_literal: true

module Stats
def self.years
%w[2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024]
end

def self.racer_days_by_discipline
def self.racer_days_by_discipline(years)
if Rails.cache.read("racer_days_by_discipline").present?
return Rails.cache.read("racer_days_by_discipline")
end

chart_data = racer_days_by_discipline_query
chart_data = racer_days_by_discipline_query(years)
Rails.cache.write("racer_days_by_discipline", chart_data, expires_in: 12.hours)
chart_data
end

def self.racer_days_by_discipline_query
def self.racer_days_by_discipline_query(years)
chart_data = Discipline.all.map { |discipline| { name: discipline.name, data: [] } }
years.each do |year|
res = Result.joins(:event).where(events: { year: year, type: "SingleDayEvent" })
Expand All @@ -37,17 +33,17 @@ def self.racer_days_by_discipline_query
chart_data
end

def self.racers_by_discipline
def self.racers_by_discipline(years)
if Rails.cache.read("racers_by_discipline").present?
return Rails.cache.read("racers_by_discipline")
end

chart_data = racers_by_discipline_query
chart_data = racers_by_discipline_query(years)
Rails.cache.write("racers_by_discipline", chart_data, expires_in: 12.hours)
chart_data
end

def self.racers_by_discipline_query
def self.racers_by_discipline_query(years)
chart_data = Discipline.all.map { |discipline| { name: discipline.name, data: [] } }
years.each do |year|
res = Result.joins(:event).where(events: { year: year, type: "SingleDayEvent" })
Expand All @@ -70,7 +66,7 @@ def self.racers_by_discipline_query
chart_data
end

def self.memberships
def self.memberships(years)
return Rails.cache.read("memberships") if Rails.cache.read("memberships").present?

chart_data = [{ name: "Memberships", data: [] }]
Expand All @@ -82,7 +78,7 @@ def self.memberships
chart_data
end

def self.total_racer_days
def self.total_racer_days(years)
return Rails.cache.read("total_racer_days") if Rails.cache.read("total_racer_days").present?

chart_data = [{ name: "Total Racer Days", data: [] }]
Expand All @@ -96,10 +92,10 @@ def self.total_racer_days
chart_data
end

def self.total_racers
def self.total_racers(years)
return Rails.cache.read("total_racers") if Rails.cache.read("total_racers").present?

chart_data = [{ name: "Total Racer Days", data: [] }]
chart_data = [{ name: "Racers", data: [] }]
years.each do |year|
res = Result.joins(:event).where(events: { year: year, type: "SingleDayEvent" })
.where(competition_result: false, team_competition_result: false)
Expand All @@ -111,10 +107,32 @@ def self.total_racers
chart_data
end

def self.total_juniors
def self.racers_by_gender(years)
return Rails.cache.read("racers_by_gender") if Rails.cache.read("racers_by_gender").present?

chart_data = [
{ name: "Not Specified", data: [] },
{ name: "Female", data: [] },
{ name: "Male", data: [] },
{ name: "NB", data: [] }
]
years.each do |year|
res = Result.joins(:event, :person).where(events: { year: year, type: "SingleDayEvent" })
.where(competition_result: false, team_competition_result: false)
.where.not(person_id: nil).group("people.gender").count
chart_data[0][:data].push(res[nil] || 0)
chart_data[1][:data].push(res["F"] || 0)
chart_data[2][:data].push(res["M"] || 0)
chart_data[3][:data].push(res["NB"] || 0)
end
Rails.cache.write("racers_by_gender", chart_data, expires_in: 12.hours)
chart_data
end

def self.total_juniors(years)
return Rails.cache.read("total_juniors") if Rails.cache.read("total_juniors").present?

chart_data = [{ name: "Total Racer Days", data: [] }]
chart_data = [{ name: "Juniors", data: [] }]
years.each do |year|
res = Result.joins(:event).where(events: { year: year, type: "SingleDayEvent" })
.where(competition_result: false, team_competition_result: false)
Expand Down
20 changes: 20 additions & 0 deletions app/views/admin/stats/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<div id="racers-by-discipline" class="stats-chart"></div>
<div id="annual-memberships" class="stats-chart"></div>
<div id="total-juniors" class="stats-chart"></div>
<div id="racers-by-gender" class="stats-chart"></div>
<script>
function loadCharts() {
Highcharts.chart('racer-days-by-discipline', {
Expand Down Expand Up @@ -100,6 +101,25 @@
},
series: <%= raw @total_juniors.to_json %>
});
Highcharts.chart('racers-by-gender', {
title: {
text: 'Racers by Gender'
},
xAxis: {
categories: <%= raw @years %>
},
yAxis: {
title: {
text: 'Racers'
},
labels: {
formatter: () => {
return Highcharts.numberFormat(this.value, 0, '.', ',');
}
}
},
series: <%= raw @racers_by_gender.to_json %>
});
}
document.addEventListener('DOMContentLoaded', loadCharts);
</script>

0 comments on commit cd8f09b

Please sign in to comment.