Move rule loading in upload.zig into a function

This commit is contained in:
mitteneer 2025-07-14 17:10:59 -04:00
parent 280cba2f9a
commit b0f7884f84
2 changed files with 21 additions and 29 deletions

View file

@ -13,10 +13,6 @@ pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
}
pub fn post(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object);
//const params = try request.params();
const UploadParams = struct {
source: enum { LFMW, LFMS, Spotify },
earliest_date: ?[]const u8,
@ -26,29 +22,8 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
const params = (try request.expectParams(UploadParams)).?;
const rule_file = try (std.fs.cwd().openFile("rules.json", .{ .mode = .read_only }) catch |err| switch (err) {
error.FileNotFound => std.fs.cwd().createFile("rules.json", .{ .read = true }),
else => err,
});
defer rule_file.close();
const rule_file_content = try rule_file.readToEndAlloc(request.allocator, 16_000_000);
const rule_list = std.json.parseFromSliceLeaky([]Data.Rule, request.allocator, rule_file_content, .{}) catch null;
//var job = try request.job("process_scrobbles");
var job = try request.job("process_scrobbles2");
// We can parse the dates better
const latest_ts = if (params.latest_date) |ld|
(try zeit.instant(.{ .source = .{ .iso8601 = ld } })).timestamp
else
(try zeit.instant(.{ .source = .now })).timestamp;
const earliest_ts = if (params.earliest_date) |ed|
(try zeit.instant(.{ .source = .{ .iso8601 = ed } })).timestamp
else
(try zeit.instant(.{ .source = .{ .unix_timestamp = 0 } })).timestamp;
var view_params = try root.put("scrobbles", .array);
const latest_ts = (try zeit.instant(.{ .source = if (params.latest_date) |ld| .{ .iso8601 = ld } else .now })).timestamp;
const earliest_ts = (try zeit.instant(.{ .source = if (params.earliest_date) |ed| .{ .iso8601 = ed } else .{ .unix_timestamp = 0 } })).timestamp;
var skipped_tracks: u64 = 0;
var limited_tracks: u64 = 0;
@ -87,6 +62,12 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
},
};
var root = try request.data(.object);
var view_params = try root.put("scrobbles", .array);
var job = try request.job("process_scrobbles2");
const rule_list = try Utils.loadRules(request.allocator);
var artists = try job.params.put("artists", .object);
var albums = try job.params.put("albums", .object);
var tracks = try job.params.put("tracks", .object);
@ -121,9 +102,8 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
const complete_scrobble = if (rule_list) |rl| try rules.applyScrobbleRule(request.allocator, filtered_scrobble, rl) else filtered_scrobble;
const row = try Utils.scrobbleToRow(request.allocator, complete_scrobble);
try view_params.append(row);
//try job_params.append(complete_scrobble);
var stored_artist_hashes = std.ArrayList(u64).init(request.allocator);
var album_hash_string = std.ArrayList(u8).init(request.allocator);

View file

@ -67,6 +67,18 @@ const ScrobbleFields = enum {
irrelevant, // Not a field I care about
};
pub fn loadRules(allocator: std.mem.Allocator) !?[]Data.Rule {
const rule_file: std.fs.File = try (std.fs.cwd().openFile("rules.json", .{ .mode = .read_only }) catch |err| switch (err) {
error.FileNotFound => std.fs.cwd().createFile("rules.json", .{ .read = true }),
else => err,
});
defer rule_file.close();
const rule_file_size = try rule_file.stat().size;
const rule_file_content = try rule_file.readToEndAlloc(allocator, rule_file_size);
return std.json.parseFromSliceLeaky([]Data.Rule, allocator, rule_file_content, .{}) catch null;
}
pub fn scrobbleIngest(allocator: std.mem.Allocator, input: []const u8) ![]Data.UnifiedScrobble {
var scanner = std.json.Scanner.initCompleteInput(allocator, input);
defer scanner.deinit();