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-03-23 23:58:49 +00:00
|
|
|
public function transformMapBoundsFilter(IQueryBuilder &$query, bool $aggregate, string $bounds, string $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,
|
2023-03-23 21:45:56 +00:00
|
|
|
string $bounds
|
2023-02-08 21:53:38 +00:00
|
|
|
): array {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
// Get the average location of each cluster
|
2023-02-10 17:55:58 +00:00
|
|
|
$id = $query->createFunction('MAX(c.id) as id');
|
|
|
|
$ct = $query->createFunction('COUNT(m.fileid) AS count');
|
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-10 17:55:58 +00:00
|
|
|
$query->select($id, $ct, $lat, $lon)
|
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
|
2023-02-10 17:55:58 +00:00
|
|
|
$gridParam = $query->createNamedParameter($gridLen, IQueryBuilder::PARAM_STR);
|
|
|
|
$query->addGroupBy($query->createFunction("FLOOR(c.lat / {$gridParam})"));
|
|
|
|
$query->addGroupBy($query->createFunction("FLOOR(c.lon / {$gridParam})"));
|
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
|
2023-03-23 21:45:56 +00:00
|
|
|
$query = $this->joinFilecache($query);
|
2023-02-08 21:53:38 +00:00
|
|
|
|
2023-02-09 05:55:12 +00:00
|
|
|
// Bound the query to the map bounds
|
2023-03-23 23:58:49 +00:00
|
|
|
$this->transformMapBoundsFilter($query, false, $bounds, 'c');
|
2023-02-08 21:53:38 +00:00
|
|
|
|
|
|
|
// Execute query
|
2023-03-24 01:12:39 +00:00
|
|
|
$res = $this->executeQueryWithCTEs($query)->fetchAll();
|
2023-02-08 21:53:38 +00:00
|
|
|
|
|
|
|
// 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-03-23 21:45:56 +00:00
|
|
|
public function getMapClusterPreviews(array $clusterIds)
|
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
|
2023-03-23 21:45:56 +00:00
|
|
|
$query = $this->joinFilecache($query);
|
2023-02-09 05:55:12 +00:00
|
|
|
|
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
|
|
|
}
|