Heartbeat.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. /// An enumeration of time periods.
  16. enum TimePeriod: Int, CaseIterable, Codable {
  17. /// The raw value is the number of calendar days within each time period.
  18. /// More types can be enabled in future iterations (i.e. `weekly = 7, monthly = 28`).
  19. case daily = 1
  20. /// The number of seconds in a given time period.
  21. var timeInterval: TimeInterval {
  22. Double(rawValue) * 86400 /* seconds in day */
  23. }
  24. }
  25. /// A structure representing SDK usage.
  26. struct Heartbeat: Codable, Equatable {
  27. /// The version of the heartbeat.
  28. private static let version: Int = 0
  29. /// An anonymous string of information (i.e. user agent) to associate the heartbeat with.
  30. let agent: String
  31. /// The date when the heartbeat was recorded.
  32. let date: Date
  33. /// The heartbeat's model version.
  34. let version: Int
  35. /// An array of `TimePeriod`s that the heartbeat is tagged with. See `TimePeriod`.
  36. ///
  37. /// Heartbeats represent anonymous data points that measure SDK usage in moving averages for
  38. /// various time periods. Because a single heartbeat can help calculate moving averages for multiple
  39. /// time periods, this property serves to capture all the time periods that the heartbeat can represent in
  40. /// a moving average.
  41. let timePeriods: [TimePeriod]
  42. /// Designated intializer.
  43. /// - Parameters:
  44. /// - agent: An anonymous string of information to associate the heartbeat with.
  45. /// - date: The date when the heartbeat was recorded.
  46. /// - version: The heartbeat's version. Defaults to the current version.
  47. init(agent: String,
  48. date: Date,
  49. timePeriods: [TimePeriod] = [],
  50. version: Int = version) {
  51. self.agent = agent
  52. self.date = date
  53. self.timePeriods = timePeriods
  54. self.version = version
  55. }
  56. }
  57. extension Heartbeat: HeartbeatsPayloadConvertible {
  58. func makeHeartbeatsPayload() -> HeartbeatsPayload {
  59. let userAgentPayloads = [
  60. HeartbeatsPayload.UserAgentPayload(agent: agent, dates: [date]),
  61. ]
  62. return HeartbeatsPayload(userAgentPayloads: userAgentPayloads)
  63. }
  64. }