diff --git a/src/app/views/albums.zig b/src/app/views/albums.zig index af72456..9c6063e 100644 --- a/src/app/views/albums.zig +++ b/src/app/views/albums.zig @@ -3,31 +3,81 @@ 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 + ; - const albums = try queries.entityQueryResult(request, queries.generateQuery(.album, .entities), .{}, .array); - try root.put("albums", albums); + var albums_jq_result = try request.repo.executeSql(query, .{}); + defer albums_jq_result.deinit(); + 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 album = try queries.entityQueryResult(request, queries.generateQuery(.album, .entity_info), .{id}, .object); - try root.put("album", album.get("entity_info")); + 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 songs = try queries.entityQueryResult(request, queries.generateQuery(.album, .entity_items), .{id}, .array); - try root.put("songs", songs); + var songs_js_result = try request.repo.executeSql(query, .{id}); + defer songs_js_result.deinit(); - const firstlast = try queries.entityQueryResult(request, queries.generateQuery(.album, .firstlast), .{id}, .array); - try root.put("firstlast", firstlast); + const Song = struct { name: []const u8, id: i32, scrobbles: i64 }; - const timescale = try queries.entityQueryResult(request, queries.generateQuery(.album, .timescale), .{id}, .array); - try root.put("yearly", timescale); + 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, + }; + try songs_view.append(row); + } return request.render(.ok); } diff --git a/src/app/views/albums/get.zmpl b/src/app/views/albums/get.zmpl index 1024ec5..3a1641b 100644 --- a/src/app/views/albums/get.zmpl +++ b/src/app/views/albums/get.zmpl @@ -10,10 +10,6 @@ @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.zig b/src/app/views/artists.zig index 37ca155..9fd9dcd 100644 --- a/src/app/views/artists.zig +++ b/src/app/views/artists.zig @@ -4,13 +4,35 @@ 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); - const artists = try queries.entityQueryResult(request, queries.generateQuery(.artist, .entities), .{}, .array); + var artists_view = try root.put("artists", .array); - try root.put("artists", artists); + 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); + } return request.render(.ok); } @@ -18,20 +40,136 @@ 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 artist = try queries.entityQueryResult(request, queries.generateQuery(.artist, .entity_info), .{id}, .object); - try root.put("artist", artist.get("entity_info")); + 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 albums = try queries.entityQueryResult(request, queries.generateQuery(.artist, .entity_items), .{id}, .array); - try root.put("albums", albums); + 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 appears = try queries.entityQueryResult(request, queries.generateQuery(.artist, .appears), .{id}, .array); - try root.put("appears", appears); + var artist_jq_result = try request.repo.executeSql(artist_info_query, .{id}); + defer artist_jq_result.deinit(); - const firstlast = try queries.entityQueryResult(request, queries.generateQuery(.artist, .firstlast), .{id}, .array); - try root.put("firstlast", firstlast); + 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 timescale = try queries.entityQueryResult(request, queries.generateQuery(.artist, .timescale), .{id}, .array); - try root.put("yearly", timescale); + 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(); return request.render(.ok); } diff --git a/src/app/views/artists/get.zmpl b/src/app/views/artists/get.zmpl index 69bd661..48c1c25 100644 --- a/src/app/views/artists/get.zmpl +++ b/src/app/views/artists/get.zmpl @@ -10,9 +10,8 @@ @partial partials/header

{{.artist.name}}

-@partial partials/firstlast_listens(scrobbles: .artist.scrobbles, rank: .artist.rank, firstlast: .firstlast) -

Yearly Performance

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

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 ed74361..322a0e8 100644 --- a/src/app/views/partials/_firstlast_listens.zmpl +++ b/src/app/views/partials/_firstlast_listens.zmpl @@ -1,13 +1,9 @@ -@args scrobbles: i64, rank: []const u8, firstlast: *ZmplValue - -@zig { - const songs = firstlast.items(.array); -} +@args scrobbles: i64, rank: []const u8, last_song: *ZmplValue, first_song: *ZmplValue
{{scrobbles}} scrobbles ({{rank}} place)
-First listen: {{songs[0].name}} ({{songs[0].date}}) +First listen: {{first_song.name}} ({{first_song.date}})
-Most recent listen: {{songs[1].name}} ({{songs[1].date}}) +Most recent listen: {{last_song.name}} ({{last_song.date}})
\ No newline at end of file diff --git a/src/app/views/partials/_timescale.zmpl b/src/app/views/partials/_timescale.zmpl deleted file mode 100644 index fdec7a1..0000000 --- a/src/app/views/partials/_timescale.zmpl +++ /dev/null @@ -1,18 +0,0 @@ -@args range: *ZmplValue - - - - - - - - - -@for (range) |itm| { - - - - -} - -
YearScrobbles
{{itm.year}}:{{itm.scrobbles}}
\ No newline at end of file diff --git a/src/app/views/scrobbles.zig b/src/app/views/scrobbles.zig index 3fe946a..321af90 100644 --- a/src/app/views/scrobbles.zig +++ b/src/app/views/scrobbles.zig @@ -3,7 +3,6 @@ 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); @@ -36,7 +35,8 @@ pub fn index(request: *jetzig.Request) !jetzig.View { try artistlist.append(.{ .name = scrobble.artist_name, .id = scrobble.artist_id }); continue :blk; } else { - const date = try Utils.dateFmt(request.allocator, scrobble.date); + 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"); 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, + .date = date.items, }; try scrobbles_view.append(row); diff --git a/src/app/views/songs.zig b/src/app/views/songs.zig index d5a7944..f91b5ed 100644 --- a/src/app/views/songs.zig +++ b/src/app/views/songs.zig @@ -1,13 +1,51 @@ const std = @import("std"); const jetzig = @import("jetzig"); -const queries = @import("../../queries.zig"); +const TableRow = @import("../../types.zig").TableRow; +const HyperlinkData = @import("../../types.zig").HyperlinkData; pub fn index(request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); + var songs_view = try root.put("songs", .array); - const songs = try queries.entityQueryResult(request, queries.generateQuery(.song, .entities), .{}, .array); - try root.put("songs", songs); + 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 + ; + 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/app/views/upload.zig b/src/app/views/upload.zig index af5d41d..c86e6f0 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("{}: {}", .{ page, r }); + std.log.debug("{}", .{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); diff --git a/src/queries.zig b/src/queries.zig deleted file mode 100644 index cc5137b..0000000 --- a/src/queries.zig +++ /dev/null @@ -1,343 +0,0 @@ -// 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 - , - }, - }, - }; -}