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

@ -66,7 +66,7 @@ class Queue extends \descartes\InternalController
*
* @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

@ -83,7 +83,7 @@ use Exception;
$this->bdd->beginTransaction();
$id_received = $this->get_model()->insert($received);
if (!$id_received)
if (!$id_received)
{
$this->bdd->rollBack();

View file

@ -77,12 +77,14 @@ namespace controllers\internals;
* @param int $id_user : User id
* @param ?int $nb_entry : Number of entry to return
* @param ?int $page : Pagination, used to calcul offset, $nb_entry * $page
* @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 : Entrys list
*/
public function list_for_user(int $id_user, ?int $nb_entry = null, ?int $page = null)
public function list_for_user(int $id_user, ?int $nb_entry = null, ?int $page = null, ?int $after_id = null, ?int $before_id = null)
{
return $this->get_model()->list_for_user($id_user, $nb_entry, $nb_entry * $page);
return $this->get_model()->list_for_user($id_user, $nb_entry, $nb_entry * $page, $after_id, $before_id);
}
/**

View file

@ -126,10 +126,13 @@ namespace controllers\publics;
*
* @param string $entry_type : Type of entries we want to list ['sended', 'received', 'scheduled', 'contact', 'group', 'conditional_group', 'phone', 'phone_group', 'media']
* @param int $page : Pagination number, Default = 0. Group of 25 results.
* @param ?int $after_id : If provided use where id > $after_id instead of offset based on page, more performant
* @param ?int $before_id : If provided use where id < $before_id instead of offset based on page, more performant
*
*
* @return : List of entries
*/
public function get_entries(string $entry_type, int $page = 0)
public function get_entries(string $entry_type, int $page = 0, ?int $after_id = null, ?int $before_id = null)
{
$entry_types = ['sended', 'received', 'scheduled', 'contact', 'group', 'conditional_group', 'phone', 'phone_group', 'media'];
@ -148,7 +151,7 @@ namespace controllers\publics;
$page = (int) $page;
$limit = 25;
$entries = $controller->list_for_user($this->user['id'], $limit, $page);
$entries = $controller->list_for_user($this->user['id'], $limit, $page, $after_id, $before_id);
//Special case for scheduled, we must add numbers because its a join
if ('scheduled' === $entry_type)
@ -221,14 +224,16 @@ namespace controllers\publics;
$return = self::DEFAULT_RETURN;
$return['response'] = $entries;
if (\count($entries) === $limit)
if (\count($entries) === $limit || ($entries && $before_id))
{
$return['next'] = \descartes\Router::url('Api', __FUNCTION__, ['entry_type' => $entry_type, 'page' => $page + 1], ['api_key' => $this->user['api_key']]);
$last_entry = end($entries);
$return['next'] = \descartes\Router::url('Api', __FUNCTION__, ['entry_type' => $entry_type, 'after_id' => $last_entry['id']], ['api_key' => $this->user['api_key']]);
}
if ($page > 0)
if ($page > 0 || ($entries && ($after_id || $before_id)))
{
$return['prev'] = \descartes\Router::url('Api', __FUNCTION__, ['entry_type' => $entry_type, 'page' => $page - 1], ['api_key' => $this->user['api_key']]);
$first_entry = $entries[0];
$return['prev'] = \descartes\Router::url('Api', __FUNCTION__, ['entry_type' => $entry_type, 'before_id' => $first_entry['id']], ['api_key' => $this->user['api_key']]);
}
$this->auto_http_code(true);