Addresses a multi-agent code review (26 confirmed findings + a URLSession leak) across correctness, concurrency, security, and cleanup: - MongoDocument.id: deterministic fallback (was UUID() on every access) to stop SwiftUI List/ForEach identity churn; getAll pagination drives its cursor off the real _id via documentId, so a missing _id stops paging instead of looping on a fabricated cursor. - View models: per-load generation guard (+ captured-tab guard on ONU detail) so a cancelled or overlapping load can't commit stale results or land data on the wrong tab. - JSONValue.intValue: guard Int(d) against NaN/inf/out-of-range (crash). - ServerTrustEvaluator: hash the real SubjectPublicKeyInfo (prepend the algorithm-specific ASN.1 SPKI header) so a standard openssl-captured public-key pin actually matches; unknown key types fail closed. - AppEnvironment.configure: clear stale cookies + Keychain creds on host change. - FirmwareFile.isCompatible: empty metadata no longer passes as compatible-for-all. - APIClient: invalidate URLSession on deinit (per-configure/probe leak). - Dashboard best-effort sections keep last-good on transient failure; unified optical thresholds; LoginView prefill-once; populatedBuckets unique ForEach id; Format rounding rollover; dead-code/docstring/dedup cleanups. Verification pending on the Mac (no Swift toolchain on the Linux dev box). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
38 lines
1.6 KiB
Swift
38 lines
1.6 KiB
Swift
import Foundation
|
|
|
|
/// Compact human formatting for counters surfaced in the UI.
|
|
enum Format {
|
|
/// Bit-rate, e.g. 224229 → "224 kbps", 7216819 → "7.2 Mbps".
|
|
static func bps(_ value: Double?) -> String {
|
|
guard let value, value >= 0 else { return "—" }
|
|
return scaled(value, divisor: 1000, units: ["bps", "kbps", "Mbps", "Gbps", "Tbps"])
|
|
}
|
|
|
|
/// Byte count, e.g. 7401438 → "7.1 MB".
|
|
static func bytes(_ value: Double?) -> String {
|
|
guard let value, value >= 0 else { return "—" }
|
|
return scaled(value, divisor: 1024, units: ["B", "KB", "MB", "GB", "TB", "PB"])
|
|
}
|
|
|
|
private static func scaled(_ value: Double, divisor: Double, units: [String]) -> String {
|
|
var x = value, i = 0
|
|
while x >= divisor && i < units.count - 1 { x /= divisor; i += 1 }
|
|
// Round at display precision FIRST, then carry to the next unit if rounding
|
|
// reached the ceiling (999.95 kbps → "1000" should read "1.0 Mbps").
|
|
var digits = (i == 0 || x >= 100) ? 0 : 1
|
|
var rounded = (x * pow(10, Double(digits))).rounded() / pow(10, Double(digits))
|
|
if rounded >= divisor && i < units.count - 1 {
|
|
i += 1
|
|
rounded /= divisor
|
|
digits = rounded >= 100 ? 0 : 1
|
|
rounded = (rounded * pow(10, Double(digits))).rounded() / pow(10, Double(digits))
|
|
}
|
|
return "\(String(format: "%.\(digits)f", rounded)) \(units[i])"
|
|
}
|
|
|
|
/// Integer percent, e.g. 13 → "13%".
|
|
static func percent(_ value: Int?) -> String {
|
|
guard let value else { return "—" }
|
|
return "\(value)%"
|
|
}
|
|
}
|