tq: refactor month days API
Signed-off-by: Varun Patil <radialapps@gmail.com>pull/877/head
parent
2d5b687350
commit
7b7ecab8e2
|
@ -41,18 +41,16 @@ class DaysController extends GenericApiController
|
||||||
$list = $this->tq->getDays(
|
$list = $this->tq->getDays(
|
||||||
$this->isRecursive(),
|
$this->isRecursive(),
|
||||||
$this->isArchive(),
|
$this->isArchive(),
|
||||||
|
$this->isMonthView(),
|
||||||
$this->getTransformations(),
|
$this->getTransformations(),
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($this->isMonthView()) {
|
if (!$this->isMonthView()) {
|
||||||
// Group days together into months
|
|
||||||
$list = $this->daysToMonths($list);
|
|
||||||
} else {
|
|
||||||
// Preload some day responses
|
// Preload some day responses
|
||||||
$this->preloadDays($list);
|
$this->preloadDays($list);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reverse response if requested. Folders still stay at top.
|
// Reverse response if requested.
|
||||||
if ($this->isReverse()) {
|
if ($this->isReverse()) {
|
||||||
$list = array_reverse($list);
|
$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
|
private function isRecursive(): bool
|
||||||
{
|
{
|
||||||
return null === $this->request->getParam('folder') || $this->request->getParam('recursive');
|
return null === $this->request->getParam('folder') || $this->request->getParam('recursive');
|
||||||
|
|
|
@ -20,6 +20,7 @@ trait TimelineQueryDays
|
||||||
*
|
*
|
||||||
* @param bool $recursive Whether to get the days recursively
|
* @param bool $recursive Whether to get the days recursively
|
||||||
* @param bool $archive Whether to get the days only from the archive folder
|
* @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
|
* @param array $queryTransforms An array of query transforms to apply to the query
|
||||||
*
|
*
|
||||||
* @return array The days response
|
* @return array The days response
|
||||||
|
@ -27,6 +28,7 @@ trait TimelineQueryDays
|
||||||
public function getDays(
|
public function getDays(
|
||||||
bool $recursive,
|
bool $recursive,
|
||||||
bool $archive,
|
bool $archive,
|
||||||
|
bool $monthView,
|
||||||
array $queryTransforms = []
|
array $queryTransforms = []
|
||||||
): array {
|
): array {
|
||||||
$query = $this->connection->getQueryBuilder();
|
$query = $this->connection->getQueryBuilder();
|
||||||
|
@ -51,7 +53,8 @@ trait TimelineQueryDays
|
||||||
// FETCH all days
|
// FETCH all days
|
||||||
$rows = $this->executeQueryWithCTEs($query)->fetchAll();
|
$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
|
// Convert monthIds to dayIds
|
||||||
$query->andWhere($query->expr()->orX(...array_map(fn ($monthId) => $query->expr()->andX(
|
$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()->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)));
|
), $dayIds)));
|
||||||
} else {
|
} else {
|
||||||
// Filter by list of dayIds
|
// Filter by list of dayIds
|
||||||
|
@ -129,7 +132,7 @@ trait TimelineQueryDays
|
||||||
|
|
||||||
// Post process the day in-place
|
// Post process the day in-place
|
||||||
foreach ($day as &$photo) {
|
foreach ($day as &$photo) {
|
||||||
$this->processDayPhoto($photo, $monthView);
|
$this->postProcessDayPhoto($photo, $monthView);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $day;
|
return $day;
|
||||||
|
@ -220,25 +223,41 @@ trait TimelineQueryDays
|
||||||
/**
|
/**
|
||||||
* Process the days response.
|
* 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['dayid'] = (int) $row['dayid'];
|
||||||
$row['count'] = (int) $row['count'];
|
$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.
|
* 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
|
* @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
|
// Convert field types
|
||||||
$row['fileid'] = (int) $row['fileid'];
|
$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);
|
return (int) (strtotime(date('Ymt', $monthId * 86400)) / 86400);
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ trait TimelineQuerySingleItem
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post process the record
|
// Post process the record
|
||||||
$this->processDayPhoto($photo);
|
$this->postProcessDayPhoto($photo);
|
||||||
|
|
||||||
return $photo;
|
return $photo;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue