mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Refactor Tracks::BulkTrackCreator to use start_at and end_at as datetime objects
This commit is contained in:
parent
244fb2b192
commit
7885374993
1 changed files with 14 additions and 6 deletions
|
|
@ -3,8 +3,8 @@
|
||||||
module Tracks
|
module Tracks
|
||||||
class BulkTrackCreator
|
class BulkTrackCreator
|
||||||
def initialize(start_at: nil, end_at: 1.day.ago.end_of_day, user_ids: [])
|
def initialize(start_at: nil, end_at: 1.day.ago.end_of_day, user_ids: [])
|
||||||
@start_at = start_at
|
@start_at = start_at&.to_datetime
|
||||||
@end_at = end_at.to_datetime
|
@end_at = end_at&.to_datetime
|
||||||
@user_ids = user_ids
|
@user_ids = user_ids
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -12,22 +12,30 @@ module Tracks
|
||||||
users.find_each do |user|
|
users.find_each do |user|
|
||||||
next if user.tracked_points.empty?
|
next if user.tracked_points.empty?
|
||||||
|
|
||||||
user_start_at = @start_at&.to_datetime || start_time(user)
|
user_start_at = start_at || start_time(user)
|
||||||
|
|
||||||
next unless user.tracked_points.where(timestamp: user_start_at.to_i..@end_at.to_i).exists?
|
next unless user.tracked_points.where(timestamp: user_start_at.to_i..end_at.to_i).exists?
|
||||||
|
|
||||||
Tracks::CreateJob.perform_later(user.id, start_at: user_start_at, end_at: @end_at, cleaning_strategy: :daily)
|
Tracks::CreateJob.perform_later(
|
||||||
|
user.id,
|
||||||
|
start_at: user_start_at,
|
||||||
|
end_at:,
|
||||||
|
cleaning_strategy: :daily
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
attr_reader :start_at, :end_at, :user_ids
|
||||||
|
|
||||||
def users
|
def users
|
||||||
@user_ids.any? ? User.active.where(id: @user_ids) : User.active
|
user_ids.any? ? User.active.where(id: user_ids) : User.active
|
||||||
end
|
end
|
||||||
|
|
||||||
def start_time(user)
|
def start_time(user)
|
||||||
latest_track = user.tracks.order(end_at: :desc).first
|
latest_track = user.tracks.order(end_at: :desc).first
|
||||||
|
|
||||||
if latest_track
|
if latest_track
|
||||||
latest_track.end_at
|
latest_track.end_at
|
||||||
else
|
else
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue