From 08a5050b7c327f4833ea1a64f9c6b027ed6211a1 Mon Sep 17 00:00:00 2001 From: tuffnerdstuff Date: Tue, 30 Mar 2021 00:01:28 +0200 Subject: [PATCH] Implemented incremental fetch --- backend-php/api/fetch.php | 6 ++++-- backend-php/include/inc.php | 20 +++++++++++++++++--- frontend/main.js | 18 +++++++++++++++++- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/backend-php/api/fetch.php b/backend-php/api/fetch.php index d4e7c2f..166f3e6 100644 --- a/backend-php/api/fetch.php +++ b/backend-php/api/fetch.php @@ -20,6 +20,8 @@ if (!$share->exists()) { } else { header("Content-Type: text/json"); + $sinceTime=$_GET["since"] ?? null; + // Solo and group shares have different internal structures. Figure out the // correct type so that it can be output. switch ($share->getType()) { @@ -34,7 +36,7 @@ if (!$share->exists()) { "expire" => $share->getExpirationTime(), "serverTime" => microtime(true), "interval" => $session->getInterval(), - "points" => $session->getPoints(), + "points" => $session->getPoints($sinceTime), "encrypted" => $session->isEncrypted(), "salt" => $session->getEncryptionSalt() )); @@ -46,7 +48,7 @@ if (!$share->exists()) { "expire" => $share->getExpirationTime(), "serverTime" => microtime(true), "interval" => $share->getAutoInterval(), - "points" => $share->getAllPoints() + "points" => $share->getAllPoints($sinceTime) )); break; } diff --git a/backend-php/include/inc.php b/backend-php/include/inc.php index 366a7c2..64b6d16 100644 --- a/backend-php/include/inc.php +++ b/backend-php/include/inc.php @@ -565,12 +565,12 @@ class GroupShare extends Share { } // Returns a map of nicknames and the users' corresponding coordinates. - public function getAllPoints() { + public function getAllPoints($sinceTime) { $points = array(); $hosts = $this->getHosts(); foreach ($hosts as $nick => $host) { if ($host->exists()) { - $points[$nick] = $host->getPoints(); + $points[$nick] = $host->getPoints($sinceTime); } } return $points; @@ -758,8 +758,22 @@ class Client { } // Returns a list of all point arrays for this session. - public function getPoints() { + public function getPoints($sinceTime) { + if (is_null($sinceTime)) { + // return all memcached points return $this->sessionData["points"]; + } else { + $newPoints = []; + // FIXME: use map instead of indices + $timeIndex = $this->isEncrypted() ? 3 : 2; + // only return points which are more recent than $oldestPointTime + foreach ($this->sessionData["points"] as $point) { + if (floatval($point[$timeIndex]) > $sinceTime) { + array_push($newPoints, $point); + } + } + return $newPoints; + } } // Generates a random session ID for new sessions. diff --git a/frontend/main.js b/frontend/main.js index 3bbda17..14581d2 100644 --- a/frontend/main.js +++ b/frontend/main.js @@ -437,7 +437,8 @@ function setNewInterval(expire, interval, serverTime) { showMessage(LANG["dialog_expired_head"], LANG["dialog_expired_body"]); } - getJSON("./api/fetch.php?id=" + id, function(data) { + // Start incremental fetch + getJSON("./api/fetch.php?id=" + id + "&since=" + getOldestPointTime(), function(data) { // Recreate the interval timers if the interval or expiration // change. if (data.expire != expire || data.interval != interval) { @@ -456,6 +457,21 @@ function setNewInterval(expire, interval, serverTime) { }, interval * 1000); } +// Scans across all most recent points and returns the time of the oldest one +function getOldestPointTime() { + var oldestTime = Number.MAX_VALUE; + var foundTime = false; + for (var share in shares) { + var points = shares[share].points + if (points && points.length > 0 ) { + var mostRecentTime = points[ points.length-1 ].time + oldestTime = mostRecentTime < oldestTime ? mostRecentTime : oldestTime; + foundTime = true; + } + } + return foundTime ? oldestTime : 0; +} + var noGPS = document.getElementById("searching"); // Whether or not an initial location has been received.