OrefFunction.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import Foundation
  2. /// After the port from Javascript to Swift is complete, we should remove the logging module:
  3. /// https://github.com/nightscout/Trio-dev/issues/293
  4. enum OrefFunctionResult {
  5. case success(RawJSON)
  6. case failure(Error)
  7. func returnOrThrow() throws -> RawJSON {
  8. switch self {
  9. case let .success(json): return json
  10. case let .failure(error): throw error
  11. }
  12. }
  13. }
  14. enum OrefFunction: String, Codable {
  15. enum ReturnType {
  16. case array
  17. case dictionary
  18. }
  19. case makeProfile
  20. case iob
  21. // since we're removing some keys from our Profile that exist in Javascript
  22. // we need to let the difference function know which keys to ignore when
  23. // calculating differences
  24. func keysToIgnore() -> Set<String> {
  25. switch self {
  26. case .makeProfile:
  27. return Set(["calc_glucose_noise", "enableEnliteBgproxy", "exercise_mode", "offline_hotspot"])
  28. case .iob:
  29. // we're only checking the first result for now
  30. return Set(stride(from: 1, to: 48, by: 1).map { String("[\($0)]") })
  31. }
  32. }
  33. // Some values might be slightly different due to Double vs Decimal
  34. // and minor algorithmic differences
  35. func approximateMatchingNumbers() -> [String: Double] {
  36. switch self {
  37. case .makeProfile:
  38. return [:]
  39. case .iob:
  40. // for iob we can get rounding errors because of Double vs Decimal
  41. // so we leave a little extra room for our comparisons
  42. return [
  43. "iob": 0.1,
  44. "activity": 0.01,
  45. "basaliob": 0.25,
  46. "bolusiob": 0.25,
  47. "netbasalinsulin": 0.25,
  48. "bolusinsulin": 0.25,
  49. // Please see this issue for context on duration:
  50. // https://github.com/nightscout/Trio-dev/issues/453
  51. "duration": 120
  52. ]
  53. }
  54. }
  55. func returnType() -> ReturnType {
  56. switch self {
  57. case .makeProfile:
  58. return .dictionary
  59. case .iob:
  60. return .array
  61. }
  62. }
  63. }