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
This commit is contained in:
mitteneer 2025-03-28 13:09:51 -04:00
parent e22dc4c949
commit cf84b4afdd
6 changed files with 22 additions and 18 deletions

View file

@ -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 = .{

View file

@ -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,

View file

@ -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(.{}),
},

View file

@ -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);
}

View file

@ -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);

View file

@ -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);
}