diff --git a/app/controllers/api/v1/countries/visited_cities_controller.rb b/app/controllers/api/v1/countries/visited_cities_controller.rb index 85e53f7d..5af80348 100644 --- a/app/controllers/api/v1/countries/visited_cities_controller.rb +++ b/app/controllers/api/v1/countries/visited_cities_controller.rb @@ -8,7 +8,7 @@ class Api::V1::Countries::VisitedCitiesController < ApiController end_at = DateTime.parse(params[:end_at]).to_i points = current_api_user - .tracked_points + .points .where(timestamp: start_at..end_at) render json: { data: CountriesAndCities.new(points).call } diff --git a/app/controllers/api/v1/points_controller.rb b/app/controllers/api/v1/points_controller.rb index 505bb123..6dd2cf93 100644 --- a/app/controllers/api/v1/points_controller.rb +++ b/app/controllers/api/v1/points_controller.rb @@ -10,7 +10,7 @@ class Api::V1::PointsController < ApiController order = params[:order] || 'desc' points = current_api_user - .tracked_points + .points .where(timestamp: start_at..end_at) .order(timestamp: order) .page(params[:page]) @@ -31,7 +31,7 @@ class Api::V1::PointsController < ApiController end def update - point = current_api_user.tracked_points.find(params[:id]) + point = current_api_user.points.find(params[:id]) point.update(lonlat: "POINT(#{point_params[:longitude]} #{point_params[:latitude]})") @@ -39,7 +39,7 @@ class Api::V1::PointsController < ApiController end def destroy - point = current_api_user.tracked_points.find(params[:id]) + point = current_api_user.points.find(params[:id]) point.destroy render json: { message: 'Point deleted successfully' } diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index f77c7a54..27453c76 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -6,6 +6,6 @@ class HomeController < ApplicationController redirect_to map_url if current_user - @points = current_user.tracked_points.without_raw_data if current_user + @points = current_user.points.without_raw_data if current_user end end diff --git a/app/controllers/map_controller.rb b/app/controllers/map_controller.rb index 82d9435f..bbb308bb 100644 --- a/app/controllers/map_controller.rb +++ b/app/controllers/map_controller.rb @@ -88,6 +88,6 @@ class MapController < ApplicationController end def points_from_user - current_user.tracked_points.without_raw_data.order(timestamp: :asc) + current_user.points.without_raw_data.order(timestamp: :asc) end end diff --git a/app/controllers/points_controller.rb b/app/controllers/points_controller.rb index a78c97c4..65d99698 100644 --- a/app/controllers/points_controller.rb +++ b/app/controllers/points_controller.rb @@ -24,7 +24,7 @@ class PointsController < ApplicationController alert: 'No points selected.', status: :see_other and return if point_ids.blank? - current_user.tracked_points.where(id: point_ids).destroy_all + current_user.points.where(id: point_ids).destroy_all redirect_to points_url(preserved_params), notice: 'Points were successfully destroyed.', @@ -58,7 +58,7 @@ class PointsController < ApplicationController end def user_points - current_user.tracked_points + current_user.points end def order_by diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 5fdcd917..2fb02162 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -86,7 +86,7 @@ module ApplicationHelper end def points_exist?(year, month, user) - user.tracked_points.where( + user.points.where( timestamp: DateTime.new(year, month).beginning_of_month..DateTime.new(year, month).end_of_month ).exists? end diff --git a/app/jobs/bulk_visits_suggesting_job.rb b/app/jobs/bulk_visits_suggesting_job.rb index 4384be6a..e52b06da 100644 --- a/app/jobs/bulk_visits_suggesting_job.rb +++ b/app/jobs/bulk_visits_suggesting_job.rb @@ -18,7 +18,7 @@ class BulkVisitsSuggestingJob < ApplicationJob users.active.find_each do |user| next unless user.safe_settings.visits_suggestions_enabled? - next if user.tracked_points.empty? + next unless user.points_count.positive? schedule_chunked_jobs(user, time_chunks) end diff --git a/app/jobs/data_migrations/migrate_points_latlon_job.rb b/app/jobs/data_migrations/migrate_points_latlon_job.rb index 90dfa096..a74be609 100644 --- a/app/jobs/data_migrations/migrate_points_latlon_job.rb +++ b/app/jobs/data_migrations/migrate_points_latlon_job.rb @@ -7,7 +7,7 @@ class DataMigrations::MigratePointsLatlonJob < ApplicationJob user = User.find(user_id) # rubocop:disable Rails/SkipsModelValidations - user.tracked_points.update_all('lonlat = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)') + user.points.update_all('lonlat = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)') # rubocop:enable Rails/SkipsModelValidations end end diff --git a/app/jobs/data_migrations/prefill_points_counter_cache_job.rb b/app/jobs/data_migrations/prefill_points_counter_cache_job.rb new file mode 100644 index 00000000..2a860c58 --- /dev/null +++ b/app/jobs/data_migrations/prefill_points_counter_cache_job.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class DataMigrations::PrefillPointsCounterCacheJob < ApplicationJob + queue_as :data_migrations + + def perform(user_id = nil) + if user_id + prefill_counter_for_user(user_id) + else + User.find_each(batch_size: 100) do |user| + prefill_counter_for_user(user.id) + end + end + end + + private + + def prefill_counter_for_user(user_id) + user = User.find(user_id) + points_count = user.points.count + + User.where(id: user_id).update_all(points_count: points_count) + + Rails.logger.info "Updated points_count for user #{user_id}: #{points_count}" + rescue ActiveRecord::RecordNotFound + Rails.logger.warn "User #{user_id} not found, skipping counter cache update" + end +end diff --git a/app/jobs/tracks/cleanup_job.rb b/app/jobs/tracks/cleanup_job.rb index 82eae62d..ad1a8e29 100644 --- a/app/jobs/tracks/cleanup_job.rb +++ b/app/jobs/tracks/cleanup_job.rb @@ -23,9 +23,9 @@ class Tracks::CleanupJob < ApplicationJob private def users_with_old_untracked_points(older_than) - User.active.joins(:tracked_points) - .where(tracked_points: { track_id: nil, timestamp: ..older_than.to_i }) - .having('COUNT(tracked_points.id) >= 2') # Only users with enough points for tracks + User.active.joins(:points) + .where(points: { track_id: nil, timestamp: ..older_than.to_i }) + .having('COUNT(points.id) >= 2') # Only users with enough points for tracks .group(:id) end end diff --git a/app/models/point.rb b/app/models/point.rb index ef00e99b..69e87681 100644 --- a/app/models/point.rb +++ b/app/models/point.rb @@ -6,7 +6,7 @@ class Point < ApplicationRecord belongs_to :import, optional: true, counter_cache: true belongs_to :visit, optional: true - belongs_to :user + belongs_to :user, counter_cache: true belongs_to :country, optional: true belongs_to :track, optional: true diff --git a/app/models/stat.rb b/app/models/stat.rb index 2cf26d04..c69be6d0 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -24,7 +24,7 @@ class Stat < ApplicationRecord end def points - user.tracked_points + user.points .without_raw_data .where(timestamp: timespan) .order(timestamp: :asc) diff --git a/app/models/trip.rb b/app/models/trip.rb index e409a47b..fca5e1e2 100644 --- a/app/models/trip.rb +++ b/app/models/trip.rb @@ -18,7 +18,7 @@ class Trip < ApplicationRecord end def points - user.tracked_points.where(timestamp: started_at.to_i..ended_at.to_i).order(:timestamp) + user.points.where(timestamp: started_at.to_i..ended_at.to_i).order(:timestamp) end def photo_previews diff --git a/app/models/user.rb b/app/models/user.rb index 3f4046a0..a6cdc146 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,14 +4,13 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :trackable - has_many :tracked_points, class_name: 'Point', dependent: :destroy + has_many :points, dependent: :destroy, counter_cache: true has_many :imports, dependent: :destroy has_many :stats, dependent: :destroy has_many :exports, dependent: :destroy has_many :notifications, dependent: :destroy has_many :areas, dependent: :destroy has_many :visits, dependent: :destroy - has_many :points, through: :imports has_many :places, through: :visits has_many :trips, dependent: :destroy has_many :tracks, dependent: :destroy @@ -35,7 +34,7 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength end def countries_visited - tracked_points + points .where.not(country_name: [nil, '']) .distinct .pluck(:country_name) @@ -43,7 +42,7 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength end def cities_visited - tracked_points.where.not(city: [nil, '']).distinct.pluck(:city).compact + points.where.not(city: [nil, '']).distinct.pluck(:city).compact end def total_distance @@ -60,11 +59,11 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength end def total_reverse_geocoded_points - tracked_points.where.not(reverse_geocoded_at: nil).count + points.where.not(reverse_geocoded_at: nil).count end def total_reverse_geocoded_points_without_data - tracked_points.where(geodata: {}).count + points.where(geodata: {}).count end def immich_integration_configured? @@ -118,7 +117,7 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength end def trial_state? - tracked_points.none? && trial? + points_count.zero? && trial? end private diff --git a/app/serializers/stats_serializer.rb b/app/serializers/stats_serializer.rb index 3a35f157..ae66afbf 100644 --- a/app/serializers/stats_serializer.rb +++ b/app/serializers/stats_serializer.rb @@ -10,7 +10,7 @@ class StatsSerializer def call { totalDistanceKm: total_distance_km, - totalPointsTracked: user.tracked_points.count, + totalPointsTracked: user.points_count, totalReverseGeocodedPoints: reverse_geocoded_points, totalCountriesVisited: user.countries_visited.count, totalCitiesVisited: user.cities_visited.count, @@ -27,7 +27,7 @@ class StatsSerializer end def reverse_geocoded_points - user.tracked_points.reverse_geocoded.count + user.points.reverse_geocoded.count end def yearly_stats diff --git a/app/services/exports/create.rb b/app/services/exports/create.rb index d885afb8..0c2feb76 100644 --- a/app/services/exports/create.rb +++ b/app/services/exports/create.rb @@ -35,7 +35,7 @@ class Exports::Create def time_framed_points user - .tracked_points + .points .where(timestamp: start_at.to_i..end_at.to_i) .order(timestamp: :asc) end diff --git a/app/services/jobs/create.rb b/app/services/jobs/create.rb index d4176652..114a6cf4 100644 --- a/app/services/jobs/create.rb +++ b/app/services/jobs/create.rb @@ -14,9 +14,9 @@ class Jobs::Create points = case job_name when 'start_reverse_geocoding' - user.tracked_points + user.points when 'continue_reverse_geocoding' - user.tracked_points.not_reverse_geocoded + user.points.not_reverse_geocoded else raise InvalidJobName, 'Invalid job name' end diff --git a/app/services/points_limit_exceeded.rb b/app/services/points_limit_exceeded.rb index 2bf8de8a..21cb802a 100644 --- a/app/services/points_limit_exceeded.rb +++ b/app/services/points_limit_exceeded.rb @@ -9,7 +9,7 @@ class PointsLimitExceeded return false if DawarichSettings.self_hosted? Rails.cache.fetch(cache_key, expires_in: 1.day) do - @user.tracked_points.count >= points_limit + @user.points_count >= points_limit end end diff --git a/app/services/stats/calculate_month.rb b/app/services/stats/calculate_month.rb index 91c2a1d7..d05bafb3 100644 --- a/app/services/stats/calculate_month.rb +++ b/app/services/stats/calculate_month.rb @@ -47,7 +47,7 @@ class Stats::CalculateMonth return @points if defined?(@points) @points = user - .tracked_points + .points .without_raw_data .where(timestamp: start_timestamp..end_timestamp) .select(:lonlat, :timestamp) @@ -60,7 +60,7 @@ class Stats::CalculateMonth def toponyms toponym_points = user - .tracked_points + .points .without_raw_data .where(timestamp: start_timestamp..end_timestamp) .select(:city, :country_name) diff --git a/app/services/tracks/generator.rb b/app/services/tracks/generator.rb index be22d021..1993477f 100644 --- a/app/services/tracks/generator.rb +++ b/app/services/tracks/generator.rb @@ -86,7 +86,7 @@ class Tracks::Generator end def load_bulk_points - scope = user.tracked_points.order(:timestamp) + scope = user.points.order(:timestamp) scope = scope.where(timestamp: timestamp_range) if time_range_defined? scope @@ -95,7 +95,7 @@ class Tracks::Generator def load_incremental_points # For incremental mode, we process untracked points # If end_at is specified, only process points up to that time - scope = user.tracked_points.where(track_id: nil).order(:timestamp) + scope = user.points.where(track_id: nil).order(:timestamp) scope = scope.where(timestamp: ..end_at.to_i) if end_at.present? scope @@ -104,7 +104,7 @@ class Tracks::Generator def load_daily_points day_range = daily_time_range - user.tracked_points.where(timestamp: day_range).order(:timestamp) + user.points.where(timestamp: day_range).order(:timestamp) end def create_track_from_segment(segment_data) @@ -195,8 +195,8 @@ class Tracks::Generator def bulk_timestamp_range return [start_at.to_i, end_at.to_i] if start_at && end_at - first_point = user.tracked_points.order(:timestamp).first - last_point = user.tracked_points.order(:timestamp).last + first_point = user.points.order(:timestamp).first + last_point = user.points.order(:timestamp).last [first_point&.timestamp || 0, last_point&.timestamp || Time.current.to_i] end @@ -207,7 +207,7 @@ class Tracks::Generator end def incremental_timestamp_range - first_point = user.tracked_points.where(track_id: nil).order(:timestamp).first + first_point = user.points.where(track_id: nil).order(:timestamp).first end_timestamp = end_at ? end_at.to_i : Time.current.to_i [first_point&.timestamp || 0, end_timestamp] diff --git a/app/services/tracks/incremental_processor.rb b/app/services/tracks/incremental_processor.rb index 62c1faed..f02305a8 100644 --- a/app/services/tracks/incremental_processor.rb +++ b/app/services/tracks/incremental_processor.rb @@ -50,7 +50,7 @@ class Tracks::IncrementalProcessor def find_previous_point @previous_point ||= - user.tracked_points + user.points .where('timestamp < ?', new_point.timestamp) .order(:timestamp) .last diff --git a/app/services/users/export_data.rb b/app/services/users/export_data.rb index dbe4f33b..7ebbf0a1 100644 --- a/app/services/users/export_data.rb +++ b/app/services/users/export_data.rb @@ -331,7 +331,7 @@ class Users::ExportData trips: user.trips.count, stats: user.stats.count, notifications: user.notifications.count, - points: user.tracked_points.count, + points: user.points_count, visits: user.visits.count, places: user.places.count } diff --git a/app/services/visits/smart_detect.rb b/app/services/visits/smart_detect.rb index 64d66440..828c364d 100644 --- a/app/services/visits/smart_detect.rb +++ b/app/services/visits/smart_detect.rb @@ -13,7 +13,7 @@ module Visits @user = user @start_at = start_at.to_i @end_at = end_at.to_i - @points = user.tracked_points.not_visited + @points = user.points.not_visited .order(timestamp: :asc) .where(timestamp: start_at..end_at) end diff --git a/app/services/visits/suggest.rb b/app/services/visits/suggest.rb index 7aab6b93..b40853fb 100644 --- a/app/services/visits/suggest.rb +++ b/app/services/visits/suggest.rb @@ -6,7 +6,7 @@ class Visits::Suggest def initialize(user, start_at:, end_at:) @start_at = start_at.to_i @end_at = end_at.to_i - @points = user.tracked_points.not_visited.order(timestamp: :asc).where(timestamp: start_at..end_at) + @points = user.points.not_visited.order(timestamp: :asc).where(timestamp: start_at..end_at) @user = user end diff --git a/app/views/devise/registrations/_points_usage.html.erb b/app/views/devise/registrations/_points_usage.html.erb index c079b93a..68880d0d 100644 --- a/app/views/devise/registrations/_points_usage.html.erb +++ b/app/views/devise/registrations/_points_usage.html.erb @@ -1,6 +1,6 @@
- You have used <%= number_with_delimiter(current_user.tracked_points.count) %> points of <%= number_with_delimiter(DawarichSettings::BASIC_PAID_PLAN_LIMIT) %> available. + You have used <%= number_with_delimiter(current_user.points_count) %> points of <%= number_with_delimiter(DawarichSettings::BASIC_PAID_PLAN_LIMIT) %> available.
- + diff --git a/app/views/settings/users/index.html.erb b/app/views/settings/users/index.html.erb index 90cb21e8..c4c6aea0 100644 --- a/app/views/settings/users/index.html.erb +++ b/app/views/settings/users/index.html.erb @@ -24,7 +24,7 @@