Repository Public License
1// ORIX Secure2// File: Packages/ORIXReporting/Sources/ORIXWorkflow/WorkflowRun.swift3// Description: The record of one workflow run (per-step status, durations, summaries, errors) and its report document.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import ORIXSecureModel910public enum WorkflowStepStatus: String, Sendable, Codable, CaseIterable {11 case succeeded12 case failed13 /// Not run because a condition was not met (for instance its review step did not succeed).14 case skipped15 /// The user declined the confirmation.16 case declined17 /// The run was cancelled while the step was waiting or running.18 case cancelled19 case notRun2021 public var title: String {22 switch self {23 case .succeeded: L("Completed")24 case .failed: L("Failed")25 case .skipped: L("Skipped")26 case .declined: L("Declined")27 case .cancelled: L("Cancelled")28 case .notRun: L("Not run")29 }30 }31}3233public struct WorkflowStepRecord: Identifiable, Hashable, Codable, Sendable {34 /// The identifier of the step in the definition.35 public var id: UUID36 public var kind: WorkflowStepKind37 public var title: String38 public var isDestructive: Bool39 public var status: WorkflowStepStatus40 public var startedAt: Date?41 /// Seconds between the start of the step and its end, confirmation wait included.42 public var duration: TimeInterval?43 public var summary: String?44 public var errorText: String?45 public var findingCount: Int4647 public init(step: WorkflowStep, status: WorkflowStepStatus = .notRun, startedAt: Date? = nil, duration: TimeInterval? = nil, summary: String? = nil, errorText: String? = nil, findingCount: Int = 0) {48 self.id = step.id49 self.kind = step.kind50 self.title = step.title51 self.isDestructive = step.isDestructive52 self.status = status53 self.startedAt = startedAt54 self.duration = duration55 self.summary = summary56 self.errorText = errorText57 self.findingCount = findingCount58 }59}6061public struct WorkflowRun: Identifiable, Hashable, Codable, Sendable {62 public enum Status: String, Sendable, Codable, CaseIterable {63 /// Every step completed.64 case succeeded65 /// The workflow reached its end, but some steps failed, were skipped or were declined.66 case completedWithIssues67 /// A step failed and stopped the workflow, or the definition was refused.68 case failed69 /// The user declined a confirmation and the workflow stopped there.70 case declined71 case cancelled7273 public var title: String {74 switch self {75 case .succeeded: L("Completed")76 case .completedWithIssues: L("Completed with issues")77 case .failed: L("Failed")78 case .declined: L("Stopped at a confirmation")79 case .cancelled: L("Cancelled")80 }81 }82 }8384 public var id: UUID85 /// The definition as it was when the run started.86 public var definition: WorkflowDefinition87 public var startedAt: Date88 public var endedAt: Date89 public var status: Status90 public var steps: [WorkflowStepRecord]91 /// Why the workflow did not start, when the definition was refused.92 public var failureReason: String?93 public var findings: [Finding]94 public var sections: [ReportDocument.Section]95 /// The values the steps left in the context.96 public var values: [String: String]9798 public init(id: UUID = UUID(), definition: WorkflowDefinition, startedAt: Date, endedAt: Date, status: Status, steps: [WorkflowStepRecord], failureReason: String? = nil, findings: [Finding] = [], sections: [ReportDocument.Section] = [], values: [String: String] = [:]) {99 self.id = id100 self.definition = definition101 self.startedAt = startedAt102 self.endedAt = endedAt103 self.status = status104 self.steps = steps105 self.failureReason = failureReason106 self.findings = findings107 self.sections = sections108 self.values = values109 }110111 public var duration: TimeInterval { max(0, endedAt.timeIntervalSince(startedAt)) }112113 public func count(_ status: WorkflowStepStatus) -> Int { steps.reduce(0) { $0 + ($1.status == status ? 1 : 0) } }114115 static func durationText(_ seconds: TimeInterval) -> String {116 if seconds < 1 { return ORIXFormat.milliseconds(seconds) }117 return L("\(seconds.formatted(.number.precision(.fractionLength(1)))) s")118 }119120 /// Identifiers of the generated sections derive from the run, so the same run always yields an equal report.121 private func sectionID(_ number: UInt8) -> UUID {122 var bytes = id.uuid123 bytes.15 = bytes.15 &+ number124 bytes.0 = bytes.0 ^ 0xA5125 return UUID(uuid: bytes)126 }127128 /// The run as a report: what was run, what each step said, the sections and findings the steps produced, and the129 /// steps that did not complete.130 public func report() -> ReportDocument {131 let completed = count(.succeeded)132 var overview: [ReportDocument.Fact] = [133 .init(L("Workflow"), definition.name),134 .init(L("Result"), status.title),135 .init(L("Started"), ORIXFormat.dateTime(startedAt)),136 .init(L("Ended"), ORIXFormat.dateTime(endedAt)),137 .init(L("Duration"), Self.durationText(duration)),138 .init(L("Steps completed"), L("\(completed) of \(steps.count)")),139 ]140 if let failureReason { overview.append(.init(L("Reason"), failureReason)) }141142 var stepFacts: [ReportDocument.Fact] = []143 var incomplete: [String] = []144 for (index, step) in steps.enumerated() {145 var parts = [step.status.title]146 if let duration = step.duration { parts.append(Self.durationText(duration)) }147 if let summary = step.summary, !summary.isEmpty { parts.append(summary) }148 if let errorText = step.errorText, !errorText.isEmpty { parts.append(errorText) }149 stepFacts.append(.init("\(index + 1). \(step.title)", parts.joined(separator: " · ")))150 if step.status != .succeeded {151 let reason = step.errorText ?? step.summary ?? step.status.title152 incomplete.append(L("Step “\(step.title)” — \(step.status.title): \(reason)"))153 }154 }155156 var reportSections = [157 ReportDocument.Section(title: L("Workflow"), paragraphs: definition.summary.isEmpty ? [] : [definition.summary], facts: overview, id: sectionID(1)),158 ReportDocument.Section(title: L("Steps"), facts: stepFacts, id: sectionID(2)),159 ]160 reportSections.append(contentsOf: sections)161 let alreadyShown = Set(sections.flatMap(\.findings))162 let remaining = findings.filter { !alreadyShown.contains($0) }163 if !remaining.isEmpty {164 reportSections.append(ReportDocument.Section(title: L("Findings"), findings: remaining, id: sectionID(3)))165 }166167 let summary = L("Workflow “\(definition.name)”: \(status.title). \(completed) of \(steps.count) steps completed.")168 return ReportDocument(kind: .workflow, title: definition.name, summary: summary, sections: reportSections, unavailable: incomplete, createdAt: endedAt, id: id)169 }170}171