dawarich/app/services/stats/bulk_calculator.rb

41 lines
905 B
Ruby
Raw Normal View History

2024-12-06 10:52:36 -05:00
# frozen_string_literal: true
module Stats
class BulkCalculator
def initialize(user_id)
@user_id = user_id
end
def call
months = extract_months(fetch_timestamps)
schedule_calculations(months)
end
private
attr_reader :user_id
def fetch_timestamps
last_calculated_at = Stat.where(user_id:).maximum(:updated_at)
last_calculated_at ||= DateTime.new(1970, 1, 1)
time_diff = last_calculated_at.to_i..Time.current.to_i
2025-07-22 16:41:12 -04:00
Point.where(user_id:, timestamp: time_diff).pluck(:timestamp).uniq
2024-12-06 10:52:36 -05:00
end
def extract_months(timestamps)
timestamps.group_by do |timestamp|
time = Time.zone.at(timestamp)
[time.year, time.month]
end.keys
end
def schedule_calculations(months)
months.each do |year, month|
Stats::CalculatingJob.perform_later(user_id, year, month)
end
end
end
end