r2

package
v0.41.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package r2 rebuilds the drives and charges of a vehicle whose vehicleState gear was unusable (R2 before #214: the gear stayed at the last D/R for days, so drives never closed and no charge opened). It re-segments from vehicle_state rows: an R2 reports power "go" while driving and "ready" otherwise, which is reliable even when its gear isn't. Plan: scratchpad/r2-repair-plan.md.

Index

Constants

View Source
const (
	// TripBreak: power not "go" for this long ends a trip.
	TripBreak = 2 * time.Minute
	// TripMinMi: a "go" run that moved less is preconditioning or
	// sitting in drive, not a trip.
	TripMinMi = 0.1
	// ChargeBreak: a gap this long between charging_active rows ends a
	// charge (the recorder's liveChargeMaxGap).
	ChargeBreak = 30 * time.Minute
	// ChargeGapCap caps each row's share of the power integral (the
	// phantom audit's rule) so a telemetry gap can't invent energy.
	ChargeGapCap = 10 * time.Minute
	// ChargeMinKWh: below this a charging_active run is noise.
	ChargeMinKWh = 1.0
)

Segmentation limits.

Variables

This section is empty.

Functions

func Apply

func Apply(ctx context.Context, d *sql.DB, p Plan, encode func([][2]float64) string, sink func([]BackupRow) error) error

Apply runs p in one transaction. sink receives the backup (every replaced row as to_jsonb, then markers for every inserted row) and must persist it durably; it is called before anything is deleted, and again for the insert markers before the commit. encode turns a trip's path into the route polyline.

func ExternalID

func ExternalID(rivianVehicleID, kind string, t time.Time) string

ExternalID is the recorder's id for a live session started at t (liveSessionID): re-running the repair, or the recorder re-saving, collapses onto the same row.

func NextLiveDriveAfter

func NextLiveDriveAfter(ctx context.Context, d *sql.DB, v Vehicle, t time.Time) (time.Time, bool, error)

NextLiveDriveAfter returns the start of the vehicle's first live drive starting at or after t (ok=false when none). The operator checks it against when the fixed recorder reached prod: drives recorded after that must stay out of the window.

func Restore

func Restore(ctx context.Context, d *sql.DB, v Vehicle, rows []BackupRow) error

Restore undoes an Apply from its backup: the inserted rows go, the replaced drives and their dependents come back. One transaction.

func Segment

func Segment(rows []Row) ([]Trip, []Charge)

Segment splits time-ordered rows into trips and charges. A row with power not reported ("") continues whatever state came before it: an R2 leaves power blank in about a fifth of its rows, mostly around resubscribes, and the odometer moving across them is still driving.

Types

type BackupRow

type BackupRow struct {
	Kind string
	Row  json.RawMessage
}

BackupRow is one row saved before the apply (Kind: drive, drive_recaps, drive_efficiency, drive_weather, drive_weather_series) or one row the apply inserted (inserted_drive, inserted_charge; Row holds {"external_id": …}).

type Charge

type Charge struct {
	Start, End       time.Time
	StartSoC, EndSoC float64
	EnergyKWh        float64 // power integral
	PeakKW           float64
	Lat, Lon         float64
	ActiveSeconds    float64
	// Pricing as the recorder would have stamped it (PriceCharges);
	// zero/empty leaves the charge unpriced.
	Cost, PricePerKWh    float64
	Currency, CostSource string
	Place                string // "home" | "away" | ""
}

Charge is a recovered charging session.

func PriceCharges

func PriceCharges(chs []Charge, p Pricing) []Charge

PriceCharges stamps each charge the way the recorder does for AC charging: at the home rate (cost_source home_rate) unless it is known to be away (more than HomeRadiusM from a saved home), which stays unpriced; no rate set, unpriced. Place is home / away when a home is saved, else unknown. A DC fast charge stays unpriced: the recorder prices those from the network, which a repair doesn't guess.

type Existing

type Existing struct {
	ID         uuid.UUID
	ExternalID string
	Start, End time.Time
	Miles      float64
	Source     string
	UserEdited bool // notes/tags (drives) or a user price (charges)
}

Existing is a stored session in the window.

func LoadCharges

func LoadCharges(ctx context.Context, d *sql.DB, v Vehicle, since, until time.Time) ([]Existing, error)

LoadCharges returns the vehicle's charges overlapping [since, until).

func LoadDrives

func LoadDrives(ctx context.Context, d *sql.DB, v Vehicle, since, until time.Time) ([]Existing, error)

LoadDrives returns the vehicle's drives overlapping [since, until).

type Plan

type Plan struct {
	Vehicle       Vehicle
	Since, Until  time.Time
	DeleteDrives  []Existing // live drives in the window, replaced
	InsertDrives  []Trip
	InsertCharges []Charge
	Skipped       []string // why something was left alone
}

Plan is what an apply does.

func MakePlan

func MakePlan(v Vehicle, since, until time.Time, trips []Trip, chs []Charge, drives, stored []Existing) (Plan, error)

MakePlan refuses any vehicle but an R2: the segmentation rests on an R2's power state; an R1's recorded gear is the better record.

MakePlan replaces every live drive inside [since, until) with the re-segmented trips and adds the recovered charges that don't overlap a stored charge. It refuses (error) when a drive in the window isn't live, sticks out of the window, or carries the user's notes/tags: those are a person's call, not a repair's.

type Pricing

type Pricing struct {
	HomeKnown        bool
	HomeLat, HomeLon float64
	HomeRate         float64
	Currency         string
	HomeRadiusM      float64
	DCFCMinKW        float64
}

Pricing is what the recorder knows when it closes an AC charge.

type Row

type Row struct {
	At            time.Time
	Power         string // power_state; "" = not reported
	ChargingState string
	OdoMi         float64
	SoC           float64
	SpeedMph      float64
	Lat, Lon      float64
	KW            float64 // charger_power_kw
}

Row is one vehicle_state sample.

func LoadRows

func LoadRows(ctx context.Context, d *sql.DB, v Vehicle, since, until time.Time) ([]Row, error)

LoadRows reads the vehicle's samples in [since, until), oldest first.

type Trip

type Trip struct {
	Start, End           time.Time
	StartOdoMi, EndOdoMi float64
	StartSoC, EndSoC     float64
	StartLat, StartLon   float64
	EndLat, EndLon       float64
	MaxSpeedMph          float64
	Path                 [][2]float64 // lat, lon
}

Trip is a re-segmented drive.

func (Trip) AvgSpeedMph

func (t Trip) AvgSpeedMph() float64

AvgSpeedMph is distance over wall time.

func (Trip) DistanceMi

func (t Trip) DistanceMi() float64

DistanceMi is the odometer distance.

func (Trip) EnergyKWh

func (t Trip) EnergyKWh(packKWh float64) float64

EnergyKWh is the SoC used times the pack (0 when SoC rose).

type Vehicle

type Vehicle struct {
	ID       uuid.UUID // vehicles.id
	UserID   uuid.UUID
	RivianID string
	Model    string
	PackKWh  float64
}

Vehicle is the vehicle being repaired.

func LoadVehicle

func LoadVehicle(ctx context.Context, d *sql.DB, rivianID string) (Vehicle, error)

LoadVehicle resolves a Rivian vehicle id.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL