OrefFunction.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 autosens
  20. case iob
  21. case meal
  22. case makeProfile
  23. // since we're removing some keys from our Profile that exist in Javascript
  24. // we need to let the difference function know which keys to ignore when
  25. // calculating differences
  26. func keysToIgnore() -> Set<String> {
  27. switch self {
  28. case .makeProfile:
  29. return Set(["calc_glucose_noise", "enableEnliteBgproxy", "exercise_mode", "offline_hotspot"])
  30. case .iob:
  31. // we're only checking the first result for now
  32. return Set(stride(from: 1, to: 48, by: 1).map { String("[\($0)]") })
  33. case .meal:
  34. // These aren't used by downstream calculations, so we
  35. // can ignore them in our comparison
  36. return Set(["maxDeviation", "minDeviation", "allDeviations", "bwCarbs", "bwFound", "journalCarbs", "nsCarbs"])
  37. case .autosens:
  38. return Set(["deviationsUnsorted"])
  39. }
  40. }
  41. // Some values might be slightly different due to Double vs Decimal
  42. // and minor algorithmic differences
  43. func approximateMatchingNumbers() -> [String: Double] {
  44. switch self {
  45. case .makeProfile:
  46. return [:]
  47. case .iob:
  48. // for iob we can get rounding errors because of Double vs Decimal
  49. // so we leave a little extra room for our comparisons
  50. return [
  51. "iob": 0.1,
  52. "activity": 0.01,
  53. "basaliob": 0.25,
  54. "bolusiob": 0.25,
  55. "netbasalinsulin": 0.25,
  56. "bolusinsulin": 0.25,
  57. // Please see this issue for context on duration:
  58. // https://github.com/nightscout/Trio-dev/issues/453
  59. "duration": 120
  60. ]
  61. case .meal:
  62. return [
  63. "carbs": 0.1,
  64. "mealCOB": 10,
  65. "currentDeviation": 1,
  66. "slopeFromMaxDeviation": 0.25,
  67. "slopeFromMinDeviation": 0.25,
  68. "lastCarbTime": 1
  69. ]
  70. case .autosens:
  71. return [
  72. "ratio": 0.011,
  73. "newisf": 1.5
  74. ]
  75. }
  76. }
  77. func returnType() -> ReturnType {
  78. switch self {
  79. case .makeProfile:
  80. return .dictionary
  81. case .iob:
  82. return .array
  83. case .meal:
  84. return .dictionary
  85. case .autosens:
  86. return .dictionary
  87. }
  88. }
  89. }