fix: set descent and ascent to 0 when out of bounds (> 32767 meters) (#4882)

* set descent and ascent to 0 when out of bounds (> 32767 meters)

* docs: add comments to clarify elevation gain and loss handling in migrations regarding smallint

---------

Co-authored-by: Jakob Lichterfeld <jakob-lichterfeld@gmx.de>
This commit is contained in:
Matthias Wirtz
2025-08-15 18:30:14 +02:00
committed by GitHub
parent 92c7a6a36d
commit 261ff722e9
3 changed files with 16 additions and 14 deletions
+1
View File
@@ -13,6 +13,7 @@
- feat(nix): use datasources.settings.datasources to allow merging Grafana sources from multiple modules (#4870 - @JakobLichterfeld)
- fix(nix): correctly set default Grafana dashboard path (#4870 - @JakobLichterfeld)
- feat(nix): allow disabling default Grafana dashboard with setDefaultDashboard option (#4870 - @JakobLichterfeld)
- fix: set descent and ascent to 0 when out of bounds (> 32767 meters) to ensure migration succeeds (#4882 - @swiffer)
#### Build, CI, internal
+10 -12
View File
@@ -291,6 +291,8 @@ defmodule TeslaMate.Log do
not is_nil(p.odometer),
limit: 1
# If the sum of elevation gains exceeds the max value of a smallint (32767), set it to 0.
# If the sum of elevation losses exceeds the max value of a smallint (32767), set it to 0.
elevation_data =
from p1 in subquery(
from p in Position,
@@ -301,20 +303,16 @@ defmodule TeslaMate.Log do
),
select: %{
elevation_gains:
sum(
fragment(
"CASE WHEN ? > 0 THEN ? ELSE 0 END",
p1.elevation_diff,
p1.elevation_diff
)
fragment(
"COALESCE(NULLIF(LEAST(SUM(CASE WHEN ? > 0 THEN ? ELSE 0 END), 32768), 32768), 0)",
p1.elevation_diff,
p1.elevation_diff
),
elevation_losses:
sum(
fragment(
"CASE WHEN ? < 0 THEN ABS(?) ELSE 0 END",
p1.elevation_diff,
p1.elevation_diff
)
fragment(
"COALESCE(NULLIF(LEAST(SUM(CASE WHEN ? < 0 THEN ABS(?) ELSE 0 END), 32768), 32768), 0)",
p1.elevation_diff,
p1.elevation_diff
)
}
@@ -7,12 +7,15 @@ defmodule TeslaMate.Repo.Migrations.AddAndCalculateElevationChanges do
add :descent, :smallint
end
# If the sum of elevation gains exceeds the max value of a smallint (32767), set it to 0.
# If the sum of elevation losses exceeds the max value of a smallint (32767), set it to 0.
execute """
WITH elevation_changes AS (
SELECT
drive_id,
SUM(CASE WHEN elevation_diff > 0 THEN elevation_diff ELSE 0 END) as ascent,
SUM(CASE WHEN elevation_diff < 0 THEN ABS(elevation_diff) ELSE 0 END) as descent
COALESCE(NULLIF(LEAST(SUM(CASE WHEN elevation_diff > 0 THEN elevation_diff ELSE 0 END), 32768), 32768), 0) as ascent,
COALESCE(NULLIF(LEAST(SUM(CASE WHEN elevation_diff < 0 THEN ABS(elevation_diff) ELSE 0 END), 32768), 32768), 0) as descent
FROM (
SELECT
drive_id,