SPB Git

Repository Public

ActionsLogic.swift 5.0 KB 106 lines swift

1import Foundation2import MCCCore34/// A possible target of an action, whatever its kind.5struct ActionTargetCandidate: Identifiable, Hashable, Sendable {6    var target: API.ActionTarget7    var label: String8    var detail: String?9    var id: String { target.kind.rawValue + ":" + target.id }10}1112/// Pure logic of the actions section (unit-tested).13enum ActionsLogic {14    /// Types applicable to a kind of target: least critical first, then by title.15    static func types(for kind: API.ActionTargetKind, in all: [API.ActionType]) -> [API.ActionType] {16        all.filter { $0.targetKinds.contains(kind) }17            .sorted { ($0.criticality.rank, $0.title, $0._type) < ($1.criticality.rank, $1.title, $1._type) }18    }1920    /// Catalog grouped by criticality (low → critical), empty groups dropped.21    static func grouped(_ all: [API.ActionType]) -> [(criticality: API.Criticality, types: [API.ActionType])] {22        API.Criticality.allCases.sorted { $0.rank < $1.rank }.compactMap { c in23            let items = all.filter { $0.criticality == c }.sorted { ($0.title, $0._type) < ($1.title, $1._type) }24            return items.isEmpty ? nil : (c, items)25        }26    }2728    /// Medium and above get an explicit review step before the request is sent. (What the server requires —29    /// step-up, justification, typed confirmation, approval — is the server's and comes from the catalog.)30    static func needsReviewStep(_ c: API.Criticality) -> Bool { c.rank >= API.Criticality.medium.rank }3132    enum Requirement: String, CaseIterable, Sendable { case stepUp, justification, confirmation, approval }3334    static func requirements(of t: API.ActionType) -> [Requirement] {35        var out: [Requirement] = []36        if t.requiresStepUp { out.append(.stepUp) }37        if t.requiresJustification { out.append(.justification) }38        if t.requiresConfirmation { out.append(.confirmation) }39        if t.requiresApproval { out.append(.approval) }40        return out41    }4243    static func filter(_ actions: [API.Action], state: API.ActionState?) -> [API.Action] {44        let items = state.map { s in actions.filter { $0.state == s } } ?? actions45        return items.sorted { ($0.createdAt, $0.id) > ($1.createdAt, $1.id) }46    }4748    static func pendingApprovalCount(_ actions: [API.Action]) -> Int { actions.filter { $0.state == .pendingApproval }.count }4950    static func targetLabel(_ a: API.Action) -> String {51        a.targetLabel ?? a.nodeAlias ?? (a.target.kind.label + " " + String(a.target.id.prefix(8)))52    }5354    /// Parameters as "name = value" lines, sorted, for the review step.55    static func parameterLines(_ params: [String: JSONValue]) -> [String] {56        params.keys.sorted().map { k in57            let v = params[k] ?? .null58            let text: String59            switch v {60            case .string(let s): text = s61            case .bool(let b): text = b ? "true" : "false"62            case .number(let n): text = n.rounded() == n && abs(n) < 1e15 ? String(Int64(n)) : String(n)63            case .null: text = "null"64            default: text = v.prettyPrinted()65            }66            return k + " = " + text67        }68    }6970    static func nodeCandidates(_ nodes: [API.Node]) -> [ActionTargetCandidate] {71        nodes.filter { $0.retiredAt == nil }.sorted { $0.alias.localizedStandardCompare($1.alias) == .orderedAscending }72            .map { ActionTargetCandidate(target: .init(kind: .node, id: $0.id), label: $0.alias, detail: $0.freshness.label) }73    }74    static func serviceCandidates(_ services: [API.Service]) -> [ActionTargetCandidate] {75        services.filter { $0.goneAt == nil }.sorted { ($0.nodeAlias, $0.name) < ($1.nodeAlias, $1.name) }76            .map { ActionTargetCandidate(target: .init(kind: .service, id: $0.id), label: $0.name, detail: $0.nodeAlias) }77    }78    static func modelCandidates(_ servers: [API.InferenceServer]) -> [ActionTargetCandidate] {79        servers.filter { $0.goneAt == nil }.sorted { ($0.nodeAlias, $0.port) < ($1.nodeAlias, $1.port) }80            .map { ActionTargetCandidate(target: .init(kind: .inferenceServer, id: $0.id), label: $0.model ?? $0.kind, detail: "\($0.nodeAlias):\($0.port)") }81    }82    static func jobCandidates(_ jobs: [API.Job]) -> [ActionTargetCandidate] {83        jobs.filter { !$0.state.isTerminal }.sorted { $0.createdAt > $1.createdAt }84            .map { ActionTargetCandidate(target: .init(kind: .job, id: $0.id), label: $0.template, detail: [$0.nodeAlias, $0.state.label].compactMap { $0 }.joined(separator: " · ")) }85    }86}8788extension ActionsLogic.Requirement {89    var title: String {90        switch self {91        case .stepUp: tr("actions.requires.step_up")92        case .justification: tr("actions.requires.justification")93        case .confirmation: tr("actions.requires.confirmation")94        case .approval: tr("actions.requires.approval")95        }96    }97    var symbol: String {98        switch self {99        case .stepUp: "person.badge.key"100        case .justification: "text.quote"101        case .confirmation: "keyboard"102        case .approval: "person.2.badge.gearshape"103        }104    }105}106
Files main
Menu

Theme