Something went wrong. Try again.
prefect server in zig
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478const std = @import("std");const Allocator = std.mem.Allocator;
const backend = @import("backend.zig");const log = @import("../logging.zig");
pub const ArtifactRow = struct { id: []const u8, created: []const u8, updated: []const u8, key: ?[]const u8, task_run_id: ?[]const u8, flow_run_id: ?[]const u8, type: ?[]const u8, data: ?[]const u8, description: ?[]const u8, metadata_: ?[]const u8,};
pub const ArtifactCollectionRow = struct { id: []const u8, created: []const u8, updated: []const u8, key: []const u8, latest_id: []const u8, task_run_id: ?[]const u8, flow_run_id: ?[]const u8, type: ?[]const u8, data: ?[]const u8, description: ?[]const u8, metadata_: ?[]const u8,};
const Col = struct { const id: usize = 0; const created: usize = 1; const updated: usize = 2; const key: usize = 3; const task_run_id: usize = 4; const flow_run_id: usize = 5; const type_: usize = 6; const data: usize = 7; const description: usize = 8; const metadata_: usize = 9;};
const CCol = struct { const id: usize = 0; const created: usize = 1; const updated: usize = 2; const key: usize = 3; const latest_id: usize = 4; const task_run_id: usize = 5; const flow_run_id: usize = 6; const type_: usize = 7; const data: usize = 8; const description: usize = 9; const metadata_: usize = 10;};
const select_cols = "id, created, updated, key, task_run_id, flow_run_id, type, data, description, metadata_";const collection_select_cols = "id, created, updated, key, latest_id, task_run_id, flow_run_id, type, data, description, metadata_";
fn rowFromResult(alloc: Allocator, r: anytype) !ArtifactRow { return ArtifactRow{ .id = try r.uuid(alloc, Col.id), .created = try r.timestampIso(alloc, Col.created), .updated = try r.timestampIso(alloc, Col.updated), .key = if (r.textOrNull(Col.key)) |v| try alloc.dupe(u8, v) else null, .task_run_id = try r.uuidOrNull(alloc, Col.task_run_id), .flow_run_id = try r.uuidOrNull(alloc, Col.flow_run_id), .type = if (r.textOrNull(Col.type_)) |v| try alloc.dupe(u8, v) else null, .data = if (r.textOrNull(Col.data)) |v| try alloc.dupe(u8, v) else null, .description = if (r.textOrNull(Col.description)) |v| try alloc.dupe(u8, v) else null, .metadata_ = if (r.textOrNull(Col.metadata_)) |v| try alloc.dupe(u8, v) else null, };}
fn collectionRowFromResult(alloc: Allocator, r: anytype) !ArtifactCollectionRow { return ArtifactCollectionRow{ .id = try r.uuid(alloc, CCol.id), .created = try r.timestampIso(alloc, CCol.created), .updated = try r.timestampIso(alloc, CCol.updated), .key = try alloc.dupe(u8, r.text(CCol.key)), .latest_id = try r.uuid(alloc, CCol.latest_id), .task_run_id = try r.uuidOrNull(alloc, CCol.task_run_id), .flow_run_id = try r.uuidOrNull(alloc, CCol.flow_run_id), .type = if (r.textOrNull(CCol.type_)) |v| try alloc.dupe(u8, v) else null, .data = if (r.textOrNull(CCol.data)) |v| try alloc.dupe(u8, v) else null, .description = if (r.textOrNull(CCol.description)) |v| try alloc.dupe(u8, v) else null, .metadata_ = if (r.textOrNull(CCol.metadata_)) |v| try alloc.dupe(u8, v) else null, };}
pub const InsertParams = struct { key: ?[]const u8 = null, task_run_id: ?[]const u8 = null, flow_run_id: ?[]const u8 = null, type: ?[]const u8 = null, data: ?[]const u8 = null, description: ?[]const u8 = null, metadata_: ?[]const u8 = null,};
pub fn insert(id: []const u8, timestamp: []const u8, params: InsertParams) !void { backend.db.exec( \\INSERT INTO artifact (id, created, updated, key, task_run_id, flow_run_id, type, data, description, metadata_) \\VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) , .{ id, timestamp, timestamp, params.key, params.task_run_id, params.flow_run_id, params.type, params.data, params.description, params.metadata_, }) catch |err| { log.err("database", "insert artifact error: {}", .{err}); return err; };}
/// Upsert into artifact_collection — on key conflict, update latest_id and/// the rest of the fields. Mirrors python prefect's/// `_insert_into_artifact_collection`.pub fn upsertCollection( id: []const u8, timestamp: []const u8, key: []const u8, latest_id: []const u8, params: InsertParams,) !void { const sql = if (backend.db.dialect == .sqlite) \\INSERT INTO artifact_collection (id, created, updated, key, latest_id, task_run_id, flow_run_id, type, data, description, metadata_) \\VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) \\ON CONFLICT (key) DO UPDATE SET \\ latest_id = excluded.latest_id, \\ updated = excluded.updated, \\ task_run_id = excluded.task_run_id, \\ flow_run_id = excluded.flow_run_id, \\ type = excluded.type, \\ data = excluded.data, \\ description = excluded.description, \\ metadata_ = excluded.metadata_ else \\INSERT INTO artifact_collection (id, created, updated, key, latest_id, task_run_id, flow_run_id, type, data, description, metadata_) \\VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) \\ON CONFLICT (key) DO UPDATE SET \\ latest_id = EXCLUDED.latest_id, \\ updated = EXCLUDED.updated, \\ task_run_id = EXCLUDED.task_run_id, \\ flow_run_id = EXCLUDED.flow_run_id, \\ type = EXCLUDED.type, \\ data = EXCLUDED.data, \\ description = EXCLUDED.description, \\ metadata_ = EXCLUDED.metadata_ ;
backend.db.exec(sql, .{ id, timestamp, timestamp, key, latest_id, params.task_run_id, params.flow_run_id, params.type, params.data, params.description, params.metadata_, }) catch |err| { log.err("database", "upsert artifact_collection error: {}", .{err}); return err; };}
pub fn getById(alloc: Allocator, id: []const u8) !?ArtifactRow { var r = try backend.db.row( "SELECT " ++ select_cols ++ " FROM artifact WHERE id = ?", .{id}, );
if (r) |*row| { defer row.deinit(); return try rowFromResult(alloc, row); } return null;}
pub fn getLatestByKey(alloc: Allocator, key: []const u8) !?ArtifactCollectionRow { var r = try backend.db.row( "SELECT " ++ collection_select_cols ++ " FROM artifact_collection WHERE key = ?", .{key}, );
if (r) |*row| { defer row.deinit(); return try collectionRowFromResult(alloc, row); } return null;}
pub fn updateById(id: []const u8, data: ?[]const u8, description: ?[]const u8, timestamp: []const u8) !bool { const affected = backend.db.execWithRowCount( "UPDATE artifact SET data = COALESCE(?, data), description = COALESCE(?, description), updated = ? WHERE id = ?", .{ data, description, timestamp, id }, ) catch |err| { log.err("database", "update artifact error: {}", .{err}); return err; }; return affected > 0;}
pub fn deleteById(id: []const u8) !bool { const affected = backend.db.execWithRowCount( "DELETE FROM artifact WHERE id = ?", .{id}, ) catch |err| { log.err("database", "delete artifact error: {}", .{err}); return err; }; return affected > 0;}
// ============================================================// Filtering// ============================================================
/// Artifact filter — mirrors python's `ArtifactFilter`.pub const ArtifactFilter = struct { /// id.any_ — match any of these ids ids_any: ?[]const []const u8 = null, /// id.not_any_ — exclude these ids ids_not_any: ?[]const []const u8 = null, /// key.any_ — exact-match keys keys_any: ?[]const []const u8 = null, /// key.like_ — substring match on key key_like: ?[]const u8 = null, /// key.exists_ — true: key IS NOT NULL, false: key IS NULL key_exists: ?bool = null, /// flow_run_id.any_ flow_run_ids_any: ?[]const []const u8 = null, /// flow_run_id.not_any_ flow_run_ids_not_any: ?[]const []const u8 = null, /// task_run_id.any_ task_run_ids_any: ?[]const []const u8 = null, /// task_run_id.not_any_ task_run_ids_not_any: ?[]const []const u8 = null, /// type.any_ types_any: ?[]const []const u8 = null, /// type.not_any_ types_not_any: ?[]const []const u8 = null,};
/// ArtifactCollection filter — mirrors python's `ArtifactCollectionFilter`.pub const ArtifactCollectionFilter = struct { latest_ids_any: ?[]const []const u8 = null, keys_any: ?[]const []const u8 = null, key_like: ?[]const u8 = null, key_exists: ?bool = null, flow_run_ids_any: ?[]const []const u8 = null, flow_run_ids_not_any: ?[]const []const u8 = null, task_run_ids_any: ?[]const []const u8 = null, task_run_ids_not_any: ?[]const []const u8 = null, types_any: ?[]const []const u8 = null, types_not_any: ?[]const []const u8 = null,};
pub const Sort = enum { id_desc, key_asc, key_desc, updated_desc, created_desc,
pub fn fromString(s: []const u8) Sort { if (std.mem.eql(u8, s, "KEY_ASC")) return .key_asc; if (std.mem.eql(u8, s, "KEY_DESC")) return .key_desc; if (std.mem.eql(u8, s, "UPDATED_DESC")) return .updated_desc; if (std.mem.eql(u8, s, "CREATED_DESC")) return .created_desc; return .id_desc; }
pub fn toOrderBy(self: Sort) []const u8 { return switch (self) { .id_desc => "id DESC", .key_asc => "key ASC", .key_desc => "key DESC", .updated_desc => "updated DESC", .created_desc => "created DESC", }; }};
const Where = struct { sql: []const u8, // " WHERE ... " or "" bindings: []const []const u8,};
fn appendInList(alloc: Allocator, where_parts: *std.ArrayListUnmanaged([]const u8), bind_values: *std.ArrayListUnmanaged([]const u8), col: []const u8, values: []const []const u8, negate: bool) !void { if (values.len == 0) return; var part = std.ArrayListUnmanaged(u8).empty; try part.appendSlice(alloc, col); if (negate) { try part.appendSlice(alloc, " NOT IN ("); } else { try part.appendSlice(alloc, " IN ("); } for (values, 0..) |v, i| { if (i > 0) try part.appendSlice(alloc, ", "); try part.append(alloc, '?'); try bind_values.append(alloc, v); } try part.append(alloc, ')'); try where_parts.append(alloc, try part.toOwnedSlice(alloc));}
fn buildArtifactWhere(alloc: Allocator, filter: ArtifactFilter) !Where { var where_parts = std.ArrayListUnmanaged([]const u8).empty; var bind_values = std.ArrayListUnmanaged([]const u8).empty;
if (filter.ids_any) |v| try appendInList(alloc, &where_parts, &bind_values, "id", v, false); if (filter.ids_not_any) |v| try appendInList(alloc, &where_parts, &bind_values, "id", v, true); if (filter.keys_any) |v| try appendInList(alloc, &where_parts, &bind_values, "key", v, false); if (filter.key_like) |k| { try where_parts.append(alloc, "key LIKE ?"); try bind_values.append(alloc, try std.fmt.allocPrint(alloc, "%{s}%", .{k})); } if (filter.key_exists) |exists| { try where_parts.append(alloc, if (exists) "key IS NOT NULL" else "key IS NULL"); } if (filter.flow_run_ids_any) |v| try appendInList(alloc, &where_parts, &bind_values, "flow_run_id", v, false); if (filter.flow_run_ids_not_any) |v| try appendInList(alloc, &where_parts, &bind_values, "flow_run_id", v, true); if (filter.task_run_ids_any) |v| try appendInList(alloc, &where_parts, &bind_values, "task_run_id", v, false); if (filter.task_run_ids_not_any) |v| try appendInList(alloc, &where_parts, &bind_values, "task_run_id", v, true); if (filter.types_any) |v| try appendInList(alloc, &where_parts, &bind_values, "type", v, false); if (filter.types_not_any) |v| try appendInList(alloc, &where_parts, &bind_values, "type", v, true);
return joinWhere(alloc, where_parts.items, bind_values.items);}
fn buildCollectionWhere(alloc: Allocator, filter: ArtifactCollectionFilter) !Where { var where_parts = std.ArrayListUnmanaged([]const u8).empty; var bind_values = std.ArrayListUnmanaged([]const u8).empty;
if (filter.latest_ids_any) |v| try appendInList(alloc, &where_parts, &bind_values, "latest_id", v, false); if (filter.keys_any) |v| try appendInList(alloc, &where_parts, &bind_values, "key", v, false); if (filter.key_like) |k| { try where_parts.append(alloc, "key LIKE ?"); try bind_values.append(alloc, try std.fmt.allocPrint(alloc, "%{s}%", .{k})); } if (filter.key_exists) |exists| { try where_parts.append(alloc, if (exists) "key IS NOT NULL" else "key IS NULL"); } if (filter.flow_run_ids_any) |v| try appendInList(alloc, &where_parts, &bind_values, "flow_run_id", v, false); if (filter.flow_run_ids_not_any) |v| try appendInList(alloc, &where_parts, &bind_values, "flow_run_id", v, true); if (filter.task_run_ids_any) |v| try appendInList(alloc, &where_parts, &bind_values, "task_run_id", v, false); if (filter.task_run_ids_not_any) |v| try appendInList(alloc, &where_parts, &bind_values, "task_run_id", v, true); if (filter.types_any) |v| try appendInList(alloc, &where_parts, &bind_values, "type", v, false); if (filter.types_not_any) |v| try appendInList(alloc, &where_parts, &bind_values, "type", v, true);
return joinWhere(alloc, where_parts.items, bind_values.items);}
fn joinWhere(alloc: Allocator, parts: []const []const u8, bindings: []const []const u8) !Where { if (parts.len == 0) { return Where{ .sql = "", .bindings = try alloc.dupe([]const u8, bindings) }; } var sql = std.ArrayListUnmanaged(u8).empty; try sql.appendSlice(alloc, " WHERE "); for (parts, 0..) |p, i| { if (i > 0) try sql.appendSlice(alloc, " AND "); try sql.appendSlice(alloc, p); } return Where{ .sql = try sql.toOwnedSlice(alloc), .bindings = try alloc.dupe([]const u8, bindings), };}
/// Interpolate bindings into SQL. Quoted strings with `'` escaping; mirrors/// the events.zig pattern (used because building a typed parameter tuple of/// variable arity is awkward in zig comptime).fn interpolateBindings(alloc: Allocator, sql: []const u8, bindings: []const []const u8) ![]const u8 { var out = std.ArrayListUnmanaged(u8).empty; var bind_idx: usize = 0; for (sql) |c| { if (c == '?' and bind_idx < bindings.len) { try out.append(alloc, '\''); for (bindings[bind_idx]) |vc| { if (vc == '\'') { try out.appendSlice(alloc, "''"); } else { try out.append(alloc, vc); } } try out.append(alloc, '\''); bind_idx += 1; } else { try out.append(alloc, c); } } return out.toOwnedSlice(alloc);}
pub fn listFiltered(alloc: Allocator, f: ArtifactFilter, sort: Sort, limit: usize, offset: usize) ![]ArtifactRow { var results = std.ArrayListUnmanaged(ArtifactRow).empty; errdefer results.deinit(alloc);
const where = try buildArtifactWhere(alloc, f); const sql = try std.fmt.allocPrint( alloc, "SELECT " ++ select_cols ++ " FROM artifact{s} ORDER BY {s} LIMIT {d} OFFSET {d}", .{ where.sql, sort.toOrderBy(), limit, offset }, ); const final_sql = try interpolateBindings(alloc, sql, where.bindings);
var rows = backend.db.query(final_sql, .{}) catch |err| { log.err("database", "filter artifacts error: {}", .{err}); return err; }; defer rows.deinit(); while (try rows.nextChecked()) |r| { try results.append(alloc, try rowFromResult(alloc, &r)); } return results.toOwnedSlice(alloc);}
pub fn listCollectionFiltered(alloc: Allocator, f: ArtifactCollectionFilter, sort: Sort, limit: usize, offset: usize) ![]ArtifactCollectionRow { var results = std.ArrayListUnmanaged(ArtifactCollectionRow).empty; errdefer results.deinit(alloc);
const where = try buildCollectionWhere(alloc, f); const sql = try std.fmt.allocPrint( alloc, "SELECT " ++ collection_select_cols ++ " FROM artifact_collection{s} ORDER BY {s} LIMIT {d} OFFSET {d}", .{ where.sql, sort.toOrderBy(), limit, offset }, ); const final_sql = try interpolateBindings(alloc, sql, where.bindings);
var rows = backend.db.query(final_sql, .{}) catch |err| { log.err("database", "filter artifact_collection error: {}", .{err}); return err; }; defer rows.deinit(); while (try rows.nextChecked()) |r| { try results.append(alloc, try collectionRowFromResult(alloc, &r)); } return results.toOwnedSlice(alloc);}
pub fn countFilter(alloc: Allocator, f: ArtifactFilter) !usize { const where = try buildArtifactWhere(alloc, f); const sql = try std.fmt.allocPrint(alloc, "SELECT COUNT(*) FROM artifact{s}", .{where.sql}); const final_sql = try interpolateBindings(alloc, sql, where.bindings); var r = try backend.db.row(final_sql, .{}); if (r) |*row| { defer row.deinit(); return @intCast(row.bigint(0)); } return 0;}
pub fn countCollectionFilter(alloc: Allocator, f: ArtifactCollectionFilter) !usize { const where = try buildCollectionWhere(alloc, f); const sql = try std.fmt.allocPrint(alloc, "SELECT COUNT(*) FROM artifact_collection{s}", .{where.sql}); const final_sql = try interpolateBindings(alloc, sql, where.bindings); var r = try backend.db.row(final_sql, .{}); if (r) |*row| { defer row.deinit(); return @intCast(row.bigint(0)); } return 0;}