feat(insights): persist correlation patterns

This commit is contained in:
Marc-André Bombeck committed 2026-07-28 14:01:11 +02:00
1 parent 8ece6f39cc
commit 666a85d311
53 files changed
+2129 -55

No files matched your search

@@ -0,0 +1,42 @@
-- Custom metrics remain isolated unless the owner explicitly opts into
-- correlation discovery. Existing definitions retain the isolated default.
ALTER TABLE "custom_metrics"
ADD COLUMN "correlation_enabled" BOOLEAN NOT NULL DEFAULT false;
-- Stable, user-scoped identity plus the latest statistically accepted evidence.
-- Dismissal baselines are retained independently of subsequent recomputation so
-- immaterial movement remains suppressed while a material change can resurface.
CREATE TABLE "correlation_patterns" (
"id" TEXT NOT NULL,
"user_id" TEXT NOT NULL,
"canonical_key" TEXT NOT NULL,
"family" TEXT NOT NULL,
"factor_key" TEXT NOT NULL,
"outcome_key" TEXT NOT NULL,
"lag_days" INTEGER NOT NULL,
"sample_size" INTEGER NOT NULL,
"effect_size" DOUBLE PRECISION NOT NULL,
"p_value" DOUBLE PRECISION NOT NULL,
"q_value" DOUBLE PRECISION,
"evidence_hash" TEXT NOT NULL,
"is_current" BOOLEAN NOT NULL DEFAULT true,
"last_computed_at" TIMESTAMP(3) NOT NULL,
"dismissed_at" TIMESTAMP(3),
"dismissed_evidence_hash" TEXT,
"dismissed_effect_size" DOUBLE PRECISION,
"dismissed_sample_size" INTEGER,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "correlation_patterns_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "correlation_patterns_user_id_canonical_key_key"
ON "correlation_patterns"("user_id", "canonical_key");
CREATE INDEX "correlation_patterns_user_id_family_is_current_idx"
ON "correlation_patterns"("user_id", "family", "is_current");
ALTER TABLE "correlation_patterns"
ADD CONSTRAINT "correlation_patterns_user_id_fkey"
FOREIGN KEY ("user_id") REFERENCES "users"("id")
ON DELETE CASCADE ON UPDATE CASCADE;
@@ -0,0 +1,39 @@
-- Data-only, idempotent extension of the built-in binary mood-tag catalogue.
-- Mirrors migration 0101's deterministic-id and key-conflict taxonomy pattern.
INSERT INTO "mood_tags" (
"id", "category_id", "key", "label_key", "icon", "sort_order",
"is_active", "kind", "scale_min", "scale_max", "inverse", "user_id",
"label_encrypted"
)
VALUES (
'mt_caffeine', 'mtc_health', 'caffeine', 'mood.tag.caffeine', 'Coffee', 5,
true, 'BINARY', 1, 5, false, NULL, NULL
)
ON CONFLICT ("key") DO UPDATE SET
"category_id" = EXCLUDED."category_id",
"label_key" = EXCLUDED."label_key",
"icon" = EXCLUDED."icon",
"sort_order" = EXCLUDED."sort_order",
"kind" = EXCLUDED."kind",
"scale_min" = EXCLUDED."scale_min",
"scale_max" = EXCLUDED."scale_max",
"inverse" = EXCLUDED."inverse";
INSERT INTO "mood_tags" (
"id", "category_id", "key", "label_key", "icon", "sort_order",
"is_active", "kind", "scale_min", "scale_max", "inverse", "user_id",
"label_encrypted"
)
VALUES (
'mt_nicotine', 'mtc_health', 'nicotine', 'mood.tag.nicotine', 'Cigarette', 6,
true, 'BINARY', 1, 5, false, NULL, NULL
)
ON CONFLICT ("key") DO UPDATE SET
"category_id" = EXCLUDED."category_id",
"label_key" = EXCLUDED."label_key",
"icon" = EXCLUDED."icon",
"sort_order" = EXCLUDED."sort_order",
"kind" = EXCLUDED."kind",
"scale_min" = EXCLUDED."scale_min",
"scale_max" = EXCLUDED."scale_max",
"inverse" = EXCLUDED."inverse";
+38 -2
View File
@@ -841,10 +841,11 @@ model User {
// marker); LabResult rows link to a Biomarker for the resolved range/unit.
biomarkers Biomarker[]
// v1.25.5 — user-defined custom metrics. A separate generic store
// (catalog + values), fully isolated from the closed MeasurementType
// system: not synced, not in FHIR, not in AI insights — log + chart only.
// (catalog + values), not synced and not in FHIR. Correlation discovery
// reads a metric only after the account explicitly opts it in.
customMetrics CustomMetric[]
customMetricEntries CustomMetricEntry[]
correlationPatterns CorrelationPattern[]
// v1.19.0 — illness / condition journal (born-gated by the module system).
// IllnessEpisode is the soft-deleted episode row; IllnessDayLog is the
// per-day timeline mirroring the CycleDayLog shape.
@@ -4961,6 +4962,9 @@ model CustomMetric {
decimals Int?
/// Optional free-text description ("what this measures"). Plaintext.
description String?
/// Explicit opt-in to expose this metric as a bounded behaviour channel in
/// correlation discovery. False keeps the metric isolated.
correlationEnabled Boolean @default(false) @map("correlation_enabled")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
/// Soft-delete tombstone. NULL = live; non-NULL = deleted at the instant.
@@ -4995,6 +4999,38 @@ model CustomMetricEntry {
@@map("custom_metric_entries")
}
/// Persisted identity and latest accepted evidence for a user-scoped
/// correlation finding. `canonicalKey` is derived from factor + outcome + lag;
/// the named fields remain queryable and make the identity portable.
model CorrelationPattern {
id String @id @default(cuid())
userId String @map("user_id")
canonicalKey String @map("canonical_key")
family String
factorKey String @map("factor_key")
outcomeKey String @map("outcome_key")
lagDays Int @map("lag_days")
sampleSize Int @map("sample_size")
effectSize Float @map("effect_size")
pValue Float @map("p_value")
qValue Float? @map("q_value")
evidenceHash String @map("evidence_hash")
isCurrent Boolean @default(true) @map("is_current")
lastComputedAt DateTime @map("last_computed_at")
dismissedAt DateTime? @map("dismissed_at")
dismissedEvidenceHash String? @map("dismissed_evidence_hash")
dismissedEffectSize Float? @map("dismissed_effect_size")
dismissedSampleSize Int? @map("dismissed_sample_size")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([userId, canonicalKey])
@@index([userId, family, isCurrent])
@@map("correlation_patterns")
}
// ─── User Achievements ───────────────────────────────────────
model UserAchievement {