소스 검색

Remove fetchLimit, introduce fetchHours based on MMAT + 0.5 hr for oref/meal

Deniz Cengiz 3 일 전
부모
커밋
9101aeb610
1개의 변경된 파일13개의 추가작업 그리고 5개의 파일을 삭제
  1. 13 5
      Trio/Sources/APS/OpenAPS/OpenAPS.swift

+ 13 - 5
Trio/Sources/APS/OpenAPS/OpenAPS.swift

@@ -99,13 +99,20 @@ final class OpenAPS {
     func fetchAndProcessGlucose(
         context: NSManagedObjectContext,
         shouldSmoothGlucose: Bool,
-        fetchLimit: Int?
+        fetchLimit: Int?,
+        fetchHours: Decimal = 24
     ) async throws -> String {
-        // make it async and await it
+        // Time window from `fetchHours` hours ago up to now. determineBasal feeds
+        // `maxMealAbsorptionTime + 0.5h` (just enough glucose to cover the longest
+        // tracked meal absorption plus a small lead-in); Autosens uses the default
+        // 24h because its sensitivity algorithm needs that full window.
+        let cutoff = Date().addingTimeInterval(-(Double(truncating: fetchHours as NSNumber) * 3600))
+        let timePredicate = NSPredicate(format: "date >= %@", cutoff as NSDate)
+
         let results = try await CoreDataStack.shared.fetchEntitiesAsync(
             ofType: GlucoseStored.self,
             onContext: context,
-            predicate: NSPredicate.predicateForOneDayAgoInMinutes,
+            predicate: timePredicate,
             key: "date",
             ascending: false,
             fetchLimit: fetchLimit,
@@ -395,11 +402,12 @@ final class OpenAPS {
         async let carbs = fetchAndProcessCarbs(additionalCarbs: simulatedCarbsAmount ?? 0, carbsDate: simulatedCarbsDate)
 
         var preferences = await storage.retrieveAsync(OpenAPS.Settings.preferences, as: Preferences.self) ?? Preferences()
-        let glucoseFetchLimit = Int((preferences.maxMealAbsorptionTime * 12).rounded(scale: 0, roundingMode: .up))
+        let glucoseFetchHours = preferences.maxMealAbsorptionTime * 12 + 0.5 // MMAT + half hour buffer
         async let glucose = fetchAndProcessGlucose(
             context: context,
             shouldSmoothGlucose: shouldSmoothGlucose,
-            fetchLimit: glucoseFetchLimit
+            fetchLimit: nil,
+            fetchHours: glucoseFetchHours
         )
 
         async let prepareTrioCustomOrefVariables = prepareTrioCustomOrefVariables()