dawarich/app/jobs/visit_suggesting_job.rb

26 lines
748 B
Ruby
Raw Normal View History

2024-08-12 16:18:11 -04:00
# frozen_string_literal: true
2024-08-05 15:23:08 -04:00
class VisitSuggestingJob < ApplicationJob
2024-09-08 10:52:35 -04:00
queue_as :visit_suggesting
sidekiq_options retry: false
2024-08-05 15:23:08 -04:00
2024-08-25 13:46:50 -04:00
def perform(user_ids: [], start_at: 1.day.ago, end_at: Time.current)
2024-08-12 16:18:11 -04:00
users = user_ids.any? ? User.where(id: user_ids) : User.all
2025-03-05 16:36:56 -05:00
start_at = start_at.to_datetime
end_at = end_at.to_datetime
2024-08-12 16:18:11 -04:00
2025-01-09 07:38:13 -05:00
users.find_each do |user|
next unless user.active?
2025-01-09 07:38:13 -05:00
next if user.tracked_points.empty?
2025-03-05 16:36:56 -05:00
# Split the time range into 24-hour chunks
# This prevents from places duplicates
time_chunks = (start_at..end_at).step(1.day).to_a
time_chunks.each do |time_chunk|
Visits::Suggest.new(user, start_at: time_chunk, end_at: time_chunk + 1.day).call
end
2025-01-09 07:38:13 -05:00
end
2024-08-05 15:23:08 -04:00
end
end