Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improved Duplicate Handling in Nightscout BG Data Processing #365

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions LoopFollow/Controllers/Nightscout/BGData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,20 @@ extension MainViewController {
nsData[i].date /= 1000
nsData[i].date.round(FloatingPointRoundingRule.toNearestOrEven)
}
print(nsData.count)

//Avoid duplicate entries messing up the graph, only use one reading per 5 minutes.
let graphHours = 24 * UserDefaultsRepository.downloadDays.value
let points = graphHours * 12 + 1
var nsData2 = [ShareGlucoseData]()
let timestamp = Date().timeIntervalSince1970
for i in 0..<points {
//Starting with "now" and then step 5 minutes back in time
let target = timestamp - Double(i) * 60 * 5
//Find the reading closest to the target, but not too far away
let closest = nsData.filter{ abs($0.date - target) < 3 * 60 }.min { abs($0.date - target) < abs($1.date - target) }
//If a reading is found, add it to the new array
if let item = closest {
nsData2.append(item)

var nsData2: [ShareGlucoseData] = []
var lastAddedTime = Double.infinity
var lastAddedSGV: Int? = nil
let minInterval: Double = 30

for reading in nsData {
if (lastAddedSGV == nil || lastAddedSGV != reading.sgv) || (lastAddedTime - reading.date >= minInterval) {
nsData2.append(reading)
lastAddedTime = reading.date
lastAddedSGV = reading.sgv
}
}
print(nsData2.count)


// merge NS and Dex data if needed; use recent Dex data and older NS data
var sourceName = "Nightscout"
if !dexData.isEmpty {
Expand Down