Compare commits

..

3 commits

Author SHA1 Message Date
7f3778e82f 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?
2025-05-24 13:59:28 -04:00
09f542e26e Add timescale partial
Bad name, idk what else to call it
2025-05-24 13:58:31 -04:00
1734e6a4bb Fix date formatting in scrobble view 2025-05-20 16:29:53 -04:00
10 changed files with 406 additions and 262 deletions

View file

@ -3,81 +3,31 @@ const jetzig = @import("jetzig");
const jetquery = @import("jetzig").jetquery; const jetquery = @import("jetzig").jetquery;
const TableRow = @import("../../types.zig").TableRow; const TableRow = @import("../../types.zig").TableRow;
const HyperlinkData = @import("../../types.zig").HyperlinkData; const HyperlinkData = @import("../../types.zig").HyperlinkData;
const queries = @import("../../queries.zig");
pub fn index(request: *jetzig.Request) !jetzig.View { pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object); 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, .{}); const albums = try queries.entityQueryResult(request, queries.generateQuery(.album, .entities), .{}, .array);
defer albums_jq_result.deinit(); 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); return request.render(.ok);
} }
pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object); 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 = const album = try queries.entityQueryResult(request, queries.generateQuery(.album, .entity_info), .{id}, .object);
\\SELECT songs.name, songs.id, COUNT(scrobbles) AS scrobbles try root.put("album", album.get("entity_info"));
\\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
;
var songs_js_result = try request.repo.executeSql(query, .{id}); const songs = try queries.entityQueryResult(request, queries.generateQuery(.album, .entity_items), .{id}, .array);
defer songs_js_result.deinit(); 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 timescale = try queries.entityQueryResult(request, queries.generateQuery(.album, .timescale), .{id}, .array);
const song = try song_row.to(Song, .{ .dupe = true, .allocator = request.allocator }); try root.put("yearly", timescale);
const row = TableRow{
.song = .{ .name = song.name, .id = song.id },
.scrobbles = song.scrobbles,
};
try songs_view.append(row);
}
return request.render(.ok); return request.render(.ok);
} }

View file

@ -10,6 +10,10 @@
<body> <body>
@partial partials/header @partial partials/header
<h1>{{.album}}</h1> <h1>{{.album}}</h1>
@partial partials/firstlast_listens(scrobbles: .album.scrobbles, rank: .album.rank, firstlast: .firstlast)
<h3>Yearly Performance</h3>
@partial partials/timescale(range: .yearly)
<h2>Songs</h2>
@partial partials/newtable(T: ColumnChoices, table_data: .songs, columns: columns) @partial partials/newtable(T: ColumnChoices, table_data: .songs, columns: columns)
</body> </body>
</html> </html>

View file

@ -4,35 +4,13 @@ const jetquery = @import("jetzig").jetquery;
const TableRow = @import("../../types.zig").TableRow; const TableRow = @import("../../types.zig").TableRow;
const dateFmt = @import("../../date_fmt.zig").dateFmt; const dateFmt = @import("../../date_fmt.zig").dateFmt;
const ordinalFmt = @import("../../ordinal_fmt.zig").ordinalFmt; const ordinalFmt = @import("../../ordinal_fmt.zig").ordinalFmt;
const queries = @import("../../queries.zig");
pub fn index(request: *jetzig.Request) !jetzig.View { pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object); 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 = try root.put("artists", artists);
\\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); 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 { pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object); var root = try request.data(.object);
const ArtistResult = struct { name: []const u8, id: i32, scrobbles: i64, rank: i64 }; const artist = try queries.entityQueryResult(request, queries.generateQuery(.artist, .entity_info), .{id}, .object);
const AlbumsResult = struct { name: []const u8, id: i32, scrobbles: i64 }; try root.put("artist", artist.get("entity_info"));
const ScrobbleResult = struct { name: []const u8, id: i32, date: i64 };
const artist_info_query = const albums = try queries.entityQueryResult(request, queries.generateQuery(.artist, .entity_items), .{id}, .array);
\\SELECT * FROM try root.put("albums", albums);
\\(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
;
var artist_jq_result = try request.repo.executeSql(artist_info_query, .{id}); const appears = try queries.entityQueryResult(request, queries.generateQuery(.artist, .appears), .{id}, .array);
defer artist_jq_result.deinit(); try root.put("appears", appears);
if (try artist_jq_result.postgresql.result.next()) |artist_row| { const firstlast = try queries.entityQueryResult(request, queries.generateQuery(.artist, .firstlast), .{id}, .array);
const artist = try artist_row.to(ArtistResult, .{ .dupe = true, .allocator = request.allocator }); try root.put("firstlast", firstlast);
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 albums_query = const timescale = try queries.entityQueryResult(request, queries.generateQuery(.artist, .timescale), .{id}, .array);
\\SELECT albums.name AS name, albums.id AS id, COUNT(scrobbles) AS scrobbles try root.put("yearly", timescale);
\\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); return request.render(.ok);
} }

