import Foundation nonisolated struct TrendSample: Sendable { let date: Date let cpu: Double? let gpu: Double? let memory: Double? var download: Double? var battery: Double? var batteryPower: Double? var systemPower: Double? var upload: Double? var read: Double? var write: Double? var cpuUser: Double? var cpuSystem: Double? var gpuRenderer: Double? var gpuTiler: Double? var memoryApp: Double? var memoryWired: Double? var memoryCompressed: Double? var breakBefore = false /// Scheduled cadence after this point, including changes before the next sample. var expectedInterval: TimeInterval = 2 } /// Bounded sampling history; no timer or UI visibility dependence. nonisolated struct MetricHistory: Sendable { static let capacity = 60 private(set) var samples: [TrendSample] = [] private(set) var interfaces: [String: [InterfaceTrendSample]] = [:] /// A slow cadence can begin and end without producing a point (for example a short hide). /// Preserve that outgoing wait on the existing point without changing earlier segments. mutating func recordScheduledInterval(_ interval: TimeInterval) { guard interval.isFinite, interval > 0, let last = samples.indices.last else { return } samples[last].expectedInterval = max(samples[last].expectedInterval, interval) for key in interfaces.keys { guard let index = interfaces[key]?.indices.last, let previous = interfaces[key]?.last?.expectedInterval else { continue } interfaces[key]?[index].expectedInterval = max(previous, interval) } } mutating func append(_ snapshot: MetricsSnapshot, breakBefore: Bool = false, expectedInterval: TimeInterval = 2) { samples.append(TrendSample(date: snapshot.date, cpu: snapshot.cpu?.usage, gpu: snapshot.gpuUsage, memory: snapshot.memory?.usage, download: snapshot.downloadRate, battery: snapshot.battery?.charge, batteryPower: snapshot.battery?.power, systemPower: snapshot.batteryState == .absent ? snapshot.systemPower : nil, upload: snapshot.uploadRate, read: snapshot.diskIO?.read, write: snapshot.diskIO?.written, cpuUser: snapshot.cpu?.user, cpuSystem: snapshot.cpu?.system, gpuRenderer: snapshot.gpu?.renderer, gpuTiler: snapshot.gpu?.tiler, memoryApp: snapshot.memory.map { Double($0.app) }, memoryWired: snapshot.memory.map { Double($0.wired) }, memoryCompressed: snapshot.memory.map { Double($0.compressed) }, breakBefore: breakBefore, expectedInterval: expectedInterval)) let current = snapshot.network ?? [] let keys = Set(current.map(\.identity)) interfaces = interfaces.filter { keys.contains($0.key) } for item in current { interfaces[item.identity, default: []].append(InterfaceTrendSample(date: snapshot.date, download: item.download, upload: item.upload, expectedInterval: expectedInterval)) if interfaces[item.identity, default: []].count > Self.capacity { interfaces[item.identity]?.removeFirst() } } if samples.count > Self.capacity { samples.removeFirst(samples.count - Self.capacity) } } } nonisolated struct InterfaceTrendSample: Sendable { let date: Date let download: Double? let upload: Double? /// Scheduled cadence from this point to the next interface sample. var expectedInterval: TimeInterval = 2 }