89 lines
4.3 KiB
Swift
89 lines
4.3 KiB
Swift
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 powerFlow: BatteryPowerFlow?
|
|
var systemPower: Double?
|
|
var desktopInput: 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]] = [:]
|
|
|
|
private(set) var sleepIntervals: [DateInterval] = []
|
|
|
|
mutating func recordSleep(from start: Date, to end: Date) {
|
|
guard let oldest = samples.first?.date, end > start, end > oldest else { return }
|
|
sleepIntervals.append(DateInterval(start: start, end: end))
|
|
if sleepIntervals.count > Self.capacity { sleepIntervals.removeFirst() }
|
|
}
|
|
|
|
/// 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) {
|
|
if let last = samples.last, snapshot.date < last.date { sleepIntervals.removeAll() }
|
|
samples.append(TrendSample(date: snapshot.date, cpu: snapshot.cpu?.usage,
|
|
gpu: snapshot.gpuUsage, memory: snapshot.memory?.usage,
|
|
download: snapshot.downloadRate,
|
|
battery: snapshot.battery?.charge, powerFlow: snapshot.battery?.flow,
|
|
systemPower: snapshot.batteryState == .absent ? snapshot.systemPower : nil,
|
|
desktopInput: snapshot.desktopInput, 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)
|
|
let oldest = samples[0].date
|
|
sleepIntervals.removeAll { $0.end <= oldest }
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|