Skip to content

Commit

Permalink
Merge pull request #27 from scottwillson/racer-days-by-gender
Browse files Browse the repository at this point in the history
Racer days by gender
  • Loading branch information
scotthillson authored Oct 9, 2024
2 parents cd8f09b + b13828d commit b474325
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 44 deletions.
13 changes: 4 additions & 9 deletions app/controllers/admin/stats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ module Admin
class StatsController < Admin::AdminController
def index
@years = %w[2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024]
if params[:nopandemic]
@years = %w[2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2021 2022 2023 2024]
end
@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)
@racer_days_by_gender = Stats.racer_days_by_gender(@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)
Expand Down
27 changes: 24 additions & 3 deletions app/services/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def self.racers_by_discipline_query(years)
years.each do |year|
res = Result.joins(:event).where(events: { year: year, type: "SingleDayEvent" })
.where(competition_result: false, team_competition_result: false)
.where.not(person_id: nil).group(:discipline).distinct
.count(:person_id)
.where.not(person_id: nil).group(:discipline).distinct.count(:person_id)
chart_data.each do |data|
if res[data[:name]].nil?
data[:data].push(0)
Expand Down Expand Up @@ -107,6 +106,28 @@ def self.total_racers(years)
chart_data
end

def self.racer_days_by_gender(years)
return Rails.cache.read("racer_days_by_gender") if Rails.cache.read("racer_days_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("racer_days_by_gender", chart_data, expires_in: 12.hours)
chart_data
end

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

Expand All @@ -119,7 +140,7 @@ def self.racers_by_gender(years)
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
.where.not(person_id: nil).group("people.gender").distinct.count(:person_id)
chart_data[0][:data].push(res[nil] || 0)
chart_data[1][:data].push(res["F"] || 0)
chart_data[2][:data].push(res["M"] || 0)
Expand Down
49 changes: 17 additions & 32 deletions app/views/admin/stats/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
<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="racer-days-by-gender" class="stats-chart"></div>
<div id="racers-by-gender" class="stats-chart"></div>
<script>
function loadCharts() {
Highcharts.chart('racer-days-by-discipline', {
title: {
text: 'By Discipline'
text: 'Racer Days by Discipline'
},
xAxis: {
categories: <%= raw @years %>
},
yAxis: {
title: {
text: 'Racer Days'
},
labels: {
formatter: () => {
return Highcharts.numberFormat(this.value, 0, '.', ',');
}
}
},
series: <%= raw @racer_days_by_discipline.to_json %>
Expand All @@ -35,30 +31,20 @@
yAxis: {
title: {
text: 'Racer Days'
},
labels: {
formatter: () => {
return Highcharts.numberFormat(this.value, 0, '.', ',');
}
}
},
series: <%= raw @total_racer_days.to_json %>
});
Highcharts.chart('racers-by-discipline', {
title: {
text: 'Racers By Discipline'
text: 'Racers by Discipline'
},
xAxis: {
categories: <%= raw @years %>
},
yAxis: {
title: {
text: 'Racers'
},
labels: {
formatter: () => {
return Highcharts.numberFormat(this.value, 0, '.', ',');
}
}
},
series: <%= raw @racers_by_discipline.to_json %>
Expand All @@ -73,11 +59,6 @@
yAxis: {
title: {
text: 'Memberships'
},
labels: {
formatter: () => {
return Highcharts.numberFormat(this.value, 0, '.', ',');
}
}
},
series: <%= raw @memberships.to_json %>
Expand All @@ -92,15 +73,24 @@
yAxis: {
title: {
text: 'Junior Racers'
},
labels: {
formatter: () => {
return Highcharts.numberFormat(this.value, 0, '.', ',');
}
}
},
series: <%= raw @total_juniors.to_json %>
});
Highcharts.chart('racer-days-by-gender', {
title: {
text: 'Racers Days by Gender'
},
xAxis: {
categories: <%= raw @years %>
},
yAxis: {
title: {
text: 'Racer Days'
}
},
series: <%= raw @racer_days_by_gender.to_json %>
});
Highcharts.chart('racers-by-gender', {
title: {
text: 'Racers by Gender'
Expand All @@ -111,11 +101,6 @@
yAxis: {
title: {
text: 'Racers'
},
labels: {
formatter: () => {
return Highcharts.numberFormat(this.value, 0, '.', ',');
}
}
},
series: <%= raw @racers_by_gender.to_json %>
Expand Down

0 comments on commit b474325

Please sign in to comment.