View file

@ -10,8 +10,9 @@
<body> <body>
@partial partials/header @partial partials/header
<h1>{{.artist.name}}</h1> <h1>{{.artist.name}}</h1>
@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)
<h3>Yearly Performance</h3>
@partial partials/timescale(range: .yearly)
<h2>Albums</h2> <h2>Albums</h2>
@partial partials/newtable(T: ColumnChoices, table_data: .albums, columns: columns) @partial partials/newtable(T: ColumnChoices, table_data: .albums, columns: columns)

View file

@ -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);
}
<div> <div>
{{scrobbles}} scrobbles ({{rank}} place) {{scrobbles}} scrobbles ({{rank}} place)
<br> <br>
First listen: <a href="/songs/{{first_song.id}}">{{first_song.name}}</a> ({{first_song.date}}) First listen: <a href="/songs/{{songs[0].id}}">{{songs[0].name}}</a> ({{songs[0].date}})
<br> <br>
Most recent listen: <a href="/songs/{{last_song.id}}">{{last_song.name}}</a> ({{last_song.date}}) Most recent listen: <a href="/songs/{{songs[1].id}}">{{songs[1].name}}</a> ({{songs[1].date}})
</div> </div>

View file

@ -0,0 +1,18 @@
@args range: *ZmplValue
<table>
<thead>
<tr>
<th>Year</th>
<th>Scrobbles</th>
</tr>
</thead>
<tbody>
@for (range) |itm| {
<tr>
<td>{{itm.year}}:</td>
<td>{{itm.scrobbles}}</td>
</tr>
}
</tbody>
</table>

View file

@ -3,6 +3,7 @@ const jetzig = @import("jetzig");
const zeit = @import("zeit"); const zeit = @import("zeit");
const TableRow = @import("../../types.zig").TableRow; const TableRow = @import("../../types.zig").TableRow;
const HyperlinkData = @import("../../types.zig").HyperlinkData; const HyperlinkData = @import("../../types.zig").HyperlinkData;
const Utils = @import("../../date_fmt.zig");
pub fn index(request: *jetzig.Request) !jetzig.View { pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object); 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 }); try artistlist.append(.{ .name = scrobble.artist_name, .id = scrobble.artist_id });
continue :blk; continue :blk;
} else { } else {
var date = std.ArrayList(u8).init(request.allocator); const date = try Utils.dateFmt(request.allocator, scrobble.date);
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 }); 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 }, .song = .{ .name = scrobble.song_name, .id = scrobble.song_id },
.album = .{ .name = scrobble.album_name, .id = scrobble.album_id }, .album = .{ .name = scrobble.album_name, .id = scrobble.album_id },
.artistlist = try artistlist.toOwnedSlice(), .artistlist = try artistlist.toOwnedSlice(),
.date = date.items, .date = date,
}; };
try scrobbles_view.append(row); try scrobbles_view.append(row);

View file

@ -1,51 +1,13 @@
const std = @import("std"); const std = @import("std");
const jetzig = @import("jetzig"); const jetzig = @import("jetzig");
const TableRow = @import("../../types.zig").TableRow; const queries = @import("../../queries.zig");
const HyperlinkData = @import("../../types.zig").HyperlinkData;
pub fn index(request: *jetzig.Request) !jetzig.View { pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object); var root = try request.data(.object);
var songs_view = try root.put("songs", .array);
const query = const songs = try queries.entityQueryResult(request, queries.generateQuery(.song, .entities), .{}, .array);
\\SELECT songs.name, songs.id, artists.name, artists.id, COUNT(scrobbles) AS scrobbles try root.put("songs", songs);
\\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); return request.render(.ok);
} }

View file

@ -66,7 +66,7 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
if (max_pages != null and page > max_pages.?) break; 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 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 } } }); 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 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 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); //const current_page = try std.fmt.parseInt(usize, parsed_lastfm_response.recenttracks.@"@attr".page, 10);

343
src/queries.zig Normal file
View file

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