feat(monitors): 接入统一面板数据与按需进程采样
This commit is contained in:
@@ -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?
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user