From 06aee05602d31e9fde04184ef3e40206215a40af Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sat, 17 May 2025 20:35:38 +0200 Subject: [PATCH] Move distance unit settings to user settings --- .devcontainer/docker-compose.yml | 1 - .env.development | 1 - app/controllers/map_controller.rb | 4 +++- app/helpers/application_helper.rb | 4 ++-- app/models/concerns/distanceable.rb | 8 -------- app/models/concerns/nearable.rb | 8 -------- app/models/stat.rb | 2 +- app/models/trip.rb | 2 +- app/models/user.rb | 2 +- app/services/areas/visits/create.rb | 8 ++++---- app/services/users/safe_settings.rb | 7 ++++++- app/views/stats/_stat.html.erb | 4 ++-- app/views/stats/_year.html.erb | 2 +- app/views/stats/index.html.erb | 6 +++--- app/views/trips/_countries.html.erb | 4 ++-- app/views/trips/_distance.html.erb | 2 +- app/views/trips/_trip.html.erb | 2 +- config/initializers/01_constants.rb | 8 ++++++++ config/initializers/geocoder.rb | 2 +- docker/docker-compose.production.yml | 2 -- docker/docker-compose.yml | 2 -- docs/How_to_install_Dawarich_in_k8s.md | 8 ++------ spec/services/stats/calculate_month_spec.rb | 6 +++--- spec/services/users/safe_settings_spec.rb | 6 ++++-- 24 files changed, 46 insertions(+), 55 deletions(-) diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml index 04fd589b..9d88506d 100644 --- a/.devcontainer/docker-compose.yml +++ b/.devcontainer/docker-compose.yml @@ -28,7 +28,6 @@ services: APPLICATION_HOSTS: localhost TIME_ZONE: Europe/London APPLICATION_PROTOCOL: http - DISTANCE_UNIT: km PROMETHEUS_EXPORTER_ENABLED: false PROMETHEUS_EXPORTER_HOST: 0.0.0.0 PROMETHEUS_EXPORTER_PORT: 9394 diff --git a/.env.development b/.env.development index e083342f..8aeb3141 100644 --- a/.env.development +++ b/.env.development @@ -4,4 +4,3 @@ DATABASE_PASSWORD=password DATABASE_NAME=dawarich_development DATABASE_PORT=5432 REDIS_URL=redis://localhost:6379/1 -DISTANCE_UNIT='km' diff --git a/app/controllers/map_controller.rb b/app/controllers/map_controller.rb index 11082b6e..aeef5dc2 100644 --- a/app/controllers/map_controller.rb +++ b/app/controllers/map_controller.rb @@ -36,7 +36,9 @@ class MapController < ApplicationController @distance ||= 0 @coordinates.each_cons(2) do - @distance += Geocoder::Calculations.distance_between([_1[0], _1[1]], [_2[0], _2[1]], units: DISTANCE_UNIT) + @distance += Geocoder::Calculations.distance_between( + [_1[0], _1[1]], [_2[0], _2[1]], units: current_user.safe_settings.distance_unit + ) end @distance.round(1) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 5b9b71e3..94c4a8a7 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -51,7 +51,7 @@ module ApplicationHelper end def year_distance_stat(year, user) - # In km or miles, depending on the application settings (DISTANCE_UNIT) + # In km or miles, depending on the user.safe_settings.distance_unit Stat.year_distance(year, user).sum { _1[1] } end @@ -76,7 +76,7 @@ module ApplicationHelper def sidebar_distance(distance) return unless distance - "#{distance} #{DISTANCE_UNIT}" + "#{distance} #{current_user.safe_settings.distance_unit}" end def sidebar_points(points) diff --git a/app/models/concerns/distanceable.rb b/app/models/concerns/distanceable.rb index 72b12792..a9aad852 100644 --- a/app/models/concerns/distanceable.rb +++ b/app/models/concerns/distanceable.rb @@ -3,14 +3,6 @@ module Distanceable extend ActiveSupport::Concern - DISTANCE_UNITS = { - km: 1000, # to meters - mi: 1609.34, # to meters - m: 1, # already in meters - ft: 0.3048, # to meters - yd: 0.9144 # to meters - }.freeze - module ClassMethods def total_distance(points = nil, unit = :km) # Handle method being called directly on relation vs with array diff --git a/app/models/concerns/nearable.rb b/app/models/concerns/nearable.rb index 270e2b9c..b217ac12 100644 --- a/app/models/concerns/nearable.rb +++ b/app/models/concerns/nearable.rb @@ -3,14 +3,6 @@ module Nearable extend ActiveSupport::Concern - DISTANCE_UNITS = { - km: 1000, # to meters - mi: 1609.34, # to meters - m: 1, # already in meters - ft: 0.3048, # to meters - yd: 0.9144 # to meters - }.freeze - class_methods do # It accepts an array of coordinates [latitude, longitude] # and an optional radius and distance unit diff --git a/app/models/stat.rb b/app/models/stat.rb index 36bb0be3..b763aa76 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -37,7 +37,7 @@ class Stat < ApplicationRecord def calculate_daily_distances(monthly_points) timespan.to_a.map.with_index(1) do |day, index| daily_points = filter_points_for_day(monthly_points, day) - distance = Point.total_distance(daily_points, DISTANCE_UNIT) + distance = Point.total_distance(daily_points, user.safe_settings.distance_unit) [index, distance.round(2)] end end diff --git a/app/models/trip.rb b/app/models/trip.rb index 64422563..8863e748 100644 --- a/app/models/trip.rb +++ b/app/models/trip.rb @@ -39,7 +39,7 @@ class Trip < ApplicationRecord end def calculate_distance - distance = Point.total_distance(points, DISTANCE_UNIT) + distance = Point.total_distance(points, user.safe_settings.distance_unit) self.distance = distance.round end diff --git a/app/models/user.rb b/app/models/user.rb index e9c072e1..b4c38b5a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -49,7 +49,7 @@ class User < ApplicationRecord end def total_distance - # In km or miles, depending on the application settings (DISTANCE_UNIT) + # In km or miles, depending on user.safe_settings.distance_unit stats.sum(:distance) end diff --git a/app/services/areas/visits/create.rb b/app/services/areas/visits/create.rb index 16efd3ef..76eb2575 100644 --- a/app/services/areas/visits/create.rb +++ b/app/services/areas/visits/create.rb @@ -31,14 +31,14 @@ class Areas::Visits::Create def area_points(area) area_radius = - if ::DISTANCE_UNIT == :km - area.radius / 1000.0 + if user.safe_settings.distance_unit == :km + area.radius / DISTANCE_UNITS[:km] else - area.radius / 1609.344 + area.radius / DISTANCE_UNITS[user.safe_settings.distance_unit.to_sym] end points = Point.where(user_id: user.id) - .near([area.latitude, area.longitude], area_radius, DISTANCE_UNIT) + .near([area.latitude, area.longitude], area_radius, user.safe_settings.distance_unit) .order(timestamp: :asc) # check if all points within the area are assigned to a visit diff --git a/app/services/users/safe_settings.rb b/app/services/users/safe_settings.rb index dab0a516..13275332 100644 --- a/app/services/users/safe_settings.rb +++ b/app/services/users/safe_settings.rb @@ -24,7 +24,8 @@ class Users::SafeSettings immich_api_key: immich_api_key, photoprism_url: photoprism_url, photoprism_api_key: photoprism_api_key, - maps: maps + maps: maps, + distance_unit: distance_unit } end # rubocop:enable Metrics/MethodLength @@ -90,4 +91,8 @@ class Users::SafeSettings def maps settings['maps'] || {} end + + def distance_unit + settings['distance_unit'] || 'km' + end end diff --git a/app/views/stats/_stat.html.erb b/app/views/stats/_stat.html.erb index 2aa333c2..3b9b4802 100644 --- a/app/views/stats/_stat.html.erb +++ b/app/views/stats/_stat.html.erb @@ -12,7 +12,7 @@ <%= link_to '🔄', update_year_month_stats_path(stat.year, stat.month), data: { turbo_method: :put }, class: 'text-sm text-gray-500 hover:underline' %> -

