chore: update counts to allow unix timeframe

This commit is contained in:
Gabe Farrell 2025-12-29 16:02:55 -05:00
parent dfe3b5c90d
commit ef27dce5e3
6 changed files with 69 additions and 33 deletions

View file

@ -10,9 +10,15 @@ import (
"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) {
var t1, t2 time.Time
if timeframe.T1u == 0 && timeframe.T2u == 0 {
t2 = time.Now()
t1 = db.StartTimeFromPeriod(timeframe.Period)
} else {
t1 = time.Unix(timeframe.T1u, 0)
t2 = time.Unix(timeframe.T2u, 0)
}
count, err := p.q.CountListens(ctx, repository.CountListensParams{
ListenedAt: t1,
ListenedAt_2: t2,
@ -23,9 +29,15 @@ 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) {
var t1, t2 time.Time
if timeframe.T1u == 0 && timeframe.T2u == 0 {
t2 = time.Now()
t1 = db.StartTimeFromPeriod(timeframe.Period)
} else {
t1 = time.Unix(timeframe.T1u, 0)
t2 = time.Unix(timeframe.T2u, 0)
}
count, err := p.q.CountTopTracks(ctx, repository.CountTopTracksParams{
ListenedAt: t1,
ListenedAt_2: t2,
@ -36,9 +48,15 @@ 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) {
var t1, t2 time.Time
if timeframe.T1u == 0 && timeframe.T2u == 0 {
t2 = time.Now()
t1 = db.StartTimeFromPeriod(timeframe.Period)
} else {
t1 = time.Unix(timeframe.T1u, 0)
t2 = time.Unix(timeframe.T2u, 0)
}
count, err := p.q.CountTopReleases(ctx, repository.CountTopReleasesParams{
ListenedAt: t1,
ListenedAt_2: t2,
@ -49,9 +67,15 @@ 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) {
var t1, t2 time.Time
if timeframe.T1u == 0 && timeframe.T2u == 0 {
t2 = time.Now()
t1 = db.StartTimeFromPeriod(timeframe.Period)
} else {
t1 = time.Unix(timeframe.T1u, 0)
t2 = time.Unix(timeframe.T2u, 0)
}
count, err := p.q.CountTopArtists(ctx, repository.CountTopArtistsParams{
ListenedAt: t1,
ListenedAt_2: t2,
@ -62,9 +86,15 @@ 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)
func (p *Psql) CountTimeListened(ctx context.Context, timeframe db.Timeframe) (int64, error) {
var t1, t2 time.Time
if timeframe.T1u == 0 && timeframe.T2u == 0 {
t2 = time.Now()
t1 = db.StartTimeFromPeriod(timeframe.Period)
} else {
t1 = time.Unix(timeframe.T1u, 0)
t2 = time.Unix(timeframe.T2u, 0)
}
count, err := p.q.CountTimeListened(ctx, repository.CountTimeListenedParams{
ListenedAt: t1,
ListenedAt_2: t2,

View file

@ -15,7 +15,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,7 +28,7 @@ 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")
@ -41,7 +41,7 @@ func TestCountAlbums(t *testing.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")
@ -54,7 +54,7 @@ func TestCountArtists(t *testing.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")
@ -67,7 +67,7 @@ func TestCountTimeListened(t *testing.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")