fix(charts): 修复浮窗层级并平滑切频与曲线移出

This commit is contained in:
2026-11-03 12:07:01 +08:00
parent 36b8e32af3
commit 7ef92d9c21
17 changed files with 271 additions and 59 deletions
+5 -3
View File
@@ -14,6 +14,7 @@ nonisolated struct TrendSample: Sendable {
var cpuUser: Double?
var cpuSystem: Double?
var breakBefore = false
var expectedInterval: TimeInterval = 2
}
/// Bounded sampling history; no timer or UI visibility dependence.
@@ -22,18 +23,18 @@ nonisolated struct MetricHistory: Sendable {
private(set) var samples: [TrendSample] = []
private(set) var interfaces: [String: [InterfaceTrendSample]] = [:]
mutating func append(_ snapshot: MetricsSnapshot, breakBefore: Bool = false) {
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, disk: snapshot.disk?.usage,
battery: snapshot.battery?.charge, upload: snapshot.uploadRate,
read: snapshot.diskIO?.read, write: snapshot.diskIO?.written,
cpuUser: snapshot.cpu?.user, cpuSystem: snapshot.cpu?.system, breakBefore: breakBefore))
cpuUser: snapshot.cpu?.user, cpuSystem: snapshot.cpu?.system, 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))
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) }
@@ -44,4 +45,5 @@ nonisolated struct InterfaceTrendSample: Sendable {
let date: Date
let download: Double?
let upload: Double?
var expectedInterval: TimeInterval = 2
}
+20 -8
View File
@@ -12,15 +12,17 @@ final class MetricsStore {
@ObservationIgnored private var generation = 0
@ObservationIgnored private var visibilityGeneration = 0
@ObservationIgnored private var task: Task<Void, Never>?
@ObservationIgnored private let sampler = SystemSampler()
@ObservationIgnored private let sampler: SystemSampler
@ObservationIgnored private var scheduledInterval = 0
@ObservationIgnored private var needsHistoryBoundary = true
@ObservationIgnored private var lastPublishedAt: ContinuousClock.Instant?
init(sampling: SamplingPreferences? = nil) {
init(sampling: SamplingPreferences? = nil, sampler: SystemSampler = SystemSampler()) {
self.sampler = sampler
self.sampling = sampling ?? SamplingPreferences()
self.sampling.onChange = { [weak self] in
guard let self, !self.isPaused, self.effectiveMilliseconds != self.scheduledInterval else { return }
self.reschedule(reset: false)
self.reschedule(reset: false, sampleImmediately: false)
}
start()
}
@@ -31,22 +33,30 @@ final class MetricsStore {
reschedule(reset: true)
}
private func reschedule(reset: Bool) {
private func reschedule(reset: Bool, sampleImmediately: Bool = true) {
generation += 1
task?.cancel()
needsHistoryBoundary = true
needsHistoryBoundary = needsHistoryBoundary || reset
scheduledInterval = effectiveMilliseconds
var delay = Duration.zero
if !sampleImmediately, let lastPublishedAt {
delay = max(.zero, .milliseconds(effectiveMilliseconds) - lastPublishedAt.duration(to: .now))
}
let initialDelay = delay
let currentGeneration = generation
task = Task { [weak self, sampler] in
guard !Task.isCancelled else { return }
if initialDelay > .zero {
do { try await Task.sleep(for: initialDelay) } catch { return }
}
if reset { await sampler.reset() }
while !Task.isCancelled {
let visibleGeneration = self?.visibilityGeneration
var next = await sampler.sample(includeProcesses: self?.isPanelVisible == true, processUpdate: { [weak self] processes in
Task { @MainActor [weak self] in
guard let self, self.generation == currentGeneration, self.visibilityGeneration == visibleGeneration,
guard let self, self.visibilityGeneration == visibleGeneration,
self.isPanelVisible, !self.isPaused else { return }
self.snapshot.processes = processes
if self.snapshot.processes.map({ $0.date > processes.date }) != true { self.snapshot.processes = processes }
}
})
guard !Task.isCancelled, self?.generation == currentGeneration else { return }
@@ -56,7 +66,8 @@ final class MetricsStore {
next.processes = completed
}
self?.snapshot = next
self?.history.append(next, breakBefore: self?.needsHistoryBoundary == true)
self?.history.append(next, breakBefore: self?.needsHistoryBoundary == true, expectedInterval: Double(self?.effectiveMilliseconds ?? 5_000) / 1000)
self?.lastPublishedAt = .now
self?.needsHistoryBoundary = false
SamplingDiagnostics.record(next)
let interval = self?.effectiveMilliseconds ?? 5_000
@@ -70,6 +81,7 @@ final class MetricsStore {
if isPaused { start() }
else {
generation += 1
visibilityGeneration += 1
task?.cancel()
task = nil
Task { await sampler.setProcessVisible(false) }