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, }
$this->isRecursive(),
$this->isArchive(),
$this->isHidden(),
$transforms,
);
// Group into dayid // Preload photos for these days
$detailMap = []; $details = $this->tq->getDay(
foreach ($allDetails as &$detail) { array_keys($drefMap),
$detailMap[$detail['dayid']][] = &$detail; $this->isRecursive(),
$this->isArchive(),
$this->isHidden(),
$this->getTransformations(),
);
// Load details into map byref
foreach ($details as $photo) {
$dayId = (int) $photo['dayid'];
if (!\array_key_exists($dayId, $drefMap)) {
continue;
} }
foreach ($preloadDays as &$day) {
$m = $detailMap[$day['dayid']]; if (!\array_key_exists('detail', $drefMap[$dayId])) {
if (isset($m) && null !== $m && \count($m) > 0) { $drefMap[$dayId]['detail'] = [];
$day['detail'] = $m;
}
} }
$drefMap[$dayId]['detail'][] = $photo;
} }
} }