SPB Git

Repository Public

AuditLogic.swift 2.3 KB 61 lines swift

1import Foundation2import MCCCore34/// Pure logic of the audit screen (unit-tested).5enum AuditLogic {6    enum Period: String, CaseIterable, Identifiable, Sendable {7        case all, day, week, month8        var id: String { rawValue }9        var seconds: TimeInterval? {10            switch self {11            case .all: nil12            case .day: 86_40013            case .week: 7 * 86_40014            case .month: 30 * 86_40015            }16        }17        var title: String {18            switch self {19            case .all: tr("audit.period.all")20            case .day: tr("audit.period.day")21            case .week: tr("audit.period.week")22            case .month: tr("audit.period.month")23            }24        }25    }2627    /// Server-side filter: outcome and period. `now` is rounded to the minute so that the same choice maps to the28    /// same resource for a while.29    static func filter(outcome: API.AuditOutcome?, period: Period, now: Date = Date()) -> AuditFilter {30        var f = AuditFilter()31        f.outcome = outcome32        if let s = period.seconds {33            let rounded = Date(timeIntervalSince1970: (now.timeIntervalSince1970 / 300).rounded(.down) * 300)34            f.from = rounded.addingTimeInterval(-s)35        }36        return f37    }3839    /// Client-side text match on what is already loaded (action, actor, target, detail, correlation id).40    static func matches(_ e: API.AuditEntry, text: String) -> Bool {41        let q = text.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()42        guard !q.isEmpty else { return true }43        return [e.action, e.actorLabel, e.targetKind ?? "", e.targetId ?? "", e.detail, e.correlationId].contains { $0.lowercased().contains(q) }44    }4546    static func sorted(_ entries: [API.AuditEntry]) -> [API.AuditEntry] { entries.sorted { $0.seq > $1.seq } }4748    static func exportFileName(from: Date, to: Date, format: AuditRepository.ExportFormat) -> String {49        let f = DateFormatter()50        f.locale = Locale(identifier: "en_US_POSIX")51        f.timeZone = TimeZone(identifier: "UTC")52        f.dateFormat = "yyyyMMdd"53        return "mcc-audit-\(f.string(from: from))-\(f.string(from: to)).\(format.rawValue)"54    }5556    static func targetText(_ e: API.AuditEntry) -> String? {57        guard let k = e.targetKind else { return nil }58        return e.targetId.map { k + " " + $0 } ?? k59    }60}61
Files main
Menu

Theme