days: make preload more efficient

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/877/head
Varun Patil 2023-10-14 15:37:05 -07:00
parent c8c6f1f8a1
commit b29ff26a18
1 changed files with 31 additions and 29 deletions

View File

@ -178,7 +178,7 @@ class DaysController extends GenericApiController
/**
* Preload a few "day" at the start of "days" response.
*
* @param array $days the days array
* @param array $days the days array (modified in place)
*/
private function preloadDays(array &$days): void
{
@ -188,45 +188,47 @@ class DaysController extends GenericApiController
return;
}
// Build identical transforms for sub queries
$transforms = $this->getTransformations();
$preloaded = 0;
$preloadDayIds = [];
$preloadDays = [];
// Construct map of dayid-day
$totalCount = 0;
$drefMap = [];
foreach ($days as &$day) {
if ($day['count'] <= 0) {
continue;
if ($count = (int) $day['count']) {
$totalCount += max($count, 10); // max 5 days
}
$preloaded += $day['count'];
$preloadDayIds[] = $day['dayid'];
$preloadDays[] = &$day;
$dayId = (int) $day['dayid'];
$drefMap[$dayId] = &$day;
if ($preloaded >= 50 || \count($preloadDayIds) > 5) { // should be enough
if ($totalCount >= 50) { // should be enough
break;
}
}
if (\count($preloadDayIds) > 0) {
$allDetails = $this->tq->getDay(
$preloadDayIds,
if (!$totalCount) {
return;
}
// Preload photos for these days
$details = $this->tq->getDay(
array_keys($drefMap),
$this->isRecursive(),
$this->isArchive(),
$this->isHidden(),
$transforms,
$this->getTransformations(),
);
// Group into dayid
$detailMap = [];
foreach ($allDetails as &$detail) {
$detailMap[$detail['dayid']][] = &$detail;
}
foreach ($preloadDays as &$day) {
$m = $detailMap[$day['dayid']];
if (isset($m) && null !== $m && \count($m) > 0) {
$day['detail'] = $m;
// Load details into map byref
foreach ($details as $photo) {
$dayId = (int) $photo['dayid'];
if (!\array_key_exists($dayId, $drefMap)) {
continue;
}
if (!\array_key_exists('detail', $drefMap[$dayId])) {
$drefMap[$dayId]['detail'] = [];
}
$drefMap[$dayId]['detail'][] = $photo;
}
}