Skip to content

Commit 1541981

Browse files
committed
fix(zero-swift): match JS on three inspector details the shared vectors check
TDigest.quantile returns NaN, as JS does, where rounding leaves the last cumulative weight below the total, instead of trapping. The connect URL asks for debugPerf at the debug log level, as JS and Rust do. An inspector query's inactivatedAt is new Date(ms)'s time value, without its fraction.
1 parent 99168aa commit 1541981

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

‎swift/Sources/Zero/Client/Engine.swift‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,8 @@ final class Engine {
817817
userID: userID,
818818
baseCookie: connectCookie.stringValue ?? connectCookie.numberValue.map(JSNumber.format),
819819
timestamp: scheduler.monotonicNow, lastMutationID: lastMutationIDReceived, wsid: wsid,
820-
profileID: profileID, additionalParams: additionalConnectParams ?? [])
820+
profileID: profileID, debugPerf: log.level == .debug,
821+
additionalParams: additionalConnectParams ?? [])
821822
addConnectEvent(attempt, "reading deleted clients")
822823
var deleted = deletedClientsBody()
823824
addConnectEvent(attempt, "reading desired queries")

‎swift/Sources/Zero/Client/Inspector.swift‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ public struct InspectorQuery: Sendable {
248248
init(row: JSONValue, clientAST: AST?, clientMetrics: QueryClientMetrics?) throws {
249249
clientID = row["clientID"]?.stringValue ?? ""
250250
id = row["queryID"]?.stringValue ?? ""
251-
inactivatedAt = row["inactivatedAt"]?.numberValue
251+
// `new Date(ms)`'s time value: the whole milliseconds, NaN out of range.
252+
inactivatedAt = row["inactivatedAt"]?.numberValue.map { ms in
253+
ms.isFinite && abs(ms) <= 8.64e15 ? ms.rounded(.towardZero) + 0 : .nan
254+
}
252255
ttl = TTL.milliseconds(row["ttl"]?.numberValue ?? 0).normalized
253256
name = row["name"]?.stringValue
254257
args = row["args"]?.arrayValue

‎swift/Sources/Zero/Util/TDigest.swift‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ extension TDigest {
274274
}
275275
let cumulative = self.cumulative
276276
let lower = binarySearch(cumulative.count) { -cumulative[$0] + index }
277+
// Past the last cumulative weight, which rounding can leave below the
278+
// total weight: JS reads past the arrays' ends, which makes NaN.
279+
if lower == cumulative.count { return .nan }
277280
if lower + 1 != cumulative.count {
278281
let z1 = index - cumulative[lower - 1]
279282
let z2 = cumulative[lower] - index

0 commit comments

Comments
 (0)