From 10777714b107f9abe8fc3ab5271128f3e5ab78c5 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Thu, 17 Jul 2025 19:19:50 +0200 Subject: [PATCH] Clean up a bit --- app/jobs/tracks/cleanup_job.rb | 5 ----- app/services/tracks/generator.rb | 21 +++++++------------ ...0250704185707_create_tracks_from_points.rb | 3 +-- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/app/jobs/tracks/cleanup_job.rb b/app/jobs/tracks/cleanup_job.rb index f9dc9c4e..82eae62d 100644 --- a/app/jobs/tracks/cleanup_job.rb +++ b/app/jobs/tracks/cleanup_job.rb @@ -1,11 +1,6 @@ # frozen_string_literal: true # Lightweight cleanup job that runs weekly to catch any missed track generation. -# This replaces the daily bulk creation job with a more targeted approach. -# -# Instead of processing all users daily, this job only processes users who have -# untracked points that are older than a threshold (e.g., 1 day), indicating -# they may have been missed by incremental processing. # # This provides a safety net while avoiding the overhead of daily bulk processing. class Tracks::CleanupJob < ApplicationJob diff --git a/app/services/tracks/generator.rb b/app/services/tracks/generator.rb index ac599b59..765253a8 100644 --- a/app/services/tracks/generator.rb +++ b/app/services/tracks/generator.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -# Simplified track generation service that replaces the complex strategy pattern. -# # This service handles both bulk and incremental track generation using a unified # approach with different modes: # @@ -9,10 +7,6 @@ # - :incremental - Processes untracked points up to a specified end time # - :daily - Processes tracks on a daily basis # -# The service maintains the same core logic as the original system but simplifies -# the architecture by removing the multiple strategy classes in favor of -# mode-based configuration. -# # Key features: # - Deterministic results (same algorithm for all modes) # - Simple incremental processing without buffering complexity @@ -62,9 +56,7 @@ class Tracks::Generator def should_clean_tracks? case mode - when :bulk then true - when :daily then true - when :incremental then false + when :bulk, :daily then true else false end end @@ -82,6 +74,7 @@ class Tracks::Generator def load_bulk_points scope = user.tracked_points.order(:timestamp) scope = scope.where(timestamp: time_range) if time_range_defined? + scope end @@ -90,11 +83,13 @@ class Tracks::Generator # If end_at is specified, only process points up to that time scope = user.tracked_points.where(track_id: nil).order(:timestamp) scope = scope.where(timestamp: ..end_at.to_i) if end_at.present? + scope end def load_daily_points day_range = daily_time_range + user.tracked_points.where(timestamp: day_range).order(:timestamp) end @@ -128,10 +123,10 @@ class Tracks::Generator def clean_existing_tracks case mode - when :bulk - clean_bulk_tracks - when :daily - clean_daily_tracks + when :bulk then clean_bulk_tracks + when :daily then clean_daily_tracks + else + raise ArgumentError, "Unknown mode: #{mode}" end end diff --git a/db/data/20250704185707_create_tracks_from_points.rb b/db/data/20250704185707_create_tracks_from_points.rb index fd744de9..7d5cffb5 100644 --- a/db/data/20250704185707_create_tracks_from_points.rb +++ b/db/data/20250704185707_create_tracks_from_points.rb @@ -15,12 +15,11 @@ class CreateTracksFromPoints < ActiveRecord::Migration[8.0] # Use explicit parameters for bulk historical processing: # - No time limits (start_at: nil, end_at: nil) = process ALL historical data - # - Replace strategy = clean slate, removes any existing tracks first Tracks::CreateJob.perform_later( user.id, start_at: nil, end_at: nil, - mode: :daily + mode: :bulk ) processed_users += 1