UploadMetrics.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. import Foundation
  17. /// A set of SDK targets for which to collect code coverage.
  18. let TARGETS_TO_COLLECT: Set = [
  19. "Auth_Example_iOS.app",
  20. "Core_Example_iOS.app",
  21. "Database_Example_iOS.app",
  22. "DynamicLinks_Example_iOS.app",
  23. "InstanceID_Example_iOS.app",
  24. "Messaging_Example_iOS.app",
  25. "Storage_Example_iOS.app",
  26. // TODO(Corrob): Add support for Firestore, Functions, and InAppMessaging.
  27. ]
  28. /// Represents a set of metric table updates to upload to the database.
  29. public struct UploadMetrics: Encodable {
  30. public var tables: [TableUpdate]
  31. public init(tables: [TableUpdate]) {
  32. self.tables = tables
  33. }
  34. /// Converts the metric table updates to a JSON format this is compatible with the Java uploader.
  35. public func json() throws -> String {
  36. let json = try JSONEncoder().encode(self)
  37. return String(data: json, encoding: .utf8)!
  38. }
  39. }
  40. /// An update to a metrics table with the new data to uplaod to the database.
  41. public struct TableUpdate: Encodable {
  42. public var table_name: String
  43. public var column_names: [String]
  44. public var replace_measurements: [[String]]
  45. public init(table_name: String, column_names: [String], replace_measurements: [[String]]) {
  46. self.table_name = table_name
  47. self.column_names = column_names
  48. self.replace_measurements = replace_measurements
  49. }
  50. /// Creates a table update for code coverage by parsing a coverage report from XCov.
  51. public static func createFrom(coverage: CoverageReport, pullRequest: Int,
  52. currentTime: String) -> TableUpdate {
  53. var metrics = [[String]]()
  54. for target in coverage.targets {
  55. if TARGETS_TO_COLLECT.contains(target.name) {
  56. var row = [String]()
  57. row.append(target.name.components(separatedBy: "_")[0])
  58. row.append(String(pullRequest))
  59. row.append(String(target.coverage))
  60. row.append(currentTime)
  61. metrics.append(row)
  62. } else {
  63. print(
  64. "WARNING - target \(target.name) is being filtered out from coverage collection. Skipping..."
  65. )
  66. }
  67. }
  68. let columnNames = ["product_name", "pull_request_id", "coverage_total", "collection_time"]
  69. return TableUpdate(table_name: "IosCodeCoverage", column_names: columnNames,
  70. replace_measurements: metrics)
  71. }
  72. }