Ignore invalid points

This commit is contained in:
Christoph Hagen 2023-09-07 15:49:00 +02:00
parent 66a04781d3
commit c6b51c98fb

View File

@ -136,6 +136,7 @@ private func readElements<T>(from url: URL) throws -> [Timestamped<T>] where T:
var result: [Timestamped<T>] = [] var result: [Timestamped<T>] = []
var currentIndex = data.startIndex var currentIndex = data.startIndex
var skippedValues = 0
while currentIndex < data.endIndex { while currentIndex < data.endIndex {
let startIndexOfTimestamp = currentIndex + byteCountLength let startIndexOfTimestamp = currentIndex + byteCountLength
guard startIndexOfTimestamp <= data.endIndex else { guard startIndexOfTimestamp <= data.endIndex else {
@ -159,14 +160,18 @@ private func readElements<T>(from url: URL) throws -> [Timestamped<T>] where T:
let timestamp = try decoder.decode(Double.self, from: timestampData) let timestamp = try decoder.decode(Double.self, from: timestampData)
let date = Date(timeIntervalSince1970: timestamp) let date = Date(timeIntervalSince1970: timestamp)
let elementData = data[currentIndex..<nextIndex] let elementData = data[currentIndex..<nextIndex]
do {
let element: T = try decoder.decode(from: elementData) let element: T = try decoder.decode(from: elementData)
result.append(.init(value: element, timestamp: date)) result.append(.init(value: element, timestamp: date))
} catch {
skippedValues += 1
}
currentIndex = nextIndex currentIndex = nextIndex
if result.count % 100 == 1 { if result.count % 100 == 1 {
print("File \(file): \(result.count) entries loaded (\(currentIndex)/\(data.endIndex) bytes)") print("File \(file): \(result.count) entries loaded (\(currentIndex)/\(data.endIndex) bytes)")
} }
} }
print("Loaded \(result.count) data points") print("Loaded \(result.count) data points (\(skippedValues) skipped)")
return result return result
} }