Skip to content

fix(drives): keep mileage when a position has no odometer - #5637

Open
mews-se wants to merge 1 commit into
teslamate-org:mainfrom
mews-se:fix-drive-start-km-null
Open

fix(drives): keep mileage when a position has no odometer#5637
mews-se wants to merge 1 commit into
teslamate-org:mainfrom
mews-se:fix-drive-start-km-null

Conversation

@mews-se

@mews-se mews-se commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Addresses case 1 of #5583 — drives losing start_km — and stays inside the Log layer as direction 3 suggests, so no state machine changes.

The bug

close_drive/2 reads a drive's mileage with first_value(p.odometer) / last_value(p.odometer) over the drive's positions. Neither skips NULLs, so a drive whose first or last position carries no odometer is closed with start_km, end_km and distance all NULL — permanently, since a closed drive is never recomputed. That is the reported symptom on Fleet Telemetry setups, where a record can be built from a single partial payload.

min and max ignore NULLs, and since the odometer never decreases within a drive they return exactly the same values as first_value/last_value whenever every position carries one. The Owner API path is therefore unchanged.

Changes

  • close_drive/2: start_km, end_km and distance come from min/max over the same window.
  • A migration recomputes those three columns for drives that are already affected, from the positions still retained. It only touches rows that are actually broken, and only when the retained positions prove a positive distance — a drive whose positions have been purged keeps its NULLs rather than gaining a fabricated 0.
  • A regression test where the odometer is missing from both the first and the last position.

Verification

mix test against postgres:18-trixie on elixir:1.20.2-otp-29. The new test fails on current main and passes with the change; the rest of the suite is unaffected.

Not covered

A drive where no position carries an odometer still ends up with NULLs. distance is nil there, and the distance >= 0.01 guard accepts nil because atoms sort above numbers in Elixir, so the drive is kept rather than deleted. That behaviour is unchanged by this PR and seemed like a separate call — happy to fold it in if you would rather have those drives dropped.

close_drive/2 reads the mileage of a drive with first_value/last_value over its
positions. Neither skips NULLs, so a drive whose first or last position carried
no odometer was closed with a NULL start_km, end_km and distance, permanently.
That happens on Fleet Telemetry setups, where a record can be built from a
single partial payload.

min and max ignore NULLs, and since the odometer never decreases within a drive
they return the same values as before whenever every position carries one. A
migration recomputes the three columns for the drives that are already affected
and still have positions to compute from.
@netlify

netlify Bot commented Aug 19, 2026

Copy link
Copy Markdown

Deploy Preview for teslamate ready!

Name Link
🔨 Latest commit 430fbab
🔍 Latest deploy log https://app.netlify.com/projects/teslamate/deploys/6a856a7365385f00085e7742
😎 Deploy Preview https://deploy-preview-5637--teslamate.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@JakobLichterfeld

JakobLichterfeld commented Aug 19, 2026

Copy link
Copy Markdown
Member

Thanks for your contribution. That being said: if an issue is assigned (like in this case), the PR will come form the assignee. So I tend to close this PR, you can convince me otherwise.

@JakobLichterfeld JakobLichterfeld added the note:discussion Details or approval are up for discussion label Aug 19, 2026
@mews-se

mews-se commented Aug 19, 2026

Copy link
Copy Markdown
Contributor Author

My bad! I mistook your thumbs up for a go earlier. I have no objection to you closing it - and feel free to use anything you find useful! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

note:discussion Details or approval are up for discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants