mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-08 23:18:15 -07:00
feat: Rewind (#116)
* wip * chore: update counts to allow unix timeframe * feat: add db functions for counting new items * wip: endpoint working * wip * wip: initial ui done * add header, adjust ui * add time listened toggle * fix layout, year param * param fixes
This commit is contained in:
parent
c0a8c64243
commit
d4ac96f780
64 changed files with 2252 additions and 1055 deletions
|
|
@ -91,8 +91,8 @@ func (d *Psql) GetAlbum(ctx context.Context, opts db.GetAlbumOpts) (*models.Albu
|
|||
}
|
||||
|
||||
seconds, err := d.CountTimeListenedToItem(ctx, db.TimeListenedOpts{
|
||||
Period: db.PeriodAllTime,
|
||||
AlbumID: ret.ID,
|
||||
Timeframe: db.Timeframe{Period: db.PeriodAllTime},
|
||||
AlbumID: ret.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetAlbum: CountTimeListenedToItem: %w", err)
|
||||
|
|
|
|||
|
|
@ -35,8 +35,8 @@ func (d *Psql) GetArtist(ctx context.Context, opts db.GetArtistOpts) (*models.Ar
|
|||
return nil, fmt.Errorf("GetArtist: CountListensFromArtist: %w", err)
|
||||
}
|
||||
seconds, err := d.CountTimeListenedToItem(ctx, db.TimeListenedOpts{
|
||||
Period: db.PeriodAllTime,
|
||||
ArtistID: row.ID,
|
||||
Timeframe: db.Timeframe{Period: db.PeriodAllTime},
|
||||
ArtistID: row.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetArtist: CountTimeListenedToItem: %w", err)
|
||||
|
|
@ -70,8 +70,8 @@ func (d *Psql) GetArtist(ctx context.Context, opts db.GetArtistOpts) (*models.Ar
|
|||
return nil, fmt.Errorf("GetArtist: CountListensFromArtist: %w", err)
|
||||
}
|
||||
seconds, err := d.CountTimeListenedToItem(ctx, db.TimeListenedOpts{
|
||||
Period: db.PeriodAllTime,
|
||||
ArtistID: row.ID,
|
||||
Timeframe: db.Timeframe{Period: db.PeriodAllTime},
|
||||
ArtistID: row.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetArtist: CountTimeListenedToItem: %w", err)
|
||||
|
|
@ -105,8 +105,8 @@ func (d *Psql) GetArtist(ctx context.Context, opts db.GetArtistOpts) (*models.Ar
|
|||
return nil, fmt.Errorf("GetArtist: CountListensFromArtist: %w", err)
|
||||
}
|
||||
seconds, err := d.CountTimeListenedToItem(ctx, db.TimeListenedOpts{
|
||||
Period: db.PeriodAllTime,
|
||||
ArtistID: row.ID,
|
||||
Timeframe: db.Timeframe{Period: db.PeriodAllTime},
|
||||
ArtistID: row.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetArtist: CountTimeListenedToItem: %w", err)
|
||||
|
|
|
|||
|
|
@ -4,15 +4,13 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gabehf/koito/internal/db"
|
||||
"github.com/gabehf/koito/internal/repository"
|
||||
)
|
||||
|
||||
func (p *Psql) CountListens(ctx context.Context, period db.Period) (int64, error) {
|
||||
t2 := time.Now()
|
||||
t1 := db.StartTimeFromPeriod(period)
|
||||
func (p *Psql) CountListens(ctx context.Context, timeframe db.Timeframe) (int64, error) {
|
||||
t1, t2 := db.TimeframeToTimeRange(timeframe)
|
||||
count, err := p.q.CountListens(ctx, repository.CountListensParams{
|
||||
ListenedAt: t1,
|
||||
ListenedAt_2: t2,
|
||||
|
|
@ -23,9 +21,8 @@ func (p *Psql) CountListens(ctx context.Context, period db.Period) (int64, error
|
|||
return count, nil
|
||||
}
|
||||
|
||||
func (p *Psql) CountTracks(ctx context.Context, period db.Period) (int64, error) {
|
||||
t2 := time.Now()
|
||||
t1 := db.StartTimeFromPeriod(period)
|
||||
func (p *Psql) CountTracks(ctx context.Context, timeframe db.Timeframe) (int64, error) {
|
||||
t1, t2 := db.TimeframeToTimeRange(timeframe)
|
||||
count, err := p.q.CountTopTracks(ctx, repository.CountTopTracksParams{
|
||||
ListenedAt: t1,
|
||||
ListenedAt_2: t2,
|
||||
|
|
@ -36,9 +33,8 @@ func (p *Psql) CountTracks(ctx context.Context, period db.Period) (int64, error)
|
|||
return count, nil
|
||||
}
|
||||
|
||||
func (p *Psql) CountAlbums(ctx context.Context, period db.Period) (int64, error) {
|
||||
t2 := time.Now()
|
||||
t1 := db.StartTimeFromPeriod(period)
|
||||
func (p *Psql) CountAlbums(ctx context.Context, timeframe db.Timeframe) (int64, error) {
|
||||
t1, t2 := db.TimeframeToTimeRange(timeframe)
|
||||
count, err := p.q.CountTopReleases(ctx, repository.CountTopReleasesParams{
|
||||
ListenedAt: t1,
|
||||
ListenedAt_2: t2,
|
||||
|
|
@ -49,9 +45,8 @@ func (p *Psql) CountAlbums(ctx context.Context, period db.Period) (int64, error)
|
|||
return count, nil
|
||||
}
|
||||
|
||||
func (p *Psql) CountArtists(ctx context.Context, period db.Period) (int64, error) {
|
||||
t2 := time.Now()
|
||||
t1 := db.StartTimeFromPeriod(period)
|
||||
func (p *Psql) CountArtists(ctx context.Context, timeframe db.Timeframe) (int64, error) {
|
||||
t1, t2 := db.TimeframeToTimeRange(timeframe)
|
||||
count, err := p.q.CountTopArtists(ctx, repository.CountTopArtistsParams{
|
||||
ListenedAt: t1,
|
||||
ListenedAt_2: t2,
|
||||
|
|
@ -62,9 +57,9 @@ func (p *Psql) CountArtists(ctx context.Context, period db.Period) (int64, error
|
|||
return count, nil
|
||||
}
|
||||
|
||||
func (p *Psql) CountTimeListened(ctx context.Context, period db.Period) (int64, error) {
|
||||
t2 := time.Now()
|
||||
t1 := db.StartTimeFromPeriod(period)
|
||||
// in seconds
|
||||
func (p *Psql) CountTimeListened(ctx context.Context, timeframe db.Timeframe) (int64, error) {
|
||||
t1, t2 := db.TimeframeToTimeRange(timeframe)
|
||||
count, err := p.q.CountTimeListened(ctx, repository.CountTimeListenedParams{
|
||||
ListenedAt: t1,
|
||||
ListenedAt_2: t2,
|
||||
|
|
@ -75,9 +70,9 @@ func (p *Psql) CountTimeListened(ctx context.Context, period db.Period) (int64,
|
|||
return count, nil
|
||||
}
|
||||
|
||||
// in seconds
|
||||
func (p *Psql) CountTimeListenedToItem(ctx context.Context, opts db.TimeListenedOpts) (int64, error) {
|
||||
t2 := time.Now()
|
||||
t1 := db.StartTimeFromPeriod(opts.Period)
|
||||
t1, t2 := db.TimeframeToTimeRange(opts.Timeframe)
|
||||
|
||||
if opts.ArtistID > 0 {
|
||||
count, err := p.q.CountTimeListenedToArtist(ctx, repository.CountTimeListenedToArtistParams{
|
||||
|
|
@ -112,3 +107,76 @@ func (p *Psql) CountTimeListenedToItem(ctx context.Context, opts db.TimeListened
|
|||
}
|
||||
return 0, errors.New("CountTimeListenedToItem: an id must be provided")
|
||||
}
|
||||
|
||||
func (p *Psql) CountListensToItem(ctx context.Context, opts db.TimeListenedOpts) (int64, error) {
|
||||
t1, t2 := db.TimeframeToTimeRange(opts.Timeframe)
|
||||
|
||||
if opts.ArtistID > 0 {
|
||||
count, err := p.q.CountListensFromArtist(ctx, repository.CountListensFromArtistParams{
|
||||
ListenedAt: t1,
|
||||
ListenedAt_2: t2,
|
||||
ArtistID: opts.ArtistID,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("CountListensToItem (Artist): %w", err)
|
||||
}
|
||||
return count, nil
|
||||
} else if opts.AlbumID > 0 {
|
||||
count, err := p.q.CountListensFromRelease(ctx, repository.CountListensFromReleaseParams{
|
||||
ListenedAt: t1,
|
||||
ListenedAt_2: t2,
|
||||
ReleaseID: opts.AlbumID,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("CountListensToItem (Album): %w", err)
|
||||
}
|
||||
return count, nil
|
||||
} else if opts.TrackID > 0 {
|
||||
count, err := p.q.CountListensFromTrack(ctx, repository.CountListensFromTrackParams{
|
||||
ListenedAt: t1,
|
||||
ListenedAt_2: t2,
|
||||
TrackID: opts.TrackID,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("CountListensToItem (Track): %w", err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
return 0, errors.New("CountListensToItem: an id must be provided")
|
||||
}
|
||||
|
||||
func (p *Psql) CountNewTracks(ctx context.Context, timeframe db.Timeframe) (int64, error) {
|
||||
t1, t2 := db.TimeframeToTimeRange(timeframe)
|
||||
count, err := p.q.CountNewTracks(ctx, repository.CountNewTracksParams{
|
||||
ListenedAt: t1,
|
||||
ListenedAt_2: t2,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("CountNewTracks: %w", err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (p *Psql) CountNewAlbums(ctx context.Context, timeframe db.Timeframe) (int64, error) {
|
||||
t1, t2 := db.TimeframeToTimeRange(timeframe)
|
||||
count, err := p.q.CountNewReleases(ctx, repository.CountNewReleasesParams{
|
||||
ListenedAt: t1,
|
||||
ListenedAt_2: t2,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("CountNewAlbums: %w", err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (p *Psql) CountNewArtists(ctx context.Context, timeframe db.Timeframe) (int64, error) {
|
||||
t1, t2 := db.TimeframeToTimeRange(timeframe)
|
||||
count, err := p.q.CountNewArtists(ctx, repository.CountNewArtistsParams{
|
||||
ListenedAt: t1,
|
||||
ListenedAt_2: t2,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("CountNewArtists: %w", err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package psql_test
|
|||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gabehf/koito/internal/db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
|
@ -15,7 +16,7 @@ func TestCountListens(t *testing.T) {
|
|||
|
||||
// Test CountListens
|
||||
period := db.PeriodWeek
|
||||
count, err := store.CountListens(ctx, period)
|
||||
count, err := store.CountListens(ctx, db.Timeframe{Period: period})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(1), count, "expected listens count to match inserted data")
|
||||
|
||||
|
|
@ -28,46 +29,97 @@ func TestCountTracks(t *testing.T) {
|
|||
|
||||
// Test CountTracks
|
||||
period := db.PeriodMonth
|
||||
count, err := store.CountTracks(ctx, period)
|
||||
count, err := store.CountTracks(ctx, db.Timeframe{Period: period})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(2), count, "expected tracks count to match inserted data")
|
||||
|
||||
truncateTestData(t)
|
||||
}
|
||||
|
||||
func TestCountNewTracks(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testDataForTopItems(t)
|
||||
testDataAbsoluteListenTimes(t)
|
||||
|
||||
// Test CountTracks
|
||||
t1, _ := time.Parse(time.DateOnly, "2025-01-01")
|
||||
t1u := t1.Unix()
|
||||
t2, _ := time.Parse(time.DateOnly, "2025-12-31")
|
||||
t2u := t2.Unix()
|
||||
count, err := store.CountNewTracks(ctx, db.Timeframe{T1u: t1u, T2u: t2u})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(1), count, "expected tracks count to match inserted data")
|
||||
|
||||
truncateTestData(t)
|
||||
}
|
||||
|
||||
func TestCountAlbums(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testDataForTopItems(t)
|
||||
|
||||
// Test CountAlbums
|
||||
period := db.PeriodYear
|
||||
count, err := store.CountAlbums(ctx, period)
|
||||
count, err := store.CountAlbums(ctx, db.Timeframe{Period: period})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(3), count, "expected albums count to match inserted data")
|
||||
|
||||
truncateTestData(t)
|
||||
}
|
||||
|
||||
func TestCountNewAlbums(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testDataForTopItems(t)
|
||||
testDataAbsoluteListenTimes(t)
|
||||
|
||||
// Test CountTracks
|
||||
t1, _ := time.Parse(time.DateOnly, "2025-01-01")
|
||||
t1u := t1.Unix()
|
||||
t2, _ := time.Parse(time.DateOnly, "2025-12-31")
|
||||
t2u := t2.Unix()
|
||||
count, err := store.CountNewAlbums(ctx, db.Timeframe{T1u: t1u, T2u: t2u})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(1), count, "expected albums count to match inserted data")
|
||||
|
||||
truncateTestData(t)
|
||||
}
|
||||
|
||||
func TestCountArtists(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testDataForTopItems(t)
|
||||
|
||||
// Test CountArtists
|
||||
period := db.PeriodAllTime
|
||||
count, err := store.CountArtists(ctx, period)
|
||||
count, err := store.CountArtists(ctx, db.Timeframe{Period: period})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(4), count, "expected artists count to match inserted data")
|
||||
|
||||
truncateTestData(t)
|
||||
}
|
||||
|
||||
func TestCountNewArtists(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testDataForTopItems(t)
|
||||
testDataAbsoluteListenTimes(t)
|
||||
|
||||
// Test CountTracks
|
||||
t1, _ := time.Parse(time.DateOnly, "2025-01-01")
|
||||
t1u := t1.Unix()
|
||||
t2, _ := time.Parse(time.DateOnly, "2025-12-31")
|
||||
t2u := t2.Unix()
|
||||
count, err := store.CountNewArtists(ctx, db.Timeframe{T1u: t1u, T2u: t2u})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(1), count, "expected artists count to match inserted data")
|
||||
|
||||
truncateTestData(t)
|
||||
}
|
||||
|
||||
func TestCountTimeListened(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testDataForTopItems(t)
|
||||
|
||||
// Test CountTimeListened
|
||||
period := db.PeriodMonth
|
||||
count, err := store.CountTimeListened(ctx, period)
|
||||
count, err := store.CountTimeListened(ctx, db.Timeframe{Period: period})
|
||||
require.NoError(t, err)
|
||||
// 3 listens in past month, each 100 seconds
|
||||
assert.Equal(t, int64(300), count, "expected total time listened to match inserted data")
|
||||
|
|
@ -79,7 +131,7 @@ func TestCountTimeListenedToArtist(t *testing.T) {
|
|||
ctx := context.Background()
|
||||
testDataForTopItems(t)
|
||||
period := db.PeriodAllTime
|
||||
count, err := store.CountTimeListenedToItem(ctx, db.TimeListenedOpts{Period: period, ArtistID: 1})
|
||||
count, err := store.CountTimeListenedToItem(ctx, db.TimeListenedOpts{Timeframe: db.Timeframe{Period: period}, ArtistID: 1})
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, 400, count)
|
||||
truncateTestData(t)
|
||||
|
|
@ -89,7 +141,7 @@ func TestCountTimeListenedToAlbum(t *testing.T) {
|
|||
ctx := context.Background()
|
||||
testDataForTopItems(t)
|
||||
period := db.PeriodAllTime
|
||||
count, err := store.CountTimeListenedToItem(ctx, db.TimeListenedOpts{Period: period, AlbumID: 2})
|
||||
count, err := store.CountTimeListenedToItem(ctx, db.TimeListenedOpts{Timeframe: db.Timeframe{Period: period}, AlbumID: 2})
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, 300, count)
|
||||
truncateTestData(t)
|
||||
|
|
@ -99,8 +151,38 @@ func TestCountTimeListenedToTrack(t *testing.T) {
|
|||
ctx := context.Background()
|
||||
testDataForTopItems(t)
|
||||
period := db.PeriodAllTime
|
||||
count, err := store.CountTimeListenedToItem(ctx, db.TimeListenedOpts{Period: period, TrackID: 3})
|
||||
count, err := store.CountTimeListenedToItem(ctx, db.TimeListenedOpts{Timeframe: db.Timeframe{Period: period}, TrackID: 3})
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, 200, count)
|
||||
truncateTestData(t)
|
||||
}
|
||||
|
||||
func TestListensToArtist(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testDataForTopItems(t)
|
||||
period := db.PeriodAllTime
|
||||
count, err := store.CountListensToItem(ctx, db.TimeListenedOpts{Timeframe: db.Timeframe{Period: period}, ArtistID: 1})
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, 4, count)
|
||||
truncateTestData(t)
|
||||
}
|
||||
|
||||
func TestListensToAlbum(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testDataForTopItems(t)
|
||||
period := db.PeriodAllTime
|
||||
count, err := store.CountListensToItem(ctx, db.TimeListenedOpts{Timeframe: db.Timeframe{Period: period}, AlbumID: 2})
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, 3, count)
|
||||
truncateTestData(t)
|
||||
}
|
||||
|
||||
func TestListensToTrack(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
testDataForTopItems(t)
|
||||
period := db.PeriodAllTime
|
||||
count, err := store.CountListensToItem(ctx, db.TimeListenedOpts{Timeframe: db.Timeframe{Period: period}, TrackID: 3})
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, 2, count)
|
||||
truncateTestData(t)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,10 @@ func (d *Psql) GetTopAlbumsPaginated(ctx context.Context, opts db.GetItemsOpts)
|
|||
t2 = time.Now()
|
||||
t1 = db.StartTimeFromPeriod(opts.Period)
|
||||
}
|
||||
if opts.From != 0 || opts.To != 0 {
|
||||
t1 = time.Unix(opts.From, 0)
|
||||
t2 = time.Unix(opts.To, 0)
|
||||
}
|
||||
if opts.Limit == 0 {
|
||||
opts.Limit = DefaultItemsPerPage
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,10 @@ func (d *Psql) GetTopArtistsPaginated(ctx context.Context, opts db.GetItemsOpts)
|
|||
t2 = time.Now()
|
||||
t1 = db.StartTimeFromPeriod(opts.Period)
|
||||
}
|
||||
if opts.From != 0 || opts.To != 0 {
|
||||
t1 = time.Unix(opts.From, 0)
|
||||
t2 = time.Unix(opts.To, 0)
|
||||
}
|
||||
if opts.Limit == 0 {
|
||||
opts.Limit = DefaultItemsPerPage
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,10 @@ func (d *Psql) GetTopTracksPaginated(ctx context.Context, opts db.GetItemsOpts)
|
|||
t2 = time.Now()
|
||||
t1 = db.StartTimeFromPeriod(opts.Period)
|
||||
}
|
||||
if opts.From != 0 || opts.To != 0 {
|
||||
t1 = time.Unix(opts.From, 0)
|
||||
t2 = time.Unix(opts.To, 0)
|
||||
}
|
||||
if opts.Limit == 0 {
|
||||
opts.Limit = DefaultItemsPerPage
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,8 +82,8 @@ func (d *Psql) GetTrack(ctx context.Context, opts db.GetTrackOpts) (*models.Trac
|
|||
}
|
||||
|
||||
seconds, err := d.CountTimeListenedToItem(ctx, db.TimeListenedOpts{
|
||||
Period: db.PeriodAllTime,
|
||||
TrackID: track.ID,
|
||||
Timeframe: db.Timeframe{Period: db.PeriodAllTime},
|
||||
TrackID: track.ID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetTrack: CountTimeListenedToItem: %w", err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue