From d1454c21a9eeefb816d38c9c7a5fefe4df57d577 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sat, 23 Mar 2024 20:40:05 +0100 Subject: [PATCH] Add StatCreatingJob to create stats asynchronously --- README.md | 5 +++++ app/controllers/imports_controller.rb | 2 ++ app/helpers/application_helper.rb | 9 ++++++++- app/jobs/stat_creating_job.rb | 7 +++++++ app/views/stats/_stat.html.erb | 2 +- app/views/stats/index.html.erb | 6 +++++- config/application.rb | 2 +- spec/jobs/stat_creating_job_spec.rb | 5 +++++ 8 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 app/jobs/stat_creating_job.rb create mode 100644 spec/jobs/stat_creating_job_spec.rb diff --git a/README.md b/README.md index c1d96c21..05328b73 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,8 @@ Dockerized with https://betterprogramming.pub/rails-6-development-with-docker-55 Then go to Portainer and update the service to use the new image +## Environment variables + +`MINIMUM_POINTS_IN_CITY` — minimum number of points in a city to consider it as a city visited, eg. `10` +`MAP_CENTER` — default map center, e.g. `55.7558,37.6176` +`TIME_ZONE` — time zone, e.g. `Europe/Berlin` diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rb index 351bc31c..8e385128 100644 --- a/app/controllers/imports_controller.rb +++ b/app/controllers/imports_controller.rb @@ -32,6 +32,8 @@ class ImportsController < ApplicationController end end + StatCreatingJob.perform_later(current_user.id) + redirect_to imports_url, notice: "#{imports.size} import files were imported successfully", status: :see_other rescue StandardError => e imports.each { |import| import&.destroy! } diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 42ffb140..df49c727 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -8,11 +8,18 @@ module ApplicationHelper end end - def url_time(stat) + def month_timespan(stat) month = DateTime.new(stat.year, stat.month).in_time_zone(Time.zone) start_at = month.beginning_of_month.to_time.strftime('%Y-%m-%dT%H:%M') end_at = month.end_of_month.to_time.strftime('%Y-%m-%dT%H:%M') { start_at:, end_at: } end + + def year_timespan(year) + start_at = DateTime.new(year).beginning_of_year.to_time.strftime('%Y-%m-%dT%H:%M') + end_at = DateTime.new(year).end_of_year.to_time.strftime('%Y-%m-%dT%H:%M') + + { start_at: start_at, end_at: end_at } + end end diff --git a/app/jobs/stat_creating_job.rb b/app/jobs/stat_creating_job.rb new file mode 100644 index 00000000..6ca0f9fe --- /dev/null +++ b/app/jobs/stat_creating_job.rb @@ -0,0 +1,7 @@ +class StatCreatingJob < ApplicationJob + queue_as :default + + def perform(user_id) + CreateStats.new(user_id).call + end +end diff --git a/app/views/stats/_stat.html.erb b/app/views/stats/_stat.html.erb index 9a39d96a..c3b946a4 100644 --- a/app/views/stats/_stat.html.erb +++ b/app/views/stats/_stat.html.erb @@ -1,7 +1,7 @@

- <%= link_to points_url(url_time(stat)), class: 'underline hover:no-underline' do %> + <%= link_to points_url(month_timespan(stat)), class: 'underline hover:no-underline' do %> <%= "#{Date::MONTHNAMES[stat.month]} of #{stat.year}" %> <% end %>

diff --git a/app/views/stats/index.html.erb b/app/views/stats/index.html.erb index 0f293576..d57b5e45 100644 --- a/app/views/stats/index.html.erb +++ b/app/views/stats/index.html.erb @@ -1,6 +1,10 @@
<% @stats.each do |year, stats| %> -

<%= year %>

+

+ <%= link_to points_url(year_timespan(year)), class: 'underline hover:no-underline' do %> + <%= year %> + <% end %> +

<% stats.each do |stat| %> <%= render stat %> diff --git a/config/application.rb b/config/application.rb index d4b5da2f..6683ea56 100644 --- a/config/application.rb +++ b/config/application.rb @@ -21,7 +21,7 @@ module Dawarich # These settings can be overridden in specific environments using the files # in config/environments, which are processed later. # - # config.time_zone = "Central Time (US & Canada)" + config.time_zone = ENV.fetch('TIME_ZONE', 'Europe/Berlin') # config.eager_load_paths << Rails.root.join("extras") # Don't generate system test files. diff --git a/spec/jobs/stat_creating_job_spec.rb b/spec/jobs/stat_creating_job_spec.rb new file mode 100644 index 00000000..11c284fd --- /dev/null +++ b/spec/jobs/stat_creating_job_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe StatCreatingJob, type: :job do + pending "add some examples to (or delete) #{__FILE__}" +end