Repository Public
1import MCCCore2import SwiftUI34/// Cluster at a glance. Every number comes from `GET /cluster/overview`; a block the server did not return (no5/// permission, dependency down) is said so, never shown as zero.6struct OverviewScreen: View {7 @Environment(Navigator.self) private var nav8 @Environment(\.mccTheme) private var theme9 @Environment(\.horizontalSizeClass) private var hSize10 @Session private var session1112 var body: some View {13 ScreenStack {14 ResourceSection(resource: session.cluster.overview) { o in15 VStack(alignment: .leading, spacing: Tokens.Space.s5) {16 availability(o)17 capacity(o.capacity)18 alerts(o)19 workloads(o)20 upcomingMaintenance21 recentActions(o.recentActions)22 }23 }24 }25 .screen("overview", title: tr("section.overview"), load: load, reload: reload)26 .toolbar {27 ToolbarItem(placement: .topBarTrailing) {28 Button { nav.sheet = .search } label: { Label(tr("commands.search"), systemImage: "magnifyingglass") }29 .accessibilityIdentifier("overview-search")30 }31 }32 }3334 private func load() async {35 async let a: Void = session.cluster.overview.loadIfNeeded()36 async let b: Void = session.maintenance.upcoming.loadIfNeeded(maxAge: 60)37 _ = await (a, b)38 }39 private func reload() async {40 async let a: Void = session.cluster.overview.load()41 async let b: Void = session.maintenance.upcoming.load()42 _ = await (a, b)43 }4445 // MARK: Availability by freshness4647 private func availability(_ o: API.ClusterOverview) -> some View {48 SectionCard(tr("overview.availability")) {49 LinkButton(title: tr("overview.see_nodes"), identifier: "overview-see-nodes") { nav.go(to: .section(.nodes)) }50 } content: {51 TileGrid(minimum: 130) {52 StatTile(value: Format.integer(o.nodes.fresh), caption: tr("overview.nodes.fresh"), tone: .ok).accessibilityIdentifier("overview-nodes-fresh")53 StatTile(value: Format.integer(o.nodes.stale), caption: tr("overview.nodes.stale"), tone: o.nodes.stale > 0 ? .stale : .neutral)54 StatTile(value: Format.integer(o.nodes.offline), caption: tr("overview.nodes.offline"), tone: o.nodes.offline > 0 ? .offline : .neutral)55 StatTile(value: Format.integer(o.nodes.maintenance), caption: tr("overview.nodes.maintenance"), tone: o.nodes.maintenance > 0 ? .maintenance : .neutral)56 }57 Text(tr("overview.nodes.total", o.nodes.total, Format.time(o.generatedAt, seconds: true)))58 .font(.footnote).foregroundStyle(theme.textSecondary)59 .accessibilityIdentifier("overview-generated-at")60 }61 }6263 // MARK: Capacity with coverage6465 private func capacity(_ c: API.ClusterCapacity) -> some View {66 SectionCard(tr("overview.capacity")) {67 VStack(alignment: .leading, spacing: Tokens.Space.s4) {68 capacityRow(tr("overview.capacity.cpu"), c.cpuCores, total: Format.integer(Int(c.cpuCores.total)) + " " + tr("overview.capacity.cores")) { Format.integer(Int($0)) }69 capacityRow(tr("overview.capacity.memory"), c.memory, total: Format.bytes(c.memory.total, binary: true)) { Format.bytes($0, binary: true) }70 capacityRow(tr("overview.capacity.storage"), c.storage, total: Format.bytes(c.storage.total)) { Format.bytes($0) }71 }72 }73 }7475 /// `used` is nil when no node reports usage: the meter says "not measured" instead of an empty bar.76 private func capacityRow(_ label: String, _ t: API.CapacityTotal, total: String, format: (Int64) -> String) -> some View {77 VStack(alignment: .leading, spacing: Tokens.Space.s1) {78 UsageMeter(label: label, ratio: t.used.flatMap { t.total > 0 ? Double($0) / Double(t.total) : nil },79 detail: t.used.map { tr("overview.capacity.used_of", format($0), total) } ?? total,80 unavailableText: tr("metric.not_measured"))81 Text(tr("overview.capacity.coverage", t.coverage)).font(.footnote).foregroundStyle(theme.textSecondary)82 }83 }8485 // MARK: Alerts and incidents8687 private func alerts(_ o: API.ClusterOverview) -> some View {88 SectionCard(tr("overview.alerts")) {89 LinkButton(title: tr("overview.see_alerts"), identifier: "overview-see-alerts") { nav.go(to: .section(.alerts)) }90 } content: {91 if let a = o.alerts {92 TileGrid(minimum: 130) {93 StatTile(value: Format.integer(a.critical), caption: tr("overview.alerts.critical"), tone: a.critical > 0 ? .critical : .neutral)94 StatTile(value: Format.integer(a.warning), caption: tr("overview.alerts.warning"), tone: a.warning > 0 ? .warning : .neutral)95 StatTile(value: Format.integer(a.info), caption: tr("overview.alerts.info"), tone: a.info > 0 ? .info : .neutral)96 if let n = o.openIncidents {97 StatTile(value: Format.integer(n), caption: tr("overview.incidents.open"), tone: n > 0 ? .warning : .neutral)98 }99 }100 } else {101 notReturned102 }103 }104 }105106 // MARK: Jobs, services, models, storage107108 private func workloads(_ o: API.ClusterOverview) -> some View {109 SectionCard(tr("overview.workloads")) {110 TileGrid(minimum: 130) {111 if let s = o.services {112 tile(.services, value: Format.integer(s.online), caption: tr("overview.services.online"), tone: .ok)113 tile(.services, value: Format.integer(s.errored), caption: tr("overview.services.errored"), tone: s.errored > 0 ? .critical : .neutral)114 if s.stopped > 0 { tile(.services, value: Format.integer(s.stopped), caption: tr("overview.services.stopped"), tone: .neutral) }115 }116 if let j = o.jobs {117 tile(.jobs, value: Format.integer(j.running), caption: tr("overview.jobs.running"), tone: j.running > 0 ? .info : .neutral)118 tile(.jobs, value: Format.integer(j.queued), caption: tr("overview.jobs.queued"), tone: .neutral)119 }120 if let m = o.models {121 tile(.models, value: "\(Format.integer(m.online)) / \(Format.integer(m.total))", caption: tr("overview.models.online"), tone: m.total > 0 && m.online == m.total ? .ok : .neutral)122 }123 if let n = o.nas {124 tile(.storage, value: "\(Format.integer(n.online)) / \(Format.integer(n.total))", caption: tr("overview.nas.online"), tone: n.offline > 0 ? .warning : .neutral)125 }126 if let b = o.backups {127 tile(.storage, value: Format.integer(b.failed), caption: tr("overview.backups.failed"), tone: b.failed > 0 ? .critical : .neutral)128 }129 }130 if o.services == nil && o.jobs == nil && o.models == nil { notReturned }131 }132 }133134 private func tile(_ section: AppSection, value: String, caption: String, tone: StatusTone) -> some View {135 Button { nav.go(to: .section(section)) } label: { StatTile(value: value, caption: caption, tone: tone).frame(minHeight: Tokens.Size.touchMin) }136 .buttonStyle(.plain)137 .hoverEffect(.highlight)138 .accessibilityHint(section.title)139 }140141 private var notReturned: some View {142 Text(tr("overview.not_returned")).font(.callout).foregroundStyle(theme.textSecondary)143 }144145 // MARK: Upcoming maintenance146147 @ViewBuilder private var upcomingMaintenance: some View {148 let r = session.maintenance.upcoming149 SectionCard(tr("overview.maintenance")) {150 LinkButton(title: tr("overview.see_calendar"), identifier: "overview-see-calendar") { nav.go(to: .section(.maintenance)) }151 } content: {152 if let list = r.value {153 if list.items.isEmpty {154 Text(tr("overview.maintenance.none")).font(.callout).foregroundStyle(theme.textSecondary)155 } else {156 ForEach(Array(list.items.prefix(4)), id: \.self) { occ in157 RouteRow(route: .maintenanceWindow(id: occ.window.id, occurrence: occ.occurrenceStart), identifier: "overview-maintenance-row") {158 VStack(alignment: .leading, spacing: Tokens.Space.s1) {159 Text(occ.window.title).font(.body).foregroundStyle(theme.textPrimary)160 Text(Format.dateTime(occ.occurrenceStart) + " → " + Format.time(occ.occurrenceEnd)).font(.footnote).foregroundStyle(theme.textSecondary)161 StatusLabel(occ.active == true ? API.MaintenanceStatus.inProgress.label : occ.window.status.label,162 tone: occ.active == true ? .maintenance : occ.window.status.tone)163 }164 }165 }166 }167 DataStamp(receivedAt: r.receivedAt, stale: r.isShowingStaleData)168 } else if let reason = r.state.unavailableReason {169 UnavailableStateView(reason: reason, error: r.state.error)170 } else if let e = r.state.error {171 InlineErrorView(e)172 } else {173 ProgressView()174 }175 }176 }177178 // MARK: Recent actions179180 @ViewBuilder private func recentActions(_ actions: [API.Action]?) -> some View {181 SectionCard(tr("overview.recent_actions")) {182 LinkButton(title: tr("overview.see_actions"), identifier: "overview-see-actions") { nav.go(to: .section(.actions)) }183 } content: {184 if let actions {185 if actions.isEmpty {186 Text(tr("overview.recent_actions.none")).font(.callout).foregroundStyle(theme.textSecondary)187 } else {188 ForEach(Array(actions.prefix(5)), id: \.id) { a in189 Button { nav.go(to: .action(a.id)) } label: {190 HStack(alignment: .firstTextBaseline, spacing: Tokens.Space.s3) {191 VStack(alignment: .leading, spacing: Tokens.Space.s1) {192 Text(a._type).font(.callout.monospaced()).foregroundStyle(theme.textPrimary)193 Text([a.targetLabel ?? a.nodeAlias, a.requestedByDisplayName, Format.relative(a.createdAt)].compactMap { $0 }.joined(separator: " · "))194 .font(.footnote).foregroundStyle(theme.textSecondary)195 }196 Spacer(minLength: Tokens.Space.s3)197 StatusBadge(a.state.label, tone: a.state.tone, compact: true)198 }199 .frame(minHeight: Tokens.Size.touchMin)200 .contentShape(Rectangle())201 }202 .buttonStyle(.plain)203 .accessibilityIdentifier("overview-action-row")204 }205 }206 } else {207 notReturned208 }209 }210 }211}212