feat(monitors): 接入统一面板数据与按需进程采样

This commit is contained in:
2026-11-02 22:58:33 +08:00
parent 7df2bbc7c1
commit 604086bbbb
14 changed files with 391 additions and 32 deletions
+19 -1
View File
@@ -8,18 +8,36 @@ nonisolated struct TrendSample: Sendable {
var download: Double?
var disk: Double?
var battery: Double?
var upload: Double?
var read: Double?
var write: Double?
}
/// 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]] = [:]
mutating func append(_ snapshot: MetricsSnapshot) {
samples.append(TrendSample(date: snapshot.date, cpu: snapshot.cpu?.usage,
gpu: snapshot.gpuUsage, memory: snapshot.memory?.usage,
download: snapshot.downloadRate, disk: snapshot.disk?.usage,
battery: snapshot.battery?.charge))
battery: snapshot.battery?.charge, upload: snapshot.uploadRate,
read: snapshot.diskIO?.read, write: snapshot.diskIO?.written))
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))
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?
}
+20 -3
View File
@@ -30,15 +30,32 @@ nonisolated struct MetricsSnapshot: Sendable {
var battery: BatteryMetrics?
var load: [Double] = []
var uptime: TimeInterval = 0
// GPU remains unavailable until a reliable IOReport subscription is validated.
var gpu: GPUMetrics?
var processes: ProcessSnapshot?
var diskHealthDate: Date?
var batteryHealthDate: Date?
var batteryState: BatteryAvailability = .unknown
// GPU total is backed by validated IOAccelerator device statistics.
var gpuUsage: Double? = nil
}
nonisolated extension MetricsSnapshot {
var downloadRate: Double? {
guard let network else { return nil }
let rates = network.compactMap(\.download)
guard rates.count == network.count else { return nil }
let included = network.filter { $0.isIncludedInTotal }
guard !included.isEmpty else { return nil }
let rates = included.compactMap(\.download)
guard rates.count == included.count else { return nil }
return rates.reduce(0, +)
}
}
nonisolated enum BatteryAvailability: Sendable { case unknown, present, absent, failed }
nonisolated extension MetricsSnapshot {
var uploadRate: Double? {
guard let included = network?.filter({ $0.isIncludedInTotal }), !included.isEmpty else { return nil }
let values = included.compactMap(\.upload)
return values.count == included.count ? values.reduce(0, +) : nil
}
}
+25
View File
@@ -0,0 +1,25 @@
import Foundation
nonisolated struct ProcessIdentity: Hashable, Sendable {
let pid: Int32
let seconds: UInt64
let microseconds: UInt64
}
nonisolated struct ProcessMetrics: Identifiable, Sendable {
let id: ProcessIdentity
let name: String
let path: String?
let cpu: Double? // One logical CPU = 100%; may exceed 1.
let residentBytes: UInt64?
let threads: Int?
let restricted: Bool
}
nonisolated struct ProcessSnapshot: Sendable {
let date: Date
let enumerated: Int
let readable: Int
let rows: [ProcessMetrics]
let failed: Bool
}