feat: retain and export latest heart-rate measurements

This commit is contained in:
Blendman974 committed 2026-11-08 01:13:55 +01:00
1 parent 8d57b7c1d0
commit a575db8e27
8 files changed
+218 -18

No files matched your search

+10 -4
View File
@@ -59,6 +59,7 @@
import {
type HealthSample,
isHeartRateMeasurement,
type Rollup,
type SampleMetric,
type SleepSession,
@@ -265,12 +266,14 @@ export class HealthStore {
return { cursor: next, samples, sleep, scanned };
}
/** Every stored sample overlapping [startMs, endMs). Exact - reads shards. */
samplesInRange(startMs: number, endMs: number): HealthSample[] {
/** Bucket samples in [startMs, endMs). Point observations are opt-in so
* existing charts and daily averages never combine two overlapping series. */
samplesInRange(startMs: number, endMs: number, includeMeasurements = false): HealthSample[] {
const out: HealthSample[] = [];
for (const name of this.shardNamesInRange(startMs, endMs)) {
for (const sample of this.loadShard(name).values()) {
if (sample.startMs >= startMs && sample.startMs < endMs) out.push(sample);
if (sample.startMs >= startMs && sample.startMs < endMs
&& (includeMeasurements || !isHeartRateMeasurement(sample))) out.push(sample);
}
}
out.sort((a, b) => a.startMs - b.startMs);
@@ -334,6 +337,7 @@ export class HealthStore {
for (const name of this.backend.list()) {
if (!name.startsWith(SAMPLE_PREFIX) || !name.endsWith(SAMPLE_SUFFIX)) continue;
for (const sample of this.loadShard(name).values()) {
if (isHeartRateMeasurement(sample)) continue;
const key = dayKey(sample.startMs);
let byMetric = perDay.get(key);
if (!byMetric) {
@@ -360,7 +364,9 @@ export class HealthStore {
private updateRollupsFor(samples: readonly HealthSample[]): void {
const file = this.loadRollups();
const touched = new Set<string>();
for (const sample of samples) touched.add(dayKey(sample.startMs));
for (const sample of samples) {
if (!isHeartRateMeasurement(sample)) touched.add(dayKey(sample.startMs));
}
for (const key of touched) {
const parts = key.split("-").map(Number);
const dayStart = new Date(parts[0]!, parts[1]! - 1, parts[2]!).getTime();