Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d2dd306ca |
@@ -0,0 +1,54 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
public struct HistoricalSample<T>: Identifiable {
|
||||||
|
public let id: UUID
|
||||||
|
public let timestamp: Date
|
||||||
|
public let value: T
|
||||||
|
|
||||||
|
public init(timestamp: Date = Date(), value: T) {
|
||||||
|
self.id = UUID()
|
||||||
|
self.timestamp = timestamp
|
||||||
|
self.value = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final class RollingRingBuffer<T> {
|
||||||
|
private var buffer: [HistoricalSample<T>]
|
||||||
|
private let capacity: Int
|
||||||
|
private let lock = NSLock()
|
||||||
|
|
||||||
|
public init(capacity: Int = 60) {
|
||||||
|
self.capacity = max(1, capacity)
|
||||||
|
self.buffer = []
|
||||||
|
self.buffer.reserveCapacity(self.capacity)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func append(_ value: T, timestamp: Date = Date()) {
|
||||||
|
lock.lock()
|
||||||
|
defer { lock.unlock() }
|
||||||
|
|
||||||
|
let sample = HistoricalSample(timestamp: timestamp, value: value)
|
||||||
|
if buffer.count >= capacity {
|
||||||
|
buffer.removeFirst()
|
||||||
|
}
|
||||||
|
buffer.append(sample)
|
||||||
|
}
|
||||||
|
|
||||||
|
public var samples: [HistoricalSample<T>] {
|
||||||
|
lock.lock()
|
||||||
|
defer { lock.unlock() }
|
||||||
|
return buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
public var count: Int {
|
||||||
|
lock.lock()
|
||||||
|
defer { lock.unlock() }
|
||||||
|
return buffer.count
|
||||||
|
}
|
||||||
|
|
||||||
|
public func clear() {
|
||||||
|
lock.lock()
|
||||||
|
defer { lock.unlock() }
|
||||||
|
buffer.removeAll(keepingCapacity: true)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
import Foundation
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
public struct TelemetrySnapshotRecord {
|
||||||
|
public let cpuLoad: Double
|
||||||
|
public let memoryUsedPercent: Double
|
||||||
|
public let networkInBps: Double
|
||||||
|
public let networkOutBps: Double
|
||||||
|
public let diskReadBps: Double
|
||||||
|
public let diskWriteBps: Double
|
||||||
|
public let cpuTemp: Double
|
||||||
|
public let systemPowerWatts: Double
|
||||||
|
public let timestamp: Date
|
||||||
|
|
||||||
|
public init(
|
||||||
|
cpuLoad: Double = 0.0,
|
||||||
|
memoryUsedPercent: Double = 0.0,
|
||||||
|
networkInBps: Double = 0.0,
|
||||||
|
networkOutBps: Double = 0.0,
|
||||||
|
diskReadBps: Double = 0.0,
|
||||||
|
diskWriteBps: Double = 0.0,
|
||||||
|
cpuTemp: Double = 0.0,
|
||||||
|
systemPowerWatts: Double = 0.0,
|
||||||
|
timestamp: Date = Date()
|
||||||
|
) {
|
||||||
|
self.cpuLoad = cpuLoad
|
||||||
|
self.memoryUsedPercent = memoryUsedPercent
|
||||||
|
self.networkInBps = networkInBps
|
||||||
|
self.networkOutBps = networkOutBps
|
||||||
|
self.diskReadBps = diskReadBps
|
||||||
|
self.diskWriteBps = diskWriteBps
|
||||||
|
self.cpuTemp = cpuTemp
|
||||||
|
self.systemPowerWatts = systemPowerWatts
|
||||||
|
self.timestamp = timestamp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Observable
|
||||||
|
public final class TelemetryHistoryStore {
|
||||||
|
public static let shared = TelemetryHistoryStore()
|
||||||
|
|
||||||
|
public let cpuHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let memoryHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let networkInHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let networkOutHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let diskReadHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let diskWriteHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let cpuTempHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
public let powerHistory = RollingRingBuffer<Double>(capacity: 60)
|
||||||
|
|
||||||
|
public private(set) var sampleCounter: UInt64 = 0
|
||||||
|
|
||||||
|
public init() {}
|
||||||
|
|
||||||
|
public func record(snapshot: TelemetrySnapshotRecord) {
|
||||||
|
cpuHistory.append(snapshot.cpuLoad, timestamp: snapshot.timestamp)
|
||||||
|
memoryHistory.append(snapshot.memoryUsedPercent, timestamp: snapshot.timestamp)
|
||||||
|
networkInHistory.append(snapshot.networkInBps, timestamp: snapshot.timestamp)
|
||||||
|
networkOutHistory.append(snapshot.networkOutBps, timestamp: snapshot.timestamp)
|
||||||
|
diskReadHistory.append(snapshot.diskReadBps, timestamp: snapshot.timestamp)
|
||||||
|
diskWriteHistory.append(snapshot.diskWriteBps, timestamp: snapshot.timestamp)
|
||||||
|
cpuTempHistory.append(snapshot.cpuTemp, timestamp: snapshot.timestamp)
|
||||||
|
powerHistory.append(snapshot.systemPowerWatts, timestamp: snapshot.timestamp)
|
||||||
|
sampleCounter &+= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
import SwiftUI
|
||||||
|
import Charts
|
||||||
|
|
||||||
|
public struct TelemetryTrendChartView: View {
|
||||||
|
public let title: String
|
||||||
|
public let unit: String
|
||||||
|
public let color: Color
|
||||||
|
public let samples: [HistoricalSample<Double>]
|
||||||
|
public let maxY: Double?
|
||||||
|
|
||||||
|
public init(
|
||||||
|
title: String,
|
||||||
|
unit: String,
|
||||||
|
color: Color,
|
||||||
|
samples: [HistoricalSample<Double>],
|
||||||
|
maxY: Double? = nil
|
||||||
|
) {
|
||||||
|
self.title = title
|
||||||
|
self.unit = unit
|
||||||
|
self.color = color
|
||||||
|
self.samples = samples
|
||||||
|
self.maxY = maxY
|
||||||
|
}
|
||||||
|
|
||||||
|
private var currentValue: Double {
|
||||||
|
samples.last?.value ?? 0.0
|
||||||
|
}
|
||||||
|
|
||||||
|
private var averageValue: Double {
|
||||||
|
guard !samples.isEmpty else { return 0.0 }
|
||||||
|
return samples.reduce(0.0) { $0 + $1.value } / Double(samples.count)
|
||||||
|
}
|
||||||
|
|
||||||
|
private var maxValue: Double {
|
||||||
|
samples.map(\.value).max() ?? 0.0
|
||||||
|
}
|
||||||
|
|
||||||
|
public var body: some View {
|
||||||
|
VStack(alignment: .leading, spacing: 10) {
|
||||||
|
HStack {
|
||||||
|
Text(title)
|
||||||
|
.font(.headline)
|
||||||
|
Spacer()
|
||||||
|
HStack(spacing: 12) {
|
||||||
|
Text(String(format: "Cur: %.1f %@", currentValue, unit))
|
||||||
|
.font(.caption)
|
||||||
|
.fontWeight(.semibold)
|
||||||
|
.foregroundColor(color)
|
||||||
|
Text(String(format: "Avg: %.1f %@", averageValue, unit))
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
Text(String(format: "Max: %.1f %@", maxValue, unit))
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Chart {
|
||||||
|
ForEach(Array(samples.enumerated()), id: \.element.id) { index, sample in
|
||||||
|
LineMark(
|
||||||
|
x: .value("Sample", index),
|
||||||
|
y: .value("Value", sample.value)
|
||||||
|
)
|
||||||
|
.interpolationMethod(.monotone)
|
||||||
|
.foregroundStyle(color)
|
||||||
|
|
||||||
|
AreaMark(
|
||||||
|
x: .value("Sample", index),
|
||||||
|
y: .value("Value", sample.value)
|
||||||
|
)
|
||||||
|
.interpolationMethod(.monotone)
|
||||||
|
.foregroundStyle(
|
||||||
|
LinearGradient(
|
||||||
|
colors: [color.opacity(0.35), color.opacity(0.05)],
|
||||||
|
startPoint: .top,
|
||||||
|
endPoint: .bottom
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.chartYScale(domain: 0...(maxY ?? max(1.0, maxValue * 1.15)))
|
||||||
|
.chartXAxis(.hidden)
|
||||||
|
.frame(height: 120)
|
||||||
|
}
|
||||||
|
.padding(14)
|
||||||
|
.background(Color(NSColor.controlBackgroundColor))
|
||||||
|
.cornerRadius(10)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
import XCTest
|
||||||
|
@testable import MacMonitor
|
||||||
|
|
||||||
|
final class MMHistoryTests: XCTestCase {
|
||||||
|
|
||||||
|
func testRollingRingBufferCapacityAndEviction() {
|
||||||
|
let buffer = RollingRingBuffer<Double>(capacity: 3)
|
||||||
|
XCTAssertEqual(buffer.count, 0)
|
||||||
|
|
||||||
|
buffer.append(10.0)
|
||||||
|
buffer.append(20.0)
|
||||||
|
XCTAssertEqual(buffer.count, 2)
|
||||||
|
XCTAssertEqual(buffer.samples.map(\.value), [10.0, 20.0])
|
||||||
|
|
||||||
|
buffer.append(30.0)
|
||||||
|
XCTAssertEqual(buffer.count, 3)
|
||||||
|
XCTAssertEqual(buffer.samples.map(\.value), [10.0, 20.0, 30.0])
|
||||||
|
|
||||||
|
// 4th append should evict oldest (10.0)
|
||||||
|
buffer.append(40.0)
|
||||||
|
XCTAssertEqual(buffer.count, 3)
|
||||||
|
XCTAssertEqual(buffer.samples.map(\.value), [20.0, 30.0, 40.0])
|
||||||
|
|
||||||
|
buffer.clear()
|
||||||
|
XCTAssertEqual(buffer.count, 0)
|
||||||
|
XCTAssertTrue(buffer.samples.isEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testTelemetryHistoryStoreRecord() {
|
||||||
|
let store = TelemetryHistoryStore()
|
||||||
|
let snapshot = TelemetrySnapshotRecord(
|
||||||
|
cpuLoad: 25.5,
|
||||||
|
memoryUsedPercent: 62.1,
|
||||||
|
networkInBps: 1024.0,
|
||||||
|
networkOutBps: 2048.0,
|
||||||
|
diskReadBps: 512.0,
|
||||||
|
diskWriteBps: 1024.0,
|
||||||
|
cpuTemp: 55.0,
|
||||||
|
systemPowerWatts: 18.5
|
||||||
|
)
|
||||||
|
store.record(snapshot: snapshot)
|
||||||
|
|
||||||
|
XCTAssertEqual(store.sampleCounter, 1)
|
||||||
|
XCTAssertEqual(store.cpuHistory.count, 1)
|
||||||
|
XCTAssertEqual(store.cpuHistory.samples.first?.value, 25.5)
|
||||||
|
XCTAssertEqual(store.memoryHistory.samples.first?.value, 62.1)
|
||||||
|
XCTAssertEqual(store.networkInHistory.samples.first?.value, 1024.0)
|
||||||
|
XCTAssertEqual(store.networkOutHistory.samples.first?.value, 2048.0)
|
||||||
|
XCTAssertEqual(store.diskReadHistory.samples.first?.value, 512.0)
|
||||||
|
XCTAssertEqual(store.diskWriteHistory.samples.first?.value, 1024.0)
|
||||||
|
XCTAssertEqual(store.cpuTempHistory.samples.first?.value, 55.0)
|
||||||
|
XCTAssertEqual(store.powerHistory.samples.first?.value, 18.5)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user