From 1734e6a4bbf83062c56dc5a4c50687cbf665c859 Mon Sep 17 00:00:00 2001 From: mitteneer Date: Tue, 20 May 2025 16:29:53 -0400 Subject: [PATCH 1/3] Fix date formatting in scrobble view --- src/app/views/scrobbles.zig | 6 +++--- src/app/views/upload.zig | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/views/scrobbles.zig b/src/app/views/scrobbles.zig index 321af90..3fe946a 100644 --- a/src/app/views/scrobbles.zig +++ b/src/app/views/scrobbles.zig @@ -3,6 +3,7 @@ const jetzig = @import("jetzig"); const zeit = @import("zeit"); const TableRow = @import("../../types.zig").TableRow; const HyperlinkData = @import("../../types.zig").HyperlinkData; +const Utils = @import("../../date_fmt.zig"); pub fn index(request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); @@ -35,8 +36,7 @@ pub fn index(request: *jetzig.Request) !jetzig.View { try artistlist.append(.{ .name = scrobble.artist_name, .id = scrobble.artist_id }); continue :blk; } else { - var date = std.ArrayList(u8).init(request.allocator); - try (try zeit.instant(.{ .source = .{ .unix_timestamp = @divFloor(scrobble.date, 1_000) } })).time().strftime(date.writer(), "%d %b %Y, %H:%M"); + const date = try Utils.dateFmt(request.allocator, scrobble.date); try artistlist.append(.{ .name = scrobble.artist_name, .id = scrobble.artist_id }); @@ -44,7 +44,7 @@ pub fn index(request: *jetzig.Request) !jetzig.View { .song = .{ .name = scrobble.song_name, .id = scrobble.song_id }, .album = .{ .name = scrobble.album_name, .id = scrobble.album_id }, .artistlist = try artistlist.toOwnedSlice(), - .date = date.items, + .date = date, }; try scrobbles_view.append(row); diff --git a/src/app/views/upload.zig b/src/app/views/upload.zig index c86e6f0..af5d41d 100644 --- a/src/app/views/upload.zig +++ b/src/app/views/upload.zig @@ -66,7 +66,7 @@ pub fn post(request: *jetzig.Request) !jetzig.View { if (max_pages != null and page > max_pages.?) break; const query: []const u8 = try std.fmt.allocPrint(request.allocator, "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={s}&api_key=b0c410a48a6078a651e0832699e3cd41&from={}&to={}&page={}&limit=1000&format=json", .{ username, earliest_timestamp, latest_timestamp, page }); const r = try client.fetch(.{ .response_storage = .{ .dynamic = &lastfm_response_buffer }, .location = .{ .url = query }, .method = .GET, .headers = .{ .user_agent = .{ .override = user_agent } } }); - std.log.debug("{}", .{r}); + std.log.debug("{}: {}", .{ page, r }); const response_string = try lastfm_response_buffer.toOwnedSlice(); const parsed_lastfm_response = try std.json.parseFromSliceLeaky(Data.LastFMWeb, request.allocator, response_string, .{ .ignore_unknown_fields = true }); //const current_page = try std.fmt.parseInt(usize, parsed_lastfm_response.recenttracks.@"@attr".page, 10); From 09f542e26e84b6a1e2dd9e5abed7a20d5547a7be Mon Sep 17 00:00:00 2001 From: mitteneer Date: Sat, 24 May 2025 13:58:31 -0400 Subject: [PATCH 2/3] Add timescale partial Bad name, idk what else to call it --- src/app/views/albums/get.zmpl | 4 ++++ src/app/views/artists/get.zmpl | 5 +++-- src/app/views/partials/_firstlast_listens.zmpl | 10 +++++++--- src/app/views/partials/_timescale.zmpl | 18 ++++++++++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 src/app/views/partials/_timescale.zmpl diff --git a/src/app/views/albums/get.zmpl b/src/app/views/albums/get.zmpl index 3a1641b..1024ec5 100644 --- a/src/app/views/albums/get.zmpl +++ b/src/app/views/albums/get.zmpl @@ -10,6 +10,10 @@ @partial partials/header

{{.album}}

+@partial partials/firstlast_listens(scrobbles: .album.scrobbles, rank: .album.rank, firstlast: .firstlast) +

Yearly Performance

+@partial partials/timescale(range: .yearly) +

Songs

@partial partials/newtable(T: ColumnChoices, table_data: .songs, columns: columns) \ No newline at end of file diff --git a/src/app/views/artists/get.zmpl b/src/app/views/artists/get.zmpl index 48c1c25..69bd661 100644 --- a/src/app/views/artists/get.zmpl +++ b/src/app/views/artists/get.zmpl @@ -10,8 +10,9 @@ @partial partials/header

{{.artist.name}}

-@partial partials/firstlast_listens(scrobbles: .artist.scrobbles, rank: .artist.rank, last_song: .last, first_song: .first) - +@partial partials/firstlast_listens(scrobbles: .artist.scrobbles, rank: .artist.rank, firstlast: .firstlast) +

Yearly Performance

+@partial partials/timescale(range: .yearly)

Albums

@partial partials/newtable(T: ColumnChoices, table_data: .albums, columns: columns) diff --git a/src/app/views/partials/_firstlast_listens.zmpl b/src/app/views/partials/_firstlast_listens.zmpl index 322a0e8..ed74361 100644 --- a/src/app/views/partials/_firstlast_listens.zmpl +++ b/src/app/views/partials/_firstlast_listens.zmpl @@ -1,9 +1,13 @@ -@args scrobbles: i64, rank: []const u8, last_song: *ZmplValue, first_song: *ZmplValue +@args scrobbles: i64, rank: []const u8, firstlast: *ZmplValue + +@zig { + const songs = firstlast.items(.array); +}
{{scrobbles}} scrobbles ({{rank}} place)
-First listen: {{first_song.name}} ({{first_song.date}}) +First listen: {{songs[0].name}} ({{songs[0].date}})
-Most recent listen: {{last_song.name}} ({{last_song.date}}) +Most recent listen: {{songs[1].name}} ({{songs[1].date}})
\ No newline at end of file diff --git a/src/app/views/partials/_timescale.zmpl b/src/app/views/partials/_timescale.zmpl new file mode 100644 index 0000000..fdec7a1 --- /dev/null +++ b/src/app/views/partials/_timescale.zmpl @@ -0,0 +1,18 @@ +@args range: *ZmplValue + + + + + + + + + +@for (range) |itm| { + + + + +} + +
YearScrobbles
{{itm.year}}:{{itm.scrobbles}}
\ No newline at end of file From 7f3778e82f74627bfe992fd03193acec06adf9ce Mon Sep 17 00:00:00 2001 From: mitteneer Date: Sat, 24 May 2025 13:59:28 -0400 Subject: [PATCH 3/3] Move SQL logic to separate function Idk if this makes any sense, and I don't really like the code atm, but the view .zig files lookk nicer? --- src/app/views/albums.zig | 72 ++------ src/app/views/artists.zig | 164 ++---------------- src/app/views/songs.zig | 44 +---- src/queries.zig | 343 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 370 insertions(+), 253 deletions(-) create mode 100644 src/queries.zig diff --git a/src/app/views/albums.zig b/src/app/views/albums.zig index 9c6063e..af72456 100644 --- a/src/app/views/albums.zig +++ b/src/app/views/albums.zig @@ -3,81 +3,31 @@ const jetzig = @import("jetzig"); const jetquery = @import("jetzig").jetquery; const TableRow = @import("../../types.zig").TableRow; const HyperlinkData = @import("../../types.zig").HyperlinkData; +const queries = @import("../../queries.zig"); pub fn index(request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); - var albums_view = try root.put("albums", .array); - const query = - \\SELECT albums.name, albums.id, artists.name, artists.id, COUNT(scrobbles) AS scrobbles - \\FROM albumsongs - \\INNER JOIN albums ON albumsongs.album_id = albums.id - \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong - \\INNER JOIN artistalbums ON artistalbums.album_id = albums.id - \\INNER JOIN artists ON artists.id = artistalbums.artist_id - \\GROUP BY albums.id, artists.id - \\ORDER BY scrobbles DESC - ; - var albums_jq_result = try request.repo.executeSql(query, .{}); - defer albums_jq_result.deinit(); + const albums = try queries.entityQueryResult(request, queries.generateQuery(.album, .entities), .{}, .array); + try root.put("albums", albums); - const Album = struct { name: []const u8, id: i32, artist_name: []const u8, artist_id: i32, scrobbles: i64 }; - - var prev_album_id: ?i32 = null; - - var row: ?TableRow = null; - var artistlist = std.ArrayList(HyperlinkData).init(request.allocator); - - blk: while (try albums_jq_result.postgresql.result.next()) |album_row| { - const album = try album_row.to(Album, .{ .dupe = true, .allocator = request.allocator }); - if (album.id == prev_album_id) { - try artistlist.append(.{ .name = album.artist_name, .id = album.artist_id }); - continue :blk; - } else { - try artistlist.append(.{ .name = album.artist_name, .id = album.artist_id }); - - row = TableRow{ - .album = .{ .name = album.name, .id = album.id }, - .artistlist = try artistlist.toOwnedSlice(), - .scrobbles = album.scrobbles, - }; - - try albums_view.append(row); - } - prev_album_id = album.id; - } return request.render(.ok); } pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); - var songs_view = try root.put("songs", .array); - const album_name = try jetzig.database.Query(.Album).find(id).select(.{ .id, .name }).execute(request.repo); - _ = try root.put("album", album_name.?.name); - const query = - \\SELECT songs.name, songs.id, COUNT(scrobbles) AS scrobbles - \\FROM albumsongs - \\INNER JOIN songs ON albumsongs.song_id = songs.id - \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id - \\WHERE albumsongs.album_id = $1 - \\GROUP BY songs.id - \\ORDER BY scrobbles DESC - ; + const album = try queries.entityQueryResult(request, queries.generateQuery(.album, .entity_info), .{id}, .object); + try root.put("album", album.get("entity_info")); - var songs_js_result = try request.repo.executeSql(query, .{id}); - defer songs_js_result.deinit(); + const songs = try queries.entityQueryResult(request, queries.generateQuery(.album, .entity_items), .{id}, .array); + try root.put("songs", songs); - const Song = struct { name: []const u8, id: i32, scrobbles: i64 }; + const firstlast = try queries.entityQueryResult(request, queries.generateQuery(.album, .firstlast), .{id}, .array); + try root.put("firstlast", firstlast); - while (try songs_js_result.postgresql.result.next()) |song_row| { - const song = try song_row.to(Song, .{ .dupe = true, .allocator = request.allocator }); - const row = TableRow{ - .song = .{ .name = song.name, .id = song.id }, - .scrobbles = song.scrobbles, - }; + const timescale = try queries.entityQueryResult(request, queries.generateQuery(.album, .timescale), .{id}, .array); + try root.put("yearly", timescale); - try songs_view.append(row); - } return request.render(.ok); } diff --git a/src/app/views/artists.zig b/src/app/views/artists.zig index 9fd9dcd..37ca155 100644 --- a/src/app/views/artists.zig +++ b/src/app/views/artists.zig @@ -4,35 +4,13 @@ const jetquery = @import("jetzig").jetquery; const TableRow = @import("../../types.zig").TableRow; const dateFmt = @import("../../date_fmt.zig").dateFmt; const ordinalFmt = @import("../../ordinal_fmt.zig").ordinalFmt; +const queries = @import("../../queries.zig"); pub fn index(request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); - var artists_view = try root.put("artists", .array); + const artists = try queries.entityQueryResult(request, queries.generateQuery(.artist, .entities), .{}, .array); - const query = - \\SELECT artists.name, artists.id, COUNT(scrobbles) AS scrobbles - \\FROM albumsongsartists - \\INNER JOIN artists ON albumsongsartists.artist_id = artists.id - \\INNER JOIN albumsongs ON albumsongsartists.albumsong_id = albumsongs.id - \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id - \\GROUP BY artists.id - \\ORDER BY scrobbles DESC; - ; - - var artists_jq_result = try request.repo.executeSql(query, .{}); - defer artists_jq_result.deinit(); - - const Artist = struct { name: []const u8, id: i32, scrobbles: i64 }; - - while (try artists_jq_result.postgresql.result.next()) |artist_row| { - const artist = try artist_row.to(Artist, .{ .dupe = true, .allocator = request.allocator }); - const row = TableRow{ - .artist = .{ .name = artist.name, .id = artist.id }, - .scrobbles = artist.scrobbles, - }; - - try artists_view.append(row); - } + try root.put("artists", artists); return request.render(.ok); } @@ -40,136 +18,20 @@ pub fn index(request: *jetzig.Request) !jetzig.View { pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); - const ArtistResult = struct { name: []const u8, id: i32, scrobbles: i64, rank: i64 }; - const AlbumsResult = struct { name: []const u8, id: i32, scrobbles: i64 }; - const ScrobbleResult = struct { name: []const u8, id: i32, date: i64 }; + const artist = try queries.entityQueryResult(request, queries.generateQuery(.artist, .entity_info), .{id}, .object); + try root.put("artist", artist.get("entity_info")); - const artist_info_query = - \\SELECT * FROM - \\(SELECT *, ROW_NUMBER() OVER (ORDER BY scrobbles DESC) AS rank FROM - \\(SELECT artists.name AS name, artists.id AS id, COUNT(scrobbles) AS scrobbles - \\FROM albumsongsartists - \\INNER JOIN artists ON albumsongsartists.artist_id = artists.id - \\INNER JOIN albumsongs ON albumsongsartists.albumsong_id = albumsongs.id - \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id - \\GROUP BY artists.id)) - \\WHERE id = $1 - ; + const albums = try queries.entityQueryResult(request, queries.generateQuery(.artist, .entity_items), .{id}, .array); + try root.put("albums", albums); - var artist_jq_result = try request.repo.executeSql(artist_info_query, .{id}); - defer artist_jq_result.deinit(); + const appears = try queries.entityQueryResult(request, queries.generateQuery(.artist, .appears), .{id}, .array); + try root.put("appears", appears); - if (try artist_jq_result.postgresql.result.next()) |artist_row| { - const artist = try artist_row.to(ArtistResult, .{ .dupe = true, .allocator = request.allocator }); - var artist_view = try root.put("artist", .object); - try artist_view.put("name", artist.name); - try artist_view.put("id", artist.id); - try artist_view.put("scrobbles", artist.scrobbles); - try artist_view.put("rank", ordinalFmt(request.allocator, artist.rank)); - } - try artist_jq_result.drain(); + const firstlast = try queries.entityQueryResult(request, queries.generateQuery(.artist, .firstlast), .{id}, .array); + try root.put("firstlast", firstlast); - const albums_query = - \\SELECT albums.name AS name, albums.id AS id, COUNT(scrobbles) AS scrobbles - \\FROM artistalbums - \\INNER JOIN albums ON albums.id = artistalbums.album_id - \\INNER JOIN albumsongs ON albumsongs.album_id = albums.id - \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id - \\WHERE artistalbums.artist_id = $1 - \\GROUP BY albums.id - \\ORDER BY scrobbles DESC - ; - - var albums_view = try root.put("albums", .array); - - var albums_jq_result = try request.repo.executeSql(albums_query, .{id}); - defer albums_jq_result.deinit(); - - while (try albums_jq_result.postgresql.result.next()) |album_row| { - const album = try album_row.to(AlbumsResult, .{ .dupe = true, .allocator = request.allocator }); - const album_table_row = TableRow{ - .album = .{ .name = album.name, .id = album.id }, - .scrobbles = album.scrobbles, - }; - - try albums_view.append(album_table_row); - } - //albums_jq_result.drain(); - - const appears_query = - \\SELECT albums.name AS name, albums.id AS id, COUNT(scrobbles) AS scrobbles - \\FROM artistalbums - \\INNER JOIN albums ON albums.id = artistalbums.album_id - \\INNER JOIN albumsongs ON albumsongs.album_id = albums.id - \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id - \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id - \\WHERE albumsongsartists.artist_id = $1 AND artistalbums.artist_id != $1 - \\GROUP BY albums.id - \\ORDER BY scrobbles DESC; - ; - - var appears_view = try root.put("appears", .array); - - var appears_jq_result = try request.repo.executeSql(appears_query, .{id}); - defer appears_jq_result.deinit(); - - while (try appears_jq_result.postgresql.result.next()) |appears_row| { - const appears = try appears_row.to(AlbumsResult, .{ .dupe = true, .allocator = request.allocator }); - const appears_table_row = TableRow{ - .album = .{ .name = appears.name, .id = appears.id }, - .scrobbles = appears.scrobbles, - }; - - try appears_view.append(appears_table_row); - } - - //appears_jq_result.drain(); - - const first_last_songs_query = - \\(SELECT songs.name AS name, songs.id AS id, scrobbles.datetime AS date - \\FROM albumsongs - \\INNER JOIN songs ON songs.id = albumsongs.song_id - \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong - \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id - \\WHERE albumsongsartists.artist_id = $1 - \\ORDER BY scrobbles.datetime DESC - \\LIMIT 1) - \\ - \\UNION ALL - \\ - \\(SELECT songs.name AS name, songs.id AS id, scrobbles.datetime AS date - \\FROM albumsongs - \\INNER JOIN songs ON songs.id = albumsongs.song_id - \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong - \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id - \\WHERE albumsongsartists.artist_id = $1 - \\ORDER BY scrobbles.datetime ASC - \\LIMIT 1) - ; - - var first_last_songs_jq_result = try request.repo.executeSql(first_last_songs_query, .{id}); - defer first_last_songs_jq_result.deinit(); - - // These look backwards, but it's correct this way - var last_song_view = try root.put("last", .object); - - if (try first_last_songs_jq_result.postgresql.result.next()) |last_song_row| { - const last_song = try last_song_row.to(ScrobbleResult, .{ .dupe = true, .allocator = request.allocator }); - try last_song_view.put("name", last_song.name); - try last_song_view.put("id", last_song.id); - try last_song_view.put("date", (try dateFmt(request.allocator, last_song.date))); - } else unreachable; - - var first_song_view = try root.put("first", .object); - - if (try first_last_songs_jq_result.postgresql.result.next()) |first_song_row| { - const first_song = try first_song_row.to(ScrobbleResult, .{ .dupe = true, .allocator = request.allocator }); - try first_song_view.put("name", first_song.name); - try first_song_view.put("id", first_song.id); - try first_song_view.put("date", (try dateFmt(request.allocator, first_song.date))); - } else unreachable; - - try first_last_songs_jq_result.drain(); + const timescale = try queries.entityQueryResult(request, queries.generateQuery(.artist, .timescale), .{id}, .array); + try root.put("yearly", timescale); return request.render(.ok); } diff --git a/src/app/views/songs.zig b/src/app/views/songs.zig index f91b5ed..d5a7944 100644 --- a/src/app/views/songs.zig +++ b/src/app/views/songs.zig @@ -1,51 +1,13 @@ const std = @import("std"); const jetzig = @import("jetzig"); -const TableRow = @import("../../types.zig").TableRow; -const HyperlinkData = @import("../../types.zig").HyperlinkData; +const queries = @import("../../queries.zig"); pub fn index(request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); - var songs_view = try root.put("songs", .array); - const query = - \\SELECT songs.name, songs.id, artists.name, artists.id, COUNT(scrobbles) AS scrobbles - \\FROM albumsongs - \\INNER JOIN songs ON albumsongs.song_id = songs.id - \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id - \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id - \\INNER JOIN artists ON artists.id = albumsongsartists.artist_id - \\GROUP BY songs.id, artists.id - \\ORDER BY scrobbles DESC, songs.name ASC - ; + const songs = try queries.entityQueryResult(request, queries.generateQuery(.song, .entities), .{}, .array); + try root.put("songs", songs); - var songs_js_result = try request.repo.executeSql(query, .{}); - defer songs_js_result.deinit(); - - const Song = struct { name: []const u8, id: i32, artist_name: []const u8, artist_id: i32, scrobbles: i64 }; - - var prev_song_id: ?i32 = null; - - var row: ?TableRow = null; - var artistlist = std.ArrayList(HyperlinkData).init(request.allocator); - - blk: while (try songs_js_result.postgresql.result.next()) |song_row| { - const song = try song_row.to(Song, .{ .dupe = true, .allocator = request.allocator }); - if (song.id == prev_song_id) { - try artistlist.append(.{ .name = song.artist_name, .id = song.artist_id }); - continue :blk; - } else { - try artistlist.append(.{ .name = song.artist_name, .id = song.artist_id }); - - row = TableRow{ - .song = .{ .name = song.name, .id = song.id }, - .artistlist = try artistlist.toOwnedSlice(), - .scrobbles = song.scrobbles, - }; - - try songs_view.append(row); - } - prev_song_id = song.id; - } return request.render(.ok); } diff --git a/src/queries.zig b/src/queries.zig new file mode 100644 index 0000000..cc5137b --- /dev/null +++ b/src/queries.zig @@ -0,0 +1,343 @@ +// Probably the worst code in the project +const jetzig = @import("jetzig"); +const TableRow = @import("types.zig").TableRow; +const HyperlinkData = @import("types.zig").HyperlinkData; +const std = @import("std"); + +pub fn entityQueryResult(request: *jetzig.Request, query: GeneratedQuery, args: anytype, root_type: enum { object, array }) !*jetzig.Data.Value { + var result = try request.repo.executeSql(query.query, args); + + defer result.deinit(); + + var Data = jetzig.Data.init(request.allocator); + var out: *jetzig.Data.Value = switch (root_type) { + .array => try Data.array(), + .object => try Data.object(), + }; + + var artist_list = if (query.ResultType == EntitiesAlbumResult or query.ResultType == EntitiesArtistResult or query.ResultType == EntitiesScrobbleResult or query.ResultType == EntitiesSongResult) + std.ArrayList(HyperlinkData).init(request.allocator); + + blk: while (try result.postgresql.result.next()) |entity_row| { + switch (query.ResultType) { + FirstlastResult, TimescaleResult, EntitiesScrobbleResult, EntitiesSongResult, EntitiesAlbumResult, EntitiesArtistResult, EntityItemsResult, AppearsResult, EntityInfoResult => |T| { + const entity = try entity_row.to(T, .{ .dupe = true, .allocator = request.allocator }); + const item: ?TableRow = switch (query.ResultType) { + EntitiesScrobbleResult, EntitiesSongResult, EntitiesAlbumResult, EntitiesArtistResult => switch (query.entity) { + .artist => TableRow{ .artist = .{ .name = entity.name, .id = entity.id }, .scrobbles = entity.scrobbles }, + .album => album_entities: { + const last_artist = artist_list.getLastOrNull(); + try artist_list.append(.{ .name = entity.artist_name, .id = entity.artist_id }); + if (last_artist) |la| { + if (la.id == entity.artist_id) continue :blk; + } + break :album_entities TableRow{ .album = .{ .name = entity.name, .id = entity.id }, .artistlist = try artist_list.toOwnedSlice(), .scrobbles = entity.scrobbles }; + }, + .song => song_entities: { + const last_artist = artist_list.getLastOrNull(); + try artist_list.append(.{ .name = entity.artist_name, .id = entity.artist_id }); + if (last_artist) |la| { + if (la.id == entity.artist_id) continue :blk; + } + break :song_entities TableRow{ .song = .{ .name = entity.name, .id = entity.id }, .artistlist = try artist_list.toOwnedSlice(), .scrobbles = entity.scrobbles }; + }, + else => unreachable, + }, + EntityItemsResult => switch (query.entity) { + .artist => TableRow{ .album = .{ .name = entity.name, .id = entity.id }, .scrobbles = entity.scrobbles }, + .album => TableRow{ .song = .{ .name = entity.name, .id = entity.id }, .scrobbles = entity.scrobbles }, + else => unreachable, + }, + AppearsResult => switch (query.entity) { + .song, .artist => TableRow{ .album = .{ .name = entity.name, .id = entity.id }, .scrobbles = entity.scrobbles }, + .album, .scrobble => unreachable, + }, + else => null, + }; + + if (item) |itm| { + switch (root_type) { + .array => try out.append(itm), + .object => switch (query.query_type) { + inline else => |qt| try out.put(@tagName(qt), itm), + }, + } + } else { + switch (root_type) { + .array => try out.append(entity), + .object => switch (query.query_type) { + inline else => |qt| try out.put(@tagName(qt), entity), + }, + } + } + }, + else => unreachable, + } + } + if (root_type == .object) {} + return out; +} + +const GeneratedQuery = struct { + entity: EntityType, + query_type: QueryTypeEnum, + ResultType: type, + query: []const u8, +}; + +const EntityType = enum { scrobble, song, album, artist }; +const QueryTypeEnum = enum { firstlast, timescale, entities, entity_items, appears, entity_info }; +const FirstlastResult = struct { name: []const u8, id: i32, date: []const u8 }; +const TimescaleResult = struct { year: []const u8, scrobbles: i64 }; +const EntitiesArtistResult = struct { name: []const u8, id: i32, scrobbles: i64 }; +const EntitiesAlbumResult = struct { name: []const u8, id: i32, artist_name: []const u8, artist_id: i32, scrobbles: i64 }; +const EntitiesSongResult = struct { name: []const u8, id: i32, artist_name: []const u8, artist_id: i32, scrobbles: i64 }; +const EntitiesScrobbleResult = struct { song_name: []const u8, song_id: i32, album_name: []const u8, album_id: i32, artist_name: []const u8, artist_id: i32, s_id: i32, date: i64 }; +const EntityItemsResult = struct { name: []const u8, id: i32, scrobbles: i64 }; +const AppearsResult = struct { name: []const u8, id: i32, scrobbles: i64 }; +const EntityInfoResult = struct { name: []const u8, id: i32, scrobbles: i64, rank: []const u8 }; + +pub fn generateQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery { + return GeneratedQuery{ + .entity = entity, + .query_type = query_type, + .ResultType = switch (query_type) { + .firstlast => FirstlastResult, + .timescale => TimescaleResult, + .entities => switch (entity) { + .scrobble => EntitiesScrobbleResult, + .song => EntitiesSongResult, + .album => EntitiesAlbumResult, + .artist => EntitiesArtistResult, + }, + .entity_items => EntityItemsResult, + .appears => AppearsResult, + .entity_info => EntityInfoResult, + }, + .query = switch (query_type) { + .firstlast => + //.ResultType = FirstlastResult, + switch (entity) { + .scrobble => unreachable, + .song => + \\(SELECT songs.name AS name, songs.id AS id, scrobbles.datetime AS date + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\WHERE albumsongsartists.artist_id = $1 + \\ORDER BY scrobbles.datetime ASC + \\LIMIT 1) + \\ + \\UNION ALL + \\ + \\(SELECT songs.name AS name, songs.id AS id, scrobbles.datetime AS date + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\WHERE albumsongsartists.artist_id = $1 + \\ORDER BY scrobbles.datetime DESC + \\LIMIT 1) + , + + .album => + \\(SELECT songs.name AS name, songs.id AS id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MM:SS') AS date + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albums ON albums.id = albumsongs.album_id + \\WHERE albums.id = $1 + \\ORDER BY scrobbles.datetime ASC + \\LIMIT 1) + \\ + \\UNION ALL + \\ + \\(SELECT songs.name AS name, songs.id AS id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MM:SS') AS date + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albums ON albums.id = albumsongs.album_id + \\WHERE albums.id = $1 + \\ORDER BY scrobbles.datetime DESC + \\LIMIT 1) + , + + .artist => + \\(SELECT songs.name AS name, songs.id AS id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MM:SS') AS date + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\WHERE albumsongsartists.artist_id = $1 + \\ORDER BY scrobbles.datetime ASC + \\LIMIT 1) + \\ + \\UNION ALL + \\ + \\(SELECT songs.name AS name, songs.id AS id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MM:SS') AS date + \\FROM albumsongs + \\INNER JOIN songs ON songs.id = albumsongs.song_id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\WHERE albumsongsartists.artist_id = $1 + \\ORDER BY scrobbles.datetime DESC + \\LIMIT 1) + , + }, + + .timescale => + //.ResultType = TimescaleResult, + switch (entity) { + .scrobble => unreachable, + .song => + \\SELECT TO_CHAR(date_trunc('year', datetime), 'YYYY') AS year, COUNT(*) as scrobbles + \\FROM scrobbles + \\INNER JOIN albumsongs ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\INNER JOIN artists ON artists.id = albumsongsartists.artist_id + \\WHERE artists.id = $1 + \\GROUP BY year + \\ORDER BY year ASC; + , + + .album => + \\SELECT TO_CHAR(date_trunc('year', datetime), 'YYYY') AS year, COUNT(*) as scrobbles + \\FROM scrobbles + \\INNER JOIN albumsongs ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albums ON albums.id = albumsongs.album_id + \\WHERE albums.id = $1 + \\GROUP BY year + \\ORDER BY year ASC; + , + + .artist => + \\SELECT TO_CHAR(date_trunc('year', datetime), 'YYYY') AS year, COUNT(*) as scrobbles + \\FROM scrobbles + \\INNER JOIN albumsongs ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\INNER JOIN artists ON artists.id = albumsongsartists.artist_id + \\WHERE artists.id = $1 + \\GROUP BY year + \\ORDER BY year ASC; + , + }, + + .entities => + //.ResultType = EntitiesResult, + switch (entity) { + .scrobble => + \\none + , + .song => + \\SELECT songs.name, songs.id, artists.name, artists.id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongs + \\INNER JOIN songs ON albumsongs.song_id = songs.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\INNER JOIN artists ON artists.id = albumsongsartists.artist_id + \\GROUP BY songs.id, artists.id + \\ORDER BY scrobbles DESC, songs.name ASC + , + .album => + \\SELECT albums.name, albums.id, artists.name, artists.id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongs + \\INNER JOIN albums ON albumsongs.album_id = albums.id + \\INNER JOIN scrobbles ON albumsongs.id = scrobbles.albumsong + \\INNER JOIN artistalbums ON artistalbums.album_id = albums.id + \\INNER JOIN artists ON artists.id = artistalbums.artist_id + \\GROUP BY albums.id, artists.id + \\ORDER BY scrobbles DESC + , + .artist => + \\SELECT artists.name AS name, artists.id AS id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongsartists + \\INNER JOIN artists ON albumsongsartists.artist_id = artists.id + \\INNER JOIN albumsongs ON albumsongsartists.albumsong_id = albumsongs.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\GROUP BY artists.id + \\ORDER BY scrobbles DESC; + , + }, + + .appears => + //.ResultType = AppearsResult, + switch (entity) { + .scrobble => unreachable, + .song => + \\ NOTHING YET + , + .album => + \\nope + , + .artist => + \\SELECT albums.name AS name, albums.id AS id, COUNT(scrobbles) AS scrobbles + \\FROM artistalbums + \\INNER JOIN albums ON albums.id = artistalbums.album_id + \\INNER JOIN albumsongs ON albumsongs.album_id = albums.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id + \\WHERE albumsongsartists.artist_id = $1 AND artistalbums.artist_id != $1 + \\GROUP BY albums.id + \\ORDER BY scrobbles DESC; + , + }, + + .entity_items => + //.ResultType = EntityItemsResult, + switch (entity) { + .scrobble => unreachable, + .song => + \\get all scrobbles + , + .album => + \\SELECT songs.name, songs.id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongs + \\INNER JOIN songs ON albumsongs.song_id = songs.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\WHERE albumsongs.album_id = $1 + \\GROUP BY songs.id + \\ORDER BY scrobbles DESC + , + .artist => + \\SELECT albums.name AS name, albums.id AS id, COUNT(scrobbles) AS scrobbles + \\FROM artistalbums + \\INNER JOIN albums ON albums.id = artistalbums.album_id + \\INNER JOIN albumsongs ON albumsongs.album_id = albums.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\WHERE artistalbums.artist_id = $1 + \\GROUP BY albums.id + \\ORDER BY scrobbles DESC + , + }, + + .entity_info => switch (entity) { + .scrobble => unreachable, + .song => + \\lol idk + , + .album => + \\SELECT * FROM + \\(SELECT *, TO_CHAR(ROW_NUMBER() OVER (ORDER BY scrobbles DESC),'FM9999th') AS rank FROM + \\(SELECT albums.name, albums.id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongs + \\INNER JOIN albums ON albumsongs.album_id = albums.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\GROUP BY albums.id)) + \\WHERE id = $1 + , + .artist => + \\SELECT * FROM + \\(SELECT *, TO_CHAR(ROW_NUMBER() OVER (ORDER BY scrobbles DESC),'FM9999th') AS rank FROM + \\(SELECT artists.name AS name, artists.id AS id, COUNT(scrobbles) AS scrobbles + \\FROM albumsongsartists + \\INNER JOIN artists ON albumsongsartists.artist_id = artists.id + \\INNER JOIN albumsongs ON albumsongsartists.albumsong_id = albumsongs.id + \\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id + \\GROUP BY artists.id)) + \\WHERE id = $1 + , + }, + }, + }; +}