Repository Public
1import MCCCore2import SwiftUI34/// Two to four nodes side by side: one chart with a series per node, and a table of hardware and headline values.5struct NodeCompareScreen: View {6 @Environment(AppModel.self) private var app7 @Environment(\.mccTheme) private var theme8 @Environment(\.dynamicTypeSize) private var size9 @Session private var session10 let nodeIDs: [String]11 @State private var metric = NodesLogic.chartMetrics[0]1213 private var query: CompareQuery { CompareQuery(nodeIDs: nodeIDs, metric: metric, range: app.range) }14 private var nodes: [API.Node] { nodeIDs.compactMap { session.nodes.node($0) } }1516 var body: some View {17 @Bindable var app = app18 let r = session.nodes.compare[query]19 ScreenStack {20 SectionCard(tr("nodes.compare.chart")) {21 Picker(tr("nodes.compare.metric"), selection: $metric) {22 ForEach(NodesLogic.chartMetrics, id: \.self) { Text(NodesLogic.metricTitle($0)).tag($0) }23 }24 .pickerStyle(.menu)25 .frame(minHeight: Tokens.Size.touchMin)26 .accessibilityIdentifier("compare-metric")27 RangePicker(range: $app.range)28 InlineResource(resource: r) { resp in29 let blocked = resp.availability.additionalProperties.filter { $0.value != .available && $0.value != .stale }30 TimeSeriesChart(title: NodesLogic.metricTitle(resp.metric), unit: resp.unit,31 series: resp.series.map { s in32 ChartSeries(id: s.nodeId ?? s.label, name: s.nodeAlias ?? s.nodeId.flatMap { session.nodes.alias($0) } ?? s.label, metricPoints: s.points)33 },34 domain: resp.from < resp.to ? resp.from...resp.to : nil, showBand: false, height: 240)35 .accessibilityIdentifier("compare-chart")36 // Nodes that cannot provide the metric are named, with the reason.37 ForEach(blocked.keys.sorted(), id: \.self) { id in38 if let a = blocked[id] {39 StatusLabel("\(session.nodes.alias(id) ?? id) : \(a.label)", tone: a.tone).font(.footnote)40 }41 }42 }43 .loads(r, maxAge: 30)44 .id(query)45 }46 SectionCard(tr("nodes.compare.table")) { table }47 }48 .screen("node-compare", title: tr("nodes.compare.title"), load: { await session.nodes.list.loadIfNeeded() },49 reload: { async let a: Void = session.nodes.list.load(); async let b: Void = r.load(); _ = await (a, b) })50 .navigationBarTitleDisplayMode(.inline)51 }5253 private var rows: [(String, (API.Node) -> String?)] {54 [(tr("nodes.hw.model"), { $0.model }),55 (tr("nodes.hw.chip"), { $0.chip }),56 (tr("nodes.hw.cpu_cores"), { $0.cpuCores.map { Format.integer($0) } }),57 (tr("nodes.hw.gpu_cores"), { $0.gpuCores.map { Format.integer($0) } }),58 (tr("nodes.hw.memory"), { $0.memBytes.map { Format.bytes($0, binary: true) } }),59 (tr("nodes.hw.storage"), { $0.storageBytes.map { Format.bytes($0) } }),60 (tr("nodes.hw.os"), { n in [n.osName, n.osVersion].compactMap { $0 }.joined(separator: " ").nilIfEmpty }),61 (tr("freshness.title"), { $0.freshness.label }),62 (tr("nodes.cpu"), { NodesLogic.percentText($0.headline?.cpuUsagePct) }),63 (tr("nodes.memory"), { NodesLogic.percentText($0.headline?.memUsedPct) }),64 (tr("nodes.disk"), { NodesLogic.percentText($0.headline?.diskUsedPct) }),65 (tr("nodes.load"), { NodesLogic.loadText($0.headline?.load1) })]66 }6768 @ViewBuilder private var table: some View {69 if nodes.isEmpty {70 Text(tr("nodes.compare.unknown")).font(.callout).foregroundStyle(theme.textSecondary)71 } else if size.isAccessibilitySize {72 // One block per node: a wide table would truncate at large text sizes.73 ForEach(nodes, id: \.id) { n in74 VStack(alignment: .leading, spacing: Tokens.Space.s2) {75 Text(n.alias).font(.headline).foregroundStyle(theme.textPrimary).accessibilityAddTraits(.isHeader)76 ForEach(rows, id: \.0) { row in InfoRow(row.0, row.1(n)) }77 }78 }79 } else {80 ScrollView(.horizontal) {81 Grid(alignment: .leading, horizontalSpacing: Tokens.Space.s5, verticalSpacing: Tokens.Space.s3) {82 GridRow {83 Text("").accessibilityHidden(true)84 ForEach(nodes, id: \.id) { n in85 Text(n.alias).font(.callout.weight(.semibold)).foregroundStyle(theme.textPrimary).accessibilityAddTraits(.isHeader)86 }87 }88 ForEach(rows, id: \.0) { row in89 GridRow {90 Text(row.0).font(.callout).foregroundStyle(theme.textSecondary)91 ForEach(nodes, id: \.id) { n in92 let v = row.1(n)93 Text(v ?? tr("metric.not_reported")).font(.callout.monospacedDigit())94 .foregroundStyle(v == nil ? theme.textTertiary : theme.textPrimary)95 .accessibilityLabel("\(n.alias), \(row.0)")96 .accessibilityValue(v ?? tr("metric.not_reported"))97 }98 }99 }100 }101 }102 .scrollBounceBehavior(.basedOnSize, axes: .horizontal)103 }104 DataStamp(receivedAt: session.nodes.list.receivedAt, stale: session.nodes.list.isShowingStaleData)105 }106}107108extension String {109 var nilIfEmpty: String? { isEmpty ? nil : self }110}111