fix(monitors): 修复短暂隐藏重开后的趋势误断层
This commit is contained in:
@@ -20,6 +20,7 @@ nonisolated struct TrendSample: Sendable {
|
||||
var memoryWired: Double?
|
||||
var memoryCompressed: Double?
|
||||
var breakBefore = false
|
||||
/// Scheduled cadence after this point, including changes before the next sample.
|
||||
var expectedInterval: TimeInterval = 2
|
||||
}
|
||||
|
||||
@@ -29,6 +30,18 @@ nonisolated struct MetricHistory: Sendable {
|
||||
private(set) var samples: [TrendSample] = []
|
||||
private(set) var interfaces: [String: [InterfaceTrendSample]] = [:]
|
||||
|
||||
/// A slow cadence can begin and end without producing a point (for example a short hide).
|
||||
/// Preserve that outgoing wait on the existing point without changing earlier segments.
|
||||
mutating func recordScheduledInterval(_ interval: TimeInterval) {
|
||||
guard interval.isFinite, interval > 0, let last = samples.indices.last else { return }
|
||||
samples[last].expectedInterval = max(samples[last].expectedInterval, interval)
|
||||
for key in interfaces.keys {
|
||||
guard let index = interfaces[key]?.indices.last,
|
||||
let previous = interfaces[key]?.last?.expectedInterval else { continue }
|
||||
interfaces[key]?[index].expectedInterval = max(previous, interval)
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -55,6 +68,7 @@ nonisolated struct InterfaceTrendSample: Sendable {
|
||||
let date: Date
|
||||
let download: Double?
|
||||
let upload: Double?
|
||||
/// Scheduled cadence from this point to the next interface sample.
|
||||
var expectedInterval: TimeInterval = 2
|
||||
}
|
||||
|
||||
@@ -72,7 +86,7 @@ nonisolated struct GPUUtilizationSummary {
|
||||
var duration = 0.0
|
||||
for (a, b) in zip(samples, samples.dropFirst()) {
|
||||
let dt = b.date.timeIntervalSince(a.date)
|
||||
guard !b.breakBefore, dt > 0, dt <= max(a.expectedInterval, b.expectedInterval) * 2.5,
|
||||
guard !b.breakBefore, dt > 0, dt <= a.expectedInterval * 2.5,
|
||||
let av = a.gpu, let bv = b.gpu else { continue }
|
||||
let overlap = b.date.timeIntervalSince(max(a.date, start))
|
||||
guard overlap > 0 else { continue }
|
||||
|
||||
@@ -54,6 +54,9 @@ final class MetricsStore {
|
||||
needsHistoryBoundary = needsHistoryBoundary || reset
|
||||
scheduledInterval = effectiveMilliseconds
|
||||
guard hasSamplingDemand else { needsHistoryBoundary = true; return }
|
||||
if !needsHistoryBoundary {
|
||||
history.recordScheduledInterval(Double(scheduledInterval) / 1000)
|
||||
}
|
||||
var delay = Duration.zero
|
||||
if !sampleImmediately, let lastPublishedAt {
|
||||
delay = max(.zero, .milliseconds(effectiveMilliseconds) - lastPublishedAt.duration(to: .now))
|
||||
|
||||
Reference in New Issue
Block a user