2023-02-08 21:53:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Memories\Db;
|
|
|
|
|
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2023-02-09 01:45:55 +00:00
|
|
|
use OCP\IDBConnection;
|
2023-02-08 21:53:38 +00:00
|
|
|
|
|
|
|
trait TimelineQueryMap
|
|
|
|
{
|
2023-02-09 01:45:55 +00:00
|
|
|
protected IDBConnection $connection;
|
|
|
|
|
2023-02-09 05:55:12 +00:00
|
|
|
public function transformMapBoundsFilter(IQueryBuilder &$query, string $userId, string $bounds, $table = 'm')
|
2023-02-08 21:53:38 +00:00
|
|
|
{
|
|
|
|
$bounds = explode(',', $bounds);
|
|
|
|
$bounds = array_map('floatval', $bounds);
|
|
|
|
if (4 !== \count($bounds)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-09 05:55:12 +00:00
|
|
|
$latCol = $table.'.lat';
|
|
|
|
$lonCol = $table.'.lon';
|
2023-02-08 21:53:38 +00:00
|
|
|
$query->andWhere(
|
|
|
|
$query->expr()->andX(
|
2023-02-09 05:55:12 +00:00
|
|
|
$query->expr()->gte($latCol, $query->createNamedParameter($bounds[0], IQueryBuilder::PARAM_STR)),
|
|
|
|
$query->expr()->lte($latCol, $query->createNamedParameter($bounds[1], IQueryBuilder::PARAM_STR)),
|
|
|
|
$query->expr()->gte($lonCol, $query->createNamedParameter($bounds[2], IQueryBuilder::PARAM_STR)),
|
|
|
|
$query->expr()->lte($lonCol, $query->createNamedParameter($bounds[3], IQueryBuilder::PARAM_STR))
|
2023-02-08 21:53:38 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMapClusters(
|
|
|
|
float $gridLen,
|
|
|
|
string $bounds,
|
|
|
|
TimelineRoot &$root
|
|
|
|
): array {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
// Get the average location of each cluster
|
2023-02-09 05:55:12 +00:00
|
|
|
$lat = $query->createFunction('AVG(c.lat) AS lat');
|
|
|
|
$lon = $query->createFunction('AVG(c.lon) AS lon');
|
2023-02-08 21:53:38 +00:00
|
|
|
$count = $query->createFunction('COUNT(m.fileid) AS count');
|
2023-02-09 05:55:12 +00:00
|
|
|
|
2023-02-10 02:14:12 +00:00
|
|
|
$query->select($lat, $lon, $count)
|
2023-02-09 08:35:35 +00:00
|
|
|
->from('memories_mapclusters', 'c')
|
2023-02-08 21:53:38 +00:00
|
|
|
;
|
|
|
|
|
2023-02-10 04:30:25 +00:00
|
|
|
// Coarse grouping
|
|
|
|
$query->addSelect($query->createFunction('MAX(c.id) as id'));
|
|
|
|
$query->addGroupBy($query->createFunction("CAST(c.lat / {$gridLen} AS INT)"));
|
|
|
|
$query->addGroupBy($query->createFunction("CAST(c.lon / {$gridLen} AS INT)"));
|
2023-02-09 05:55:12 +00:00
|
|
|
|
|
|
|
// JOIN with memories for files from the current user
|
2023-02-09 08:35:35 +00:00
|
|
|
$query->innerJoin('c', 'memories', 'm', $query->expr()->eq('c.id', 'm.mapcluster'));
|
2023-02-09 05:55:12 +00:00
|
|
|
|
2023-02-08 21:53:38 +00:00
|
|
|
// JOIN with filecache for existing files
|
|
|
|
$query = $this->joinFilecache($query, $root, true, false);
|
|
|
|
|
2023-02-09 05:55:12 +00:00
|
|
|
// Bound the query to the map bounds
|
|
|
|
$this->transformMapBoundsFilter($query, '', $bounds, 'c');
|
2023-02-08 21:53:38 +00:00
|
|
|
|
|
|
|
// Execute query
|
|
|
|
$cursor = $this->executeQueryWithCTEs($query);
|
|
|
|
$res = $cursor->fetchAll();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
// Post-process results
|
|
|
|
$clusters = [];
|
2023-02-09 05:55:12 +00:00
|
|
|
foreach ($res as &$cluster) {
|
|
|
|
$c = [
|
2023-02-08 21:53:38 +00:00
|
|
|
'center' => [
|
2023-02-09 05:55:12 +00:00
|
|
|
(float) $cluster['lat'],
|
|
|
|
(float) $cluster['lon'],
|
2023-02-08 21:53:38 +00:00
|
|
|
],
|
|
|
|
'count' => (float) $cluster['count'],
|
|
|
|
];
|
2023-02-09 05:55:12 +00:00
|
|
|
if (\array_key_exists('id', $cluster)) {
|
|
|
|
$c['id'] = (int) $cluster['id'];
|
|
|
|
}
|
|
|
|
$clusters[] = $c;
|
2023-02-08 21:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $clusters;
|
|
|
|
}
|
2023-02-09 05:55:12 +00:00
|
|
|
|
2023-02-10 01:27:03 +00:00
|
|
|
public function getMapClusterPreviews(array $clusterIds, TimelineRoot &$root)
|
2023-02-09 05:55:12 +00:00
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
// SELECT all photos with this tag
|
2023-02-10 01:27:03 +00:00
|
|
|
$fileid = $query->createFunction('MAX(m.fileid) AS fileid');
|
|
|
|
$query->select($fileid)->from('memories', 'm')->where(
|
|
|
|
$query->expr()->in('m.mapcluster', $query->createNamedParameter($clusterIds, IQueryBuilder::PARAM_INT_ARRAY))
|
2023-02-09 05:55:12 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// WHERE these photos are in the user's requested folder recursively
|
|
|
|
$query = $this->joinFilecache($query, $root, true, false);
|
|
|
|
|
2023-02-10 01:27:03 +00:00
|
|
|
// GROUP BY the cluster
|
|
|
|
$query->groupBy('m.mapcluster');
|
2023-02-09 05:55:12 +00:00
|
|
|
|
2023-02-10 01:27:03 +00:00
|
|
|
// Get the fileIds
|
2023-02-09 05:55:12 +00:00
|
|
|
$cursor = $this->executeQueryWithCTEs($query);
|
2023-02-10 01:27:03 +00:00
|
|
|
$fileIds = $cursor->fetchAll(\PDO::FETCH_COLUMN);
|
|
|
|
|
|
|
|
// SELECT these files from the filecache
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('m.fileid', 'm.dayid', 'm.mapcluster', 'f.etag')
|
|
|
|
->from('memories', 'm')
|
|
|
|
->innerJoin('m', 'filecache', 'f', $query->expr()->eq('m.fileid', 'f.fileid'))
|
2023-02-10 01:27:18 +00:00
|
|
|
->where($query->expr()->in('m.fileid', $query->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)))
|
|
|
|
;
|
2023-02-10 01:27:03 +00:00
|
|
|
$files = $query->executeQuery()->fetchAll();
|
2023-02-09 05:55:12 +00:00
|
|
|
|
|
|
|
// Post-process
|
2023-02-10 01:27:03 +00:00
|
|
|
foreach ($files as &$row) {
|
2023-02-09 05:55:12 +00:00
|
|
|
$row['fileid'] = (int) $row['fileid'];
|
2023-02-10 01:27:03 +00:00
|
|
|
$row['mapcluster'] = (int) $row['mapcluster'];
|
|
|
|
$row['dayid'] = (int) $row['dayid'];
|
2023-02-09 05:55:12 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 01:27:03 +00:00
|
|
|
return $files;
|
2023-02-09 05:55:12 +00:00
|
|
|
}
|
2023-02-08 21:53:38 +00:00
|
|
|
}
|