|
| 1 | +// |
| 2 | +// Created by Jakub Bednář on 04/11/2021. |
| 3 | +// |
| 4 | + |
| 5 | +import ArgumentParser |
| 6 | +import Combine |
| 7 | +import CSV |
| 8 | +import Foundation |
| 9 | +import InfluxDBSwift |
| 10 | + |
| 11 | +struct WriteDataInBatches: ParsableCommand { |
| 12 | + @Option(name: .shortAndLong, help: "The name or id of the bucket destination.") |
| 13 | + private var bucket: String |
| 14 | + |
| 15 | + @Option(name: .shortAndLong, help: "The name or id of the organization destination.") |
| 16 | + private var org: String |
| 17 | + |
| 18 | + @Option(name: .shortAndLong, help: "Authentication token.") |
| 19 | + private var token: String |
| 20 | + |
| 21 | + @Option(name: .shortAndLong, help: "HTTP address of InfluxDB.") |
| 22 | + private var url: String |
| 23 | + |
| 24 | + public func run() throws { |
| 25 | + guard let settingsURL = Bundle.module.url(forResource: "vix-daily", withExtension: "csv") else { |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + guard let stream = InputStream(url: settingsURL) else { |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + var subscriptions = Set<AnyCancellable>() |
| 34 | + |
| 35 | + let dateFormatter = DateFormatter() |
| 36 | + dateFormatter.dateFormat = "yyyy-MM-dd" |
| 37 | + |
| 38 | + // Initialize Client with default Bucket and Organization |
| 39 | + let client = InfluxDBClient( |
| 40 | + url: url, |
| 41 | + token: token, |
| 42 | + options: InfluxDBClient.InfluxDBOptions(bucket: self.bucket, org: self.org)) |
| 43 | + |
| 44 | + // Initialize WriteAPI |
| 45 | + let writeAPI = client.makeWriteAPI() |
| 46 | + |
| 47 | + // Prepare batches by Combine |
| 48 | + let batches = try CSVReader(stream: stream, hasHeaderRow: true) |
| 49 | + .publisher |
| 50 | + // create Point from CSV line |
| 51 | + .map { row in |
| 52 | + toPoint(row: row, dateFormatter: dateFormatter) |
| 53 | + } |
| 54 | + // specify size of batch |
| 55 | + .collect(500) |
| 56 | + // write batch |
| 57 | + .flatMap(maxPublishers: .max(1)) { batch -> AnyPublisher<Void, InfluxDBClient.InfluxDBError> in |
| 58 | + print("Writing \(batch.count) items ...") |
| 59 | + return writeAPI.write(points: batch) |
| 60 | + } |
| 61 | + |
| 62 | + batches |
| 63 | + .sink(receiveCompletion: { result in |
| 64 | + // process result of import |
| 65 | + switch result { |
| 66 | + case .finished: |
| 67 | + print("Import finished!") |
| 68 | + case let .failure(error): |
| 69 | + print("Unexpected error: \(error)") |
| 70 | + } |
| 71 | + self.atExit(client: client) |
| 72 | + }, receiveValue: { |
| 73 | + // batch is successfully written |
| 74 | + print(" > success") |
| 75 | + }) |
| 76 | + .store(in: &subscriptions) |
| 77 | + |
| 78 | + // Wait to end of script |
| 79 | + RunLoop.current.run() |
| 80 | + } |
| 81 | + |
| 82 | + /// Parse the CSV line into Point |
| 83 | + /// |
| 84 | + /// - Parameters: |
| 85 | + /// - row: CSV line |
| 86 | + /// - dateFormatter: for parsing date |
| 87 | + /// - Returns: parsed InfluxDBClient.Point |
| 88 | + private func toPoint(row: [String], dateFormatter: DateFormatter) -> InfluxDBClient.Point { |
| 89 | + InfluxDBClient |
| 90 | + .Point("financial-analysis") |
| 91 | + .addTag(key: "type", value: "vix-daily") |
| 92 | + .addField(key: "open", value: .double(Double(row[1])!)) |
| 93 | + .addField(key: "high", value: .double(Double(row[2])!)) |
| 94 | + .addField(key: "low", value: .double(Double(row[3])!)) |
| 95 | + .addField(key: "close", value: .double(Double(row[4])!)) |
| 96 | + .time(time: .date(dateFormatter.date(from: row[0])!)) |
| 97 | + } |
| 98 | + |
| 99 | + private func atExit(client: InfluxDBClient, error: InfluxDBClient.InfluxDBError? = nil) { |
| 100 | + // Dispose the Client |
| 101 | + client.close() |
| 102 | + // Exit script |
| 103 | + Self.exit(withError: error) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// extension CSVReader: Sequence { } |
| 108 | + |
| 109 | +WriteDataInBatches.main() |
0 commit comments