|
@@ -67,7 +67,13 @@ extension Stat.StateModel {
|
|
|
let calendar = Calendar.current
|
|
let calendar = Calendar.current
|
|
|
|
|
|
|
|
// Group entries by hour for hourly statistics
|
|
// Group entries by hour for hourly statistics
|
|
|
- let hourlyGrouped = Dictionary(grouping: fetchedResults) { entry in
|
|
|
|
|
|
|
+ let now = Date()
|
|
|
|
|
+ let twentyDaysAgo = Calendar.current.date(byAdding: .day, value: -20, to: now) ?? now
|
|
|
|
|
+
|
|
|
|
|
+ let hourlyGrouped = Dictionary(grouping: fetchedResults.filter { entry in
|
|
|
|
|
+ guard let date = entry.pumpEvent?.timestamp else { return false }
|
|
|
|
|
+ return date >= twentyDaysAgo && date <= now
|
|
|
|
|
+ }) { entry in
|
|
|
let components = calendar.dateComponents(
|
|
let components = calendar.dateComponents(
|
|
|
[.year, .month, .day, .hour],
|
|
[.year, .month, .day, .hour],
|
|
|
from: entry.pumpEvent?.timestamp ?? Date()
|
|
from: entry.pumpEvent?.timestamp ?? Date()
|