From cf84b4afdd33964e25e42523d36e5194838a36da Mon Sep 17 00:00:00 2001 From: mitteneer Date: Fri, 28 Mar 2025 13:09:51 -0400 Subject: [PATCH] Remove unnecessary db search for scrobbles I forgot I made scrobbles owned by their respective songs/albums/artists, so I can just query that instead --- build.zig.zon | 2 +- src/app/database/Schema.zig | 2 +- .../2025-02-17_22-38-46_create_scrobbles.zig | 2 +- src/app/views/albums.zig | 12 +++++++----- src/app/views/artists.zig | 11 ++++++----- src/app/views/songs.zig | 11 ++++++----- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index 3ba5697..823b42c 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -22,7 +22,7 @@ }, .zeit = .{ .url = "https://github.com/rockorager/zeit/archive/refs/heads/main.tar.gz", - .hash = "122022233835adc719535a8e7cefdd2902a67bbfb7ef198441ca9ce89c0593f488c2", + .hash = "zeit-0.6.0-5I6bk5daAgC-P60TjxRqW0bYknfCGxJp-03eS9UjGrO7", }, }, .paths = .{ diff --git a/src/app/database/Schema.zig b/src/app/database/Schema.zig index 63ea28e..231c283 100644 --- a/src/app/database/Schema.zig +++ b/src/app/database/Schema.zig @@ -115,7 +115,7 @@ pub const Scrobble = jetquery.Model( struct { id: i32, song_id: i32, - album_id: ?i32, + album_id: i32, date: jetquery.DateTime, created_at: jetquery.DateTime, updated_at: jetquery.DateTime, diff --git a/src/app/database/migrations/2025-02-17_22-38-46_create_scrobbles.zig b/src/app/database/migrations/2025-02-17_22-38-46_create_scrobbles.zig index ac9a04c..9764d99 100644 --- a/src/app/database/migrations/2025-02-17_22-38-46_create_scrobbles.zig +++ b/src/app/database/migrations/2025-02-17_22-38-46_create_scrobbles.zig @@ -8,7 +8,7 @@ pub fn up(repo: anytype) !void { &.{ t.primaryKey("id", .{}), t.column("song_id", .integer, .{}), - t.column("album_id", .integer, .{ .optional = true }), + t.column("album_id", .integer, .{}), t.column("date", .datetime, .{}), t.timestamps(.{}), }, diff --git a/src/app/views/albums.zig b/src/app/views/albums.zig index 84bcde8..69413f8 100644 --- a/src/app/views/albums.zig +++ b/src/app/views/albums.zig @@ -8,14 +8,15 @@ pub fn index(request: *jetzig.Request) !jetzig.View { const query = jetzig.database.Query(.Album) .select(.{ .id, .name }) .include(.albumartists, .{ .select = .{.artist_id} }) + .include(.scrobbles, .{ .select = .{.id} }) .orderBy(.{ .name = .asc }); const albums = try request.repo.all(query); for (albums) |album| { - const scrobbles = try jetzig.database.Query(.Scrobble) - .where(.{ .album_id = album.id }) - .count() - .execute(request.repo); + //const scrobbles = try jetzig.database.Query(.Scrobble) + // .where(.{ .album_id = album.id }) + // .count() + // .execute(request.repo); var album_view = try albums_view.append(.object); var artist_infos = try album_view.put("artist_info", .array); @@ -31,7 +32,8 @@ pub fn index(request: *jetzig.Request) !jetzig.View { try album_view.put("name", album.name); try album_view.put("url", album.id); - try album_view.put("scrobbles", scrobbles); + //try album_view.put("scrobbles", scrobbles); + try album_view.put("scrobbles", (album.scrobbles).len); } return request.render(.ok); } diff --git a/src/app/views/artists.zig b/src/app/views/artists.zig index f007b85..545b7a4 100644 --- a/src/app/views/artists.zig +++ b/src/app/views/artists.zig @@ -7,17 +7,18 @@ pub fn index(request: *jetzig.Request) !jetzig.View { var artists_view = try root.put("artists", .array); const query = jetzig.database.Query(.Artist) .select(.{ .id, .name }) + .include(.scrobbleartists, .{ .select = .{.id} }) .orderBy(.{ .name = .asc }); const artists = try request.repo.all(query); for (artists) |artist| { - const scrobbles = try jetzig.database.Query(.Scrobbleartist) - .where(.{ .artist_id = artist.id }) - .count() - .execute(request.repo); + //const scrobbles = try jetzig.database.Query(.Scrobbleartist) + // .where(.{ .artist_id = artist.id }) + // .count() + // .execute(request.repo); var artist_view = try artists_view.append(.object); try artist_view.put("name", artist.name); try artist_view.put("url", artist.id); - try artist_view.put("scrobbles", scrobbles); + try artist_view.put("scrobbles", (artist.scrobbleartists).len); } return request.render(.ok); diff --git a/src/app/views/songs.zig b/src/app/views/songs.zig index 2064433..fbb7d99 100644 --- a/src/app/views/songs.zig +++ b/src/app/views/songs.zig @@ -7,14 +7,15 @@ pub fn index(request: *jetzig.Request) !jetzig.View { const query = jetzig.database.Query(.Song) .select(.{ .id, .name }) .include(.songartists, .{ .select = .{.artist_id} }) + .include(.scrobbles, .{ .select = .{.id} }) .orderBy(.{ .name = .asc }); const songs = try request.repo.all(query); for (songs) |song| { - const scrobbles = try jetzig.database.Query(.Scrobble) - .where(.{ .song_id = song.id }) - .count() - .execute(request.repo); + //const scrobbles = try jetzig.database.Query(.Scrobble) + // .where(.{ .song_id = song.id }) + // .count() + // .execute(request.repo); var song_view = try songs_view.append(.object); var artist_infos = try song_view.put("artist_info", .array); @@ -29,7 +30,7 @@ pub fn index(request: *jetzig.Request) !jetzig.View { } try song_view.put("name", song.name); try song_view.put("url", song.id); - try song_view.put("scrobbles", scrobbles); + try song_view.put("scrobbles", (song.scrobbles).len); } return request.render(.ok); }