diff --git a/common_queries.md b/common_queries.md new file mode 100644 index 0000000..8344358 --- /dev/null +++ b/common_queries.md @@ -0,0 +1,41 @@ +Get all albums from specified artist: +```sql +SELECT artists.name, albums.name +FROM "Albumartists" +INNER JOIN artists +ON "Albumartists".artist_id = artists.id +INNER JOIN albums +ON "Albumartists".album_id = albums.id +WHERE artists.name = {ARTIST}; +``` + +Get all songs from specified artist: +```sql +SELECT artists.name, songs.name +FROM "Songartists" +INNER JOIN artists +ON "Songartists".artist_id = artists.id +INNER JOIN songs +ON "Songartists".song_id = songs.id +WHERE artists.name = {ARTIST}; +``` + +Get all songs from any album of the specified name: +```sql +SELECT songs.name +FROM "Albumsongs" +INNER JOIN albums +ON "Albumsongs".album_id = albums.id +INNER JOIN songs +ON "Albumsongs".song_id = songs.id +WHERE albums.name = {ALBUM}; +``` + +Sort all songs by plays (does not list artist or album): +```sql +SELECT songs.name, COUNT(scrobbles.song_id) AS scount +FROM songs, scrobbles +WHERE songs.id = scrobbles.song_id +GROUP BY songs.id +ORDER BY scount DESC; +``` diff --git a/src/app/jobs/process_scrobbles.zig b/src/app/jobs/process_scrobbles.zig index 95ebc52..ee308cb 100644 --- a/src/app/jobs/process_scrobbles.zig +++ b/src/app/jobs/process_scrobbles.zig @@ -23,9 +23,19 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig const scrobble: Scrobble = .{ .track = item.getT(.string, "track").?, .artist = item.getT(.string, "artist").?, .album = item.getT(.string, "album") orelse "", .date = @as(u64, @bitCast(@as(i64, @truncate(item.getT(.integer, "date").? * 1000)))) }; // Make hashes - const album_hash = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(scrobble.album))); - const artist_hash = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(scrobble.artist))); - const song_hash = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(scrobble.track))); + //const album_hash = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(scrobble.album))); + //const artist_hash = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(scrobble.artist))); + //const song_hash = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(scrobble.track))); + + // Create a buffer to hold the metadata to hash. Numbers based on the title of a + // particularly long Sufjan Stevens song title, and we're gonna pray the metadata + // does not exceed three times it's length. + var buffer = [_]u8{undefined} ** (288 * 3); + const artist_id = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(scrobble.artist))); + const album_prehash = try std.fmt.bufPrint(&buffer, "{s}{s}", .{ scrobble.artist, scrobble.album }); + const album_id = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(album_prehash))); + const song_prehash = try std.fmt.bufPrint(&buffer, "{s}{s}{s}", .{ scrobble.artist, scrobble.album, scrobble.track }); + const song_id = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(song_prehash))); // Make IDs // Song: Song hash XOR artist hash XOR album hash @@ -50,14 +60,8 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig // then a descriptive string can be provided to // differentiate after the fact, or in a rule. - var album_id: i32 = 0; - const song_id = (song_hash ^ artist_hash ^ album_hash); - if (artist_hash == album_hash) { - album_id = album_hash; - } else { - album_id = (artist_hash ^ album_hash); - } - const artist_id = artist_hash; + //var album_id: i32 = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(formed))); + //const song_id = (song_hash ^ artist_hash ^ album_hash); // Inserts const album_insert = jetzig.database.Query(.Album).insert(.{ .id = album_id, .name = scrobble.album, .length = null }); diff --git a/src/app/views/upload.zig b/src/app/views/upload.zig index 1d4429a..1de242f 100644 --- a/src/app/views/upload.zig +++ b/src/app/views/upload.zig @@ -73,7 +73,7 @@ pub fn post(request: *jetzig.Request) !jetzig.View { // the requirement. Until then, if it goes 30 seconds, or the // reason_end field reads "trackdone", then it counts as a Scrobble. // May consider giving user control to the minimum millisecond requirement. - if (scrobble.reason_end != null and (scrobble.ms_played < 30_000 and !std.mem.eql(u8, scrobble.reason_end.?, "trackdone"))) { + if (scrobble.ms_played < 30_000 and (scrobble.reason_end == null or !std.mem.eql(u8, scrobble.reason_end.?, "trackdone"))) { skipped_tracks += 1; continue :appends; }