Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Sources/DocAutomation/WorkflowRun.swift3// Description: What a workflow execution leaves behind: the per-step record, warnings, outputs, structured errors and progress values.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import DocModel9import DocContainer10import DocAudit1112/// A failure of one step, naming the step and the file it was working on.13public struct WorkflowError: Error, Sendable, Hashable, Codable, CustomStringConvertible {14 public enum Kind: String, Sendable, Codable {15 case invalidWorkflow, missingInput, importFailed, operationFailed, auditFailed, exportFailed, outputExists, pathEscapesOutputFolder, cancelled16 }17 public var kind: Kind18 /// 0-based index of the step, nil when the failure precedes the first step.19 public var stepIndex: Int?20 public var stepName: String21 /// The input file (or the name of the passed-in document).22 public var file: String23 public var message: String2425 public init(kind: Kind, stepIndex: Int?, stepName: String, file: String, message: String) {26 self.kind = kind; self.stepIndex = stepIndex; self.stepName = stepName; self.file = file; self.message = message27 }2829 public var description: String {30 let step = stepIndex.map { "step \($0 + 1) (\(stepName))" } ?? "workflow"31 return file.isEmpty ? "\(step): \(message)" : "\(file): \(step): \(message)"32 }33}3435/// A converter or step warning, with the step that produced it.36public struct WorkflowWarning: Sendable, Hashable, Codable, Identifiable {37 public var stepIndex: Int38 public var severity: FormatWarning.Severity39 public var feature: String40 public var detail: String41 public var count: Int42 public var id: String { "\(stepIndex)|\(severity.rawValue)|\(feature)|\(detail)" }4344 public init(stepIndex: Int, severity: FormatWarning.Severity, feature: String, detail: String, count: Int = 1) {45 self.stepIndex = stepIndex; self.severity = severity; self.feature = feature; self.detail = detail; self.count = count46 }47 init(_ w: FormatWarning, step: Int) { self.init(stepIndex: step, severity: w.severity, feature: w.feature, detail: w.detail, count: w.count) }48}4950public struct WorkflowStepRecord: Sendable, Hashable, Codable {51 public enum Outcome: String, Sendable, Codable { case succeeded, skipped, failed, cancelled, notRun }52 public var index: Int53 public var type: String54 public var name: String55 public var outcome: Outcome56 /// Seconds.57 public var duration: Double58 /// What the step did ("3 changes accepted"), or why it was skipped or failed.59 public var detail: String60}6162public struct AuditSummary: Sendable, Hashable, Codable {63 public var errors: Int64 public var warnings: Int65 public var infos: Int66 public var total: Int { errors + warnings + infos }67 public init(errors: Int = 0, warnings: Int = 0, infos: Int = 0) { self.errors = errors; self.warnings = warnings; self.infos = infos }68 public init(_ findings: [AuditFinding]) {69 self.init(errors: findings.filter { $0.severity == .error }.count, warnings: findings.filter { $0.severity == .warning }.count,70 infos: findings.filter { $0.severity == .info }.count)71 }72}7374/// The record of one execution of a workflow on one input.75public struct WorkflowRun: Sendable, Hashable, Codable, Identifiable {76 public enum Status: String, Sendable, Codable { case succeeded, failed, cancelled }77 public var id: String78 public var workflowID: String79 public var workflowName: String80 /// Path of the input file, or the name of the passed-in document.81 public var input: String82 public var started: Date83 public var ended: Date84 public var status: Status85 public var steps: [WorkflowStepRecord]86 public var warnings: [WorkflowWarning]87 /// Paths of the files written.88 public var outputs: [String]89 public var audit: AuditSummary?90 public var error: WorkflowError?9192 public var duration: Double { ended.timeIntervalSince(started) }93 public var inputName: String { (input as NSString).lastPathComponent }94}9596/// Progress of one run. `fraction` is the share of the steps completed (0…1).97public struct WorkflowProgress: Sendable, Hashable {98 public enum Phase: String, Sendable { case started, finished }99 public var input: String100 public var stepIndex: Int101 public var stepCount: Int102 public var stepName: String103 public var phase: Phase104 public var fraction: Double105}106107/// Progress of a batch: which file, and its own step progress (nil when the file has just finished).108public struct BatchProgress: Sendable {109 public var fileIndex: Int110 public var fileCount: Int111 public var input: URL112 public var step: WorkflowProgress?113 /// Set when the file is done.114 public var finished: WorkflowRun?115 /// Files finished so far, this one included.116 public var completedFiles: Int117}118119public struct BatchResult: Sendable {120 /// One record per input, in input order. Inputs never started because of a cancellation are marked `cancelled`.121 public var runs: [WorkflowRun]122 public var started: Date123 public var ended: Date124 public var duration: Double { ended.timeIntervalSince(started) }125 public var succeeded: [WorkflowRun] { runs.filter { $0.status == .succeeded } }126 public var failed: [WorkflowRun] { runs.filter { $0.status == .failed } }127 public var cancelled: [WorkflowRun] { runs.filter { $0.status == .cancelled } }128}129