Server: InfluxDB 2 storage, live telemetry endpoint, ABRP forwarder
- MongoDB replaced by InfluxDB 2 (one bucket, installationId tag). Consumption is stored per chunk at its recordedAt and summed on read, so a chunk the car re-sends from its cache overwrites itself instead of being counted twice ($inc did count it twice). Lifetime is a running total derived on read. - POST /v1/telemetry takes batches of samples in ABRP's tlm fields; every field is bounded, unknown fields are dropped, unread signals stay absent. - After a successful write the newest fresh sample is forwarded to ABRP with ABRP_API_KEY / ABRP_USER_TOKEN from the server environment; rate limited, never queued. The car no longer needs ABRP credentials. - Self-hosting: standalone output, Dockerfile (tests run in the build, runs as UID 10001), .env.example and README for the new setup. - Dev shell gains Node 22 and InfluxDB 2. Verified locally against InfluxDB 2.7.12: all endpoints, retry idempotency, and the dashboard pages. Unit tests: 17 pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import type { ConsumptionChunk, DailyConsumption, Totals } from "./types.ts";
|
||||
|
||||
/**
|
||||
* Folds chunks into days, newest day first, each with its running lifetime total.
|
||||
* Pure, so the arithmetic is tested without a database.
|
||||
*/
|
||||
export function aggregateDays(chunks: ConsumptionChunk[]): DailyConsumption[] {
|
||||
const byDate = new Map<string, DailyConsumption & { sohAt: number }>();
|
||||
for (const chunk of chunks) {
|
||||
let day = byDate.get(chunk.date);
|
||||
if (!day) {
|
||||
day = {
|
||||
installationId: chunk.installationId,
|
||||
date: chunk.date,
|
||||
recordedAt: chunk.recordedAt,
|
||||
day: { km: 0, kwh: 0, hours: 0, socDrop: 0 },
|
||||
sohAt: -Infinity,
|
||||
};
|
||||
byDate.set(chunk.date, day);
|
||||
}
|
||||
day.day.km += chunk.day.km;
|
||||
day.day.kwh += chunk.day.kwh;
|
||||
day.day.hours += chunk.day.hours;
|
||||
day.day.socDrop += chunk.day.socDrop;
|
||||
const at = chunk.recordedAt.getTime();
|
||||
if (at > day.recordedAt.getTime()) day.recordedAt = chunk.recordedAt;
|
||||
// The newest manual SoH of the day wins.
|
||||
if (chunk.sohPercent !== undefined && at > day.sohAt) {
|
||||
day.sohPercent = chunk.sohPercent;
|
||||
day.sohAt = at;
|
||||
}
|
||||
}
|
||||
const ascending = [...byDate.values()].sort((a, b) => a.date.localeCompare(b.date));
|
||||
const running: Totals = { km: 0, kwh: 0, hours: 0, socDrop: 0 };
|
||||
const days = ascending.map(({ sohAt: _sohAt, ...day }) => {
|
||||
running.km += day.day.km;
|
||||
running.kwh += day.day.kwh;
|
||||
running.hours += day.day.hours;
|
||||
running.socDrop += day.day.socDrop;
|
||||
return { ...day, lifetime: { ...running } };
|
||||
});
|
||||
return days.reverse();
|
||||
}
|
||||
Reference in New Issue
Block a user