Add a new page to show stats for a specific year

This commit is contained in:
Eugene Burmakin 2024-03-24 19:46:55 +01:00
parent 96a9900661
commit 2953ca6499
7 changed files with 41 additions and 17 deletions

View file

@ -5,6 +5,11 @@ class StatsController < ApplicationController
@stats = current_user.stats.group_by(&:year).sort_by { _1 }.reverse
end
def show
@year = params[:year].to_i
@stats = current_user.stats.where(year: @year)
end
def update
StatCreatingJob.perform_later(current_user.id)

View file

@ -30,4 +30,10 @@ class Stat < ApplicationRecord
[data[:day], data[:distance].round(2)]
end
end
def self.year_distance(year)
stats = where(year: year).order(:month)
stats.map { |stat| [Date::MONTHNAMES[stat.month], stat.distance] }
end
end

View file

@ -10,13 +10,13 @@
<div class="card-actions justify-end">
<%= stat.toponyms.count %> countries, <%= stat.toponyms.sum { _1['cities'].count } %> cities
</div>
<%= column_chart(
stat.daily_distance,
height: '100px',
suffix: ' km',
xtitle: 'Days',
ytitle: 'Distance'
) %>
<% end %>
<%= column_chart(
stat.daily_distance,
height: '100px',
suffix: ' km',
xtitle: 'Days',
ytitle: 'Distance'
) %>
</div>
</div>

View file

@ -0,0 +1,18 @@
<h2 class='text-3xl font-bold mt-10'>
<%= link_to year, "/stats/#{year}", class: 'underline hover:no-underline' %>
<%= link_to '[Map]', points_url(year_timespan(year)), class: 'underline hover:no-underline' %>
</h2>
<div class='my-10'>
<%= column_chart(
Stat.year_distance(year),
height: '200px',
suffix: ' km',
xtitle: 'Days',
ytitle: 'Distance'
) %>
</div>
<div class="mt-5 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 p-4">
<% stats.each do |stat| %>
<%= render stat %>
<% end %>
</div>

View file

@ -41,15 +41,6 @@
<%= link_to 'Update stats', stats_path, method: :post, class: 'btn btn-primary mt-5' %>
<% @stats.each do |year, stats| %>
<h2 class='text-3xl font-bold mt-10'>
<%= link_to points_url(year_timespan(year)), class: 'underline hover:no-underline' do %>
<%= year %>
<% end %>
</h2>
<div class="mt-5 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 p-4">
<% stats.each do |stat| %>
<%= render stat %>
<% end %>
</div>
<%= render partial: 'stats/year', locals: { year: year, stats: stats } %>
<% end %>
</div>

View file

@ -0,0 +1,3 @@
<div class="w-full">
<%= render partial: 'stats/year', locals: { year: @year, stats: @stats } %>
</div>

View file

@ -6,6 +6,7 @@ Rails.application.routes.draw do
post :update
end
end
get 'stats/:year', to: 'stats#show', constraints: { year: /\d{4}/ }
root to: 'home#index'
devise_for :users