Skip to content

Commit c78f338

Browse files
committed
Exclude stem downloads from track download counts
The per-track and per-user-total download count queries matched every track_downloads row by parent_track_id alone, so for an original track each stem download (which shares the same parent_track_id but has a different track_id) was counted as an additional download of the original. Require d.track_id = t.track_id in both queries so only the actual original-track download rows contribute to the count; stem tracks keep counting their own stem-specific rows as before. https://claude.ai/code/session_01M8ZDgw87vg9S2weug8Jj9P
1 parent 92aa8c4 commit c78f338

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)