Repository Public
1import MCCCore2import SwiftUI34enum NetworkLogic {5 static func linkTone(_ s: API.TopologyLink.StatePayload) -> StatusTone {6 switch s {7 case .up: .ok8 case .down: .critical9 case .unknown: .neutral10 }11 }1213 /// A measured value, or the availability label that explains why there is none.14 static func measured(_ value: Double?, availability: API.Availability, format: (Double) -> String) -> String {15 if let value, availability == .available || availability == .stale { return format(value) }16 return availability == .available ? tr("metric.not_reported") : availability.label17 }1819 /// Links down first, then unknown, then up.20 static func sorted(_ links: [API.TopologyLink]) -> [API.TopologyLink] {21 func rank(_ s: API.TopologyLink.StatePayload) -> Int { s == .down ? 0 : s == .unknown ? 1 : 2 }22 return links.sorted { rank($0.state) == rank($1.state) ? $0.id < $1.id : rank($0.state) < rank($1.state) }23 }2425 static func linkCounts(_ links: [API.TopologyLink]) -> (up: Int, down: Int, unknown: Int) {26 (links.filter { $0.state == .up }.count, links.filter { $0.state == .down }.count, links.filter { $0.state == .unknown }.count)27 }28}2930/// One network interface (node detail and network summary).31struct InterfaceRow: View {32 @Environment(\.mccTheme) private var theme33 let interface: API.NetworkInterface34 var showsNode = true35 var body: some View {36 let i = interface37 VStack(alignment: .leading, spacing: Tokens.Space.s1) {38 AdaptiveLine(spacing: Tokens.Space.s3) {39 Text((showsNode ? i.nodeAlias + " · " : "") + i.name).font(.callout.weight(.medium).monospaced()).foregroundStyle(theme.textPrimary)40 StatusLabel(tr(i.up ? "network.up" : "network.down"), tone: i.up ? .ok : .neutral).font(.footnote)41 Text(trDynamic("network.kind." + i.kind.rawValue)).font(.footnote).foregroundStyle(theme.textSecondary)42 }43 let addrs = i.ipv4 + i.ipv644 if !addrs.isEmpty { Text(addrs.joined(separator: ", ")).font(.footnote.monospaced()).foregroundStyle(theme.textSecondary).textSelection(.enabled) }45 if let mac = i.mac { Text(mac).font(.footnote.monospaced()).foregroundStyle(theme.textTertiary) }46 AdaptiveLine {47 HeadlineChip(label: tr("network.speed"), value: i.linkSpeedMbps.map { tr("network.mbps", $0) })48 HeadlineChip(label: tr("network.rx"), value: i.rxBytesPerS.map { Format.bytesPerSecond($0) })49 HeadlineChip(label: tr("network.tx"), value: i.txBytesPerS.map { Format.bytesPerSecond($0) })50 }51 FreshnessLine(i.freshness, observedAt: i.observedAt).font(.footnote)52 }53 .frame(maxWidth: .infinity, alignment: .leading)54 .accessibilityElement(children: .combine)55 .accessibilityIdentifier("interface-row")56 }57}5859/// Network summary. The topology is given as lists (hubs, links with their state), readable by VoiceOver and at60/// any text size, rather than as a drawing.61struct NetworkScreen: View {62 @Environment(\.mccTheme) private var theme63 @Session private var session6465 var body: some View {66 ScreenStack {67 SectionCard(tr("network.topology")) {68 InlineResource(resource: session.infrastructure.topology) { t in69 let c = NetworkLogic.linkCounts(t.links)70 TileGrid(minimum: 130) {71 CountTile(value: Format.integer(c.up), caption: tr("network.links.up"), tone: .ok)72 CountTile(value: Format.integer(c.down), caption: tr("network.links.down"), tone: c.down > 0 ? .critical : .neutral)73 CountTile(value: Format.integer(c.unknown), caption: tr("network.links.unknown"), tone: .neutral)74 }75 let hubs = t.nodes.filter { $0.kind != .node }76 if !hubs.isEmpty {77 Text(tr("network.hubs")).font(.subheadline.weight(.medium)).foregroundStyle(theme.textPrimary).accessibilityAddTraits(.isHeader)78 ForEach(hubs, id: \.id) { h in79 InfoRow(h.label + " — " + trDynamic("network.topology.kind." + h.kind.rawValue), h.address, monospaced: true)80 }81 }82 Text(tr("network.links", t.links.count)).font(.subheadline.weight(.medium)).foregroundStyle(theme.textPrimary).accessibilityAddTraits(.isHeader)83 if t.links.isEmpty { Text(tr("network.links.none")).font(.callout).foregroundStyle(theme.textSecondary) }84 let names = Dictionary(t.nodes.map { ($0.id, $0.label) }, uniquingKeysWith: { a, _ in a })85 ForEach(NetworkLogic.sorted(t.links), id: \.id) { l in86 VStack(alignment: .leading, spacing: Tokens.Space.s1) {87 AdaptiveLine(spacing: Tokens.Space.s3) {88 Text("\(names[l.source] ?? l.source) ↔ \(names[l.target] ?? l.target)").font(.callout.weight(.medium)).foregroundStyle(theme.textPrimary)89 StatusLabel(trDynamic("network.link.state." + l.state.rawValue), tone: NetworkLogic.linkTone(l.state)).font(.footnote)90 }91 Text([trDynamic("network.link.kind." + l.kind.rawValue), l.interface].compactMap { $0 }.joined(separator: " · "))92 .font(.footnote).foregroundStyle(theme.textSecondary)93 AdaptiveLine {94 HeadlineChip(label: tr("network.latency"), value: NetworkLogic.measured(l.latencyMs, availability: l.latencyAvailability) { Format.milliseconds($0) })95 HeadlineChip(label: tr("network.loss"), value: NetworkLogic.measured(l.lossPct, availability: l.lossAvailability) { Format.percent($0, fractionDigits: 1) })96 }97 FreshnessLine(l.freshness, observedAt: l.observedAt).font(.footnote)98 }99 .frame(maxWidth: .infinity, alignment: .leading)100 .accessibilityElement(children: .combine)101 .accessibilityIdentifier("network-link-row")102 Divider().overlay(theme.borderSubtle)103 }104 Text(tr("network.generated_at", Format.time(t.generatedAt, seconds: true))).font(.footnote).foregroundStyle(theme.textTertiary)105 }106 }107 SectionCard(tr("network.interfaces")) {108 InlineResource(resource: session.infrastructure.interfaces) { list in109 if list.isEmpty { Text(tr("network.interfaces.none")).font(.callout).foregroundStyle(theme.textSecondary) }110 ForEach(list, id: \.self) { i in111 InterfaceRow(interface: i)112 Divider().overlay(theme.borderSubtle)113 }114 }115 }116 }117 .screen("network", title: tr("section.network"), load: { await load(force: false) }, reload: { await load(force: true) })118 }119120 private func load(force: Bool) async {121 async let a: Void = force ? session.infrastructure.topology.load() : session.infrastructure.topology.loadIfNeeded()122 async let b: Void = force ? session.infrastructure.interfaces.load() : session.infrastructure.interfaces.loadIfNeeded()123 _ = await (a, b)124 }125}126