feat: add direct car telemetry management
This commit is contained in:
@@ -21,6 +21,7 @@ type StoredSample = {
|
||||
};
|
||||
|
||||
export type TelemetryPeriod = "24h" | "7d" | "30d" | "1y" | "all";
|
||||
export type ClearTelemetryMode = "history" | "all";
|
||||
|
||||
export type TelemetryHistoryPoint = {
|
||||
timestamp: number;
|
||||
@@ -252,6 +253,39 @@ export class CarTelemetryStore {
|
||||
transaction();
|
||||
}
|
||||
|
||||
clear(mode: ClearTelemetryMode): { mode: ClearTelemetryMode; deletedSamples: number; deletedRollups: number } {
|
||||
const transaction = this.database.transaction(() => {
|
||||
const deletedSamples = this.database.prepare("DELETE FROM car_telemetry_samples").run().changes;
|
||||
const deletedRollups = this.database.prepare("DELETE FROM car_telemetry_hourly").run().changes;
|
||||
|
||||
if (mode === "all") {
|
||||
this.database.prepare(
|
||||
`UPDATE car_telemetry_stats SET
|
||||
first_sample_at = NULL,
|
||||
last_sample_at = NULL,
|
||||
sample_count = 0,
|
||||
peak_speed_kph = NULL,
|
||||
peak_speed_at = NULL,
|
||||
peak_charging_power_kw = NULL,
|
||||
peak_charging_power_at = NULL,
|
||||
max_observed_range_km = NULL,
|
||||
max_observed_range_at = NULL,
|
||||
tracked_distance_km = 0,
|
||||
driving_seconds = 0,
|
||||
charging_seconds = 0,
|
||||
charge_sessions = 0,
|
||||
soc_used_percent = 0
|
||||
WHERE id = 1`
|
||||
).run();
|
||||
}
|
||||
|
||||
return { mode, deletedSamples, deletedRollups };
|
||||
});
|
||||
|
||||
this.#samplesSincePrune = 0;
|
||||
return transaction();
|
||||
}
|
||||
|
||||
overview() {
|
||||
const stats = this.database.prepare("SELECT * FROM car_telemetry_stats WHERE id = 1").get() as Record<string, number | null>;
|
||||
const latest = rowToStored(this.database.prepare("SELECT * FROM car_telemetry_samples ORDER BY recorded_at DESC LIMIT 1").get() as Record<string, unknown> | undefined);
|
||||
|
||||
Reference in New Issue
Block a user