Skip to content

Commit

Permalink
Update Visits::Calculate to return only one visit per city per day
Browse files Browse the repository at this point in the history
  • Loading branch information
Freika committed Jun 25, 2024
1 parent f0e94f3 commit 7844bf1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/controllers/map_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class MapController < ApplicationController
def index
@points = current_user.tracked_points.without_raw_data.where('timestamp >= ? AND timestamp <= ?', start_at, end_at).order(timestamp: :asc)

@countries_and_cities = CountriesAndCities.new(@points).call
@countries_and_cities = Visits::Calculate.new(@points).uniq_visits
@coordinates =
@points.pluck(:latitude, :longitude, :battery, :altitude, :timestamp, :velocity, :id)
.map { [_1.to_f, _2.to_f, _3.to_s, _4.to_s, _5.to_s, _6.to_s, _7] }
Expand Down
7 changes: 7 additions & 0 deletions app/services/visits/calculate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ def call
normalize_result(city_visits)
end

def uniq_visits
# Only one visit per city per day
call.flat_map do |country|
{ country: country[:country], cities: country[:cities].uniq { [_1[:city], Time.at(_1[:timestamp]).to_date] } }
end
end

private

attr_reader :points
Expand Down
1 change: 1 addition & 0 deletions app/views/shared/_right_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

<% if REVERSE_GEOCODING_ENABLED && @countries_and_cities&.any? %>
<hr class='my-5'>
<h2 class='text-lg font-semibold'>Countries and cities</h2>
<% @countries_and_cities.each do |country| %>
<% next if country[:cities].empty? %>

Expand Down

0 comments on commit 7844bf1

Please sign in to comment.