SPB Git

Repository Public

AlertsLogic.swift 4.0 KB 90 lines swift

1import Foundation2import MCCCore34enum AlertListFilter: String, CaseIterable, Identifiable, Sendable {5    case open, all, critical, warning, info6    var id: String { rawValue }7    var title: String {8        switch self {9        case .open: tr("alerts.filter.open")10        case .all: tr("common.all")11        case .critical: API.Severity.critical.label12        case .warning: API.Severity.warning.label13        case .info: API.Severity.info.label14        }15    }16}1718/// Pure list logic of the alerts section (unit-tested).19enum AlertsLogic {20    /// Open alerts first, then by severity (critical first), then the most recent first.21    static func sorted(_ alerts: [API.Alert]) -> [API.Alert] {22        alerts.sorted { a, b in23            if a.state.isOpen != b.state.isOpen { return a.state.isOpen }24            if a.severity.rank != b.severity.rank { return a.severity.rank > b.severity.rank }25            if a.startedAt != b.startedAt { return a.startedAt > b.startedAt }26            return a.id < b.id27        }28    }2930    static func filter(_ alerts: [API.Alert], by filter: AlertListFilter, query: String = "") -> [API.Alert] {31        let q = query.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()32        return alerts.filter { a in33            switch filter {34            case .open: guard a.state.isOpen else { return false }35            case .all: break36            case .critical: guard a.severity == .critical else { return false }37            case .warning: guard a.severity == .warning else { return false }38            case .info: guard a.severity == .info else { return false }39            }40            guard !q.isEmpty else { return true }41            return [a.summary, a.ruleName, a.nodeAlias ?? "", a.label].contains { $0.lowercased().contains(q) }42        }43    }4445    /// Unresolved incidents first, then by severity, then the most recent first.46    static func sorted(_ incidents: [API.Incident]) -> [API.Incident] {47        incidents.sorted { a, b in48            let ao = a.state != .resolved, bo = b.state != .resolved49            if ao != bo { return ao }50            if a.severity.rank != b.severity.rank { return a.severity.rank > b.severity.rank }51            if a.openedAt != b.openedAt { return a.openedAt > b.openedAt }52            return a.id < b.id53        }54    }5556    static func filter(_ incidents: [API.Incident], query: String) -> [API.Incident] {57        let q = query.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()58        guard !q.isEmpty else { return incidents }59        return incidents.filter { $0.title.lowercased().contains(q) || $0.summary.lowercased().contains(q) }60    }6162    /// Chronological order (oldest first); ties keep the server's order.63    static func timeline(_ events: [API.TimelineEvent]) -> [API.TimelineEvent] {64        events.enumerated().sorted { ($0.element.ts, $0.offset) < ($1.element.ts, $1.offset) }.map(\.element)65    }6667    /// Silences: active first, then the ones ending last.68    static func sorted(_ silences: [API.Silence]) -> [API.Silence] {69        silences.sorted { a, b in70            if a.active != b.active { return a.active }71            if a.endsAt != b.endsAt { return a.endsAt > b.endsAt }72            return a.id < b.id73        }74    }7576    /// What a scope covers: "all", or the resolved aliases / tags.77    static func scopeParts(_ scope: API.AlertScope, alias: (String) -> String?) -> (all: Bool, nodes: [String], tags: [String]) {78        let nodes = (scope.nodeIds ?? []).map { alias($0) ?? String($0.prefix(8)) }79        let tags = scope.tags ?? []80        return (scope.all == true || (nodes.isEmpty && tags.isEmpty), nodes, tags)81    }8283    static func silenceRequest(reason: String, allNodes: Bool, nodeIDs: [String], endsAt: Date) -> API.CreateSilenceRequest? {84        let r = reason.trimmingCharacters(in: .whitespacesAndNewlines)85        guard !r.isEmpty, allNodes || !nodeIDs.isEmpty else { return nil }86        let scope = allNodes ? API.AlertScope(all: true) : API.AlertScope(nodeIds: nodeIDs.sorted())87        return API.CreateSilenceRequest(reason: r, scope: scope, endsAt: endsAt)88    }89}90
Files main
Menu

Theme