Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions api/dbv1/get_track_download_counts.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/dbv1/get_user_track_download_count_total.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions api/dbv1/queries/get_track_download_counts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ SELECT
(
SELECT count(*)::bigint
FROM track_downloads d
WHERE (t.stem_of IS NOT NULL
AND d.parent_track_id = (t.stem_of->>'parent_track_id')::int
AND d.track_id = t.track_id)
OR (t.stem_of IS NULL AND d.parent_track_id = t.track_id)
WHERE d.track_id = t.track_id
AND (
(t.stem_of IS NOT NULL
AND d.parent_track_id = (t.stem_of->>'parent_track_id')::int)
OR (t.stem_of IS NULL
AND d.parent_track_id = t.track_id)
)
) AS download_count
FROM tracks t
WHERE t.track_id = ANY(@track_ids::int[])
Expand Down
4 changes: 2 additions & 2 deletions api/dbv1/queries/get_user_track_download_count_total.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ WHERE EXISTS (
WHERE t.owner_id = @user_id
AND t.is_current = true
AND t.is_delete = false
AND d.track_id = t.track_id
AND (
(t.stem_of IS NULL AND d.parent_track_id = t.track_id)
OR (t.stem_of IS NOT NULL
AND (t.stem_of->>'parent_track_id')::int = d.parent_track_id
AND d.track_id = t.track_id)
AND (t.stem_of->>'parent_track_id')::int = d.parent_track_id)
)
);
26 changes: 20 additions & 6 deletions api/v1_track_download_count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ func TestV1TrackDownloadCount(t *testing.T) {
ctx := context.Background()
require.NotNil(t, app.writePool, "test requires write pool")

// Track 200 is "Culca Canyon" (eYJyn). Insert two download rows so download_count is 2.
// Track 200 is "Culca Canyon" (eYJyn). Insert two original-track download rows
// plus two stem download rows (same parent_track_id, different track_id).
// Only the originals should be counted.
_, err := app.writePool.Exec(ctx, `
INSERT INTO track_downloads (txhash, blocknumber, parent_track_id, track_id, user_id)
VALUES ('tx-dl-1', 101, 200, 200, 1), ('tx-dl-2', 101, 200, 200, 2)
VALUES
('tx-dl-1', 101, 200, 200, 1),
('tx-dl-2', 101, 200, 200, 2),
('tx-dl-stem-1', 101, 200, 9001, 1),
('tx-dl-stem-2', 101, 200, 9002, 1)
`)
require.NoError(t, err)

Expand All @@ -37,10 +43,14 @@ func TestV1TracksDownloadCounts(t *testing.T) {
ctx := context.Background()
require.NotNil(t, app.writePool, "test requires write pool")

// Track 200 (eYJyn) gets 2 downloads; track 201 (eYZmn) has none.
// Track 200 (eYJyn) gets 2 original-track downloads plus a stem download that
// should be ignored; track 201 (eYZmn) has none.
_, err := app.writePool.Exec(ctx, `
INSERT INTO track_downloads (txhash, blocknumber, parent_track_id, track_id, user_id)
VALUES ('tx-dl-1', 101, 200, 200, 1), ('tx-dl-2', 101, 200, 200, 2)
VALUES
('tx-dl-1', 101, 200, 200, 1),
('tx-dl-2', 101, 200, 200, 2),
('tx-dl-stem-1', 101, 200, 9001, 1)
`)
require.NoError(t, err)

Expand All @@ -64,10 +74,14 @@ func TestV1UserTracksDownloadCount(t *testing.T) {
ctx := context.Background()
require.NotNil(t, app.writePool, "test requires write pool")

// Track 200 (eYJyn) is owned by user 2. Insert two download rows.
// Track 200 (eYJyn) is owned by user 2. Insert two original-track download rows
// plus a stem download row that should be excluded from the total.
_, err := app.writePool.Exec(ctx, `
INSERT INTO track_downloads (txhash, blocknumber, parent_track_id, track_id, user_id)
VALUES ('tx-user-total-1', 101, 200, 200, 1), ('tx-user-total-2', 101, 200, 200, 2)
VALUES
('tx-user-total-1', 101, 200, 200, 1),
('tx-user-total-2', 101, 200, 200, 2),
('tx-user-total-stem-1', 101, 200, 9001, 1)
`)
require.NoError(t, err)

Expand Down
Loading