Skip to content

Write workout routes and heartbeat series as file-backed recordings - #160

Open
PSchmiedmayer wants to merge 7 commits into
fhir-measurement-catalogfrom
healthkit-file-recordings
Open

PSchmiedmayer wants to merge 7 commits into
fhir-measurement-catalogfrom
healthkit-file-recordings

Conversation

@PSchmiedmayer

@PSchmiedmayer PSchmiedmayer commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

♻️ Current situation & Problem

This PR builds on #161.

The exporter converts a workout route or heartbeat series from an array of all its samples (Record.workoutRoute(_:locations:), Record.heartbeatSeries(_:beats:)) and embeds the encoded CSV in the DocumentReference. A long route or beat series therefore holds every sample and the complete CSV in memory, and the CSV travels inside the record.

Trove reads routes and beats with HKWorkoutRouteQueryDescriptor and HKHeartbeatSeriesQueryDescriptor, writes each series into a recording file under a byte limit at recordings/<sample UUID>.csv, and uploads the bytes separately from the event that carries the record. That needs a recording written from the query results as they arrive and referenced by a relative URL. The change reuses the registered CSV formats, row encoding and DocumentReference of the inline path; only the attachment changes from embedded data to a URL with the same size and SHA-1 hash.

⚙️ Release Notes

  • HealthKitRecordingFile holds a complete route or beat series written into a RecordingFile, with its relativePath and registered format. It has no public initializer; two @concurrent factories build one from a Sendable AsyncSequence, so the synchronous file writes never run on the caller's actor:

    let recording = try await HealthKitRecordingFile.workoutRoute(
        route,
        locations: HKWorkoutRouteQueryDescriptor(route).results(for: healthStore),
        relativePath: "recordings/\(route.uuid.uuidString.lowercased()).csv",
        maximumByteCount: recordingByteLimit
    )
    try await exporter.export(records: [.fileRecording(recording)], at: conversionInstant) { export in
        // Keep the record and recording.file together in the application's persistent queue.
    }
  • Rows go through RecordingCSVFileWriter with the row mapping the inline path uses, so a file holds exactly the bytes of the inline CSV for the same samples.

  • The sequence must deliver exactly HKSeriesSample.count elements, otherwise RecordingError.elementCountMismatch is thrown; a sample that states no elements throws emptyRecordingSeries. This keeps a truncated query result from becoming a complete-looking record.

  • No partial recording is returned: query errors, cancellation, invalid rows and an exceeded maximumByteCount (1 to Int32.max, inclusive) all throw. A relativePath that fails the shared relative path check throws RecordingError.invalidRelativePath before anything is written.

  • Record.fileRecording(_:) exports the same DocumentReference as the inline recording, with a relative URL in place of embedded data. A file-backed route still requires options.route = .authorized. The array-based cases are unchanged.

  • The file is a temporary snapshot. The caller copies it into persistent storage before advancing its query anchor and uploads it with recording.file.readChunks(_:) or recording.file.chunks.

  • Breaking: HealthKitFHIRExporter.Record gains the case fileRecording(_:), so an exhaustive switch over Record needs a new branch.

📚 Documentation

GroveHealthKitFHIR.md has a new "File-backed recordings" section and lists HealthKitRecordingFile in the exporter topic group. The new type, its factories, RecordingError and Record.fileRecording(_:) have symbol documentation.

✅ Testing

  • HealthKitRecordingFileTests (new): route and heartbeat files match the inline bytes, hash, size and DocumentReference envelope, and a route is omitted under the default route option; query failure and cancellation return no recording; the exact byte limit is accepted and one byte less fails; a non-finite beat time throws; 0, 1 and 3 locations for a two-location route throw elementCountMismatch; absolute, parent, empty-segment, URL, percent-encoded and query paths throw invalidRelativePath.
  • HealthKitDisclosureOptionsTests.everyOutputKindFollowsThePolicies adds a file-backed heartbeat series as an output kind.
  • StoredSampleFixtures can restate a series sample's count and numberOfDataPoints, and AssemblyFixtures converts a .fileRecording record.

Code of Conduct & Contributing Guidelines

By creating and submitting this pull request, you agree to follow our Code of Conduct and Contributing Guidelines:

@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 675c9612-c8cd-49b5-b7f5-069abe43fe0b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@PSchmiedmayer
PSchmiedmayer added this pull request to stack #166 November 18, 2026 16:52
A workout route or heartbeat series was exported from an array of its
samples, encoded in memory. HealthKitRecordingFile writes either one from
an asynchronous sequence directly into a recording file, requiring the
series' complete sample count, and Record.fileRecording exports it as the
same DocumentReference as the inline recording, referencing the file at a
relative path. The disclosure options apply to it as to every other
record.
`HealthKitRecordingFile.RecordingError.incompleteSeries` is thrown for too few
and for too many elements, so it becomes `elementCountMismatch`.
RecordingError.invalidRelativePath names every case the shared relative
attachment path check refuses.
@PSchmiedmayer
PSchmiedmayer marked this pull request as ready for review December 6, 2026 06:24
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.

1 participant