Move refresh config and callers to ListeningStats

This commit is contained in:
Claudio Ortolina
2026-03-27 23:14:09 +00:00
parent fa5e13f303
commit 61dd3f45c7
12 changed files with 23 additions and 40 deletions
+5 -3
View File
@@ -53,14 +53,16 @@ user_agent = "MusicLibrary/0.1.0 ( cloud8421@gmail.com )"
config :music_library, LastFm, config :music_library, LastFm,
user: "username", user: "username",
auto_refresh: true,
# refresh every 5 minutes
refresh_interval: System.convert_time_unit(300, :second, :millisecond),
api_key: "change me", api_key: "change me",
shared_secret: "change me", shared_secret: "change me",
user_agent: user_agent, user_agent: user_agent,
api_cooldown: 500 api_cooldown: 500
config :music_library, MusicLibrary.ListeningStats.Refresh,
auto_refresh: true,
# refresh every 5 minutes
refresh_interval: System.convert_time_unit(300, :second, :millisecond)
config :music_library, MusicBrainz, user_agent: user_agent, api_cooldown: 500 config :music_library, MusicBrainz, user_agent: user_agent, api_cooldown: 500
config :music_library, Discogs, config :music_library, Discogs,
+1 -1
View File
@@ -81,7 +81,7 @@ config :music_library, monitoring_routes: true
config :music_library, MusicLibrary.Mailer, adapter: Swoosh.Adapters.Local config :music_library, MusicLibrary.Mailer, adapter: Swoosh.Adapters.Local
config :music_library, LastFm, config :music_library, MusicLibrary.ListeningStats.Refresh,
refresh_interval: System.convert_time_unit(500, :second, :millisecond) refresh_interval: System.convert_time_unit(500, :second, :millisecond)
# Do not include metadata nor timestamps in development logs # Do not include metadata nor timestamps in development logs
+2 -1
View File
@@ -56,13 +56,14 @@ config :phoenix_live_view,
config :music_library, monitoring_routes: true config :music_library, monitoring_routes: true
config :music_library, LastFm, config :music_library, LastFm,
auto_refresh: false,
req_options: [ req_options: [
plug: {Req.Test, LastFm.API}, plug: {Req.Test, LastFm.API},
max_retries: 0 max_retries: 0
], ],
api_cooldown: 0 api_cooldown: 0
config :music_library, MusicLibrary.ListeningStats.Refresh, auto_refresh: false
config :music_library, MusicBrainz, config :music_library, MusicBrainz,
req_options: [ req_options: [
plug: {Req.Test, MusicBrainz.API}, plug: {Req.Test, MusicBrainz.API},
+1 -7
View File
@@ -3,15 +3,9 @@ defmodule LastFm do
Last.fm API facade for scrobbling and listening history. Last.fm API facade for scrobbling and listening history.
""" """
alias LastFm.{API, Feed, Refresh, Scrobble, Track, Worker} alias LastFm.{API, Scrobble, Track, Worker}
alias MusicLibrary.{BackgroundRepo, Repo} alias MusicLibrary.{BackgroundRepo, Repo}
@spec subscribe_to_feed() :: :ok
def subscribe_to_feed, do: Feed.subscribe()
@spec refresh_scrobbled_tracks() :: :ok | {:error, term()}
def refresh_scrobbled_tracks, do: Refresh.refresh()
@spec get_tracks(keyword()) :: {:ok, [Track.t()]} | {:error, term()} @spec get_tracks(keyword()) :: {:ok, [Track.t()]} | {:error, term()}
def get_tracks(opts) do def get_tracks(opts) do
last_fm_config = last_fm_config() last_fm_config = last_fm_config()
-14
View File
@@ -3,8 +3,6 @@ defmodule LastFm.Config do
api_key: String.t(), api_key: String.t(),
shared_secret: String.t(), shared_secret: String.t(),
user: String.t(), user: String.t(),
auto_refresh: boolean(),
refresh_interval: pos_integer(),
user_agent: String.t(), user_agent: String.t(),
req_options: Keyword.t(), req_options: Keyword.t(),
api_cooldown: non_neg_integer() api_cooldown: non_neg_integer()
@@ -14,8 +12,6 @@ defmodule LastFm.Config do
defstruct api_key: "", defstruct api_key: "",
shared_secret: "", shared_secret: "",
user: "", user: "",
auto_refresh: true,
refresh_interval: 60_000,
user_agent: "change me", user_agent: "change me",
req_options: [], req_options: [],
api_cooldown: 500 api_cooldown: 500
@@ -33,16 +29,6 @@ defmodule LastFm.Config do
type: :string, type: :string,
required: true required: true
], ],
auto_refresh: [
type: :boolean,
required: false,
default: true
],
refresh_interval: [
type: :pos_integer,
required: false,
default: 60_000
],
user_agent: [ user_agent: [
type: :string, type: :string,
required: false, required: false,
+1 -1
View File
@@ -4,7 +4,7 @@ defmodule LastFm.Import do
@spec batch(keyword()) :: {:ok, non_neg_integer()} | {:error, term()} @spec batch(keyword()) :: {:ok, non_neg_integer()} | {:error, term()}
def batch(opts) do def batch(opts) do
with {:ok, tracks} <- LastFm.get_tracks(opts) do with {:ok, tracks} <- LastFm.get_tracks(opts) do
LastFm.Feed.update(tracks) MusicLibrary.ListeningStats.update(tracks)
end end
end end
end end
+3 -1
View File
@@ -24,7 +24,9 @@ defmodule MusicLibrary.Application do
repos: Application.fetch_env!(:music_library, :ecto_repos), skip: skip_migrations?()}, repos: Application.fetch_env!(:music_library, :ecto_repos), skip: skip_migrations?()},
{Task.Supervisor, name: MusicLibrary.TaskSupervisor}, {Task.Supervisor, name: MusicLibrary.TaskSupervisor},
{Phoenix.PubSub, name: MusicLibrary.PubSub}, {Phoenix.PubSub, name: MusicLibrary.PubSub},
{LastFm.Supervisor, LastFm.Config.resolve(:music_library)}, {MusicLibrary.ListeningStats.Refresh,
{LastFm.Config.resolve(:music_library),
Application.fetch_env!(:music_library, MusicLibrary.ListeningStats.Refresh)}},
# Start a worker by calling: MusicLibrary.Worker.start_link(arg) # Start a worker by calling: MusicLibrary.Worker.start_link(arg)
# {MusicLibrary.Worker, arg}, # {MusicLibrary.Worker, arg},
# Start to serve requests, typically the last entry # Start to serve requests, typically the last entry
@@ -222,7 +222,7 @@ defmodule MusicLibraryWeb.ScrobbledTracksLive.Index do
) )
if connected?(socket) do if connected?(socket) do
LastFm.subscribe_to_feed() ListeningStats.subscribe()
end end
{:ok, socket} {:ok, socket}
@@ -288,7 +288,7 @@ defmodule MusicLibraryWeb.ScrobbledTracksLive.Index do
end end
def handle_event("refresh_lastfm_feed", _, socket) do def handle_event("refresh_lastfm_feed", _, socket) do
LastFm.refresh_scrobbled_tracks() ListeningStats.refresh()
{:noreply, socket} {:noreply, socket}
end end
@@ -65,7 +65,7 @@ defmodule MusicLibraryWeb.StatsLive.Index do
|> Collection.group_records_by_release_group() |> Collection.group_records_by_release_group()
if connected?(socket) do if connected?(socket) do
LastFm.subscribe_to_feed() ListeningStats.subscribe()
end end
{:ok, {:ok,
@@ -93,7 +93,7 @@ defmodule MusicLibraryWeb.StatsLive.Index do
@impl true @impl true
def handle_event("refresh_lastfm_feed", _, socket) do def handle_event("refresh_lastfm_feed", _, socket) do
LastFm.refresh_scrobbled_tracks() ListeningStats.refresh()
{:noreply, socket} {:noreply, socket}
end end
-3
View File
@@ -6,8 +6,6 @@ defmodule LastFm.ConfigTest do
assert %LastFm.Config{ assert %LastFm.Config{
api_key: api_key, api_key: api_key,
user: user, user: user,
auto_refresh: false,
refresh_interval: refresh_interval,
user_agent: user_agent user_agent: user_agent
} = } =
LastFm.Config.resolve(:music_library) LastFm.Config.resolve(:music_library)
@@ -15,7 +13,6 @@ defmodule LastFm.ConfigTest do
assert is_binary(api_key) assert is_binary(api_key)
assert is_binary(user) assert is_binary(user)
assert is_binary(user_agent) assert is_binary(user_agent)
assert is_integer(refresh_interval)
end end
end end
end end
+5 -4
View File
@@ -1,7 +1,8 @@
defmodule LastFm.FeedTest do defmodule LastFm.FeedTest do
use MusicLibrary.DataCase use MusicLibrary.DataCase
alias LastFm.{Album, Artist, Feed, Track} alias LastFm.{Album, Artist, Track}
alias MusicLibrary.ListeningStats
@track_one %Track{ @track_one %Track{
musicbrainz_id: "5689211e-9afa-3c3e-8e34-63dc0de45ef1", musicbrainz_id: "5689211e-9afa-3c3e-8e34-63dc0de45ef1",
@@ -38,13 +39,13 @@ defmodule LastFm.FeedTest do
describe "update and broadcast" do describe "update and broadcast" do
test "stores the track and broadcasts the updated track count" do test "stores the track and broadcasts the updated track count" do
:ok = Feed.subscribe() :ok = ListeningStats.subscribe()
assert {:ok, 2} == Feed.update([@track_two, @track_one]) assert {:ok, 2} == ListeningStats.update([@track_two, @track_one])
assert_receive %{track_count: 2} assert_receive %{track_count: 2}
# Tracks have already been inserted, count of new tracks is 0 # Tracks have already been inserted, count of new tracks is 0
assert {:ok, 0} == Feed.update([@track_two, @track_one]) assert {:ok, 0} == ListeningStats.update([@track_two, @track_one])
assert_receive %{track_count: 0} assert_receive %{track_count: 0}
end end
end end
@@ -225,7 +225,7 @@ defmodule MusicLibraryWeb.StatsLive.IndexTest do
last_fm_data: %{} last_fm_data: %{}
} }
LastFm.Feed.update([ MusicLibrary.ListeningStats.update([
machinarium_soundtrack_track, machinarium_soundtrack_track,
the_last_flight_track, the_last_flight_track,
the_mystery_of_time_track, the_mystery_of_time_track,