Add strategy for more efficient pagination in API listing of entries by using before_id and after_id wheres instead of pagination if possible

This commit is contained in:
osaajani 2024-12-20 23:32:56 +01:00
parent 04b0f1267a
commit 0e11bcda17
10 changed files with 156 additions and 28 deletions

View file

@ -26,10 +26,13 @@ namespace models;
* @param int $id_user : user id
* @param ?int $limit : Number of entry to return or null
* @param ?int $offset : Number of entry to ignore or null
* @param ?int $after_id : If provided use where id > $after_id instead of offset
* @param ?int $before_id : If provided use where id < $before_id instead of offset
*
*
* @return array
*/
public function list_for_user(int $id_user, $limit, $offset)
public function list_for_user(int $id_user, $limit, $offset, ?int $after_id = null, ?int $before_id = null)
{
$query = '
SELECT `call`.*, contact.name as contact_name, phone.name as phone_name
@ -42,6 +45,37 @@ namespace models;
WHERE `call`.id_user = :id_user
';
$params = [
'id_user' => $id_user,
];
if ($after_id || $before_id)
{
$offset = null;
}
if ($after_id)
{
$query .= '
AND `call`.id > :after_id
ORDER BY `call`.id
';
$params['after_id'] = $after_id;
}
elseif ($before_id)
{
$query .= '
AND `call`.id < :before_id
ORDER BY `call`.id DESC
';
$params['before_id'] = $before_id;
}
else
{
$query .= ' ORDER BY `call`.id';
}
if (null !== $limit)
{
$limit = (int) $limit;
@ -54,9 +88,10 @@ namespace models;
}
}
$params = [
'id_user' => $id_user,
];
if ($before_id)
{
return array_reverse($this->_run_query($query, $params));
}
return $this->_run_query($query, $params);
}

View file

@ -20,10 +20,12 @@ namespace models;
* @param int $id_user : user id
* @param ?int $limit : Number of entry to return or null
* @param ?int $offset : Number of entry to ignore or null
* @param ?int $after_id : If provided use where id > $after_id instead of offset
* @param ?int $before_id : If provided use where id < $before_id instead of offset
*
* @return array
*/
public function list_for_user(int $id_user, $limit, $offset)
public function list_for_user(int $id_user, $limit, $offset, ?int $after_id = null, ?int $before_id = null)
{
$query = '
SELECT g.*, COUNT(gc.id) as nb_contact
@ -31,9 +33,43 @@ namespace models;
LEFT JOIN group_contact as gc
ON g.id = gc.id_group
WHERE id_user = :id_user
GROUP BY g.id
';
$params = [
'id_user' => $id_user,
];
if ($after_id || $before_id)
{
$offset = null;
}
if ($after_id)
{
$query .= '
AND g.id > :after_id
GROUP BY g.id
ORDER BY g.id
';
$params['after_id'] = $after_id;
}
elseif ($before_id)
{
$query .= '
AND g.id < :before_id
GROUP BY g.id
ORDER BY g.id DESC
';
$params['before_id'] = $before_id;
}
else
{
$query .= '
GROUP BY g.id
ORDER BY g.id
';
}
if (null !== $limit)
{
$limit = (int) $limit;
@ -46,10 +82,11 @@ namespace models;
}
}
$params = [
'id_user' => $id_user,
];
if ($before_id)
{
return array_reverse($this->_run_query($query, $params));
}
return $this->_run_query($query, $params);
}

View file

@ -20,10 +20,12 @@ namespace models;
* @param int $id_user : user id
* @param ?int $limit : Number of entry to return or null
* @param ?int $offset : Number of entry to ignore or null
* @param ?int $after_id : If provided use where id > $after_id instead of offset
* @param ?int $before_id : If provided use where id < $before_id instead of offset
*
* @return array
*/
public function list_for_user(int $id_user, $limit, $offset)
public function list_for_user(int $id_user, $limit, $offset, ?int $after_id = null, ?int $before_id = null)
{
$query = '
SELECT pg.*, COUNT(pgp.id) as nb_phone
@ -31,9 +33,43 @@ namespace models;
LEFT JOIN phone_group_phone as pgp
ON pg.id = pgp.id_phone_group
WHERE pg.id_user = :id_user
GROUP BY pg.id
';
$params = [
'id_user' => $id_user,
];
if ($after_id || $before_id)
{
$offset = null;
}
if ($after_id)
{
$query .= '
AND pg.id > :after_id
GROUP BY pg.id
ORDER BY g.id
';
$params['after_id'] = $after_id;
}
elseif ($before_id)
{
$query .= '
AND pg.id < :before_id
GROUP BY pg.id
ORDER BY g.id DESC
';
$params['before_'] = $before_id;
}
else
{
$query .= '
GROUP BY pg.id
ORDER BY pg.id
';
}
if (null !== $limit)
{
$limit = (int) $limit;
@ -46,9 +82,10 @@ namespace models;
}
}
$params = [
'id_user' => $id_user,
];
if ($before_id)
{
return array_reverse($this->_run_query($query, $params));
}
return $this->_run_query($query, $params);
}

View file

@ -70,7 +70,7 @@ class RedisQueue implements Queue
*
* @return mixed $message : The oldest message or null if no message found, can be anything
* @param ?string $tag : A tag to associate to the message for routing purposes, if null will read from general queue
* @param mixed : The message to add to the queue, can be anything, the queue will have to treat it by itself
* @param mixed : The message return from the queue, can be anything, null if no message found
*/
public function read(?string $tag = null)
{

View file

@ -70,12 +70,22 @@ namespace models;
* @param int $id_user : user id
* @param int $limit : Number of entry to return
* @param int $offset : Number of entry to ignore
* @param ?int $after_id : If provided use where id > $after_id instead of offset
* @param ?int $before_id : If provided use where id < $before_id instead of offset
*
* @return array
*/
public function list_for_user(int $id_user, $limit, $offset)
public function list_for_user(int $id_user, $limit, $offset, ?int $after_id = null, ?int $before_id = null)
{
return $this->_select($this->get_table_name(), ['id_user' => $id_user], null, false, $limit, $offset);
if ($after_id !== null) {
return $this->_select($this->get_table_name(), ['id_user' => $id_user, '>id' => $after_id], 'id', false, $limit);
}
if ($before_id !== null) {
return array_reverse($this->_select($this->get_table_name(), ['id_user' => $id_user, '<id' => $before_id], 'id', true, $limit));
}
return $this->_select($this->get_table_name(), ['id_user' => $id_user], 'id', false, $limit, $offset);
}
/**