Add DISTANCE_UNIT environment variable

This commit is contained in:
Eugene Burmakin 2024-08-28 23:54:00 +02:00
parent 0cc5ac0860
commit 5d14b406bd
15 changed files with 42 additions and 22 deletions

View file

@ -1 +1 @@
0.12.2
0.13.0

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -41,9 +41,10 @@
<div
class="w-full"
data-controller="maps"
data-distance_unit="<%= DISTANCE_UNIT %>"
data-api_key="<%= current_user.api_key %>"
data-user_settings=<%= current_user.settings.to_json %>
data-controller="maps"
data-coordinates="<%= @coordinates %>"
data-timezone="<%= Rails.configuration.time_zone %>">
<div data-maps-target="container" class="h-[25rem] w-auto min-h-screen">

View file

@ -5,7 +5,7 @@
<%= "#{Date::MONTHNAMES[stat.month]} of #{stat.year}" %>
<% end %>
</h2>
<p><%= stat.distance %>km</p>
<p><%= stat.distance %><%= DISTANCE_UNIT %></p>
<% if REVERSE_GEOCODING_ENABLED %>
<div class="card-actions justify-end">
<%= countries_and_cities_stat_for_month(stat) %>
@ -15,7 +15,7 @@
<%= column_chart(
stat.daily_distance,
height: '100px',
suffix: ' km',
suffix: " #{DISTANCE_UNIT}",
xtitle: 'Days',
ytitle: 'Distance'
) %>

View file

@ -6,7 +6,7 @@
<%= column_chart(
Stat.year_distance(year, current_user),
height: '200px',
suffix: ' km',
suffix: " #{DISTANCE_UNIT}",
xtitle: 'Days',
ytitle: 'Distance'
) %>

View file

@ -4,7 +4,7 @@
<div class="stats stats-vertical lg:stats-horizontal shadow w-full bg-base-200">
<div class="stat text-center">
<div class="stat-value text-primary">
<%= number_with_delimiter(current_user.total_km) %> km
<%= number_with_delimiter(current_user.total_distance) %> <%= DISTANCE_UNIT %>
</div>
<div class="stat-title">Total distance</div>
</div>
@ -32,8 +32,8 @@
<%= link_to '[Map]', map_url(year_timespan(year)), class: 'underline hover:no-underline' %>
</h2>
<p>
<% 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 %>
</p>
<% 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'
) %>

View file

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

View file

@ -2,7 +2,7 @@
settings = {
timeout: 5,
units: :km,
units: DISTANCE_UNIT.to_sym,
cache: Redis.new,
always_raise: :all,
cache_options: {

View file

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