adds lifecycle columns to source and media_item tables

This commit is contained in:
Kieran Eglin 2024-02-09 20:38:03 -08:00
parent aeef2c7588
commit 3162ef492a
No known key found for this signature in database
GPG key ID: 193984967FCF432D
8 changed files with 54 additions and 3 deletions

View file

@ -15,11 +15,12 @@ defmodule Pinchflat.Media.MediaItem do
media_id
original_url
livestream
media_downloaded_at
media_filepath
source_id
subtitle_filepaths
thumbnail_filepath
metadata_filepath
source_id
)a
@required_fields ~w(title original_url livestream media_id source_id)a
@ -28,6 +29,8 @@ defmodule Pinchflat.Media.MediaItem do
field :media_id, :string
field :original_url, :string
field :livestream, :boolean, default: false
field :media_downloaded_at, :utc_datetime
field :media_filepath, :string
field :thumbnail_filepath, :string
field :metadata_filepath, :string

View file

@ -35,7 +35,11 @@ defmodule Pinchflat.MediaClient.VideoDownloader do
case download_for_media_profile(media_item.media_id, media_profile, backend) do
{:ok, parsed_json} ->
parser = metadata_parser(backend)
parsed_attrs = parser.parse_for_media_item(parsed_json)
parsed_attrs =
parsed_json
|> parser.parse_for_media_item()
|> Map.merge(%{media_downloaded_at: DateTime.utc_now()})
# Don't forgor to use preloaded associations or updates to
# associations won't work!

View file

@ -167,6 +167,10 @@ defmodule Pinchflat.MediaSource do
{:ok, source}
end
# IDEA: this uses a pattern where `kickoff_indexing_task` controls whether
# it should run based on the source, but `maybe_handle_media_tasks` handles that
# logic itself. Consider updating one or the other to be consistent (once I've
# decided which I like more)
defp maybe_run_indexing_task(changeset, source) do
case changeset.data do
# If the changeset is new (not persisted), attempt indexing no matter what

View file

@ -16,11 +16,19 @@ defmodule Pinchflat.MediaSource.Source do
friendly_name
index_frequency_minutes
download_media
last_indexed_at
original_url
media_profile_id
)a
@required_fields @allowed_fields -- ~w(index_frequency_minutes friendly_name)a
@required_fields ~w(
collection_name
collection_id
collection_type
download_media
original_url
media_profile_id
)a
schema "sources" do
field :friendly_name, :string
@ -29,6 +37,7 @@ defmodule Pinchflat.MediaSource.Source do
field :collection_type, Ecto.Enum, values: [:channel, :playlist]
field :index_frequency_minutes, :integer, default: 60 * 24
field :download_media, :boolean, default: true
field :last_indexed_at, :utc_datetime
# This should only be used for user reference going forward
# as the collection_id should be used for all API calls
field :original_url, :string

View file

@ -5,6 +5,7 @@ defmodule Pinchflat.Tasks.SourceTasks do
alias Pinchflat.Media
alias Pinchflat.Tasks
alias Pinchflat.MediaSource
alias Pinchflat.MediaSource.Source
alias Pinchflat.MediaClient.SourceDetails
alias Pinchflat.Workers.MediaIndexingWorker
@ -42,6 +43,7 @@ defmodule Pinchflat.Tasks.SourceTasks do
"""
def index_media_items(%Source{} = source) do
{:ok, media_attributes} = SourceDetails.get_media_attributes(source.original_url)
MediaSource.update_source(source, %{last_indexed_at: DateTime.utc_now()})
media_attributes
|> Enum.map(fn media_attrs ->

View file

@ -0,0 +1,14 @@
defmodule Pinchflat.Repo.Migrations.AddLifecycleColumnsToSourcesAndMedia do
use Ecto.Migration
def change do
alter table(:media_items) do
add :media_downloaded_at, :utc_datetime
add :details_updated_at, :utc_datetime
end
alter table(:sources) do
add :last_indexed_at, :utc_datetime
end
end
end

View file

@ -57,6 +57,12 @@ defmodule Pinchflat.MediaClient.VideoDownloaderTest do
:ok
end
test "it sets the media_downloaded_at", %{media_item: media_item} do
assert media_item.media_downloaded_at == nil
assert {:ok, updated_media_item} = VideoDownloader.download_for_media_item(media_item)
assert DateTime.diff(DateTime.utc_now(), updated_media_item.media_downloaded_at) < 1
end
test "it extracts the title", %{media_item: media_item} do
assert {:ok, updated_media_item} = VideoDownloader.download_for_media_item(media_item)
assert updated_media_item.title == "Trying to Wheelie Without the Rear Brake"

View file

@ -103,6 +103,15 @@ defmodule Pinchflat.Tasks.SourceTasksTest do
assert Enum.all?(first_run, fn %MediaItem{} -> true end)
assert Enum.all?(duplicate_run, fn %Ecto.Changeset{} -> true end)
end
test "it updates the source's last_indexed_at field", %{source: source} do
assert source.last_indexed_at == nil
SourceTasks.index_media_items(source)
source = Repo.reload!(source)
assert DateTime.diff(DateTime.utc_now(), source.last_indexed_at) < 1
end
end
describe "enqueue_pending_media_tasks/1" do