Fix albums not being hashed correctly

Also provides more actions for rules, but they don't seem to work...
This commit is contained in:
mitteneer 2025-04-27 15:48:47 -04:00
parent 5e58e81ca7
commit 18d4df0a5c
3 changed files with 43 additions and 38 deletions

View file

@ -18,6 +18,9 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig
//_ = env;
if (params.getT(.array, "scrobbles")) |scrobbles| {
for (scrobbles.items()) |item| {
// Probably want to include artist name here, but not sure how to yet
const track_artist_count = item.getT(.array, "artists_track").?.count();
const album_artist_count = item.getT(.array, "artists_album").?.count();
var track_artist_name_buffer = try allocator.alloc([]const u8, track_artist_count);
@ -25,6 +28,16 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig
var track_artist_id_buffer = try allocator.alloc(i32, track_artist_count);
var album_artist_id_buffer = try allocator.alloc(i32, album_artist_count);
const scrobble: Data.Scrobble = .{
.track = item.getT(.string, "track").?,
.artists_track = track_artist_name_buffer,
.album = item.getT(.string, "album") orelse "",
.artists_album = album_artist_name_buffer,
.date = @as(u64, @bitCast(@as(i64, @truncate(item.getT(.integer, "date").? * 1000)))),
};
var id_prehash = std.ArrayList(u8).init(allocator);
for (item.getT(.array, "artists_track").?.items(), 0..track_artist_count) |artist, i| {
const artist_name = try artist.coerce([]const u8);
track_artist_name_buffer[i] = artist_name;
@ -35,19 +48,9 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig
const artist_name = try artist.coerce([]const u8);
album_artist_name_buffer[i] = artist_name;
album_artist_id_buffer[i] = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(artist_name)));
try id_prehash.appendSlice(artist_name);
}
const scrobble: Data.Scrobble = .{
.track = item.getT(.string, "track").?,
.artists_track = track_artist_name_buffer,
.album = item.getT(.string, "album") orelse "",
.artists_album = album_artist_name_buffer,
.date = @as(u64, @bitCast(@as(i64, @truncate(item.getT(.integer, "date").? * 1000)))),
};
// Probably want to include artist name here, but not sure how to yet
var id_prehash = std.ArrayList(u8).init(allocator);
try id_prehash.appendSlice(scrobble.album);
const album_id = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(id_prehash.items)));
try id_prehash.appendSlice(scrobble.track);

View file

@ -22,6 +22,8 @@ pub fn edit(id: []const u8, request: *jetzig.Request) !jetzig.View {
pub fn post(request: *jetzig.Request) !jetzig.View {
const params = try request.params();
std.log.debug("{s}", .{try params.toJson()});
var job = try request.job("process_rule");
_ = try job.params.put("name", params.get("rule-title"));
@ -30,6 +32,7 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
var conditionals = try job.params.put("conditionals", .array);
inline for (0..5) |i| {
if (!std.mem.eql(u8, "", params.getT(.string, comptime std.fmt.comptimePrint("match-txt{}", .{i})).?)) {
//if (params.getT(.string, comptime std.fmt.comptimePrint("match-txt{}", .{i})) != null) {
var cond = try conditionals.append(.object);
try cond.put("match_on", params.get(comptime std.fmt.comptimePrint("match-on{}", .{i})));
try cond.put("match_cond", params.get(comptime std.fmt.comptimePrint("match-cond{}", .{i})));
@ -38,11 +41,15 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
}
var actions = try job.params.put("actions", .array);
var act0 = try actions.append(.object);
try act0.put("action", params.get("action"));
try act0.put("action_on", params.get("action-on"));
try act0.put("action_txt", params.get("action-txt"));
inline for (0..5) |i| {
if (!std.mem.eql(u8, "", params.getT(.string, comptime std.fmt.comptimePrint("action-txt{}", .{i})).?)) {
//if (params.getT(.string, comptime std.fmt.comptimePrint("action-txt{}", .{i})) != null) {
var act = try actions.append(.object);
try act.put("action", params.get(comptime std.fmt.comptimePrint("action{}", .{i})));
try act.put("action_on", params.get(comptime std.fmt.comptimePrint("action-on{}", .{i})));
try act.put("action_txt", params.get(comptime std.fmt.comptimePrint("action-txt{}", .{i})));
}
}
try job.schedule();
return request.render(.created);

View file

@ -14,8 +14,8 @@ Add a rule below.
<br>
Match
<select name="cond-req" id="cond-req">
<option value="any">any</option>
<option value="all">all</option>
<option value="any">any</option>
<option value="all">all</option>
</select>
conditonals.
<br>
@ -38,27 +38,22 @@ If
<input type="checkbox" name="accent-sens{{i}}" id="accent-sens{{i}}">
<br>
}
<button type="button" onclick="condAdd()">
Add Conditional
</button>
<script>
</script>
<div id="cond-ins"></div>
then
<select name="action" id="action">
<option value="replace">replace</option>
<option value="add">add</option>
</select>
<select name="action-on" id="action-on">
<option value="artists_track">artist (song)</option>
<option value="artists_album">artist (album)</option>
<option value="album">album</option>
<option value="track">song</option>
</select>
with
<input type="text" name="action-txt" id="action-txt">
@for (0..5) |i| {
<select name="action{{i}}" id="action{{i}}">
<option value="replace">replace</option>
<option value="add">add</option>
</select>
<select name="action-on{{i}}" id="action-on{{i}}">
<option value="artists_track">artist (song)</option>
<option value="artists_album">artist (album)</option>
<option value="album">album</option>
<option value="track">song</option>
</select>
with
<input type="text" name="action-txt{{i}}" id="action-txt{{i}}">
<br>
}
<button type="submit" value="Submit">Submit</button>
</form>