feat(monitors): 接入统一面板数据与按需进程采样
This commit is contained in:
@@ -6,6 +6,9 @@ final class MetricsStore {
|
||||
private(set) var snapshot = MetricsSnapshot()
|
||||
private(set) var history = MetricHistory()
|
||||
private(set) var isPaused = false
|
||||
private(set) var isPanelVisible = false
|
||||
@ObservationIgnored private var generation = 0
|
||||
@ObservationIgnored private var visibilityGeneration = 0
|
||||
@ObservationIgnored private var task: Task<Void, Never>?
|
||||
@ObservationIgnored private let sampler = SystemSampler()
|
||||
|
||||
@@ -14,11 +17,16 @@ final class MetricsStore {
|
||||
func start() {
|
||||
guard task == nil else { return }
|
||||
isPaused = false
|
||||
generation += 1
|
||||
let currentGeneration = generation
|
||||
task = Task { [weak self, sampler] in
|
||||
await sampler.reset()
|
||||
await sampler.setProcessVisible(self?.isPanelVisible == true)
|
||||
while !Task.isCancelled {
|
||||
let next = await sampler.sample()
|
||||
guard !Task.isCancelled else { return }
|
||||
let visibleGeneration = self?.visibilityGeneration
|
||||
var next = await sampler.sample()
|
||||
guard !Task.isCancelled, self?.generation == currentGeneration else { return }
|
||||
if self?.isPanelVisible != true || self?.visibilityGeneration != visibleGeneration { next.processes = nil }
|
||||
self?.snapshot = next
|
||||
self?.history.append(next)
|
||||
SamplingDiagnostics.record(next)
|
||||
@@ -31,12 +39,23 @@ final class MetricsStore {
|
||||
func togglePause() {
|
||||
if isPaused { start() }
|
||||
else {
|
||||
generation += 1
|
||||
task?.cancel()
|
||||
task = nil
|
||||
Task { await sampler.setProcessVisible(false) }
|
||||
isPaused = true
|
||||
SamplingDiagnostics.record(snapshot, event: "pause")
|
||||
}
|
||||
}
|
||||
|
||||
func setPanelVisible(_ visible: Bool) {
|
||||
guard isPanelVisible != visible else { return }
|
||||
visibilityGeneration += 1
|
||||
isPanelVisible = visible
|
||||
if !visible { snapshot.processes = nil }
|
||||
let active = visible && !isPaused
|
||||
Task { await sampler.setProcessVisible(active) }
|
||||
}
|
||||
|
||||
deinit { task?.cancel() }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user