Skip to content

Commit

Permalink
Merge pull request #111 from happy-software/playlist_deltas
Browse files Browse the repository at this point in the history
Store a `PlaylistDelta` each time we take a snapshot
  • Loading branch information
hebron-george authored Nov 7, 2024
2 parents 3c7f81a + 1e235f5 commit 75cfcbf
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
8 changes: 8 additions & 0 deletions app/models/playlist_delta.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class PlaylistDelta < ApplicationRecord
belongs_to :playlist_snapshot
belongs_to :tracked_playlist

def change_datetime
playlist_snapshot.created_at
end
end
11 changes: 10 additions & 1 deletion app/models/playlist_snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class PlaylistSnapshot < ApplicationRecord
belongs_to :tracked_playlist, foreign_key: :playlist_id, primary_key: :playlist_id
has_one :playlist_delta

BROKEN_STATUSES = ['Deleted video', 'Private video']

def self.capture_all_tracked_playlists!
Expand All @@ -16,7 +18,14 @@ def self.capture_all_tracked_playlists!
diff = PlaylistDifferenceCalculator.calculate_diffs(current_playlist_items, previous_playlist_items)

if diff.any_changes?
PlaylistSnapshot.create!(playlist_id: tp.playlist_id, playlist_items: current_playlist_items)
snapshot = PlaylistSnapshot.create!(playlist_id: tp.playlist_id, playlist_items: current_playlist_items)
PlaylistDelta.create!(
added: diff.added_songs,
removed: diff.removed_songs,
playlist_snapshot: snapshot,
tracked_playlist: snapshot.tracked_playlist,
)

playlist_name = TrackedPlaylist.find_by_playlist_id(tp.playlist_id)&.name
PlaylistDifferenceRenderer.post_diff(diff, tp.playlist_id, playlist_name)
end
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20241107022348_create_playlist_delta.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreatePlaylistDelta < ActiveRecord::Migration[6.1]
def change
create_table :playlist_delta do |t|
t.references :tracked_playlist, null: false, foreign_key: true, index: true
t.references :playlist_snapshot, null: false, foreign_key: true, index: true
t.jsonb :added, default: {}, null: false
t.jsonb :removed, default: {}, null: false

t.timestamps
end
end
end
43 changes: 20 additions & 23 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,28 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_07_16_224501) do
ActiveRecord::Schema.define(version: 2024_11_07_022348) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "plpgsql"

create_table "playlist_delta", force: :cascade do |t|
t.bigint "tracked_playlist_id", null: false
t.bigint "playlist_snapshot_id", null: false
t.jsonb "added", default: {}, null: false
t.jsonb "removed", default: {}, null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["playlist_snapshot_id"], name: "index_playlist_delta_on_playlist_snapshot_id"
t.index ["tracked_playlist_id"], name: "index_playlist_delta_on_tracked_playlist_id"
end

create_table "playlist_items", id: false, force: :cascade do |t|
t.integer "id", null: false
t.integer "playlist_snapshot_id"
end

create_table "playlist_settings", force: :cascade do |t|
t.string "playlist_id"
t.jsonb "settings"
Expand All @@ -30,28 +47,6 @@
t.index ["playlist_id"], name: "index_playlist_snapshots_on_playlist_id"
end

create_table "playlists", force: :cascade do |t|
t.string "channel_username"
t.string "channel_id"
t.string "youtube_id"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["youtube_id"], name: "index_playlists_on_youtube_id", unique: true
end

create_table "songs", force: :cascade do |t|
t.string "video_id"
t.string "title"
t.string "uploader"
t.datetime "video_uploaded_date"
t.jsonb "description"
t.string "playlist_id"
t.string "playlist_index"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "tracked_playlists", force: :cascade do |t|
t.string "playlist_id"
t.datetime "created_at", null: false
Expand All @@ -66,4 +61,6 @@
t.index ["playlist_id"], name: "index_tracked_playlists_on_playlist_id", unique: true
end

add_foreign_key "playlist_delta", "playlist_snapshots"
add_foreign_key "playlist_delta", "tracked_playlists"
end
5 changes: 0 additions & 5 deletions spec/models/playlist_snapshot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
allow(PlaylistDifferenceRenderer).to receive(:post_diff)
end

after do
TrackedPlaylist.all.each(&:destroy)
PlaylistSnapshot.all.each(&:destroy)
end

let(:mocked_yt_response) do
{
'song_1' => {'title'=>'song_1_title',
Expand Down

0 comments on commit 75cfcbf

Please sign in to comment.