StatusCounts.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  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 GRPC
  17. /// Count the number seen of each status code.
  18. public struct StatusCounts {
  19. public private(set) var counts: [Int: Int64] = [:]
  20. public init() {}
  21. /// Add one to the count of this sort of status code.
  22. /// - parameters:
  23. /// - status: The code to count.
  24. public mutating func add(status: GRPCStatus.Code) {
  25. // Only record failures
  26. if status != .ok {
  27. if let previousCount = self.counts[status.rawValue] {
  28. self.counts[status.rawValue] = previousCount + 1
  29. } else {
  30. self.counts[status.rawValue] = 1
  31. }
  32. }
  33. }
  34. /// Merge another set of counts into this one.
  35. /// - parameters:
  36. /// - source: The other set of counts to merge into this.
  37. public mutating func merge(source: StatusCounts) {
  38. for sourceCount in source.counts {
  39. if let existingCount = self.counts[sourceCount.key] {
  40. self.counts[sourceCount.key] = existingCount + sourceCount.value
  41. } else {
  42. self.counts[sourceCount.key] = sourceCount.value
  43. }
  44. }
  45. }
  46. }