Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,30 @@ 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;
let elevationLoss = 0;
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 !== undefined) {
elevationChange = latLng.alt - lastAlt;
}
if (
typeof config.map.maxPointDistance === "number" &&
config.map.maxPointDistance > 0
Expand All @@ -153,6 +159,9 @@ const _getTravelStats = (locationHistory) => {
else elevationLoss += -elevationChange;
}
}
if (latLng.alt !== undefined) {
lastAlt = latLng.alt;
}
lastLatLng = latLng;
});
});
Expand Down
31 changes: 31 additions & 0 deletions tests/actions.test.js
Original file line number Diff line number Diff line change
@@ -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 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, /* no alt */ acc: 5 },
{ lat: 52.220619, lon: 8.104137, alt: 95, acc: 5 },
{ lat: 52.227348, lon: 8.094349, /* no alt */ acc: 5 },
],
},
};

const stats = _getTravelStats(testData);

expect(stats.distanceTravelled).toBeCloseTo(2080.9568);
expect(stats.elevationGain).toBe(5);
expect(stats.elevationLoss).toBe(10);
});

});
Loading