|
| 1 | +/* |
| 2 | + * Copyright 2019 Google |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import Foundation |
| 18 | + |
| 19 | +/// A mapping of SDKs to an ID that will represent that SDK in the database. |
| 20 | +let TARGET_TO_SDK_INDEX = [ |
| 21 | + "Auth_Example_iOS.app": 0.0, |
| 22 | + "Core_Example_iOS.app": 1.0, |
| 23 | + "Database_Example_iOS.app": 2.0, |
| 24 | + "DynamicLinks_Example_iOS.app": 3.0, |
| 25 | + "InstanceID_Example_iOS.app": 4.0, |
| 26 | + "Messaging_Example_iOS.app": 5.0, |
| 27 | + "Storage_Example_iOS.app": 6.0, |
| 28 | + // TODO(Corrob): Add support for Firestore, Functions, and InAppMessaging. |
| 29 | +] |
| 30 | + |
| 31 | +/// Represents a set of metric table updates to upload to the database. |
| 32 | +public struct UploadMetrics: Encodable { |
| 33 | + public var tables: [TableUpdate] |
| 34 | + |
| 35 | + public init(tables: [TableUpdate]) { |
| 36 | + self.tables = tables |
| 37 | + } |
| 38 | + |
| 39 | + /// Converts the metric table updates to a JSON format this is compatible with the Java uploader. |
| 40 | + public func json() throws -> String { |
| 41 | + let json = try JSONEncoder().encode(self) |
| 42 | + return String(data: json, encoding: .utf8)! |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// An update to a metrics table with the new data to uplaod to the database. |
| 47 | +public struct TableUpdate: Encodable { |
| 48 | + public var table_name: String |
| 49 | + public var column_names: [String] |
| 50 | + public var replace_measurements: [[Double]] |
| 51 | + |
| 52 | + public init(table_name: String, column_names: [String], replace_measurements: [[Double]]) { |
| 53 | + self.table_name = table_name |
| 54 | + self.column_names = column_names |
| 55 | + self.replace_measurements = replace_measurements |
| 56 | + } |
| 57 | + |
| 58 | + /// Creates a table update for code coverage by parsing a coverage report from XCov. |
| 59 | + public static func createFrom(coverage: CoverageReport, pullRequest: Int) -> TableUpdate { |
| 60 | + var metrics = [[Double]]() |
| 61 | + for target in coverage.targets { |
| 62 | + let sdkKey = TARGET_TO_SDK_INDEX[target.name] |
| 63 | + if sdkKey == nil { |
| 64 | + print("WARNING - target \(target.name) has no mapping to an SDK id. Skipping...") |
| 65 | + } else { |
| 66 | + var row = [Double]() |
| 67 | + row.append(Double(pullRequest)) |
| 68 | + row.append(sdkKey!) |
| 69 | + row.append(target.coverage) |
| 70 | + metrics.append(row) |
| 71 | + } |
| 72 | + } |
| 73 | + let columnNames = ["pull_request_id", "sdk_id", "coverage_percent"] |
| 74 | + return TableUpdate(table_name: "Coverage1", column_names: columnNames, replace_measurements: metrics) |
| 75 | + } |
| 76 | +} |
0 commit comments