Skip to content

Fix: Delete Glucose entries in Nightscout API#980

Open
ryceg wants to merge 9 commits intonightscout:devfrom
ryceg:fix/delete-glucose-ns
Open

Fix: Delete Glucose entries in Nightscout API#980
ryceg wants to merge 9 commits intonightscout:devfrom
ryceg:fix/delete-glucose-ns

Conversation

@ryceg
Copy link
Copy Markdown

@ryceg ryceg commented Feb 18, 2026

This PR amends the Nightscout API to correctly target the entries collection, and uses the _id for discrimination since Trio's glucose entries have an _id instead of an id

@kristinkand
Copy link
Copy Markdown

Hi, after @dnzxy nudge I cloned @rhysgg fix/delete-glucose-ns branch into mine and built it. But after waiting for two loops, the old bg still shows on NS after deleting it from Trio.

For testing purpose we'd entered a manual bg this morning since we then see a big red dot on NS.
Did I miss smth?

@dnzxy
Copy link
Copy Markdown
Contributor

dnzxy commented Feb 19, 2026

@kristinkand this PR should fix deletions of manual glucose readings of NS. Tbf, I'm not entirely sure if that was actually broken before.

What is still "missing" is deletion of normal readings (sgvs).

I believe adding the following snippet to Trio/Sources/Services/Network/Nightscout/NightscoutAPI.swift

func deleteGlucose(withId id: String) async throws {
        var components = URLComponents()
        components.scheme = url.scheme
        components.host = url.host
        components.port = url.port
        components.path = "/api/v1/entries/"
        components.queryItems = [
            URLQueryItem(name: "find[_id][$eq]", value: id)
        ]

        guard let url = components.url else {
            throw URLError(.badURL)
        }

        var request = URLRequest(url: url)
        request.allowsConstrainedNetworkAccess = false
        request.timeoutInterval = Config.timeout
        request.httpMethod = "DELETE"

        if let secret = secret {
            request.addValue(secret.sha1(), forHTTPHeaderField: "api-secret")
        }

        let (_, response) = try await URLSession.shared.data(for: request)

        guard let httpResponse = response as? HTTPURLResponse, (200 ... 299).contains(httpResponse.statusCode) else {
            throw URLError(.badServerResponse)
        }

        debugPrint("Delete glucose from NS successful for entry with ID \(id)")
    }

and this snippet to Trio/Sources/Services/Network/Nightscout/NightscoutManager.swift

func deleteGlucose(withID id: String) async {
        guard let nightscout = nightscoutAPI, isUploadEnabled else { return }

        do {
            try await nightscout.deleteGlucose(withId: id)
        } catch {
            debug(
                .nightscout,
                "\(DebuggingIdentifiers.failed) Failed to delete CGM Glucose Reading from Nightscout with error: \(error.localizedDescription)"
            )
        }
    }

plus adding func deleteGlucose(withID id: String) async to the protocol definition in the top of the file

as well as these changes to the History module:
Trio/Sources/Modules/History/HistoryProvider.swift

func deleteGlucoseFromNightscout(withID id: String) {
            Task.detached { [weak self] in
                guard let self = self else { return }
                await self.nightscoutManager.deleteGlucose(withID: id)
            }
        }

Trio/Sources/Modules/History/HistoryDataFlow.swift

// add to protocol pattern
    func deleteGlucoseFromNightscout(withID id: String)

In Trio/Sources/Modules/History/HistoryStateModel.swift find func deleteGlucoseFromServices(_ treatmentObjectID: NSManagedObjectID) async and add/find

// Delete from Nightscout
                    if let id = glucoseToDelete.id?.uuidString {
                        self.provider.deleteGlucoseFromNightscout(withID: id)
                        self.provider.deleteManualGlucoseFromNightscout(withID: id)
                    }

should probably solve this :)

If you feel confident giving this a try, please do.
Otherwise, @ryceg or I can take a look and add it as a commit.


Edit to fix DateTable; module is now called History (https://github.com/nightscout/Trio/tree/dev/Trio/Sources/Modules/History)

@kristinkand
Copy link
Copy Markdown

I rebuild successfully after editing the different files. However the red dot of the manual bg is still there.
Once again, we added a manual bg in Trio (same as CGM bg) which shows with a red dot in NS. Afterwards we deleted it from Trio, but it's still showing in NS after several loops.

@kristinkand
Copy link
Copy Markdown

One more comment: After the helpful hand from @dnzxy , I reverted the first two commits @ryceg , but left in place all the "snippets" as posted by @dnzxy . Now when I delete a manual bg in Trio, it's being deleted from NS too (refresh NS or wait for a loop).
So, only the deletion of "normal" bgs from NS is now missing. How do we fix that?

@kristinkand
Copy link
Copy Markdown

I attempted to build after your last commits @ryceg However every build comes back with two errors: missing url path to submodule..xdrip and something about homebrew.
To make sure I'd cloned the latest version, I nuked my earlier copies of this branch and pulled down a new copy.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@kristinkand
Copy link
Copy Markdown

I built again, now it builds successfully after the removal of the submodule. However the normal bg values are still not deleted from NS. If I build without @dnzxy's suggestions, nothing is being deleted neither manual nor normal bg values.

@dnzxy dnzxy marked this pull request as draft March 17, 2026 21:07
@dnzxy
Copy link
Copy Markdown
Contributor

dnzxy commented Mar 30, 2026

Has anyone been able to test this since the last changes?

@dnzxy dnzxy marked this pull request as ready for review April 14, 2026 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants