Add StatCreatingJob to create stats asynchronously

This commit is contained in:
Eugene Burmakin 2024-03-23 20:40:05 +01:00
parent accb4dc04c
commit d1454c21a9
8 changed files with 34 additions and 4 deletions

View file

@ -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`

View file

@ -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! }

View file

@ -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

View file

@ -0,0 +1,7 @@
class StatCreatingJob < ApplicationJob
queue_as :default
def perform(user_id)
CreateStats.new(user_id).call
end
end

View file

@ -1,7 +1,7 @@
<div id="<%= dom_id stat %>" class="card w-full bg-base-200 shadow-xl">
<div class="card-body">
<h2 class="card-title">
<%= 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 %>
</h2>

View file

@ -1,6 +1,10 @@
<div class="w-full">
<% @stats.each do |year, stats| %>
<h2 class='text-3xl font-bold mt-10'><%= year %></h2>
<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-2 md:grid-cols-2 lg:grid-cols-3 gap-6 p-4">
<% stats.each do |stat| %>
<%= render stat %>

View file

@ -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.

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe StatCreatingJob, type: :job do
pending "add some examples to (or delete) #{__FILE__}"
end