mirror of
https://github.com/gabehf/Koito.git
synced 2026-04-22 20:11:50 -07:00
feat: add db functions for counting new items
This commit is contained in:
parent
ef27dce5e3
commit
3b585f748a
9 changed files with 235 additions and 24 deletions
|
|
@ -142,3 +142,60 @@ func (p *Psql) CountTimeListenedToItem(ctx context.Context, opts db.TimeListened
|
|||
}
|
||||
return 0, errors.New("CountTimeListenedToItem: an id must be provided")
|
||||
}
|
||||
|
||||
func (p *Psql) CountNewTracks(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.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) {
|
||||
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.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) {
|
||||
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.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"
|
||||
|
|
@ -35,6 +36,23 @@ func TestCountTracks(t *testing.T) {
|
|||
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)
|
||||
|
|
@ -48,6 +66,23 @@ func TestCountAlbums(t *testing.T) {
|
|||
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)
|
||||
|
|
@ -61,6 +96,23 @@ func TestCountArtists(t *testing.T) {
|
|||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue