FastRiseCondition.swift 792 B

123456789101112131415161718192021222324252627
  1. // LoopFollow
  2. // FastRiseCondition.swift
  3. import Foundation
  4. /// Fires when N consecutive BG deltas are ≥ `delta` mg/dL.
  5. struct FastRiseCondition: AlarmCondition {
  6. static let type: AlarmType = .fastRise
  7. init() {}
  8. func evaluate(alarm: Alarm, data: AlarmData, now _: Date) -> Bool {
  9. guard
  10. let rise = alarm.delta, rise > 0,
  11. let streak = alarm.monitoringWindow, streak > 0,
  12. data.bgReadings.count >= streak + 1
  13. else { return false }
  14. // grab the last (streak + 1) readings, newest last
  15. let recent = data.bgReadings.suffix(streak + 1).map(\.sgv)
  16. // every forward delta must hit the threshold
  17. return zip(recent.dropFirst(), recent).allSatisfy {
  18. Double($0 - $1) >= rise
  19. }
  20. }
  21. }