diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index c3ceacad..f1915a9c 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + class StatsController < ApplicationController before_action :authenticate_user! def index - @stats = current_user.stats.group_by(&:year).sort_by { _1 }.reverse + @stats = current_user.stats.group_by(&:year).sort.reverse end def show diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index b853d767..8d4cdb6b 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -37,10 +37,23 @@ module ApplicationHelper %w[info success warning error accent secondary primary] end - def countries_and_cities_stat(year, user) - data = Stat.year_cities_and_countries(year, user) - countries = data[:countries] - cities = data[:cities] + def countries_and_cities_stat_for_year(year, stats) + data = { countries: [], cities: [] } + + stats.select { _1.year == year }.each do + data[:countries] << _1.toponyms.flatten.map { |t| t['country'] }.uniq.compact + data[:cities] << _1.toponyms.flatten.flat_map { |t| t['cities'].map { |c| c['city'] } }.compact.uniq + end + + data[:cities].flatten!.uniq! + data[:countries].flatten!.uniq! + + "#{data[:countries].count} countries, #{data[:cities].count} cities" + end + + def countries_and_cities_stat_for_month(stat) + countries = stat.toponyms.count { _1['country'] } + cities = stat.toponyms.sum { _1['cities'].count } "#{countries} countries, #{cities} cities" end diff --git a/app/models/point.rb b/app/models/point.rb index df627fc9..f6671dba 100644 --- a/app/models/point.rb +++ b/app/models/point.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true class Point < ApplicationRecord - # self.ignored_columns = %w[raw_data] - belongs_to :import, optional: true belongs_to :user, optional: true diff --git a/app/models/stat.rb b/app/models/stat.rb index 5209f8f3..36353863 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -41,13 +41,16 @@ class Stat < ApplicationRecord end def self.year_cities_and_countries(year, user) - points = user.tracked_points.where(timestamp: DateTime.new(year).beginning_of_year..DateTime.new(year).end_of_year) + start_at = DateTime.new(year).beginning_of_year + end_at = DateTime.new(year).end_of_year + + points = user.tracked_points.without_raw_data.where(timestamp: start_at..end_at) data = CountriesAndCities.new(points).call { countries: data.map { _1[:country] }.uniq.count, - cities: data.sum { |country| country[:cities].count } + cities: data.sum { _1[:cities].count } } end diff --git a/app/serializers/export_serializer.rb b/app/serializers/export_serializer.rb index 3b9d7d2c..a5d94744 100644 --- a/app/serializers/export_serializer.rb +++ b/app/serializers/export_serializer.rb @@ -9,7 +9,7 @@ class ExportSerializer end def call - Oj.dump({ user_email => { 'dawarich-export' => export_points } }) + { user_email => { 'dawarich-export' => export_points } }.to_json end private diff --git a/app/services/countries_and_cities.rb b/app/services/countries_and_cities.rb index 7ba4fb02..026484d1 100644 --- a/app/services/countries_and_cities.rb +++ b/app/services/countries_and_cities.rb @@ -8,9 +8,7 @@ class CountriesAndCities def call grouped_records = group_points mapped_with_cities = map_with_cities(grouped_records) - filtered_cities = filter_cities(mapped_with_cities) - normalize_result(filtered_cities) end @@ -50,7 +48,7 @@ class CountriesAndCities { country:, cities: cities.map do |city, data| - { city:, points: data[:points], timestamp: data[:last_timestamp], stayed_for: data[:stayed_for]} + { city:, points: data[:points], timestamp: data[:last_timestamp], stayed_for: data[:stayed_for] } end } end diff --git a/app/views/stats/_reverse_geocoding_stats.html.erb b/app/views/stats/_reverse_geocoding_stats.html.erb new file mode 100644 index 00000000..8bdbde05 --- /dev/null +++ b/app/views/stats/_reverse_geocoding_stats.html.erb @@ -0,0 +1,47 @@ +
<%= stat.distance %>km
<% 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 - <% end %> + <% end %>
<% if REVERSE_GEOCODING_ENABLED %>