diff --git a/Makefile b/Makefile index 551969cc..1393abf6 100644 --- a/Makefile +++ b/Makefile @@ -67,6 +67,7 @@ production_migrate: ssh dokku_frey 'dokku run dawarich bundle exec rails db:migrate' build_and_push: + git tag -l "$(version)" docker build . -t dawarich:$(version) --platform=linux/amd64 docker tag dawarich:$(version) registry.chibi.rodeo/dawarich:$(version) docker push registry.chibi.rodeo/dawarich:$(version) diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb new file mode 100644 index 00000000..7e06173b --- /dev/null +++ b/app/controllers/stats_controller.rb @@ -0,0 +1,7 @@ +class StatsController < ApplicationController + before_action :authenticate_user! + + def index + @stats = current_user.stats.group_by(&:year).sort_by { _1 }.reverse + end +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index b2ddb195..42ffb140 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -7,4 +7,12 @@ module ApplicationHelper 'bg-blue-100 text-blue-700 border-blue-300' end end + + def url_time(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 end diff --git a/app/models/stat.rb b/app/models/stat.rb new file mode 100644 index 00000000..7fd717d9 --- /dev/null +++ b/app/models/stat.rb @@ -0,0 +1,5 @@ +class Stat < ApplicationRecord + validates :year, :month, presence: true + + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index c58a7639..50a5ee56 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,4 +6,5 @@ class User < ApplicationRecord has_many :imports, dependent: :destroy has_many :points, through: :imports + has_many :stats end diff --git a/app/services/create_stats.rb b/app/services/create_stats.rb new file mode 100644 index 00000000..a985dd71 --- /dev/null +++ b/app/services/create_stats.rb @@ -0,0 +1,56 @@ + +class CreateStats + attr_reader :years, :months, :user + + def initialize(user_id) + @user = User.find(user_id) + @years = (1970..Time.current.year).to_a + @months = (1..12).to_a + end + + def call + years.flat_map do |year| + months.map do |month| + beginning_of_month_timestamp = DateTime.new(year, month).beginning_of_month.to_i + end_of_month_timestamp = DateTime.new(year, month).end_of_month.to_i + + points = points(beginning_of_month_timestamp, end_of_month_timestamp) + + next if points.empty? + + Stat.create( + year: year, + month: month, + distance: distance(points), + toponyms: toponyms(points), + user: user + ) + end + end.compact + end + + private + + def points(beginning_of_month_timestamp, end_of_month_timestamp) + Point + .where(timestamp: beginning_of_month_timestamp..end_of_month_timestamp) + .order(:timestamp) + .select(:latitude, :longitude, :timestamp, :city, :country) + end + + def distance(points) + km = 0 + + points.each_cons(2) do + km += Geocoder::Calculations.distance_between( + [_1.latitude, _1.longitude], [_2.latitude, _2.longitude], units: :km + ) + end + + km + end + + def toponyms(points) + CountriesAndCities.new(points).call + end +end diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index f73d3f7b..39b49ac2 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -7,6 +7,7 @@ <%= link_to 'Dawarich', root_path, class: 'btn btn-ghost normal-case text-xl'%> @@ -20,6 +21,7 @@