<%= number_with_delimiter stat.distance %><%= DISTANCE_UNIT %>

+

<%= number_with_delimiter stat.distance %><%= current_user.safe_settings.distance_unit %>

<% if DawarichSettings.reverse_geocoding_enabled? %>
<%= countries_and_cities_stat_for_month(stat) %> @@ -22,7 +22,7 @@ <%= column_chart( stat.daily_distance, height: '100px', - suffix: " #{DISTANCE_UNIT}", + suffix: " #{current_user.safe_settings.distance_unit}", xtitle: 'Days', ytitle: 'Distance' ) %> diff --git a/app/views/stats/_year.html.erb b/app/views/stats/_year.html.erb index 3156a9e9..886e2c96 100644 --- a/app/views/stats/_year.html.erb +++ b/app/views/stats/_year.html.erb @@ -6,7 +6,7 @@ <%= column_chart( Stat.year_distance(year, current_user), height: '200px', - suffix: " #{DISTANCE_UNIT}", + suffix: " #{current_user.safe_settings.distance_unit}", xtitle: 'Days', ytitle: 'Distance' ) %> diff --git a/app/views/stats/index.html.erb b/app/views/stats/index.html.erb index f8a881ee..4eea46ed 100644 --- a/app/views/stats/index.html.erb +++ b/app/views/stats/index.html.erb @@ -4,7 +4,7 @@
- <%= number_with_delimiter(current_user.total_distance) %> <%= DISTANCE_UNIT %> + <%= number_with_delimiter(current_user.total_distance) %> <%= current_user.safe_settings.distance_unit %>
Total distance
@@ -39,7 +39,7 @@

