summaryrefslogtreecommitdiff
path: root/Sources/BubombCore/Models/ExecutionRecord.swift
blob: 760f87ec9a2bf17d358be8fecffa9aab277c0260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import Foundation

public enum ExecutionStatus: String, Codable, Equatable, Hashable, Sendable {
    case succeeded = "Succeeded"
    case failed = "Failed"
    case cancelled = "Cancelled"
    case timedOut = "Timed Out"

    public var badgeColorName: String {
        switch self {
        case .succeeded: return "green"
        case .failed: return "red"
        case .cancelled: return "orange"
        case .timedOut: return "purple"
        }
    }
}

/// Metadata and logs from a payload detonation.
public struct ExecutionRecord: Identifiable, Codable, Equatable, Hashable, Sendable {
    public var id: UUID
    public var payloadId: UUID
    public var payloadName: String
    public var payloadTypeDescription: String
    public var startedAt: Date
    public var finishedAt: Date
    public var duration: TimeInterval
    public var status: ExecutionStatus
    public var exitCode: Int?
    public var standardOutput: String
    public var standardError: String
    public var errorMessage: String?

    public init(
        id: UUID = UUID(),
        payloadId: UUID,
        payloadName: String,
        payloadTypeDescription: String,
        startedAt: Date,
        finishedAt: Date = Date(),
        duration: TimeInterval = 0,
        status: ExecutionStatus,
        exitCode: Int? = nil,
        standardOutput: String = "",
        standardError: String = "",
        errorMessage: String? = nil
    ) {
        self.id = id
        self.payloadId = payloadId
        self.payloadName = payloadName
        self.payloadTypeDescription = payloadTypeDescription
        self.startedAt = startedAt
        self.finishedAt = finishedAt
        self.duration = duration
        self.status = status
        self.exitCode = exitCode
        self.standardOutput = standardOutput
        self.standardError = standardError
        self.errorMessage = errorMessage
    }

    public var formattedDuration: String {
        if duration < 1.0 {
            return String(format: "%.0f ms", duration * 1000)
        } else {
            return String(format: "%.2f s", duration)
        }
    }

    public var combinedOutput: String {
        var out = ""
        if !standardOutput.isEmpty {
            out += standardOutput
        }
        if !standardError.isEmpty {
            if !out.isEmpty && !out.hasSuffix("\n") {
                out += "\n"
            }
            out += standardError
        }
        return out
    }
}