mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
feat(integrations): let the ledger hold every failing leg at once
`failing_leg` was one slot, so two legs of the same provider failing in overlapping windows overwrote each other. The row remembered whichever failed last, and that leg recovering then cleared an error the other one was still causing — the ledger forgot a live failure and painted the card green over it. The slot becomes a set. A failure adds its leg, a success removes its own, and the error state, the per-kind buckets and the streak anchor clear only once the set is empty. The array is ordered oldest failure first, so the last element is the leg that wrote the message `last_error` holds; when that leg recovers while others are still failing, the message is cleared rather than left describing a leg that is now healthy. An unattributed failure adds nothing and removes nothing. A single-leg provider therefore behaves exactly as before, and a multi-leg provider no longer loses what its other legs already reported. The column never reached a client — it is absent from the status serialiser and from the OpenAPI contract — so it is replaced rather than shimmed.
This commit is contained in:
1 parent
3088753a92
commit
06935256b0
18 files changed
+433
-58
No files matched your search
@@ -130,12 +130,27 @@ export const GET = apiHandler(async () => {
|
||||
|
||||
const row = await prisma.user.findUnique({
|
||||
where: { id: user.id },
|
||||
select: { healthKitConfigJson: true, healthKitLastSyncedAt: true },
|
||||
select: {
|
||||
healthKitConfigJson: true,
|
||||
healthKitLastSyncedAt: true,
|
||||
healthKitLastSyncTrigger: true,
|
||||
healthKitLastBackgroundSyncAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
const stored = parseStored(row?.healthKitConfigJson ?? null);
|
||||
const entries = mergeWithDefaults(stored);
|
||||
const lastSyncedAt = row?.healthKitLastSyncedAt?.toISOString() ?? null;
|
||||
// What triggered the most recent accepted batch, and when data last arrived
|
||||
// without the app being opened. `lastSyncedAt` alone cannot separate those
|
||||
// two cases, which is the whole diagnostic: a card that only ever says "last
|
||||
// sync 10 minutes ago" reads the same whether the phone is delivering by
|
||||
// itself or whether every arrival was the user opening the app. Null in
|
||||
// either field is honest absence — an older client that declares no trigger,
|
||||
// or a pipe that has never delivered in the background.
|
||||
const lastSyncTrigger = row?.healthKitLastSyncTrigger ?? null;
|
||||
const lastBackgroundSyncAt =
|
||||
row?.healthKitLastBackgroundSyncAt?.toISOString() ?? null;
|
||||
|
||||
// Apple Health is push-based: the iOS app delivers and nothing here polls, so
|
||||
// liveness is a data question, not an attempt question. Without a threshold a
|
||||
@@ -157,6 +172,8 @@ export const GET = apiHandler(async () => {
|
||||
return apiSuccess({
|
||||
entries,
|
||||
lastSyncedAt,
|
||||
lastSyncTrigger,
|
||||
lastBackgroundSyncAt,
|
||||
syncHealth,
|
||||
metricFreshness: classifyMetricFreshness(samples, syncHealth.verdict),
|
||||
});
|
||||
|
||||
@@ -169,12 +169,14 @@ const batchEntrySchema = z.object({
|
||||
});
|
||||
|
||||
// v1.32.8 (iOS #66) — optional per-REQUEST diagnostic tag naming what woke the
|
||||
// client for this sync. Purely observational: it flows into the
|
||||
// `measurement.batch.ingest` wide event and NOTHING else — never persisted,
|
||||
// never part of the dedup key, never an input to attribution or how a sample is
|
||||
// stored. Only the client knows its own wake reason, so it is client-asserted
|
||||
// by necessity; being diagnostic-only, that carries no trust surface. Optional
|
||||
// so every pre-#66 caller stays byte-for-byte unchanged.
|
||||
// client for this sync. Purely diagnostic: it flows into the
|
||||
// `measurement.batch.ingest` wide event and onto the account's HealthKit
|
||||
// diagnostic columns, which the Apple Health card reads. It is never part of
|
||||
// the dedup key and never an input to attribution or how a sample is stored —
|
||||
// sending it or omitting it produces byte-identical row outcomes. Only the
|
||||
// client knows its own wake reason, so it is client-asserted by necessity;
|
||||
// being diagnostic-only, that carries no trust surface. Optional so every
|
||||
// pre-#66 caller stays byte-for-byte unchanged.
|
||||
const syncTriggerEnum = z.enum(["foreground", "background", "push"]);
|
||||
|
||||
const batchPayloadSchema = z.object({
|
||||
@@ -644,10 +646,28 @@ async function postBatch(request: NextRequest): Promise<Response> {
|
||||
// This batch route is the native HealthKit ingestion boundary. Stamp only
|
||||
// after every attempted write has reached a durable, non-failed verdict;
|
||||
// MANUAL-only and skipped-only batches are not HealthKit syncs.
|
||||
//
|
||||
// The trigger the client declared is stamped alongside, and a background- or
|
||||
// push-triggered batch additionally dates `healthKitLastBackgroundSyncAt`.
|
||||
// Without that second timestamp there is no way to tell a phone that delivers
|
||||
// on its own from one that only ever delivers while the app is open — both
|
||||
// look like a moving `healthKitLastSyncedAt`. A batch that declares no
|
||||
// trigger writes null over the previous value rather than leaving a stale one
|
||||
// standing: the field describes THIS sync, and an older build's silence is
|
||||
// not evidence about it.
|
||||
if (healthKitSyncSucceeded) {
|
||||
const syncedAt = new Date();
|
||||
const deliveredWithoutTheApp =
|
||||
syncTrigger === "background" || syncTrigger === "push";
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: { healthKitLastSyncedAt: new Date() },
|
||||
data: {
|
||||
healthKitLastSyncedAt: syncedAt,
|
||||
healthKitLastSyncTrigger: syncTrigger ?? null,
|
||||
...(deliveredWithoutTheApp
|
||||
? { healthKitLastBackgroundSyncAt: syncedAt }
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user