From 7b7ecab8e205263e1ee8df1fd12b321c92067cda Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sat, 14 Oct 2023 17:01:56 -0700 Subject: [PATCH] tq: refactor month days API Signed-off-by: Varun Patil --- lib/Controller/DaysController.php | 43 +++--------------------------- lib/Db/TimelineQueryDays.php | 39 ++++++++++++++++++++------- lib/Db/TimelineQuerySingleItem.php | 2 +- 3 files changed, 33 insertions(+), 51 deletions(-) diff --git a/lib/Controller/DaysController.php b/lib/Controller/DaysController.php index a666d598..69c4a39c 100644 --- a/lib/Controller/DaysController.php +++ b/lib/Controller/DaysController.php @@ -41,18 +41,16 @@ class DaysController extends GenericApiController $list = $this->tq->getDays( $this->isRecursive(), $this->isArchive(), + $this->isMonthView(), $this->getTransformations(), ); - if ($this->isMonthView()) { - // Group days together into months - $list = $this->daysToMonths($list); - } else { + if (!$this->isMonthView()) { // Preload some day responses $this->preloadDays($list); } - // Reverse response if requested. Folders still stay at top. + // Reverse response if requested. if ($this->isReverse()) { $list = array_reverse($list); } @@ -205,41 +203,6 @@ class DaysController extends GenericApiController } } - /** - * Convert days response to months response. - * The dayId is used to group the days into months. - */ - private function daysToMonths(array $days): array - { - $months = []; - foreach ($days as $day) { - $dayId = $day['dayid']; - $time = $dayId * 86400; - $monthid = strtotime(date('Ym', $time).'01') / 86400; - - if (empty($months) || $months[\count($months) - 1]['dayid'] !== $monthid) { - $months[] = [ - 'dayid' => $monthid, - 'count' => 0, - ]; - } - - $months[\count($months) - 1]['count'] += $day['count']; - } - - return $months; - } - - /** - * Convert list of month IDs to list of dayIds. - * - * @return int[] The list of dayIds - */ - private function monthIdToDayIds(int $monthId): array - { - return range($monthId, (int) (strtotime(date('Ymt', $monthId * 86400)) / 86400)); - } - private function isRecursive(): bool { return null === $this->request->getParam('folder') || $this->request->getParam('recursive'); diff --git a/lib/Db/TimelineQueryDays.php b/lib/Db/TimelineQueryDays.php index dfd9c8e2..97d6d995 100644 --- a/lib/Db/TimelineQueryDays.php +++ b/lib/Db/TimelineQueryDays.php @@ -20,6 +20,7 @@ trait TimelineQueryDays * * @param bool $recursive Whether to get the days recursively * @param bool $archive Whether to get the days only from the archive folder + * @param bool $monthView Whether the response should be in month view * @param array $queryTransforms An array of query transforms to apply to the query * * @return array The days response @@ -27,6 +28,7 @@ trait TimelineQueryDays public function getDays( bool $recursive, bool $archive, + bool $monthView, array $queryTransforms = [] ): array { $query = $this->connection->getQueryBuilder(); @@ -51,7 +53,8 @@ trait TimelineQueryDays // FETCH all days $rows = $this->executeQueryWithCTEs($query)->fetchAll(); - return $this->processDays($rows); + // Post process the days + return $this->postProcessDays($rows, $monthView); } /** @@ -104,7 +107,7 @@ trait TimelineQueryDays // Convert monthIds to dayIds $query->andWhere($query->expr()->orX(...array_map(fn ($monthId) => $query->expr()->andX( $query->expr()->gte('m.dayid', $query->createNamedParameter($monthId, IQueryBuilder::PARAM_INT)), - $query->expr()->lte('m.dayid', $query->createNamedParameter($this->monthEndDayId($monthId), IQueryBuilder::PARAM_INT)) + $query->expr()->lte('m.dayid', $query->createNamedParameter($this->dayIdMonthEnd($monthId), IQueryBuilder::PARAM_INT)) ), $dayIds))); } else { // Filter by list of dayIds @@ -129,7 +132,7 @@ trait TimelineQueryDays // Post process the day in-place foreach ($day as &$photo) { - $this->processDayPhoto($photo, $monthView); + $this->postProcessDayPhoto($photo, $monthView); } return $day; @@ -220,25 +223,41 @@ trait TimelineQueryDays /** * Process the days response. * - * @param array $days + * @param array $rows the days response + * @param bool $monthView Whether the response is in month view */ - private function processDays($days): array + private function postProcessDays(array $rows, bool $monthView): array { - foreach ($days as &$row) { + foreach ($rows as &$row) { $row['dayid'] = (int) $row['dayid']; $row['count'] = (int) $row['count']; } - return $days; + // Convert to months if needed + if ($monthView) { + return array_values(array_reduce($rows, function ($carry, $item) { + $monthId = $this->dayIdToMonthId($item['dayid']); + + if (!\array_key_exists($monthId, $carry)) { + $carry[$monthId] = ['dayid' => $monthId, 'count' => 0]; + } + + $carry[$monthId]['count'] += $item['count']; + + return $carry; + }, [])); + } + + return $rows; } /** * Process the single day response. * - * @param array $row The day response + * @param array $row A photo in the day response * @param bool $monthView Whether the response is in month view */ - private function processDayPhoto(array &$row, bool $monthView = false): void + private function postProcessDayPhoto(array &$row, bool $monthView = false): void { // Convert field types $row['fileid'] = (int) $row['fileid']; @@ -310,7 +329,7 @@ trait TimelineQueryDays } } - private function monthEndDayId(int $monthId): int + private function dayIdMonthEnd(int $monthId): int { return (int) (strtotime(date('Ymt', $monthId * 86400)) / 86400); } diff --git a/lib/Db/TimelineQuerySingleItem.php b/lib/Db/TimelineQuerySingleItem.php index 7af47047..57968500 100644 --- a/lib/Db/TimelineQuerySingleItem.php +++ b/lib/Db/TimelineQuerySingleItem.php @@ -36,7 +36,7 @@ trait TimelineQuerySingleItem } // Post process the record - $this->processDayPhoto($photo); + $this->postProcessDayPhoto($photo); return $photo; }