diff --git a/Gemfile b/Gemfile index 323acf81..92b6a822 100644 --- a/Gemfile +++ b/Gemfile @@ -16,7 +16,6 @@ gem 'tailwindcss-rails' gem 'turbo-rails' gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] gem "importmap-rails" -gem "mapkick-rb" gem "chartkick" gem 'geocoder' gem 'sidekiq' diff --git a/Gemfile.lock b/Gemfile.lock index 3af5afb6..c8a7957f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -136,7 +136,6 @@ GEM net-imap net-pop net-smtp - mapkick-rb (0.1.5) marcel (1.0.4) method_source (1.0.0) mini_mime (1.1.5) @@ -333,7 +332,6 @@ DEPENDENCIES foreman geocoder importmap-rails - mapkick-rb pg pry-byebug pry-rails diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 7e06173b..f50905bf 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -4,4 +4,10 @@ class StatsController < ApplicationController def index @stats = current_user.stats.group_by(&:year).sort_by { _1 }.reverse end + + def update + StatCreatingJob.perform_later(current_user.id) + + redirect_to stats_path, notice: 'Stats are being updated', status: :see_other + end end diff --git a/app/services/create_stats.rb b/app/services/create_stats.rb index 4d1ee1d0..04125574 100644 --- a/app/services/create_stats.rb +++ b/app/services/create_stats.rb @@ -18,9 +18,11 @@ class CreateStats points = points(beginning_of_month_timestamp, end_of_month_timestamp) next if points.empty? - stat = Stat.create(year:, month:, user:, distance: distance(points), toponyms: toponyms(points)) - - stat.update(daily_distance: stat.distance_by_day) if stat.persisted? + stat = Stat.find_or_initialize_by(year: year, month: month, user: user) + stat.distance = distance(points) + stat.toponyms = toponyms(points) + stat.daily_distance = stat.distance_by_day + stat.save stat end diff --git a/app/views/stats/index.html.erb b/app/views/stats/index.html.erb index dd75dd9a..9dc41fbf 100644 --- a/app/views/stats/index.html.erb +++ b/app/views/stats/index.html.erb @@ -38,13 +38,15 @@ <% end %> + <%= link_to 'Update stats', stats_path, method: :post, class: 'btn btn-primary mt-5' %> + <% @stats.each do |year, stats| %>