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
- func Apply(ctx context.Context, d *sql.DB, p Plan, encode func([][2]float64) string, ...) error
- func ExternalID(rivianVehicleID, kind string, t time.Time) string
- func NextLiveDriveAfter(ctx context.Context, d *sql.DB, v Vehicle, t time.Time) (time.Time, bool, error)
- func Restore(ctx context.Context, d *sql.DB, v Vehicle, rows []BackupRow) error
- func Segment(rows []Row) ([]Trip, []Charge)
- type BackupRow
- type Charge
- type Existing
- type Plan
- type Pricing
- type Row
- type Trip
- type Vehicle
Constants ¶
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 ¶
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 ¶
Restore undoes an Apply from its backup: the inserted rows go, the replaced drives and their dependents come back. One transaction.
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 ¶
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.
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.
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 ¶
AvgSpeedMph is distance over wall time.