reorg packages

This commit is contained in:
sentriz
2023-10-04 20:52:06 +01:00
parent a669ba8598
commit 97e9675dca
9 changed files with 13 additions and 13 deletions

View File

@@ -1,117 +0,0 @@
//nolint:revive
package artistinfocache
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/jinzhu/gorm"
"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/lastfm"
)
const keepFor = 30 * time.Hour * 24
type ArtistInfoCache struct {
db *db.DB
lastfmClient *lastfm.Client
}
func New(db *db.DB, lastfmClient *lastfm.Client) *ArtistInfoCache {
return &ArtistInfoCache{db: db, lastfmClient: lastfmClient}
}
func (a *ArtistInfoCache) GetOrLookup(ctx context.Context, artistID int) (*db.ArtistInfo, error) {
var artist db.Artist
if err := a.db.Find(&artist, "id=?", artistID).Error; err != nil {
return nil, fmt.Errorf("find artist in db: %w", err)
}
var artistInfo db.ArtistInfo
if err := a.db.Find(&artistInfo, "id=?", artistID).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fmt.Errorf("find artist info in db: %w", err)
}
if artistInfo.ID == 0 || artistInfo.Biography == "" /* prev not found maybe */ || time.Since(artistInfo.UpdatedAt) > keepFor {
return a.Lookup(ctx, &artist)
}
return &artistInfo, nil
}
func (a *ArtistInfoCache) Get(ctx context.Context, artistID int) (*db.ArtistInfo, error) {
var artistInfo db.ArtistInfo
if err := a.db.Find(&artistInfo, "id=?", artistID).Error; err != nil {
return nil, fmt.Errorf("find artist info in db: %w", err)
}
return &artistInfo, nil
}
func (a *ArtistInfoCache) Lookup(ctx context.Context, artist *db.Artist) (*db.ArtistInfo, error) {
var artistInfo db.ArtistInfo
artistInfo.ID = artist.ID
if err := a.db.FirstOrCreate(&artistInfo, "id=?", artistInfo.ID).Error; err != nil {
return nil, fmt.Errorf("first or create artist info: %w", err)
}
info, err := a.lastfmClient.ArtistGetInfo(artist.Name)
if err != nil {
return nil, fmt.Errorf("get upstream info: %w", err)
}
artistInfo.ID = artist.ID
artistInfo.Biography = info.Bio.Summary
artistInfo.MusicBrainzID = info.MBID
artistInfo.LastFMURL = info.URL
var similar []string
for _, sim := range info.Similar.Artists {
similar = append(similar, sim.Name)
}
artistInfo.SetSimilarArtists(similar)
url, _ := a.lastfmClient.StealArtistImage(info.URL)
artistInfo.ImageURL = url
topTracksResponse, err := a.lastfmClient.ArtistGetTopTracks(artist.Name)
if err != nil {
return nil, fmt.Errorf("get top tracks: %w", err)
}
var topTracks []string
for _, tr := range topTracksResponse.Tracks {
topTracks = append(topTracks, tr.Name)
}
artistInfo.SetTopTracks(topTracks)
if err := a.db.Save(&artistInfo).Error; err != nil {
return nil, fmt.Errorf("save upstream info: %w", err)
}
return &artistInfo, nil
}
func (a *ArtistInfoCache) Refresh() error {
q := a.db.
Where("artist_infos.id IS NULL OR artist_infos.updated_at<?", time.Now().Add(-keepFor)).
Joins("LEFT JOIN artist_infos ON artist_infos.id=artists.id")
var artist db.Artist
if err := q.Find(&artist).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("finding non cached artist: %w", err)
}
if artist.ID == 0 {
return nil
}
if _, err := a.Lookup(context.Background(), &artist); err != nil {
return fmt.Errorf("looking up non cached artist %s: %w", artist.Name, err)
}
log.Printf("cached artist info for %q", artist.Name)
return nil
}

View File

@@ -1,59 +0,0 @@
package artistinfocache
import (
"context"
"net/http"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/lastfm"
"go.senan.xyz/gonic/lastfm/mockclient"
"go.senan.xyz/gonic/mockfs"
)
func TestInfoCache(t *testing.T) {
t.Parallel()
m := mockfs.New(t)
m.AddItems()
m.ScanAndClean()
assert := assert.New(t)
var artist db.Artist
assert.NoError(m.DB().Preload("Info").Find(&artist).Error)
assert.Greater(artist.ID, 0)
assert.Nil(artist.Info)
var count atomic.Int32
lastfmClient := lastfm.NewClientCustom(
mockclient.New(t, func(w http.ResponseWriter, r *http.Request) {
switch method := r.URL.Query().Get("method"); method {
case "artist.getInfo":
count.Add(1)
w.Write(mockclient.ArtistGetInfoResponse)
case "artist.getTopTracks":
w.Write(mockclient.ArtistGetTopTracksResponse)
}
}),
func() (apiKey string, secret string, err error) {
return "", "", nil
},
)
cache := New(m.DB(), lastfmClient)
_, err := cache.GetOrLookup(context.Background(), artist.ID)
require.NoError(t, err)
_, err = cache.GetOrLookup(context.Background(), artist.ID)
require.NoError(t, err)
require.Equal(t, int32(1), count.Load())
assert.NoError(m.DB().Preload("Info").Find(&artist, "id=?", artist.ID).Error)
assert.Greater(artist.ID, 0)
assert.NotNil(artist.Info)
assert.Equal("Summary", artist.Info.Biography)
}

View File

@@ -16,10 +16,10 @@ import (
"go.senan.xyz/gonic/jukebox"
"go.senan.xyz/gonic/lastfm"
"go.senan.xyz/gonic/playlist"
"go.senan.xyz/gonic/podcasts"
"go.senan.xyz/gonic/podcast"
"go.senan.xyz/gonic/scanner"
"go.senan.xyz/gonic/scrobble"
"go.senan.xyz/gonic/server/ctrlsubsonic/artistinfocache"
"go.senan.xyz/gonic/artistinfocache"
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
"go.senan.xyz/gonic/transcode"
@@ -59,14 +59,14 @@ type Controller struct {
jukebox *jukebox.Jukebox
playlistStore *playlist.Store
scrobblers []scrobble.Scrobbler
podcasts *podcasts.Podcasts
podcasts *podcast.Podcasts
transcoder transcode.Transcoder
lastFMClient *lastfm.Client
artistInfoCache *artistinfocache.ArtistInfoCache
resolveProxyPath ProxyPathResolver
}
func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, podcastsPath string, cacheAudioPath string, cacheCoverPath string, jukebox *jukebox.Jukebox, playlistStore *playlist.Store, scrobblers []scrobble.Scrobbler, podcasts *podcasts.Podcasts, transcoder transcode.Transcoder, lastFMClient *lastfm.Client, artistInfoCache *artistinfocache.ArtistInfoCache, resolveProxyPath ProxyPathResolver) (*Controller, error) {
func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, podcastsPath string, cacheAudioPath string, cacheCoverPath string, jukebox *jukebox.Jukebox, playlistStore *playlist.Store, scrobblers []scrobble.Scrobbler, podcasts *podcast.Podcasts, transcoder transcode.Transcoder, lastFMClient *lastfm.Client, artistInfoCache *artistinfocache.ArtistInfoCache, resolveProxyPath ProxyPathResolver) (*Controller, error) {
c := Controller{
ServeMux: http.NewServeMux(),

View File

@@ -16,7 +16,7 @@ import (
"github.com/jinzhu/gorm"
"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/server/ctrlsubsonic/artistinfocache"
"go.senan.xyz/gonic/artistinfocache"
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"