SPB Git

Repository Public

TopologyLayout.swift 3.3 KB 73 lines swift

1import CoreGraphics2import Foundation3import MCCCore45/// Deterministic layout of the logical topology: hubs (LAN gateway, WireGuard hub) in the centre, nodes on an6/// ellipse around them, ordered by label. Same input → same picture, whatever the order the server lists things in.7enum TopologyLayout {8    static func isHub(_ n: API.TopologyNode) -> Bool { n.kind != .node }910    static func positions(nodes: [API.TopologyNode], in size: CGSize, margin: CGFloat = 70) -> [String: CGPoint] {11        var out: [String: CGPoint] = [:]12        guard size.width > 0, size.height > 0 else { return out }13        let centre = CGPoint(x: size.width / 2, y: size.height / 2)14        let hubs = nodes.filter(isHub).sorted { ($0.kind.rawValue, $0.label, $0.id) < ($1.kind.rawValue, $1.label, $1.id) }15        let leaves = nodes.filter { !isHub($0) }.sorted { ($0.label.lowercased(), $0.id) < ($1.label.lowercased(), $1.id) }16        let spread = min(size.width * 0.22, 180)17        for (i, h) in hubs.enumerated() {18            let offset = hubs.count == 1 ? 0 : (CGFloat(i) - CGFloat(hubs.count - 1) / 2) * spread19            out[h.id] = CGPoint(x: centre.x + offset, y: centre.y)20        }21        let rx = max(40, size.width / 2 - margin), ry = max(40, size.height / 2 - margin * 0.7)22        for (i, n) in leaves.enumerated() {23            // Start at the top, go clockwise.24            let angle = -CGFloat.pi / 2 + 2 * .pi * CGFloat(i) / CGFloat(max(1, leaves.count))25            out[n.id] = CGPoint(x: centre.x + rx * cos(angle), y: centre.y + ry * sin(angle))26        }27        return out28    }2930    /// Dash pattern per link kind and state: kind and state are readable without colour.31    static func dash(kind: API.TopologyLink.KindPayload, state: API.TopologyLink.StatePayload) -> [CGFloat] {32        switch (kind, state) {33        case (_, .down): [2, 6]34        case (_, .unknown): [1, 4]35        case (.lan, .up): []36        case (.wireguard, .up): [8, 4]37        }38    }3940    static func tone(_ state: API.TopologyLink.StatePayload) -> StatusTone {41        switch state {42        case .up: .ok43        case .down: .critical44        case .unknown: .neutral45        }46    }4748    /// Latency / loss text: the measured value only when the server says it is available, else the availability.49    static func measure(_ value: Double?, availability: API.Availability, format: (Double) -> String) -> String {50        if availability == .available, let value { return format(value) }51        return availability == .available ? tr("metric.not_reported") : availability.label52    }53}5455enum StorageLogic {56    /// Used fraction of a volume, nil when either size is absent (not measured) or the total is zero.57    static func usedRatio(used: Int64?, total: Int64?) -> Double? {58        guard let used, let total, total > 0 else { return nil }59        return min(1, max(0, Double(used) / Double(total)))60    }6162    static func filter(_ volumes: [API.Volume], nodeID: String?, kind: API.VolumeKind?, minUsedPct: Double) -> [API.Volume] {63        volumes.filter { v in64            if let nodeID, v.nodeId != nodeID { return false }65            if let kind, v.kind != kind { return false }66            if minUsedPct > 0 {67                guard let r = usedRatio(used: v.usedBytes, total: v.totalBytes), r * 100 >= minUsedPct else { return false }68            }69            return true70        }71    }72}73
Files main
Menu

Theme