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