Compare commits
No commits in common. "78e416eeaf95c881e5f17e545a48726458422b4f" and "65136a44d652dbda40b76597ddc59ee34dc83a1a" have entirely different histories.
78e416eeaf
...
65136a44d6
8 changed files with 66 additions and 143 deletions
|
|
@ -17,8 +17,8 @@
|
|||
// internet connectivity.
|
||||
.dependencies = .{
|
||||
.jetzig = .{
|
||||
.url = "https://github.com/jetzig-framework/jetzig/archive/1feb18fb74e626fe068ec67532318640a9cb83be.tar.gz",
|
||||
.hash = "jetzig-0.0.0-IpAgLfMzDwDyAqZ05btcLDd9dfE_bxUbfOI_Wx7a19ed",
|
||||
.url = "https://github.com/jetzig-framework/jetzig/archive/86d82026ab574d4e5c3c6cc3817dda84b510001a.tar.gz",
|
||||
.hash = "jetzig-0.0.0-IpAgLTkzDwDKmsY9MqM41EHDXWGkViiECa0lzV8xl17x",
|
||||
},
|
||||
.zeit = .{
|
||||
.url = "https://github.com/rockorager/zeit/archive/refs/heads/main.tar.gz",
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig
|
|||
.artists_track = track_artist_name_buffer,
|
||||
.album = item.getT(.string, "album") orelse "",
|
||||
.artists_album = album_artist_name_buffer,
|
||||
.date = @as(i64, @truncate(item.getT(.integer, "date").?)),
|
||||
.date = @as(u64, @bitCast(@as(i64, @truncate(item.getT(.integer, "date").? * 1000)))),
|
||||
};
|
||||
|
||||
var id_prehash = std.ArrayList(u8).init(allocator);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
const std = @import("std");
|
||||
const jetzig = @import("jetzig");
|
||||
const jetquery = @import("jetzig").jetquery;
|
||||
const dateFmt = @import("../../date_fmt.zig").dateFmt;
|
||||
|
||||
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||
var root = try request.data(.object);
|
||||
|
|
@ -35,34 +34,12 @@ 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_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
|
||||
;
|
||||
|
||||
var artist_jq_result = try request.repo.executeSql(artist_info_query, .{id});
|
||||
defer artist_jq_result.deinit();
|
||||
|
||||
if (try artist_jq_result.postgresql.result.next()) |artist_row| {
|
||||
const artist = try artist_row.to(ArtistResult, .{ .dupe = true, .allocator = request.allocator });
|
||||
try root.put("artist", artist);
|
||||
}
|
||||
try artist_jq_result.drain();
|
||||
|
||||
const albums_query =
|
||||
\\SELECT albums.name AS name, albums.id AS id, COUNT(scrobbles) AS scrobbles
|
||||
var albums_view = try root.put("albums", .array);
|
||||
var appears = try root.put("appears", .array);
|
||||
const artist_name = try jetzig.database.Query(.Artist).find(id).select(.{ .id, .name }).execute(request.repo);
|
||||
_ = try root.put("artist", artist_name.?.name);
|
||||
const query =
|
||||
\\SELECT albums.name, albums.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
|
||||
|
|
@ -72,19 +49,8 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|||
\\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 });
|
||||
try albums_view.append(album);
|
||||
}
|
||||
//albums_jq_result.drain();
|
||||
|
||||
const appears_query =
|
||||
\\SELECT albums.name AS name, albums.id AS id, COUNT(scrobbles) AS scrobbles
|
||||
\\SELECT albums.name, albums.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
|
||||
|
|
@ -95,60 +61,30 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|||
\\ORDER BY scrobbles DESC;
|
||||
;
|
||||
|
||||
var appears_view = try root.put("appears", .array);
|
||||
var albums_jq_result = try request.repo.executeSql(query, .{id});
|
||||
defer albums_jq_result.deinit();
|
||||
|
||||
const Album = struct { name: []const u8, id: i32, scrobbles: i64 };
|
||||
|
||||
while (try albums_jq_result.postgresql.result.next()) |album_row| {
|
||||
const album = try album_row.to(Album, .{ .dupe = true, .allocator = request.allocator });
|
||||
var album_view = try albums_view.append(.object);
|
||||
try album_view.put("name", album.name);
|
||||
try album_view.put("url", album.id);
|
||||
try album_view.put("scrobbles", album.scrobbles);
|
||||
}
|
||||
|
||||
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 });
|
||||
try appears_view.append(appears);
|
||||
while (try appears_jq_result.postgresql.result.next()) |album_row| {
|
||||
const album = try album_row.to(Album, .{ .dupe = true, .allocator = request.allocator });
|
||||
var album_view = try appears.append(.object);
|
||||
try album_view.put("name", album.name);
|
||||
try album_view.put("url", album.id);
|
||||
try album_view.put("scrobbles", album.scrobbles);
|
||||
}
|
||||
|
||||
//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
|
||||
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 root.put("last_song_name", last_song.name);
|
||||
try root.put("last_song_id", last_song.id);
|
||||
try root.put("last_song_date", (try dateFmt(request.allocator, last_song.date)));
|
||||
}
|
||||
|
||||
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 root.put("first_song_name", first_song.name);
|
||||
try root.put("first_song_id", first_song.id);
|
||||
try root.put("first_song_date", (try dateFmt(request.allocator, first_song.date)));
|
||||
}
|
||||
|
||||
try first_last_songs_jq_result.drain();
|
||||
|
||||
return request.render(.ok);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,12 +5,7 @@
|
|||
</head>
|
||||
<body>
|
||||
@partial partials/header
|
||||
<h1>{{.artist.name}}</h1>
|
||||
{{.artist.scrobbles}} scrobbles ({{.artist.rank}}th place)
|
||||
<br>
|
||||
First listen: <a href="/songs/{{.first_song_id}}">{{.first_song_name}}</a> ({{.first_song_date}})
|
||||
<br>
|
||||
Most recent listen: <a href="/songs/{{.last_song_id}}">{{.last_song_name}}</a> ({{.last_song_date}})
|
||||
<h1>{{.artist}}</h1>
|
||||
<h2>Albums</h2>
|
||||
<table id="myTable">
|
||||
<thead>
|
||||
|
|
@ -21,7 +16,7 @@ Most recent listen: <a href="/songs/{{.last_song_id}}">{{.last_song_name}}</a> (
|
|||
<tbody>
|
||||
@for (.albums) |album| {
|
||||
<tr>
|
||||
<td class=cell><a href="/albums/{{album.id}}">{{album.name}}</a></td>
|
||||
<td class=cell><a href="/albums/{{album.url}}">{{album.name}}</a></td>
|
||||
<td class=cell>{{album.scrobbles}}</td>
|
||||
</tr>
|
||||
}
|
||||
|
|
@ -37,7 +32,7 @@ Most recent listen: <a href="/songs/{{.last_song_id}}">{{.last_song_name}}</a> (
|
|||
<tbody>
|
||||
@for (.appears) |album| {
|
||||
<tr>
|
||||
<td class=cell><a href="/albums/{{album.id}}">{{album.name}}</a></td>
|
||||
<td class=cell><a href="/albums/{{album.url}}">{{album.name}}</a></td>
|
||||
<td class=cell>{{album.scrobbles}}</td>
|
||||
</tr>
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,54 +5,60 @@ const zeit = @import("zeit");
|
|||
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||
var root = try request.data(.object);
|
||||
var scrobbles_view = try root.put("scrobbles", .array);
|
||||
//const query = jetzig.database.Query(.Scrobble)
|
||||
// .select(.{ .id, .date })
|
||||
// .include(.song, .{ .select = .{ .id, .name } })
|
||||
// .include(.album, .{ .select = .{ .id, .name } })
|
||||
// .include(.scrobbleartists, .{ .select = .{.artist_id} })
|
||||
// .orderBy(.{ .date = .desc });
|
||||
//const scrobbles = try request.repo.all(query);
|
||||
|
||||
const query =
|
||||
\\SELECT songs.name, songs.id, albums.name, albums.id, artists.name, artists.id, scrobbles.id, scrobbles.datetime
|
||||
\\SELECT songs.name, songs.id, albums.name, albums.id, scrobbles.datetime
|
||||
\\FROM albumsongs
|
||||
\\INNER JOIN songs ON songs.id = albumsongs.song_id
|
||||
\\INNER JOIN albums ON albums.id = albumsongs.album_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
|
||||
\\ORDER BY scrobbles.datetime ASC
|
||||
;
|
||||
|
||||
var scrobbles_jq_result = try request.repo.executeSql(query, .{});
|
||||
defer scrobbles_jq_result.deinit();
|
||||
var scrobbles_js_result = try request.repo.executeSql(query, .{});
|
||||
defer scrobbles_js_result.deinit();
|
||||
|
||||
const Scrobble = 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 Scrobble = struct { song_name: []const u8, song_id: i32, album_name: []const u8, album_id: i32, date: i64 };
|
||||
|
||||
var prev_s_id: ?i32 = null;
|
||||
var prev_artist_infos: ?*jetzig.zmpl.Data.Value = null;
|
||||
|
||||
blk: while (try scrobbles_jq_result.postgresql.result.next()) |scrobble_row| {
|
||||
while (try scrobbles_js_result.postgresql.result.next()) |scrobble_row| {
|
||||
const scrobble = try scrobble_row.to(Scrobble, .{ .dupe = true, .allocator = request.allocator });
|
||||
if (scrobble.s_id == prev_s_id) {
|
||||
var artist_info = try prev_artist_infos.?.append(.object);
|
||||
try artist_info.put("name", scrobble.artist_name);
|
||||
try artist_info.put("url", scrobble.artist_id);
|
||||
continue :blk;
|
||||
}
|
||||
// Not appending the scrobble directly because we don't want the unix timestamp or scrobble id
|
||||
var scrobble_view = try scrobbles_view.append(.object);
|
||||
|
||||
var artist_infos = try scrobble_view.put("artist_info", .array);
|
||||
var artist_info = try artist_infos.append(.object);
|
||||
try artist_info.put("name", scrobble.artist_name);
|
||||
try artist_info.put("url", scrobble.artist_id);
|
||||
|
||||
try scrobble_view.put("song_name", scrobble.song_name);
|
||||
try scrobble_view.put("song_id", scrobble.song_id);
|
||||
try scrobble_view.put("album_name", scrobble.album_name);
|
||||
try scrobble_view.put("album_id", scrobble.album_id);
|
||||
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 (try zeit.instant(.{ .source = .{ .unix_timestamp = @divFloor(scrobble.date, 1_000_000) } })).time().strftime(date.writer(), "%d %b %Y, %H:%M");
|
||||
try scrobble_view.put("date", date.items);
|
||||
|
||||
prev_artist_infos = artist_infos;
|
||||
prev_s_id = scrobble.s_id;
|
||||
}
|
||||
|
||||
//for (scrobbles) |scrobble| {
|
||||
// var scrobble_view = try scrobbles_view.append(.object);
|
||||
|
||||
// var artist_infos = try scrobble_view.put("artist_info", .array);
|
||||
// for (scrobble.scrobbleartists) |artist| {
|
||||
// var artist_info = try artist_infos.append(.object);
|
||||
// const artist_data = try jetzig.database.Query(.Artist)
|
||||
// .find(artist.artist_id)
|
||||
// .select(.{ .id, .name })
|
||||
// .execute(request.repo);
|
||||
// try artist_info.put("name", artist_data.?.name);
|
||||
// try artist_info.put("id", artist_data.?.id);
|
||||
// }
|
||||
|
||||
// try scrobble_view.put("song_name", scrobble.song.name);
|
||||
// try scrobble_view.put("song_id", scrobble.song.id);
|
||||
// try scrobble_view.put("album_name", scrobble.album.name);
|
||||
// try scrobble_view.put("album_id", scrobble.album.id);
|
||||
// try scrobble_view.put("date", scrobble.date);
|
||||
//}
|
||||
return request.render(.ok);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@
|
|||
<thead>
|
||||
<tr>
|
||||
<th>Song</th>
|
||||
<th>Artist(s)</th>
|
||||
<th>Album</th>
|
||||
<th data-type="date" data-format="DD MMM YYYY, HH:mm">Date</th>
|
||||
</tr>
|
||||
|
|
@ -19,11 +18,6 @@
|
|||
@for (.scrobbles) |scrobble| {
|
||||
<tr>
|
||||
<td class=cell><a href="/songs/{{scrobble.song_id}}">{{scrobble.song_name}}</a></td>
|
||||
<td class=cell>
|
||||
@for (scrobble.get("artist_info").?) |ai| {
|
||||
<a href="/artists/{{ai.url}}">{{ai.name}}</a>
|
||||
}
|
||||
</td>
|
||||
<td class=cell><a href="/albums/{{scrobble.album_id}}">{{scrobble.album_name}}</a></td>
|
||||
<td class=cell>{{scrobble.date}}</td>
|
||||
</tr>
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
const std = @import("std");
|
||||
const zeit = @import("zeit");
|
||||
|
||||
pub fn dateFmt(allocator: std.mem.Allocator, epoch: i64) ![]const u8 {
|
||||
var date = std.ArrayList(u8).init(allocator);
|
||||
try (try zeit.instant(.{ .source = .{ .unix_timestamp = @divFloor(epoch, 1_000) } })).time().strftime(date.writer(), "%d %b %Y, %H:%M");
|
||||
return date.items;
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@ pub const ImportedScrobble = struct {
|
|||
track: []const u8,
|
||||
artist: []const u8,
|
||||
album: []const u8 = "",
|
||||
date: i64,
|
||||
date: i128,
|
||||
};
|
||||
|
||||
pub const Scrobble = struct {
|
||||
|
|
@ -10,7 +10,7 @@ pub const Scrobble = struct {
|
|||
artists_track: []const []const u8,
|
||||
album: []const u8 = "",
|
||||
artists_album: []const []const u8,
|
||||
date: i64,
|
||||
date: i128,
|
||||
};
|
||||
|
||||
// From lastfmstats.com
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue