Skip to content

Commit

Permalink
Merge pull request #24 from scottwillson/total-racer-days
Browse files Browse the repository at this point in the history
Total racer days
  • Loading branch information
scotthillson authored Oct 7, 2024
2 parents 12e206e + 264b071 commit 45d7afa
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
6 changes: 6 additions & 0 deletions app/assets/stylesheets/_utilities.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@
.margin-4 {
margin: 4rem;
}

.stats-chart {
height:800px;
margin: 4rem 0;
width:100%;
}
5 changes: 3 additions & 2 deletions app/controllers/admin/stats_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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]
@chart_data = Stats.racer_days_by_discipline(@years)
@years = Stats.years
@racer_days_by_discipline = Stats.racer_days_by_discipline
@total_racer_days = Stats.total_racer_days
end
end
end
20 changes: 19 additions & 1 deletion app/services/stats.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# frozen_string_literal: true

module Stats
def self.racer_days_by_discipline(years)
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
return Rails.cache.read("racer_days_by_discipline") if Rails.cache.read("racer_days_by_discipline").present?

chart_data = []
Expand All @@ -28,4 +32,18 @@ def self.racer_days_by_discipline(years)
Rails.cache.write("racer_days_by_discipline", chart_data, expires_in: 12.hours)
chart_data
end

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

chart_data = [{ name: "Total Racer Days", data: [] }]
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).count
chart_data[0][:data].push(res)
end
Rails.cache.write("total_racer_days", chart_data, expires_in: 12.hours)
chart_data
end
end
47 changes: 31 additions & 16 deletions app/views/admin/stats/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
<div id="racer-days-by-discipline" style="width:100%; height:800px;"></div>
<div id="racer-days-by-discipline" class="stats-chart"></div>
<div id="total-racer-days" class="stats-chart"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
Highcharts.chart('racer-days-by-discipline', {
title: {
text: 'By Discipline'
},
xAxis: {
categories: <%= raw @years %>
},
yAxis: {
document.addEventListener('DOMContentLoaded', function() {
Highcharts.chart('racer-days-by-discipline', {
title: {
text: 'Racer Days'
}
},
series: <%= raw @chart_data.to_json %>
text: 'By Discipline'
},
xAxis: {
categories: <%= raw @years %>
},
yAxis: {
title: {
text: 'Racer Days'
}
},
series: <%= raw @racer_days_by_discipline.to_json %>
});
Highcharts.chart('total-racer-days', {
title: {
text: 'Total Racer Days'
},
xAxis: {
categories: <%= raw @years %>
},
yAxis: {
title: {
text: 'Racer Days'
}
},
series: <%= raw @total_racer_days.to_json %>
});
});
});
</script>
</script>

0 comments on commit 45d7afa

Please sign in to comment.