SPB Git

Repository Public

AuditLogic.swift 2.3 KB 66 lines swift

1import Foundation2import MCCCore34/// The filter form of the audit screen, as typed; `filter` is what is sent to `GET /audit`.5struct AuditFilterForm: Equatable {6    var actorID = ""7    var action = ""8    var targetKind = ""9    var targetID = ""10    var outcome: API.AuditOutcome?11    var useFrom = false12    var from = Date().addingTimeInterval(-86_400)13    var useTo = false14    var to = Date()15    var correlationID = ""1617    private static func clean(_ s: String) -> String? {18        let t = s.trimmingCharacters(in: .whitespacesAndNewlines)19        return t.isEmpty ? nil : t20    }2122    var filter: AuditFilter {23        var f = AuditFilter()24        f.actorID = Self.clean(actorID)25        f.action = Self.clean(action)26        f.targetKind = Self.clean(targetKind)27        f.targetID = Self.clean(targetID)28        f.outcome = outcome29        f.from = useFrom ? from : nil30        f.to = useTo ? to : nil31        f.correlationID = Self.clean(correlationID)32        return f33    }3435    var isEmpty: Bool { filter == AuditFilter() }3637    /// A reversed range can only return nothing: flagged before the request.38    var rangeInvalid: Bool { useFrom && useTo && from >= to }39}4041enum AuditLogic {42    /// "user: spb", "cli: mccctl:spb" …43    static func actor(_ e: API.AuditEntry) -> String { "\(e.actorKind.rawValue): \(e.actorLabel)" }4445    static func target(_ e: API.AuditEntry) -> String? {46        guard let kind = e.targetKind, !kind.isEmpty else { return nil }47        return e.targetId.map { "\(kind): \($0)" } ?? kind48    }4950    static func shortHash(_ h: String) -> String { h.count > 16 ? String(h.prefix(8)) + "…" + String(h.suffix(8)) : h }5152    /// Default file name of an export: UTC bounds, no characters that need escaping.53    static func exportFileName(from: Date, to: Date, format: AuditRepository.ExportFormat) -> String {54        let f = DateFormatter()55        f.locale = Locale(identifier: "en_US_POSIX")56        f.timeZone = TimeZone(identifier: "UTC")57        f.dateFormat = "yyyyMMdd'T'HHmm'Z'"58        return "mcc-audit-\(f.string(from: from))-\(f.string(from: to)).\(format.rawValue)"59    }6061    /// The export range accepted by the server: ordered, at most 366 days.62    static func exportRangeValid(from: Date, to: Date) -> Bool {63        from < to && to.timeIntervalSince(from) <= 366 * 86_40064    }65}66
Files main
Menu

Theme