mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
feat(nutrients): track energy, macronutrients, sodium, potassium and alcohol
The catalog gains energy (kcal), protein, carbohydrates, fat, sugar and fiber (g), sodium and potassium (mg) and alcohol (g of ethanol, no HealthKit type). Each code now names its group and whether a supplement's composition may list it; references name their authority (EFSA, or IOM for protein and carbohydrates, NIH ODS for chromium). Day totals fold through one rule everywhere: the largest of the day-total sources (Apple Health, Health Connect, an external bridge) counts, manual water and supplement doses add on top. This also stops Apple Health and Health Connect totals for the same day from adding up. The nutrients page gets an energy and macronutrient card with an inline daily target, and an alcohol card next to caffeine. The correlation scan gives the macros their own budget of two channels and leaves logged alcohol out. Daily series and the MCP tool return the target the person set. Refs #1402.
This commit is contained in:
1 parent
a68ec9421f
commit
8f96c2f0a2
47 files changed
+1947
-430
No files matched your search
@@ -45,9 +45,11 @@ const moduleDisabled = {
|
||||
/**
|
||||
* The catalog table, rendered into the endpoint description so the
|
||||
* OpenAPI file is the one authoritative home of the code ↔ HK ↔ unit
|
||||
* contract (µg is `ug` on the wire; water is mL; never IU).
|
||||
* contract (µg is `ug` on the wire; water is mL; energy is kcal; never IU,
|
||||
* never kJ). Only the codes HealthKit carries; the batch also accepts
|
||||
* `alcohol`, which no HealthKit type does.
|
||||
*/
|
||||
const catalogTable = NUTRIENT_DEFINITIONS.filter((d) => d.syncable)
|
||||
const catalogTable = NUTRIENT_DEFINITIONS.filter((d) => d.hkIdentifier !== null)
|
||||
.map((d) => `| ${d.code} | ${d.hkIdentifier} | ${d.unit} |`)
|
||||
.join("\n");
|
||||
|
||||
@@ -60,7 +62,12 @@ const nutrientCatalogEntry = z
|
||||
syncable: z
|
||||
.boolean()
|
||||
.describe(
|
||||
"Whether HealthKit may send this code in the nutrient batch. `dha` and `epa` are not syncable: they appear in responses only.",
|
||||
"Whether the nutrient batch accepts this code. `dha` and `epa` are not syncable: they appear in responses only.",
|
||||
),
|
||||
ingredient: z
|
||||
.boolean()
|
||||
.describe(
|
||||
"Whether a supplement's composition may name this code. False for water, energy, the macronutrients and alcohol.",
|
||||
),
|
||||
inputUnits: z
|
||||
.array(z.object({ unit: z.string(), factorToCanonical: z.number() }))
|
||||
@@ -103,9 +110,9 @@ export const nutrientPaths: NonNullable<ZodOpenApiObject["paths"]> = {
|
||||
post: {
|
||||
parameters: [idempotencyKeyParameter],
|
||||
tags: ["Nutrients"],
|
||||
summary: "Ingest micronutrient day totals (v1.28)",
|
||||
summary: "Ingest nutrient day totals (v1.28)",
|
||||
description:
|
||||
"Bulk upsert of supplement-style daily totals synced from HealthKit — vitamins, minerals, water, caffeine. Day totals only: compute HKStatisticsCollectionQuery cumulative sums day-anchored in the user's current IANA timezone; `day` is yyyy-MM-dd in that timezone (the same rule as the `stats:` day keys). Never post raw samples. Upsert key is (day, nutrient): a re-post replaces the stored total and reports `updated`. Post the current + previous local day per sync; backfill 30 days on first enable. Max 500 entries; Idempotency-Key supported; rate limit 60/min. Per-entry skips (`unit_mismatch` | `value_out_of_range` | `day_invalid`) are terminal — log and drop, do not retry the entry. `upsert_failed` is not one of them: it is a server-side write failure for the entry's whole write group, nothing was stored, and the entry should be sent again on a later sync; a response carrying it is `Cache-Control: no-store` and is not replayed for its `Idempotency-Key`. Behind the opt-in `nutrients` module (403 `module.disabled` when off). Query HealthKit with exactly the listed unit per code (`ug` = microgram; do not convert to IU):\n\n| code | HealthKit identifier | unit |\n| --- | --- | --- |\n" +
|
||||
"Bulk upsert of daily nutrient totals: vitamins, minerals, water, caffeine, energy (kcal), the macronutrients (g) and alcohol (g of ethanol). Two kinds of caller. The native client syncs from HealthKit with a cookie session or its login token, and its rows are stored as source APPLE_HEALTH. A bridge (a food tracker, a script) posts with a `measurements:write` token, and its rows are stored as source EXTERNAL; the body never chooses the source. A bridge may post any past day, and posts 0 to correct a day it got wrong. On the reads, the largest of the day-total sources (EXTERNAL, APPLE_HEALTH, HEALTH_CONNECT) counts for a day, so a tracker that also writes into Apple Health is not counted twice. Units are fixed per code (table below); energy is kcal, and a kJ value is skipped `unit_mismatch`, never converted.\n\nFor the native client: day totals only: compute HKStatisticsCollectionQuery cumulative sums day-anchored in the user's current IANA timezone; `day` is yyyy-MM-dd in that timezone (the same rule as the `stats:` day keys). Never post raw samples. Upsert key is (day, nutrient): a re-post replaces the stored total and reports `updated`. Post the current + previous local day per sync; backfill 30 days on first enable. Max 500 entries; Idempotency-Key supported; rate limit 60/min. Per-entry skips (`unit_mismatch` | `value_out_of_range` | `day_invalid`) are terminal — log and drop, do not retry the entry. `upsert_failed` is not one of them: it is a server-side write failure for the entry's whole write group, nothing was stored, and the entry should be sent again on a later sync; a response carrying it is `Cache-Control: no-store` and is not replayed for its `Idempotency-Key`. Behind the opt-in `nutrients` module (403 `module.disabled` when off). Query HealthKit with exactly the listed unit per code (`ug` = microgram; do not convert to IU). `alcohol` has no HealthKit type and is posted by bridges only, in g:\n\n| code | HealthKit identifier | unit |\n| --- | --- | --- |\n" +
|
||||
catalogTable,
|
||||
requestBody: {
|
||||
required: true,
|
||||
@@ -159,7 +166,7 @@ export const nutrientPaths: NonNullable<ZodOpenApiObject["paths"]> = {
|
||||
tags: ["Nutrients"],
|
||||
summary: "One nutrient's day-bucketed series (v1.29)",
|
||||
description:
|
||||
"Dense per-day series for one catalog code over the last `days` days (default 30) — one entry per calendar day, 0 for a day with no logged data, summed ACROSS SOURCES (a day may carry both an APPLE_HEALTH row and a MANUAL row since migration 0249). Feeds the `/insights/nutrients` hydration + caffeine charts. Also returns the EFSA reference resolved against the caller's profile sex — `null` when the profile has no sex on file (never guessed). Behind the opt-in `nutrients` module.",
|
||||
"Dense per-day series for one catalog code over the last `days` days (default 30) — one entry per calendar day, 0 for a day with no logged data, folded across sources (the largest of the day-total sources APPLE_HEALTH, HEALTH_CONNECT and EXTERNAL, plus MANUAL and SUPPLEMENT on top). Feeds the `/insights/nutrients` charts. Also returns the dietary reference resolved against the caller's profile sex — `null` when the profile has no sex on file (never guessed) or the nutrient has none — and the user's own target for the nutrient, if set. Behind the opt-in `nutrients` module.",
|
||||
requestParams: { query: nutrientDailyQuerySchema },
|
||||
responses: {
|
||||
"200": {
|
||||
|
||||
Reference in new issue
Block a user