From 9ddab7137059fc4d520902eeaf0737557047d316 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Wed, 26 Oct 2022 11:10:24 -0700 Subject: [PATCH] Use single query for day preload --- lib/Controller/ApiController.php | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 41c35c28..ae5c964d 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -669,20 +669,38 @@ class ApiController extends Controller $uid = $this->userSession->getUser()->getUID(); $transforms = $this->getTransformations(false); $preloaded = 0; + $preloadDayIds = []; + $preloadDays = []; foreach ($days as &$day) { - $day['detail'] = $this->timelineQuery->getDay( + $preloaded += $day['count']; + $preloadDayIds[] = $day['dayid']; + $preloadDays[] = &$day; + + if ($preloaded >= 50) { // should be enough + break; + } + } + + if (\count($preloadDayIds) > 0) { + $allDetails = $this->timelineQuery->getDay( $folder, $uid, - [$day['dayid']], + $preloadDayIds, $recursive, $archive, $transforms, ); - $day['count'] = \count($day['detail']); // make sure count is accurate - $preloaded += $day['count']; - if ($preloaded >= 50) { // should be enough - break; + // 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; + } } } }