git.iko.soy/parseable-macos
on master help sign in
Fix decoding of Parseable responses and alert pagination

Checked against the Parseable server source: - /about sends camelCase uiVersion/deploymentId/grpcPort (never snake_case), and newer servers send `license` as an object. - /logstream/{name}/info key spellings vary by version (created-at, time_partition, static_schema_flag as the string "true", camelCase on newer servers); none of cache-enabled/time-partition/static-schema-flag was ever sent. - Storage stats name the field `format`, not `type`; 1.x "<n> Bytes" sizes are now formatted. - Parameterized Arrow types such as p_timestamp's {"Timestamp": ["Millisecond", null]} showed as "{1 fields}". - Legacy column alert rules with an object config lost their rule spec. - /api/v1/alerts paginates (100 by default), so a stream's alerts past the first page were missing; fetch all pages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015p9SN94YqDtQkEisiPQ3xJ

commitf4c4ceda876461b5b3d6a2eaa1c16194375d912d authorClaude <noreply@anthropic.com> dateWed 30 Sep 2026, 10:49 (1 week ago) parentd947711
5 files changed+253−20
ParseableViewer/Models/AlertModels.swift+21 ParseableViewer/Models/LogStream.swift+97−11 ParseableViewer/Models/ServerInfo.swift+33−7 ParseableViewer/Services/ParseableClient.swift+44−2 ParseableViewerTests/ParseableClientTests.swift+58
ParseableViewer/Models/AlertModels.swift+21
@@ -85,6 +85,27 @@ struct AlertRule: Identifiable, Codable, Sendable {
8585struct AlertRuleSpec: Codable, Sendable {
8686 let type: String?
8787 let config: String?
88+
89+ private enum CodingKeys: String, CodingKey {
90+ case type, config
91+ }
92+
93+ init(from decoder: Decoder) throws {
94+ let container = try decoder.container(keyedBy: CodingKeys.self)
95+ self.type = try? container.decode(String.self, forKey: .type)
96+ // The legacy per-stream rule is serialized as `{"type": …, "config": …}`
97+ // where only composite rules carry a string config; column rules carry an
98+ // object (`{"column": …, "operator": …, "value": …}`). Decoding that as a
99+ // String threw, which made the whole spec — and its type badge — vanish.
100+ // Keep non-string configs as compact JSON instead.
101+ if let string = try? container.decode(String.self, forKey: .config) {
102+ self.config = string
103+ } else if let value = try? container.decode(JSONValue.self, forKey: .config), value != .null {
104+ self.config = value.compactJSON
105+ } else {
106+ self.config = nil
107+ }
108+ }
88109}
89110
90111struct AlertTarget: Codable, Sendable {
ParseableViewer/Models/LogStream.swift+97−11
@@ -80,11 +80,39 @@ struct SchemaField: Identifiable, Codable, Hashable, Sendable {
8080 if let typeStr = try? container.decode(String.self, forKey: .dataType) {
8181 self.dataType = typeStr
8282 } else if let typeObj = try? container.decode(JSONValue.self, forKey: .dataType) {
83− self.dataType = typeObj.displayString
83+ self.dataType = Self.describeArrowType(typeObj)
8484 } else {
8585 self.dataType = "Unknown"
8686 }
8787 }
88+
89+ /// Renders a serde-serialized Arrow `DataType`. Parameterized types are
90+ /// externally tagged objects — `p_timestamp`, present in every Parseable
91+ /// stream, arrives as `{"Timestamp": ["Millisecond", null]}` and a list as
92+ /// `{"List": {<field>}}` — which `displayString` collapsed to the placeholder
93+ /// "{1 fields}". Produce Arrow's own notation instead, e.g.
94+ /// `Timestamp(Millisecond, None)` / `List(Utf8)`.
95+ static func describeArrowType(_ value: JSONValue) -> String {
96+ switch value {
97+ case .null:
98+ return "None"
99+ case .string(let s):
100+ return s
101+ case .array(let items):
102+ return items.map { describeArrowType($0) }.joined(separator: ", ")
103+ case .object(let dict):
104+ // A nested `Field` (list/map element, struct member): show its type.
105+ if let fieldType = dict["data_type"] {
106+ return describeArrowType(fieldType)
107+ }
108+ if dict.count == 1, let entry = dict.first {
109+ return "\(entry.key)(\(describeArrowType(entry.value)))"
110+ }
111+ return value.compactJSON
112+ default:
113+ return value.displayString
114+ }
115+ }
88116}
89117
90118/// Formats a byte count that may arrive from the server as a fractional or
@@ -106,14 +134,59 @@ private func formatByteSize(_ value: Int) -> String? {
106134/// Normalizes a byte count that arrived as a JSON *string*. A bare numeric string
107135/// (e.g. "1048576") is a raw byte count and is formatted like the numeric branches
108136/// so identical sizes render consistently ("1 MB") regardless of whether the server
109−/// quoted the value; an already-formatted string (e.g. "1.2 GB") is passed through
110−/// unchanged.
137+/// quoted the value. Parseable 1.x — the versions that quote sizes — sends them as
138+/// "<n> Bytes" (e.g. "1048576 Bytes"), so that unit suffix is accepted too. Any
139+/// other already-formatted string (e.g. "1.2 GB") is passed through unchanged.
111140private func formatByteSize(_ value: String) -> String {
112− let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines)
141+ var trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines)
142+ if trimmed.lowercased().hasSuffix(" bytes") {
143+ trimmed = String(trimmed.dropLast(" bytes".count))
144+ }
113145 if let n = Int(trimmed) { return formatByteSize(n) ?? value }
114146 return value
115147}
116148
149+/// A string-backed coding key for fields whose name differs across Parseable
150+/// versions (kebab-case, snake_case, and camelCase spellings of the same field).
151+private struct FlexibleCodingKey: CodingKey {
152+ let stringValue: String
153+ var intValue: Int? { nil }
154+ init(stringValue: String) { self.stringValue = stringValue }
155+ init?(intValue: Int) { nil }
156+}
157+
158+private extension KeyedDecodingContainer where Key == FlexibleCodingKey {
159+ /// The first of `keys` that decodes as a string.
160+ func firstString(_ keys: String...) -> String? {
161+ for key in keys {
162+ if let value = try? decode(String.self, forKey: FlexibleCodingKey(stringValue: key)) {
163+ return value
164+ }
165+ }
166+ return nil
167+ }
168+
169+ /// The first of `keys` that decodes as a boolean — either a JSON bool or the
170+ /// strings "true"/"false" (Parseable serializes `static_schema_flag` as the
171+ /// string "true").
172+ func firstBool(_ keys: String...) -> Bool? {
173+ for key in keys {
174+ let codingKey = FlexibleCodingKey(stringValue: key)
175+ if let value = try? decode(Bool.self, forKey: codingKey) {
176+ return value
177+ }
178+ if let string = try? decode(String.self, forKey: codingKey) {
179+ switch string.lowercased() {
180+ case "true": return true
181+ case "false": return false
182+ default: continue
183+ }
184+ }
185+ }
186+ return nil
187+ }
188+}
189+
117190struct StreamStats: Codable, Sendable {
118191 let ingestion: IngestionStats?
119192 let storage: StorageStats?
@@ -165,7 +238,11 @@ struct StreamStats: Codable, Sendable {
165238
166239 init(from decoder: Decoder) throws {
167240 let container = try decoder.container(keyedBy: CodingKeys.self)
168− self.type = try? container.decode(String.self, forKey: .type)
241+ // Parseable names this field `format` (e.g. "parquet") in every
242+ // version; `type` never appears, so "Storage Type" always read N/A.
243+ // Keep `type` as a fallback.
244+ self.type = (try? decoder.container(keyedBy: FlexibleCodingKey.self))?
245+ .firstString("format", "type")
169246 if let s = try? container.decode(String.self, forKey: .size) {
170247 self.size = formatByteSize(s)
171248 } else if let n = try? container.decode(Int.self, forKey: .size) {
@@ -208,11 +285,20 @@ struct StreamInfo: Codable, Sendable {
208285 }
209286
210287 init(from decoder: Decoder) throws {
211− let container = try decoder.container(keyedBy: CodingKeys.self)
212− self.createdAt = try? container.decode(String.self, forKey: .createdAt)
213− self.firstEventAt = try? container.decode(String.self, forKey: .firstEventAt)
214− self.cacheEnabled = try? container.decode(Bool.self, forKey: .cacheEnabled)
215− self.timePartition = try? container.decode(String.self, forKey: .timePartition)
216− self.staticSchemaFlag = try? container.decode(Bool.self, forKey: .staticSchemaFlag)
288+ // The /info key spellings vary by Parseable version: 1.x–2.x send
289+ // `created-at`/`first-event-at` with the other fields in snake_case
290+ // (`cache_enabled`, `time_partition`, `static_schema_flag`), while newer
291+ // servers send camelCase throughout (`createdAt`, `timePartition`, …).
292+ // The kebab-case `cache-enabled`/`time-partition`/`static-schema-flag`
293+ // decoded previously were never sent by any version, so those rows always
294+ // showed "N/A". The schema flag also arrives as the string "true".
295+ let container = try decoder.container(keyedBy: FlexibleCodingKey.self)
296+ self.createdAt = container.firstString("created-at", "createdAt", "created_at")
297+ self.firstEventAt = container.firstString("first-event-at", "firstEventAt", "first_event_at")
298+ self.cacheEnabled = container.firstBool("cache_enabled", "cacheEnabled", "cache-enabled")
299+ self.timePartition = container.firstString("time_partition", "timePartition", "time-partition")
300+ self.staticSchemaFlag = container.firstBool(
301+ "static_schema_flag", "staticSchemaFlag", "static-schema-flag"
302+ )
217303 }
218304}
ParseableViewer/Models/ServerInfo.swift+33−7
@@ -15,33 +15,59 @@ struct ServerAbout: Codable, Sendable {
1515 let llmActive: Bool?
1616 let oidcActive: Bool?
1717
18+ // Parseable's /about handler has always sent camelCase keys (`uiVersion`,
19+ // `deploymentId`, `grpcPort`), like the other fields here. Decoding only the
20+ // snake_case spellings — which the server never sends — left UI Version and
21+ // Deployment ID permanently "N/A" and hid the gRPC Port row. The snake_case
22+ // forms are kept as a fallback.
1823 private enum CodingKeys: String, CodingKey {
1924 case version, commit, mode, staging, store, license
20− case uiVersion = "ui_version"
21− case deploymentId = "deployment_id"
22− case grpcPort = "grpc_port"
25+ case uiVersion
26+ case deploymentId
27+ case grpcPort
2328 case updateAvailable
2429 case latestVersion
2530 case llmActive
2631 case oidcActive
2732 }
2833
34+ private enum SnakeCaseKeys: String, CodingKey {
35+ case uiVersion = "ui_version"
36+ case deploymentId = "deployment_id"
37+ case grpcPort = "grpc_port"
38+ }
39+
2940 init(from decoder: Decoder) throws {
3041 let container = try decoder.container(keyedBy: CodingKeys.self)
42+ let snakeCase = try? decoder.container(keyedBy: SnakeCaseKeys.self)
3143 self.version = try? container.decode(String.self, forKey: .version)
32− self.uiVersion = try? container.decode(String.self, forKey: .uiVersion)
44+ self.uiVersion = (try? container.decode(String.self, forKey: .uiVersion))
45+ ?? (try? snakeCase?.decode(String.self, forKey: .uiVersion))
3346 self.commit = try? container.decode(String.self, forKey: .commit)
34− self.deploymentId = try? container.decode(String.self, forKey: .deploymentId)
47+ self.deploymentId = (try? container.decode(String.self, forKey: .deploymentId))
48+ ?? (try? snakeCase?.decode(String.self, forKey: .deploymentId))
3549 self.mode = try? container.decode(String.self, forKey: .mode)
3650 self.staging = try? container.decode(String.self, forKey: .staging)
3751 self.store = try? container.decode(StoreInfo.self, forKey: .store)
38− self.license = try? container.decode(String.self, forKey: .license)
39− self.grpcPort = try? container.decode(Int.self, forKey: .grpcPort)
52+ self.license = (try? container.decode(String.self, forKey: .license))
53+ ?? (try? container.decode(JSONValue.self, forKey: .license)).flatMap(Self.describeLicense)
54+ self.grpcPort = (try? container.decode(Int.self, forKey: .grpcPort))
55+ ?? (try? snakeCase?.decode(Int.self, forKey: .grpcPort))
4056 self.updateAvailable = try? container.decode(Bool.self, forKey: .updateAvailable)
4157 self.latestVersion = try? container.decode(String.self, forKey: .latestVersion)
4258 self.llmActive = try? container.decode(Bool.self, forKey: .llmActive)
4359 self.oidcActive = try? container.decode(Bool.self, forKey: .oidcActive)
4460 }
61+
62+ /// Current servers send `license` as an object (`{"name": "AGPL-3.0-only",
63+ /// "plan": "OSS", …}`) rather than a string, which decoded to nil ("N/A").
64+ private static func describeLicense(_ value: JSONValue) -> String? {
65+ guard case .object(let fields) = value, let name = fields["name"]?.stringValue else { return nil }
66+ if let plan = fields["plan"]?.stringValue, !plan.isEmpty {
67+ return "\(name) (\(plan))"
68+ }
69+ return name
70+ }
4571}
4672
4773struct StoreInfo: Codable, Sendable {
ParseableViewer/Services/ParseableClient.swift+44−2
@@ -409,9 +409,51 @@ final class ParseableClient: Sendable {
409409 return try Self.jsonDecoder.decode(AlertConfig.self, from: data)
410410 }
411411
412+ // `/api/v1/alerts` paginates (default page size 100, max 1000) over ALL
413+ // alerts server-wide, ordered by state/severity/title. Filtering only the
414+ // first page by stream (below) silently missed any stream whose alerts sort
415+ // past it. Request full pages until a short one. Servers that predate
416+ // pagination ignore the parameters and return everything each time, so a
417+ // page that adds nothing new also ends the loop.
418+ func modernAlerts() async throws -> (data: Data, config: AlertConfig) {
419+ let pageSize = 1000
420+ var firstData: Data?
421+ var version: String?
422+ var collected: [AlertRule] = []
423+ var seenIDs = Set<String>()
424+ var offset = 0
425+ for _ in 0..<50 {
426+ let data: Data
427+ do {
428+ data = try await performRequest(
429+ method: "GET",
430+ path: "/api/v1/alerts",
431+ queryItems: [
432+ URLQueryItem(name: "limit", value: String(pageSize)),
433+ URLQueryItem(name: "offset", value: String(offset)),
434+ ]
435+ )
436+ } catch ParseableError.serverError(let code, _) where code == 400 && offset == 0 {
437+ // A server that rejects the paging parameters: one plain request.
438+ let data = try await performRequest(method: "GET", path: "/api/v1/alerts")
439+ return (data, try Self.jsonDecoder.decode(AlertConfig.self, from: data))
440+ }
441+ let page = try Self.jsonDecoder.decode(AlertConfig.self, from: data)
442+ if firstData == nil {
443+ firstData = data
444+ version = page.version
445+ }
446+ let alerts = page.alerts ?? []
447+ let fresh = alerts.filter { seenIDs.insert($0.id).inserted }
448+ collected.append(contentsOf: fresh)
449+ if alerts.count < pageSize || fresh.isEmpty { break }
450+ offset += alerts.count
451+ }
452+ return (firstData ?? Data(), AlertConfig(alerts: collected, version: version))
453+ }
454+
412455 do {
413− let data = try await performRequest(method: "GET", path: "/api/v1/alerts")
414− let config = try Self.jsonDecoder.decode(AlertConfig.self, from: data)
456+ let (data, config) = try await modernAlerts()
415457 // AlertConfig decodes leniently (every field via try?), so a 200 with
416458 // an unexpected body yields an empty, non-throwing config rather than
417459 // a DecodingError — indistinguishable from a server that genuinely has
ParseableViewerTests/ParseableClientTests.swift+58
@@ -646,4 +646,62 @@ final class ParseableClientTests: XCTestCase {
646646 // Carriage returns in values should be wrapped in quotes per RFC 4180
647647 XCTAssertTrue(csv.contains("\"line1\r\nline2\""))
648648 }
649+
650+ // MARK: - Server response key spellings
651+
652+ func testServerAboutDecodesCamelCaseKeys() throws {
653+ let json = #"{"uiVersion": "v1.2", "deploymentId": "01H", "grpcPort": 8001}"#.data(using: .utf8)!
654+ let about = try JSONDecoder().decode(ServerAbout.self, from: json)
655+ XCTAssertEqual(about.uiVersion, "v1.2")
656+ XCTAssertEqual(about.deploymentId, "01H")
657+ XCTAssertEqual(about.grpcPort, 8001)
658+ }
659+
660+ func testStreamInfoDecodesSnakeCaseKeys() throws {
661+ let json = #"{"created-at": "c", "first-event-at": "f", "time_partition": "ts", "static_schema_flag": "true", "cache_enabled": true}"#
662+ .data(using: .utf8)!
663+ let info = try JSONDecoder().decode(StreamInfo.self, from: json)
664+ XCTAssertEqual(info.createdAt, "c")
665+ XCTAssertEqual(info.firstEventAt, "f")
666+ XCTAssertEqual(info.timePartition, "ts")
667+ XCTAssertEqual(info.staticSchemaFlag, true)
668+ XCTAssertEqual(info.cacheEnabled, true)
669+ }
670+
671+ func testStreamInfoDecodesCamelCaseKeys() throws {
672+ let json = #"{"createdAt": "c", "firstEventAt": "f", "timePartition": "ts", "staticSchemaFlag": "true"}"#
673+ .data(using: .utf8)!
674+ let info = try JSONDecoder().decode(StreamInfo.self, from: json)
675+ XCTAssertEqual(info.createdAt, "c")
676+ XCTAssertEqual(info.firstEventAt, "f")
677+ XCTAssertEqual(info.timePartition, "ts")
678+ XCTAssertEqual(info.staticSchemaFlag, true)
679+ }
680+
681+ func testStorageStatsReadsFormatAndUnitSuffixedSizes() throws {
682+ let json = #"{"storage": {"size": "2048 Bytes", "format": "parquet"}}"#.data(using: .utf8)!
683+ let stats = try JSONDecoder().decode(StreamStats.self, from: json)
684+ XCTAssertEqual(stats.storage?.type, "parquet")
685+ XCTAssertEqual(stats.storage?.size, ByteCountFormatter.string(fromByteCount: 2048, countStyle: .file))
686+ }
687+
688+ func testSchemaFieldDescribesParameterizedArrowTypes() throws {
689+ let json = #"{"name": "p_timestamp", "data_type": {"Timestamp": ["Millisecond", null]}}"#.data(using: .utf8)!
690+ let field = try JSONDecoder().decode(SchemaField.self, from: json)
691+ XCTAssertEqual(field.dataType, "Timestamp(Millisecond, None)")
692+ }
693+
694+ func testLegacyAlertRuleWithObjectConfigKeepsRuleType() throws {
695+ let json = #"{"name": "a", "rule": {"type": "column", "config": {"column": "status", "operator": "=", "value": 500}}}"#
696+ .data(using: .utf8)!
697+ let rule = try JSONDecoder().decode(AlertRule.self, from: json)
698+ XCTAssertEqual(rule.rule?.type, "column")
699+ XCTAssertNotNil(rule.rule?.config)
700+ }
701+
702+ func testServerAboutDecodesObjectLicense() throws {
703+ let json = #"{"license": {"name": "AGPL-3.0-only", "plan": "OSS"}}"#.data(using: .utf8)!
704+ let about = try JSONDecoder().decode(ServerAbout.self, from: json)
705+ XCTAssertEqual(about.license, "AGPL-3.0-only (OSS)")
706+ }
649707}