Implemented incremental fetch

This commit is contained in:
tuffnerdstuff 2021-03-30 00:01:28 +02:00
parent b95f517ada
commit 08a5050b7c
3 changed files with 38 additions and 6 deletions

View File

@ -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;
}

View File

@ -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.

View File

@ -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.