From b6553ab989bec6c311c361ca72c1c67d3dd913da Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Mon, 13 Apr 2020 23:50:31 +0200 Subject: [PATCH] Fix delta calculations when fetching part of track --- .tests/tests/InternalAPITest.php | 33 ++++++++++++++++++++++++++++++++ utils/getpositions.php | 6 ++++++ 2 files changed, 39 insertions(+) diff --git a/.tests/tests/InternalAPITest.php b/.tests/tests/InternalAPITest.php index 7d5f13c..b56c497 100644 --- a/.tests/tests/InternalAPITest.php +++ b/.tests/tests/InternalAPITest.php @@ -294,6 +294,39 @@ class InternalAPITest extends UloggerAPITestCase { $this->assertEquals(count($json), 0, "Wrong count of positions"); } + public function testGetPositionsAfterId() { + + $this->assertTrue($this->authenticate(), "Authentication failed"); + + $trackId = $this->addTestTrack($this->testUserId); + $afterId = $this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp); + $this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp + 1, $this->testLat + 1); + $this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count"); + $this->assertEquals(2, $this->getConnection()->getRowCount("positions"), "Wrong row count"); + + $options = [ + "http_errors" => false, + "query" => [ "userid" => $this->testUserId, "trackid" => $trackId, "afterid" => $afterId ], + ]; + $response = $this->http->get("/utils/getpositions.php", $options); + $this->assertEquals(200, $response->getStatusCode(), "Unexpected status code"); + + $json = json_decode($response->getBody()); + + $this->assertNotNull($json, "JSON object is null"); + $this->assertEquals(count($json), 1, "Wrong count of positions"); + + $position = $json[0]; + $this->assertEquals((int) $position->id, $afterId + 1, "Wrong position id"); + $this->assertEquals((float) $position->latitude, $this->testLat + 1, "Wrong latitude"); + $this->assertEquals((float) $position->longitude, $this->testLon, "Wrong longitude"); + $this->assertEquals((int) $position->timestamp, $this->testTimestamp + 1, "Wrong timestamp"); + $this->assertEquals((string) $position->username, $this->testAdminUser, "Wrong username"); + $this->assertEquals((string) $position->trackname, $this->testTrackName, "Wrong trackname"); + $this->assertEquals((int) $position->meters, 111195, "Wrong distance delta"); + $this->assertEquals((int) $position->seconds, 1, "Wrong timestamp delta"); + } + /* gettracks.php */ diff --git a/utils/getpositions.php b/utils/getpositions.php index cb80a18..5045efe 100644 --- a/utils/getpositions.php +++ b/utils/getpositions.php @@ -55,6 +55,12 @@ $result = []; if ($positionsArr === false) { $result = [ "error" => true ]; } else if (!empty($positionsArr)) { + if ($afterId) { + $afterPosition = new uPosition($afterId); + if ($afterPosition->isValid) { + $prevPosition = $afterPosition; + } + } foreach ($positionsArr as $position) { $meters = !$last && isset($prevPosition) ? $position->distanceTo($prevPosition) : 0; $seconds = !$last && isset($prevPosition) ? $position->secondsTo($prevPosition) : 0;