feat(dawarich): fetch daily movement values from a Dawarich instance

Connect, status, places, settings, test, sync and disconnect replace their
stubs. A nightly job (03:20) and the connect backfill read the timeline,
visits and residency endpoints through safeFetch and store one row of daily
values per local day: distances, places visited, minutes away from the home
place and the sealed country with its travel-day verdict. A private instance
needs its exact origin in DAWARICH_PRIVATE_ORIGINS.

The connection joins the shared ledger and the connection health model,
watched for errors only. The day carries a movement block, the model
projection drops the travel country, the two movement channels join the
correlation matrix under the environment module, travel days become trip
suggestions, and the days travel in the environment backup section.
This commit is contained in:
Marc-André Bombeck committed 2026-10-16 01:20:53 +02:00
1 parent 1e7fc08f57
commit b838ed4403
95 files changed
+4466 -158

No files matched your search

@@ -438,6 +438,7 @@ function makePrisma() {
userAchievement: { findMany: vi.fn().mockResolvedValue([]) },
environmentContext: { findMany: vi.fn().mockResolvedValue([]) },
environmentTravelLocation: { findMany: vi.fn().mockResolvedValue([]) },
dawarichDay: { findMany: vi.fn().mockResolvedValue([]) },
// The Coach transcript. Deliberately NOT `?? []` at the call site, so a
// future `include` that forgets the relation fails here loudly instead of
// exporting an account whose Coach never spoke.
@@ -304,6 +304,7 @@ function sourceClient() {
userAchievement: { findMany: vi.fn().mockResolvedValue([]) },
environmentContext: { findMany: vi.fn().mockResolvedValue([]) },
environmentTravelLocation: { findMany: vi.fn().mockResolvedValue([]) },
dawarichDay: { findMany: vi.fn().mockResolvedValue([]) },
coachConversation: { findMany: vi.fn().mockResolvedValue([]) },
moodTagCategory: { findMany: vi.fn().mockResolvedValue([]) },
moodTagHidden: { findMany: vi.fn().mockResolvedValue([]) },
+4 -2
View File
@@ -531,6 +531,10 @@ export const TWO_ENDED_MODELS = [
// fortnight at home, days after a restore that reported success.
"EnvironmentContext",
"EnvironmentTravelLocation",
// v1.43 — the Dawarich movement days ride the same environment section
// (`src/lib/export/environment-backup.ts`): owned values a pruned instance
// can no longer hand back.
"DawarichDay",
// How a schedule got to where it is. The register said a restored
// medication keeps today's schedule and loses the record of when the dose or
// the timing moved. Measured, it costs more than a record: the era minter
@@ -638,8 +642,6 @@ export const STRUCTURALLY_UNATTRIBUTABLE: Readonly<Record<string, string>> = {
export const COVERAGE_PENDING: Readonly<Record<string, string>> = {
// v1.43 — the new tables are declared here with the schema and move to
// TWO_ENDED_MODELS when their section writer and restore branch land.
DawarichDay:
"The daily movement values fetched from Dawarich: distances, places visited, time away from home, travel days. Days the Dawarich instance has since pruned cannot be fetched again, so they are lost for good.",
WorkoutRoute:
"GPS traces. Deliberately absent from the payload today and DISCLOSED as absent in the file's own manifest, which is why this is a documented exclusion rather than a silent one — but it is still a loss for a self-hoster with no other copy.",
WorkoutSamples:
+1
View File
@@ -195,6 +195,7 @@ const SUMMARY_COUNTS = [
"userAchievements",
"environmentContexts",
"environmentTravelLocations",
"dawarichDays",
"ecgRecordings",
] as const satisfies readonly (keyof BackupSummary & keyof FullBackupCounts)[];
+131 -3
View File
@@ -72,12 +72,24 @@
* alike. Every one is optional on the way in: a file written before them
* says nothing about air quality, and the restored day then reads as never
* fetched (`aqFetchedAt` absent), so the nightly gap fill asks for it.
*
* ## The Dawarich days (v1.43)
*
* The movement context belongs to the same module and rides the same section
* (`dawarichDays`): distances, places visited, minutes away from home, the
* travel-day verdict, and the day's country. The country is the one sealed
* column and follows the two purposes like a location: readable
* `countryCode` in a portable file, `countryCodeEncrypted` (base64) in a
* disaster-recovery file. A file written before v1.43 carries no key and
* restores none. The connection itself (URL, key, preferences) is a
* credential and never travels.
*/
import { Buffer } from "node:buffer";
import type { Prisma, PrismaClient } from "@/generated/prisma/client";
import type { EnvironmentLocationSource } from "@/generated/prisma/client";
import type { AirQualityDay } from "@/lib/environment/air-quality-contract";
import { openDayCountry, sealDayCountry } from "@/lib/dawarich/connection";
import {
openLocation,
readLocation,
@@ -178,14 +190,36 @@ export interface EnvironmentTravelLocationBackupEntry {
updatedAt: string;
}
/** One local day of Dawarich movement values. */
export interface DawarichDayBackupEntry {
/** `YYYY-MM-DD` in the account's zone. A string end to end. */
date: string;
totalDistanceM: number | null;
drivingDistanceM: number | null;
trainDistanceM: number | null;
placesVisited: number | null;
awayFromHomeMinutes: number | null;
/** Readable ISO 3166-1 alpha-2; portable files only. */
countryCode: string | null;
/** The sealed country, base64; disaster-recovery files only. */
countryCodeEncrypted?: string | null;
isTravelDay: boolean | null;
/** When the day was last fetched from Dawarich. Verbatim. */
fetchedAt: string;
createdAt: string;
updatedAt: string;
}
export interface EnvironmentBackupSection {
environmentContexts: EnvironmentContextBackupEntry[];
environmentTravelLocations: EnvironmentTravelLocationBackupEntry[];
dawarichDays: DawarichDayBackupEntry[];
}
export interface EnvironmentBackupCounts {
environmentContexts: number;
environmentTravelLocations: number;
dawarichDays: number;
}
/**
@@ -246,6 +280,20 @@ const ENVIRONMENT_TRAVEL_LOCATION_BACKUP_SELECT = {
updatedAt: true,
} as const satisfies Prisma.EnvironmentTravelLocationSelect;
const DAWARICH_DAY_BACKUP_SELECT = {
date: true,
totalDistanceM: true,
drivingDistanceM: true,
trainDistanceM: true,
placesVisited: true,
awayFromHomeMinutes: true,
countryCodeEncrypted: true,
isTravelDay: true,
fetchedAt: true,
createdAt: true,
updatedAt: true,
} as const satisfies Prisma.DawarichDaySelect;
/** A sealed column as it rides a disaster-recovery file. */
function toBase64(bytes: Uint8Array | null): string | null {
return bytes && bytes.byteLength > 0
@@ -287,13 +335,13 @@ function locationFields(
export async function buildEnvironmentBackupSection(
prisma: Pick<
PrismaClient,
"environmentContext" | "environmentTravelLocation"
"environmentContext" | "environmentTravelLocation" | "dawarichDay"
>,
userId: string,
options: EnvironmentBackupOptions = {},
): Promise<EnvironmentBackupSection> {
const disasterRecovery = options.purpose === "disaster-recovery";
const [contextRows, travelRows] = await Promise.all([
const [contextRows, travelRows, dawarichRows] = await Promise.all([
prisma.environmentContext.findMany({
where: { userId },
orderBy: { date: "asc" },
@@ -304,6 +352,11 @@ export async function buildEnvironmentBackupSection(
orderBy: { startDate: "asc" },
select: ENVIRONMENT_TRAVEL_LOCATION_BACKUP_SELECT,
}),
prisma.dawarichDay.findMany({
where: { userId },
orderBy: { date: "asc" },
select: DAWARICH_DAY_BACKUP_SELECT,
}),
]);
return {
@@ -363,6 +416,24 @@ export async function buildEnvironmentBackupSection(
createdAt: row.createdAt.toISOString(),
updatedAt: row.updatedAt.toISOString(),
})),
dawarichDays: dawarichRows.map((row) => ({
date: row.date,
totalDistanceM: row.totalDistanceM,
drivingDistanceM: row.drivingDistanceM,
trainDistanceM: row.trainDistanceM,
placesVisited: row.placesVisited,
awayFromHomeMinutes: row.awayFromHomeMinutes,
...(disasterRecovery
? {
countryCode: null,
countryCodeEncrypted: toBase64(row.countryCodeEncrypted),
}
: { countryCode: openDayCountry(row.countryCodeEncrypted) }),
isTravelDay: row.isTravelDay,
fetchedAt: row.fetchedAt.toISOString(),
createdAt: row.createdAt.toISOString(),
updatedAt: row.updatedAt.toISOString(),
})),
};
}
@@ -373,6 +444,7 @@ export function countEnvironmentBackupSection(
return {
environmentContexts: section.environmentContexts.length,
environmentTravelLocations: section.environmentTravelLocations.length,
dawarichDays: section.dawarichDays.length,
};
}
@@ -380,6 +452,7 @@ export function countEnvironmentBackupSection(
export interface EnvironmentRestoreCleared {
environmentContexts: number;
environmentTravelLocations: number;
dawarichDays: number;
}
/**
@@ -474,9 +547,40 @@ function restoredLocation(
return sealLocation({ lat: entry.lat, lon: entry.lon, label: entry.label });
}
export type RestoredDawarichDay = Pick<DawarichDayBackupEntry, "date"> &
OptionalNullable<Omit<DawarichDayBackupEntry, "date">>;
/**
* The sealed country a restored day is written with: the file's sealed value
* as it came (named in `unopened` when this host cannot open it), or the
* readable code sealed under this host's key; null with neither.
*/
function restoredCountry(
entry: RestoredDawarichDay,
unopened: string[],
): Uint8Array<ArrayBuffer> | null {
if (typeof entry.countryCodeEncrypted === "string") {
const buffer = Buffer.from(entry.countryCodeEncrypted, "base64");
if (buffer.byteLength > 0) {
const bytes = new Uint8Array(new ArrayBuffer(buffer.byteLength));
bytes.set(buffer);
if (openDayCountry(bytes) === null) {
unopened.push(`dawarichDays.${entry.date}`);
}
return bytes;
}
}
return typeof entry.countryCode === "string" &&
/^[A-Z]{2}$/.test(entry.countryCode)
? sealDayCountry(entry.countryCode)
: null;
}
export interface EnvironmentRestoreInput {
environmentContexts: RestoredEnvironmentContext[];
environmentTravelLocations: RestoredEnvironmentTravelLocation[];
/** Absent in a file written before v1.43; nothing to restore then. */
dawarichDays?: RestoredDawarichDay[];
}
/**
@@ -502,9 +606,10 @@ export async function restoreEnvironmentData(
): Promise<EnvironmentRestoreCleared> {
// Sealed locations this host's keys do not open, by file path.
const unopened: string[] = [];
const [clearedTravel, clearedContexts] = await Promise.all([
const [clearedTravel, clearedContexts, clearedDawarich] = await Promise.all([
tx.environmentTravelLocation.deleteMany({ where: { userId: ownerId } }),
tx.environmentContext.deleteMany({ where: { userId: ownerId } }),
tx.dawarichDay.deleteMany({ where: { userId: ownerId } }),
]);
if (payload.environmentTravelLocations.length > 0) {
@@ -570,6 +675,28 @@ export async function restoreEnvironmentData(
});
}
const dawarichDays = payload.dawarichDays ?? [];
if (dawarichDays.length > 0) {
await tx.dawarichDay.createMany({
data: dawarichDays.map((entry) => ({
userId: ownerId,
date: entry.date,
totalDistanceM: entry.totalDistanceM ?? null,
drivingDistanceM: entry.drivingDistanceM ?? null,
trainDistanceM: entry.trainDistanceM ?? null,
placesVisited: entry.placesVisited ?? null,
awayFromHomeMinutes: entry.awayFromHomeMinutes ?? null,
countryCodeEncrypted: restoredCountry(entry, unopened),
isTravelDay: entry.isTravelDay ?? null,
// Verbatim, like the weather rows: when Dawarich was read, not when
// the row came back. A file without it reads as fetched now.
fetchedAt: entry.fetchedAt ? new Date(entry.fetchedAt) : new Date(),
...(entry.createdAt ? { createdAt: new Date(entry.createdAt) } : {}),
...(entry.updatedAt ? { updatedAt: new Date(entry.updatedAt) } : {}),
})),
});
}
if (skips) {
recordUnknownKeys(
skips,
@@ -582,5 +709,6 @@ export async function restoreEnvironmentData(
return {
environmentContexts: clearedContexts.count,
environmentTravelLocations: clearedTravel.count,
dawarichDays: clearedDawarich.count,
};
}
+2
View File
@@ -198,6 +198,7 @@ export interface RestoreResponse {
userAchievements: number;
environmentContexts: number;
environmentTravelLocations: number;
dawarichDays: number;
ecgRecordings: number;
symptomDefinitions: number;
lifeEvents: number;
@@ -2833,6 +2834,7 @@ export async function restoreBackup(
environmentContexts: environmentCleared.environmentContexts,
environmentTravelLocations:
environmentCleared.environmentTravelLocations,
dawarichDays: environmentCleared.dawarichDays,
ecgRecordings: ecgCleared.ecgRecordings,
symptomDefinitions: symptomsCleared.symptomDefinitions,
lifeEvents: lifeEventsCleared.lifeEvents,