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 @@ +
+
+ <%= number_with_delimiter current_user.total_reverse_geocoded %> +
+
Reverse geocoded points
+
+ +
+
+ <%= number_with_delimiter current_user.total_countries %> +
+
Countries visited
+ + + + + +
+ +
+
+ <%= current_user.total_cities %> +
+
Cities visited
+ + + + +
diff --git a/app/views/stats/_stat.html.erb b/app/views/stats/_stat.html.erb index cb5c9a12..f833d209 100644 --- a/app/views/stats/_stat.html.erb +++ b/app/views/stats/_stat.html.erb @@ -8,7 +8,7 @@

<%= stat.distance %>km

<% if REVERSE_GEOCODING_ENABLED %>
- <%= stat.toponyms.count %> countries, <%= stat.toponyms.sum { _1['cities'].count } %> cities + <%= countries_and_cities_stat_for_month(stat) %>
<% end %> <% if stat.daily_distance %> diff --git a/app/views/stats/index.html.erb b/app/views/stats/index.html.erb index 88438564..9aeafb4c 100644 --- a/app/views/stats/index.html.erb +++ b/app/views/stats/index.html.erb @@ -9,59 +9,13 @@
- <%= number_with_delimiter current_user.tracked_points.without_raw_data.count(:id) %> + <%= number_with_delimiter current_user.tracked_points.count %>
Geopoints tracked
<% if REVERSE_GEOCODING_ENABLED %> -
-
- <%= number_with_delimiter current_user.total_reverse_geocoded %> -
-
Reverse geocoded points
-
- -
-
- <%= number_with_delimiter current_user.total_countries %> -
-
Countries visited
- - - - - -
- -
-
- <%= current_user.total_cities %> -
-
Cities visited
- - - - -
+ <%= render 'stats/reverse_geocoding_stats' %> <% end %> @@ -78,13 +32,11 @@

<% 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 %>
- <% cache [current_user, 'countries_and_cities_stat', year], skip_digest: true do %> - <%= countries_and_cities_stat(year, current_user) %> - <% end %> + <%= countries_and_cities_stat_for_year(year, stats) %>
<% end %> <%= column_chart( diff --git a/spec/requests/export_spec.rb b/spec/requests/export_spec.rb index 3b6ccbb9..7fc75992 100644 --- a/spec/requests/export_spec.rb +++ b/spec/requests/export_spec.rb @@ -3,16 +3,13 @@ require 'rails_helper' RSpec.describe 'Exports', type: :request do - describe 'GET /create' do + describe 'GET /download' do before do - stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags') - .to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {}) - sign_in create(:user) end it 'returns http success' do - get '/export' + get '/export/download' expect(response).to have_http_status(:success) end diff --git a/swagger/v1/swagger.yaml b/swagger/v1/swagger.yaml index 3722d11b..d7ba37fa 100644 --- a/swagger/v1/swagger.yaml +++ b/swagger/v1/swagger.yaml @@ -180,7 +180,7 @@ paths: lat: 52.502397 lon: 13.356718 tid: Swagger - tst: 1717062606 + tst: 1717786543 servers: - url: http://{defaultHost} variables: