Skip to content

Commit ee4ddad

Browse files
dylanjeffersclaude
andauthored
Fix track download count queries to exclude stem downloads (#769)
## Summary This PR fixes the track download count queries to properly exclude stem track downloads from the count. Previously, the queries were incorrectly counting downloads of stem tracks as downloads of their parent tracks. ## Key Changes - **Query Logic Fix**: Restructured the WHERE clause in `get_track_download_counts.sql` and `get_user_track_download_count_total.sql` to add an explicit `d.track_id = t.track_id` condition. This ensures that only downloads of the exact track (not stems of that track) are counted. - For original tracks: downloads where `parent_track_id = track_id` - For stem tracks: downloads where `parent_track_id` matches the stem's parent and `track_id` matches the stem itself - **Test Updates**: Enhanced test cases in `v1_track_download_count_test.go` to verify the fix: - Added stem download rows to test data to ensure they are properly excluded from counts - Updated test comments to clarify the expected behavior - Reformatted INSERT statements for better readability ## Implementation Details The key fix moves the `d.track_id = t.track_id` condition outside the OR clause and into the main WHERE clause. This ensures that: 1. For original tracks (where `stem_of IS NULL`), we count downloads where the download's `parent_track_id` equals the track's `track_id` 2. For stem tracks (where `stem_of IS NOT NULL`), we count downloads where the download's `parent_track_id` matches the stem's parent AND the download's `track_id` matches the stem's `track_id` This prevents stem track downloads from being incorrectly attributed to their parent tracks. https://claude.ai/code/session_01M8ZDgw87vg9S2weug8Jj9P Co-authored-by: Claude <noreply@anthropic.com>
1 parent 92aa8c4 commit ee4ddad

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

api/dbv1/get_track_download_counts.sql.go

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/dbv1/get_user_track_download_count_total.sql.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/dbv1/queries/get_track_download_counts.sql

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ SELECT
44
(
55
SELECT count(*)::bigint
66
FROM track_downloads d
7-
WHERE (t.stem_of IS NOT NULL
8-
AND d.parent_track_id = (t.stem_of->>'parent_track_id')::int
9-
AND d.track_id = t.track_id)
10-
OR (t.stem_of IS NULL AND d.parent_track_id = t.track_id)
7+
WHERE d.track_id = t.track_id
8+
AND (
9+
(t.stem_of IS NOT NULL
10+
AND d.parent_track_id = (t.stem_of->>'parent_track_id')::int)
11+
OR (t.stem_of IS NULL
12+
AND d.parent_track_id = t.track_id)
13+
)
1114
) AS download_count
1215
FROM tracks t
1316
WHERE t.track_id = ANY(@track_ids::int[])

api/dbv1/queries/get_user_track_download_count_total.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ WHERE EXISTS (
99
WHERE t.owner_id = @user_id
1010
AND t.is_current = true
1111
AND t.is_delete = false
12+
AND d.track_id = t.track_id
1213
AND (
1314
(t.stem_of IS NULL AND d.parent_track_id = t.track_id)
1415
OR (t.stem_of IS NOT NULL
15-
AND (t.stem_of->>'parent_track_id')::int = d.parent_track_id
16-
AND d.track_id = t.track_id)
16+
AND (t.stem_of->>'parent_track_id')::int = d.parent_track_id)
1717
)
1818
);

api/v1_track_download_count_test.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ func TestV1TrackDownloadCount(t *testing.T) {
1717
ctx := context.Background()
1818
require.NotNil(t, app.writePool, "test requires write pool")
1919

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

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

40-
// Track 200 (eYJyn) gets 2 downloads; track 201 (eYZmn) has none.
46+
// Track 200 (eYJyn) gets 2 original-track downloads plus a stem download that
47+
// should be ignored; track 201 (eYZmn) has none.
4148
_, err := app.writePool.Exec(ctx, `
4249
INSERT INTO track_downloads (txhash, blocknumber, parent_track_id, track_id, user_id)
43-
VALUES ('tx-dl-1', 101, 200, 200, 1), ('tx-dl-2', 101, 200, 200, 2)
50+
VALUES
51+
('tx-dl-1', 101, 200, 200, 1),
52+
('tx-dl-2', 101, 200, 200, 2),
53+
('tx-dl-stem-1', 101, 200, 9001, 1)
4454
`)
4555
require.NoError(t, err)
4656

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

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

0 commit comments

Comments
 (0)