Use find() instead of where() for song/albums views and add artists to song view

I think this makes things faster, but I may just be comparing to the scrobbles view which is terribly slow
This commit is contained in:
mitteneer 2025-03-28 10:11:07 -04:00
parent 05e9c05742
commit 0522a023b5
4 changed files with 22 additions and 4 deletions

View file

@ -10,6 +10,7 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
.include(.albumartists, .{ .select = .{.artist_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);
var album_view = try albums_view.append(.object);
@ -17,8 +18,8 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
var artist_infos = try album_view.put("artist_info", .array);
for (album.albumartists) |artist| {
var artist_info = try artist_infos.append(.object);
const artist_data = try jetzig.database.Query(.Artist).select(.{ .id, .name }).where(.{ .id = artist.artist_id }).all(request.repo);
for (artist_data) |ad| {
const artist_data = try jetzig.database.Query(.Artist).find(artist.artist_id).select(.{ .id, .name }).execute(request.repo);
if (artist_data) |ad| {
try artist_info.put("name", ad.name);
try artist_info.put("id", ad.id);
}

View file

@ -4,7 +4,7 @@ const jetzig = @import("jetzig");
pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object);
var scrobbles_view = try root.put("scrobbles", .array);
const query = jetzig.database.Query(.Scrobble).select(.{.date})
const query = jetzig.database.Query(.Scrobble).select(.{ .id, .date })
.include(.song, .{ .select = .{ .id, .name } })
.include(.album, .{ .select = .{ .id, .name } })
.include(.scrobbleartists, .{ .select = .{.artist_id} })

View file

@ -9,9 +9,20 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
.include(.songartists, .{ .select = .{.artist_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);
var song_view = try songs_view.append(.object);
var artist_infos = try song_view.put("artist_info", .array);
for (song.songartists) |artist| {
var artist_info = try artist_infos.append(.object);
const artist_data = try jetzig.database.Query(.Artist).find(artist.artist_id).select(.{ .id, .name }).execute(request.repo);
if (artist_data) |ad| {
try artist_info.put("name", ad.name);
try artist_info.put("id", ad.id);
}
}
try song_view.put("name", song.name);
try song_view.put("url", song.id);
try song_view.put("scrobbles", scrobbles);

View file

@ -10,6 +10,7 @@
<thead>
<tr>
<th>Name</th>
<th>Artists(s)</th>
<th>Scrobbles</th>
</tr>
</thead>
@ -17,7 +18,12 @@
@for (.songs) |song| {
<tr>
<td class=cell><a href="/songs/{{song.url}}">{{song.name}}</a></td>
<td>{{song.scrobbles}}</td>
<td class=cell>
@for (song.get("artist_info").?) |ai| {
<a href="/artists/{{ai.id}}">{{ai.name}}</a>
}
</td>
<td class=cell>{{song.scrobbles}}</td>
</tr>
}
</tbody>