refactor(project): 清理闲置展示链路与重复采样状态

This commit is contained in:
2026-11-09 00:41:09 +08:00
parent 7b3abe37c5
commit b73d51c953
57 changed files with 210 additions and 2822 deletions
+6 -10
View File
@@ -17,30 +17,26 @@ nonisolated struct ProcessEnergyMonitor: Sendable {
}
mutating func update(processes: ProcessSnapshot, readings: [ProcessEnergyReading], capability: EnergyCapability, maximumInterval: TimeInterval = 30) -> ProcessEnergySnapshot {
let processesByID = Dictionary(processes.rows.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first })
let identities = Set(processes.rows.map(\.id))
var next: [ProcessIdentity: ProcessEnergyReading] = [:]
var usages: [ProcessEnergyUsage] = []
var measured = 0
if capability == .verifiedCPU && !processes.failed {
for reading in readings where next[reading.id] == nil {
guard let process = processesByID[reading.id], reading.status == .available,
guard identities.contains(reading.id), reading.status == .available,
reading.nanojoules != nil, reading.birth > 0, reading.uptime.isFinite else { continue }
next[reading.id] = reading
if let (power, seconds) = Self.watts(current: reading, previous: previous[reading.id], maximumInterval: maximumInterval) {
measured += 1
let usage = ProcessEnergyUsage(process: process, watts: power, seconds: seconds)
usages.append(usage)
usages.append(ProcessEnergyUsage(id: reading.id, watts: power, seconds: seconds))
}
}
}
previous = next // Failed / missing readings invalidate the old baseline.
usages.sort { $0.watts == $1.watts ? $0.id.pid < $1.id.pid : $0.watts > $1.watts }
let state: ProcessEnergySnapshot.State
if capability == .unsupported { state = .unsupported }
else if capability == .unavailable || processes.failed || (capability == .verifiedCPU && next.isEmpty) { state = .unavailable }
else if measured == 0 { state = .waiting }
else { state = measured < processes.enumerated ? .partial : .available }
else if usages.isEmpty { state = .waiting }
else { state = usages.count < processes.enumerated ? .partial : .available }
return ProcessEnergySnapshot(date: Date(), candidates: processes.enumerated, readable: next.count,
measured: measured, rows: usages, state: state)
rows: usages, state: state)
}
}