<% cache [current_user, 'year_distance_stat', year], skip_digest: true do %> - <%= number_with_delimiter year_distance_stat(year, current_user) %><%= DISTANCE_UNIT %> + <%= number_with_delimiter year_distance_stat(year, current_user) %><%= current_user.safe_settings.distance_unit %> <% end %>

<% if DawarichSettings.reverse_geocoding_enabled? %> @@ -50,7 +50,7 @@ <%= column_chart( Stat.year_distance(year, current_user), height: '200px', - suffix: " #{DISTANCE_UNIT}", + suffix: " #{current_user.safe_settings.distance_unit}", xtitle: 'Days', ytitle: 'Distance' ) %> diff --git a/app/views/trips/_countries.html.erb b/app/views/trips/_countries.html.erb index ba6a0b99..9dc0adc2 100644 --- a/app/views/trips/_countries.html.erb +++ b/app/views/trips/_countries.html.erb @@ -1,10 +1,10 @@ <% if trip.countries.any? %>

- <%= "#{trip.countries.join(', ')} (#{trip.distance} #{DISTANCE_UNIT})" %> + <%= "#{trip.countries.join(', ')} (#{trip.distance} #{current_user.safe_settings.distance_unit})" %>

<% elsif trip.visited_countries.present? %>

- <%= "#{trip.visited_countries.join(', ')} (#{trip.distance} #{DISTANCE_UNIT})" %> + <%= "#{trip.visited_countries.join(', ')} (#{trip.distance} #{current_user.safe_settings.distance_unit})" %>

<% else %>

diff --git a/app/views/trips/_distance.html.erb b/app/views/trips/_distance.html.erb index 853742f1..89f0ef92 100644 --- a/app/views/trips/_distance.html.erb +++ b/app/views/trips/_distance.html.erb @@ -1,5 +1,5 @@ <% if trip.distance.present? %> - <%= trip.distance %> <%= DISTANCE_UNIT %> + <%= trip.distance %> <%= current_user.safe_settings.distance_unit %> <% else %> Calculating... diff --git a/app/views/trips/_trip.html.erb b/app/views/trips/_trip.html.erb index b694c6c3..f3145125 100644 --- a/app/views/trips/_trip.html.erb +++ b/app/views/trips/_trip.html.erb @@ -5,7 +5,7 @@ <%= trip.name %>

- <%= "#{human_date(trip.started_at)} – #{human_date(trip.ended_at)}, #{trip.distance} #{DISTANCE_UNIT}" %> + <%= "#{human_date(trip.started_at)} – #{human_date(trip.ended_at)}, #{trip.distance} #{current_user.safe_settings.distance_unit}" %>

'custom', 'url' => 'https://custom.example.com' } + maps: { 'name' => 'custom', 'url' => 'https://custom.example.com' }, + distance_unit: 'km' } ) end