105 lines
4.5 KiB
Swift
105 lines
4.5 KiB
Swift
import Foundation
|
|
import Observation
|
|
|
|
@MainActor @Observable
|
|
final class MetricsStore {
|
|
private(set) var snapshot = MetricsSnapshot()
|
|
private(set) var history = MetricHistory()
|
|
private(set) var isPaused = false
|
|
private(set) var isPanelVisible = false
|
|
let sampling: SamplingPreferences
|
|
var effectiveMilliseconds: Int { isPanelVisible ? sampling.panelMilliseconds : sampling.backgroundMilliseconds }
|
|
@ObservationIgnored private var generation = 0
|
|
@ObservationIgnored private var visibilityGeneration = 0
|
|
@ObservationIgnored private var task: Task<Void, Never>?
|
|
@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, 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, sampleImmediately: false)
|
|
}
|
|
start()
|
|
}
|
|
|
|
func start() {
|
|
guard task == nil else { return }
|
|
isPaused = false
|
|
reschedule(reset: true)
|
|
}
|
|
|
|
private func reschedule(reset: Bool, sampleImmediately: Bool = true) {
|
|
generation += 1
|
|
task?.cancel()
|
|
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.visibilityGeneration == visibleGeneration,
|
|
self.isPanelVisible, !self.isPaused else { return }
|
|
if self.snapshot.processes.map({ $0.date > processes.date }) != true { self.snapshot.processes = processes }
|
|
}
|
|
})
|
|
guard !Task.isCancelled, self?.generation == currentGeneration else { return }
|
|
if self?.isPanelVisible != true || self?.visibilityGeneration != visibleGeneration { next.processes = nil }
|
|
else if let completed = self?.snapshot.processes, completed.date >= next.date {
|
|
// A fast detached process result may reach MainActor before this quick snapshot.
|
|
next.processes = completed
|
|
}
|
|
self?.snapshot = next
|
|
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
|
|
do { try await Task.sleep(for: .milliseconds(interval)) }
|
|
catch { return }
|
|
}
|
|
}
|
|
}
|
|
|
|
func togglePause() {
|
|
if isPaused { start() }
|
|
else {
|
|
generation += 1
|
|
visibilityGeneration += 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) }
|
|
if !isPaused { reschedule(reset: false) }
|
|
}
|
|
|
|
deinit { task?.cancel() }
|
|
}
|