diff --git a/src/app/jobs/process_scrobbles.zig b/src/app/jobs/process_scrobbles.zig index ae3cfa8..b2ae22e 100644 --- a/src/app/jobs/process_scrobbles.zig +++ b/src/app/jobs/process_scrobbles.zig @@ -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); diff --git a/src/app/views/rules.zig b/src/app/views/rules.zig index 957f5d2..7a2df91 100644 --- a/src/app/views/rules.zig +++ b/src/app/views/rules.zig @@ -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); diff --git a/src/app/views/rules/index.zmpl b/src/app/views/rules/index.zmpl index f97ef35..5661f97 100644 --- a/src/app/views/rules/index.zmpl +++ b/src/app/views/rules/index.zmpl @@ -14,8 +14,8 @@ Add a rule below.
Match conditonals.
@@ -38,27 +38,22 @@ If
} - - - - -
then - - -with - +@for (0..5) |i| { + + + with + +
+}