From e52f44f168909eb694d1f0c1b4ab17543729620a Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Thu, 7 Sep 2023 15:23:44 +0200 Subject: [PATCH] Attempt metric log migration --- Package.swift | 2 + Sources/App/configure.swift | 98 +++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/Package.swift b/Package.swift index 8f28cbd..030677c 100644 --- a/Package.swift +++ b/Package.swift @@ -11,6 +11,7 @@ let package = Package( .package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.9.0"), .package(url: "https://github.com/christophhagen/ClairvoyantVapor", from: "0.2.0"), .package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.1"), + .package(url:"https://github.com/christophhagen/CBORCoding", from: "1.0.0"), ], targets: [ .target( @@ -20,6 +21,7 @@ let package = Package( .product(name: "Clairvoyant", package: "Clairvoyant"), .product(name: "ClairvoyantVapor", package: "ClairvoyantVapor"), .product(name: "ClairvoyantBinaryCodable", package: "ClairvoyantBinaryCodable"), + .product(name: "CBORCoding", package: "CBORCoding") ], swiftSettings: [ // Enable better optimizations when building in Release configuration. Despite the use of diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 8d7b00a..353a048 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -23,6 +23,8 @@ private let dateFormatter: DateFormatter = { public func configure(_ app: Application) async throws { let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory) let logFolder = storageFolder.appendingPathComponent("logs") + try await migrate(folder: logFolder) + fatalError("Done") let monitor = MetricObserver(logFileFolder: logFolder, logMetricId: "sesame.log") MetricObserver.standard = monitor @@ -86,3 +88,99 @@ func log(_ message: String) { } observer.log(message) } + +import CBORCoding + +private func migrate(folder: URL) async throws { + try await migrateMetric("sesame.log", containing: String.self, in: folder) + try await migrateMetric("sesame.status", containing: ServerStatus.self, in: folder) + try await migrateMetric("sesame.connected", containing: Bool.self, in: folder) + try await migrateMetric("sesame.messages", containing: Int.self, in: folder) +} + +private func migrateMetric(_ id: String, containing type: T.Type, in folder: URL) async throws where T: MetricValue { + print("Processing metric \(id)") + let file = id.hashed() + let url = folder.appendingPathComponent(file) + + let files = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil) + .filter { Int($0.lastPathComponent) != nil } + print("Found \(files.count) files for \(id)") + + let all: [Timestamped] = try files.map(readElements(from:)) + .reduce([], +) + .sorted { $0.timestamp < $1.timestamp } + + print("Found \(all.count) items for \(id)") + + try FileManager.default.removeItem(at: url) + + print("Removed log folder") + + // TODO: Write values back to disk + let observer = MetricObserver(logFileFolder: folder, logMetricId: "sesame.migration") + let metric: Metric = observer.addMetric(id: id) + try await metric.update(all) + + print("Finished metric \(id)") +} + +private func readElements(from url: URL) throws -> [Timestamped] where T: MetricValue { + let data = try Data(contentsOf: url) + let file = url.lastPathComponent + print("File \(file): Loaded \(data.count) bytes") + + let decoder = CBORDecoder() + let timestampLength = 9 + let byteCountLength = 2 + + var result: [Timestamped] = [] + var currentIndex = data.startIndex + while currentIndex < data.endIndex { + let startIndexOfTimestamp = currentIndex + byteCountLength + guard startIndexOfTimestamp <= data.endIndex else { + print("File \(file): Only \(data.endIndex - currentIndex) bytes, needed \(byteCountLength) for byte count") + throw MetricError.logFileCorrupted + } + guard let byteCount = UInt16(fromData: data[currentIndex..= timestampLength else { + print("File \(file): Only \(byteCount) bytes, needed \(timestampLength) for timestamp") + throw MetricError.logFileCorrupted + } + let timestampData = data[startIndexOfTimestamp.. Data { + Data([UInt8(self >> 8 & 0xFF), UInt8(self & 0xFF)]) + } + + init?(fromData data: T) { + guard data.count == 2 else { + return nil + } + let bytes = Array(data) + self = UInt16(UInt32(bytes[0]) << 8 | UInt32(bytes[1])) + } +}