diff --git a/.app_version b/.app_version index 26acbf08..54d1a4f2 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.12.2 +0.13.0 diff --git a/.env.development b/.env.development index 08a48784..3f83ed07 100644 --- a/.env.development +++ b/.env.development @@ -5,3 +5,4 @@ DATABASE_NAME=dawarich_development DATABASE_PORT=5432 REDIS_URL=redis://localhost:6379/1 PHOTON_API_HOST='photon.komoot.io' +DISTANCE_UNIT='mi' diff --git a/CHANGELOG.md b/CHANGELOG.md index 99feeae7..f59c7409 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.13.0] — 2024-08-28 + +### Added + +- Support for miles. To switch to miles, provide `DISTANCE_UNIT` environment variable with value `mi` in the `docker-compose.yml` file. Default value is `km`. + +It's recommended to update your stats manually after changing the `DISTANCE_UNIT` environment variable. You can do this by clicking the "Update stats" button on the Stats page. + ## [0.12.2] — 2024-08-28 ### Added diff --git a/app/controllers/map_controller.rb b/app/controllers/map_controller.rb index be255f08..0dfef36b 100644 --- a/app/controllers/map_controller.rb +++ b/app/controllers/map_controller.rb @@ -35,7 +35,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: :km) + @distance += Geocoder::Calculations.distance_between( + [_1[0], _1[1]], [_2[0], _2[1]], units: DISTANCE_UNIT.to_sym + ) end @distance.round(1) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f0531cc0..fd5b86e2 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -50,7 +50,8 @@ module ApplicationHelper "#{countries} countries, #{cities} cities" end - def year_distance_stat_in_km(year, user) + def year_distance_stat(year, user) + # In km or miles, depending on the application settings (DISTANCE_UNIT) Stat.year_distance(year, user).sum { _1[1] } end @@ -81,7 +82,7 @@ module ApplicationHelper def sidebar_distance(distance) return unless distance - "#{distance} km" + "#{distance} #{DISTANCE_UNIT}" end def sidebar_points(points) diff --git a/app/models/user.rb b/app/models/user.rb index a7241596..806b3b2f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -35,7 +35,8 @@ class User < ApplicationRecord .compact end - def total_km + def total_distance + # In km or miles, depending on the application settings (DISTANCE_UNIT) stats.sum(:distance) end diff --git a/app/services/areas/visits/create.rb b/app/services/areas/visits/create.rb index 5bec87c6..115525bd 100644 --- a/app/services/areas/visits/create.rb +++ b/app/services/areas/visits/create.rb @@ -30,10 +30,15 @@ class Areas::Visits::Create end def area_points(area) - area_radius_in_km = area.radius / 1000.0 + area_radius = + if DISTANCE_UNIT.to_sym == :km + area.radius / 1000.0 + else + area.radius / 1609.344 + end points = Point.where(user_id: user.id) - .near([area.latitude, area.longitude], area_radius_in_km) + .near([area.latitude, area.longitude], area_radius, units: DISTANCE_UNIT.to_sym) .order(timestamp: :asc) # check if all points within the area are assigned to a visit diff --git a/app/services/create_stats.rb b/app/services/create_stats.rb index 92e903d7..b245ee0a 100644 --- a/app/services/create_stats.rb +++ b/app/services/create_stats.rb @@ -50,15 +50,15 @@ class CreateStats end def distance(points) - km = 0 + distance = 0 points.each_cons(2) do - km += Geocoder::Calculations.distance_between( - [_1.latitude, _1.longitude], [_2.latitude, _2.longitude], units: :km + distance += Geocoder::Calculations.distance_between( + [_1.latitude, _1.longitude], [_2.latitude, _2.longitude], units: DISTANCE_UNIT.to_sym ) end - km + distance end def toponyms(points) diff --git a/app/views/map/index.html.erb b/app/views/map/index.html.erb index d01c0041..3db424f9 100644 --- a/app/views/map/index.html.erb +++ b/app/views/map/index.html.erb @@ -41,9 +41,10 @@
<%= stat.distance %>km
+<%= stat.distance %><%= DISTANCE_UNIT %>
<% if REVERSE_GEOCODING_ENABLED %>- <% cache [current_user, 'year_distance_stat_in_km', year], skip_digest: true do %> - <%= number_with_delimiter year_distance_stat_in_km(year, current_user) %>km + <% cache [current_user, 'year_distance_stat', year], skip_digest: true do %> + <%= number_with_delimiter year_distance_stat(year, current_user) %><%= DISTANCE_UNIT %> <% end %>
<% if REVERSE_GEOCODING_ENABLED %> @@ -44,7 +44,7 @@ <%= column_chart( Stat.year_distance(year, current_user), height: '200px', - suffix: ' km', + suffix: " #{DISTANCE_UNIT}", xtitle: 'Days', ytitle: 'Distance' ) %> diff --git a/config/initializers/00_constants.rb b/config/initializers/00_constants.rb index 7251d114..198a7472 100644 --- a/config/initializers/00_constants.rb +++ b/config/initializers/00_constants.rb @@ -3,3 +3,4 @@ MIN_MINUTES_SPENT_IN_CITY = ENV.fetch('MIN_MINUTES_SPENT_IN_CITY', 60).to_i REVERSE_GEOCODING_ENABLED = ENV.fetch('REVERSE_GEOCODING_ENABLED', 'true') == 'true' PHOTON_API_HOST = ENV.fetch('PHOTON_API_HOST', nil) +DISTANCE_UNIT = ENV.fetch('DISTANCE_UNIT', 'km') diff --git a/config/initializers/geocoder.rb b/config/initializers/geocoder.rb index 5352049e..7dedd07a 100644 --- a/config/initializers/geocoder.rb +++ b/config/initializers/geocoder.rb @@ -2,7 +2,7 @@ settings = { timeout: 5, - units: :km, + units: DISTANCE_UNIT.to_sym, cache: Redis.new, always_raise: :all, cache_options: { diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index bd7a2c30..25421848 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -50,8 +50,8 @@ RSpec.describe User, type: :model do end end - describe '#total_km' do - subject { user.total_km } + describe '#total_distance' do + subject { user.total_distance } let!(:stat1) { create(:stat, user:, distance: 10) } let!(:stat2) { create(:stat, user:, distance: 20) }