tw: refactor and add comments to map

pull/417/head
Varun Patil 2023-02-10 09:36:20 -08:00
parent 87e0b05726
commit 4ae8537691
2 changed files with 40 additions and 1 deletions

View File

@ -171,7 +171,6 @@ class TimelineWrite
try { try {
$mapCluster = $this->mapGetCluster($mapCluster, $lat, $lon); $mapCluster = $this->mapGetCluster($mapCluster, $lat, $lon);
$mapCluster = $mapCluster <= 0 ? null : $mapCluster;
} catch (\Error $e) { } catch (\Error $e) {
$logger = \OC::$server->get(LoggerInterface::class); $logger = \OC::$server->get(LoggerInterface::class);
$logger->log(3, 'Error updating map cluster data: '.$e->getMessage(), ['app' => 'memories']); $logger->log(3, 'Error updating map cluster data: '.$e->getMessage(), ['app' => 'memories']);
@ -185,6 +184,9 @@ class TimelineWrite
} }
} }
// NULL if invalid
$mapCluster = $mapCluster <= 0 ? null : $mapCluster;
// Parameters for insert or update // Parameters for insert or update
$params = [ $params = [
'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), 'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),

View File

@ -13,6 +13,16 @@ trait TimelineWriteMap
{ {
protected IDBConnection $connection; protected IDBConnection $connection;
/**
* Get the cluster ID for a given point.
* If the cluster ID changes, update the old cluster and the new cluster.
*
* @param int $prevCluster The current cluster ID of the point
* @param float $lat The latitude of the point
* @param float $lon The longitude of the point
*
* @return int The new cluster ID
*/
protected function mapGetCluster(int $prevCluster, float $lat, float $lon): int protected function mapGetCluster(int $prevCluster, float $lat, float $lon): int
{ {
// Get all possible clusters within CLUSTER_DEG radius // Get all possible clusters within CLUSTER_DEG radius
@ -59,6 +69,13 @@ trait TimelineWriteMap
return $minId; return $minId;
} }
/**
* Add a point to a cluster.
*
* @param int $clusterId The ID of the cluster
* @param float $lat The latitude of the point
* @param float $lon The longitude of the point
*/
protected function mapAddToCluster(int $clusterId, float $lat, float $lon): void protected function mapAddToCluster(int $clusterId, float $lat, float $lon): void
{ {
if ($clusterId <= 0) { if ($clusterId <= 0) {
@ -77,6 +94,14 @@ trait TimelineWriteMap
$this->mapUpdateAggregates($clusterId); $this->mapUpdateAggregates($clusterId);
} }
/**
* Create a new cluster.
*
* @param float $lat The latitude of the point
* @param float $lon The longitude of the point
*
* @return int The ID of the new cluster
*/
private function mapCreateCluster(float $lat, float $lon): int private function mapCreateCluster(float $lat, float $lon): int
{ {
$query = $this->connection->getQueryBuilder(); $query = $this->connection->getQueryBuilder();
@ -95,6 +120,13 @@ trait TimelineWriteMap
return $clusterId; return $clusterId;
} }
/**
* Remove a point from a cluster.
*
* @param int $clusterId The ID of the cluster
* @param float $lat The latitude of the point
* @param float $lon The longitude of the point
*/
private function mapRemoveFromCluster(int $clusterId, float $lat, float $lon): void private function mapRemoveFromCluster(int $clusterId, float $lat, float $lon): void
{ {
if ($clusterId <= 0) { if ($clusterId <= 0) {
@ -113,6 +145,11 @@ trait TimelineWriteMap
$this->mapUpdateAggregates($clusterId); $this->mapUpdateAggregates($clusterId);
} }
/**
* Update the aggregate values of a cluster.
*
* @param int $clusterId The ID of the cluster
*/
private function mapUpdateAggregates(int $clusterId): void private function mapUpdateAggregates(int $clusterId): void
{ {
if ($clusterId <= 0) { if ($clusterId <= 0) {