From ac3962d985adf753f27df9462da80b8e36a49101 Mon Sep 17 00:00:00 2001 From: Martin Schreiber Date: Wed, 16 Apr 2025 07:07:36 +0200 Subject: [PATCH 1/8] Fix Elevation gain/loss --- src/store/actions.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/store/actions.js b/src/store/actions.js index 87d68e8..3570b49 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -130,16 +130,21 @@ const _getTravelStats = (locationHistory) => { Object.keys(locationHistory).forEach((user) => { Object.keys(locationHistory[user]).forEach((device) => { let lastLatLng = null; + let lastAlt = null; locationHistory[user][device].forEach((location) => { if ( config.filters.minAccuracy !== null && location.acc > config.filters.minAccuracy ) return; - const latLng = L.latLng(location.lat, location.lon, location.alt ?? 0); + const latLng = L.latLng(location.lat, location.lon, location.alt); if (lastLatLng !== null) { const distance = distanceBetweenCoordinates(lastLatLng, latLng); - const elevationChange = latLng.alt - lastLatLng.alt; + // calculate the elevationChange only if there is an alt available + let elevationChange = 0; + if (lastAlt != null && latLng.alt != null) { + elevationChange = latLng.alt - lastAlt; + } if ( typeof config.map.maxPointDistance === "number" && config.map.maxPointDistance > 0 @@ -153,6 +158,9 @@ const _getTravelStats = (locationHistory) => { else elevationLoss += -elevationChange; } } + if (latLng.alt) { + lastAlt = latLng.alt; + } lastLatLng = latLng; }); }); From e2ccbea390f8782efc7a1afc690a037d6f937d5f Mon Sep 17 00:00:00 2001 From: Martin Schreiber Date: Wed, 16 Apr 2025 07:07:36 +0200 Subject: [PATCH 2/8] Fix Elevation gain/loss --- src/store/actions.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/store/actions.js b/src/store/actions.js index 87d68e8..0ace114 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -130,16 +130,21 @@ const _getTravelStats = (locationHistory) => { Object.keys(locationHistory).forEach((user) => { Object.keys(locationHistory[user]).forEach((device) => { let lastLatLng = null; + let lastAlt = null; locationHistory[user][device].forEach((location) => { if ( config.filters.minAccuracy !== null && location.acc > config.filters.minAccuracy ) return; - const latLng = L.latLng(location.lat, location.lon, location.alt ?? 0); + const latLng = L.latLng(location.lat, location.lon, location.alt); if (lastLatLng !== null) { const distance = distanceBetweenCoordinates(lastLatLng, latLng); - const elevationChange = latLng.alt - lastLatLng.alt; + // calculate the elevationChange only if there is an alt available + let elevationChange = 0; + if (lastAlt != null && latLng.alt != null) { + elevationChange = latLng.alt - lastAlt; + } if ( typeof config.map.maxPointDistance === "number" && config.map.maxPointDistance > 0 @@ -153,6 +158,9 @@ const _getTravelStats = (locationHistory) => { else elevationLoss += -elevationChange; } } + if (latLng.alt != null) { + lastAlt = latLng.alt; + } lastLatLng = latLng; }); }); From 0445de062e84145c43c7c3312c9444eae6558a9c Mon Sep 17 00:00:00 2001 From: Martin Schreiber Date: Thu, 24 Apr 2025 06:01:48 +0200 Subject: [PATCH 3/8] Update src/store/actions.js Co-authored-by: Linus Groh --- src/store/actions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/store/actions.js b/src/store/actions.js index 0ace114..cf5dc22 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -142,7 +142,7 @@ const _getTravelStats = (locationHistory) => { const distance = distanceBetweenCoordinates(lastLatLng, latLng); // calculate the elevationChange only if there is an alt available let elevationChange = 0; - if (lastAlt != null && latLng.alt != null) { + if (lastAlt !== null && latLng.alt !== null) { elevationChange = latLng.alt - lastAlt; } if ( From 9c16dec9151a8aea4c18860f27bf9980af4bec69 Mon Sep 17 00:00:00 2001 From: Martin Schreiber Date: Thu, 24 Apr 2025 06:02:04 +0200 Subject: [PATCH 4/8] Update src/store/actions.js Co-authored-by: Linus Groh --- src/store/actions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/store/actions.js b/src/store/actions.js index cf5dc22..ad85569 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -158,7 +158,7 @@ const _getTravelStats = (locationHistory) => { else elevationLoss += -elevationChange; } } - if (latLng.alt != null) { + if (latLng.alt !== null) { lastAlt = latLng.alt; } lastLatLng = latLng; From d782c7b5b0f8858a19dd58f6c01ae8f3c185997a Mon Sep 17 00:00:00 2001 From: Martin Schreiber Date: Thu, 24 Apr 2025 17:39:51 +0200 Subject: [PATCH 5/8] Update actions.js I am so sorry, you are right, !== null just covers the null case and not undefined. So either just != null or checking both..... --- src/store/actions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/store/actions.js b/src/store/actions.js index ad85569..a5f0652 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -142,7 +142,7 @@ const _getTravelStats = (locationHistory) => { const distance = distanceBetweenCoordinates(lastLatLng, latLng); // calculate the elevationChange only if there is an alt available let elevationChange = 0; - if (lastAlt !== null && latLng.alt !== null) { + if (lastAlt !== null && lastAlt !== undefined && latLng.alt !== null && latLng.alt !== undefined) { elevationChange = latLng.alt - lastAlt; } if ( @@ -158,7 +158,7 @@ const _getTravelStats = (locationHistory) => { else elevationLoss += -elevationChange; } } - if (latLng.alt !== null) { + if (latLng.alt !== null && latLng.alt !== undefined) { lastAlt = latLng.alt; } lastLatLng = latLng; From 859829746b1af44d2c47e82c042ffb37c80b48c9 Mon Sep 17 00:00:00 2001 From: Martin Schreiber Date: Fri, 25 Apr 2025 07:31:33 +0200 Subject: [PATCH 6/8] removing unnecessary null/undefined checks --- src/store/actions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/store/actions.js b/src/store/actions.js index a5f0652..5c600d8 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -142,7 +142,7 @@ const _getTravelStats = (locationHistory) => { const distance = distanceBetweenCoordinates(lastLatLng, latLng); // calculate the elevationChange only if there is an alt available let elevationChange = 0; - if (lastAlt !== null && lastAlt !== undefined && latLng.alt !== null && latLng.alt !== undefined) { + if (lastAlt !== null && latLng.alt !== undefined) { elevationChange = latLng.alt - lastAlt; } if ( @@ -158,7 +158,7 @@ const _getTravelStats = (locationHistory) => { else elevationLoss += -elevationChange; } } - if (latLng.alt !== null && latLng.alt !== undefined) { + if (latLng.alt !== undefined) { lastAlt = latLng.alt; } lastLatLng = latLng; From 0066849bb57cf73fd89e9e8fb18ad19e79feb179 Mon Sep 17 00:00:00 2001 From: Martin Schreiber Date: Fri, 25 Apr 2025 07:43:45 +0200 Subject: [PATCH 7/8] adding unittest --- tests/actions.test.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/actions.test.js diff --git a/tests/actions.test.js b/tests/actions.test.js new file mode 100644 index 0000000..248ea2f --- /dev/null +++ b/tests/actions.test.js @@ -0,0 +1,31 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; +import { _getTravelStats } from "@/store/actions"; + +import { L } from "leaflet"; + +describe("_getTravelStats", () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it("calculates total distance and elevation", () => { + const testData = { + user1: { + device1: [ + { lat: 52.214908, lon: 8.116938, alt: 100, acc: 5 }, + { lat: 52.214908, lon: 8.116938, alt: 105, acc: 5 }, + { lat: 52.220619, lon: 8.104137, acc: 5 }, + { lat: 52.220619, lon: 8.104137, alt: 95, acc: 5 }, + { lat: 52.227348, lon: 8.094349, acc: 5 }, + ], + }, + }; + + const stats = _getTravelStats(testData); + + expect(stats.distanceTravelled).toBeCloseTo(2080.9568); + expect(stats.elevationGain).toBe(5); + expect(stats.elevationLoss).toBe(10); + }); + +}); From edf6ada9ce8cb6e12a8875405f9f280f0c716def Mon Sep 17 00:00:00 2001 From: Martin SCHREIBER Date: Fri, 25 Apr 2025 17:21:48 +0200 Subject: [PATCH 8/8] adding tests for elevation and distance calculatioadding tests for elevation and distance calculationn --- src/store/actions.js | 3 ++- tests/actions.test.js | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/store/actions.js b/src/store/actions.js index 5c600d8..b719aa6 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -122,7 +122,8 @@ const getLastLocations = async ({ commit, state }) => { commit(types.SET_LAST_LOCATIONS, lastLocations); }; -const _getTravelStats = (locationHistory) => { +// Export for testing purposes only +export const _getTravelStats = (locationHistory) => { const start = Date.now(); let distanceTravelled = 0; let elevationGain = 0; diff --git a/tests/actions.test.js b/tests/actions.test.js index 248ea2f..edd8656 100644 --- a/tests/actions.test.js +++ b/tests/actions.test.js @@ -8,15 +8,15 @@ describe("_getTravelStats", () => { vi.clearAllMocks(); }); - it("calculates total distance and elevation", () => { + it("calculates total distance and elevation with and without alt", () => { const testData = { user1: { device1: [ { lat: 52.214908, lon: 8.116938, alt: 100, acc: 5 }, { lat: 52.214908, lon: 8.116938, alt: 105, acc: 5 }, - { lat: 52.220619, lon: 8.104137, acc: 5 }, + { lat: 52.220619, lon: 8.104137, /* no alt */ acc: 5 }, { lat: 52.220619, lon: 8.104137, alt: 95, acc: 5 }, - { lat: 52.227348, lon: 8.094349, acc: 5 }, + { lat: 52.227348, lon: 8.094349, /* no alt */ acc: 5 }, ], }, };