-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from happy-software/backpop_playlist_deltas
Backpop PlaylistDelta
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace :backpop_playlist_deltas do | ||
desc 'Run a one-time job to back pop PlaylistSnapshot diffs into PlaylistDelta records' | ||
task :run => :environment do | ||
start_time = Time.now | ||
puts "Started at #{start_time}" | ||
PlaylistSnapshot.all.each do |snapshot| | ||
next if snapshot.playlist_delta.present? | ||
|
||
previous_snapshot = PlaylistSnapshot.where(playlist_id: snapshot.tracked_playlist.playlist_id).where("created_at < ?", snapshot.created_at).newest | ||
next unless previous_snapshot.present? | ||
|
||
current_playlist_items = snapshot.playlist_items | ||
previous_playlist_items = PlaylistSnapshot.get_working_songs(previous_snapshot.playlist_items) | ||
diff = PlaylistDifferenceCalculator.calculate_diffs(current_playlist_items, previous_playlist_items) | ||
next unless diff.any_changes? | ||
|
||
PlaylistDelta.create!( | ||
added: diff.added_songs, | ||
removed: diff.removed_songs, | ||
playlist_snapshot: snapshot, | ||
tracked_playlist: snapshot.tracked_playlist, | ||
) | ||
end | ||
|
||
finish_time = Time.now | ||
puts "Finished at: #{finish_time}" | ||
puts "Total Runtime: #{finish_time - start_time}s" | ||
end | ||
end |