From b72df8c70b9ab69ebb361b523d05fdcbdd4b94e3 Mon Sep 17 00:00:00 2001 From: Alex Runyan Date: Tue, 30 Apr 2024 14:48:22 -0400 Subject: [PATCH 1/3] Added pagination to query for TaskController for better performance --- ProcessMaker/Http/Controllers/Api/TaskController.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Api/TaskController.php b/ProcessMaker/Http/Controllers/Api/TaskController.php index 27ed5a81d0..f8af123d3a 100644 --- a/ProcessMaker/Http/Controllers/Api/TaskController.php +++ b/ProcessMaker/Http/Controllers/Api/TaskController.php @@ -130,6 +130,9 @@ public function index(Request $request, $getTotal = false, User $user = null) // Apply filter overdue $query->overdue($request->input('overdue')); + // Apply pagination + $query->paginate($request->input('per_page', 10)); + try { $response = $query->get(); } catch (QueryException $e) { @@ -263,15 +266,15 @@ private function handleQueryException($e) { $regex = '~Column not found: 1054 Unknown column \'(.*?)\' in \'where clause\'~'; preg_match($regex, $e->getMessage(), $m); - + $message = __('PMQL Is Invalid.'); - + if (count($m) > 1) { $message .= ' ' . __('Column not found: ') . '"' . $m[1] . '"'; } - + \Log::error($e->getMessage()); - + return response([ 'message' => $message, ], 422); From b00c6d0a5e45bd9e4edb17c5373434e4814862cc Mon Sep 17 00:00:00 2001 From: Alex Runyan Date: Tue, 30 Apr 2024 23:08:06 -0400 Subject: [PATCH 2/3] Add pagination conditionally --- ProcessMaker/Http/Controllers/Api/TaskController.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Api/TaskController.php b/ProcessMaker/Http/Controllers/Api/TaskController.php index f8af123d3a..29a2ab70e2 100644 --- a/ProcessMaker/Http/Controllers/Api/TaskController.php +++ b/ProcessMaker/Http/Controllers/Api/TaskController.php @@ -98,7 +98,7 @@ class TaskController extends Controller * ), * ) */ - public function index(Request $request, $getTotal = false, User $user = null) + public function index(Request $request, $getTotal = false, User $user = null, $paginate = null) { // If a specific user is specified, use it; otherwise use the authorized user // This is necessary to produce accurate counts for Saved Searches @@ -130,8 +130,11 @@ public function index(Request $request, $getTotal = false, User $user = null) // Apply filter overdue $query->overdue($request->input('overdue')); - // Apply pagination - $query->paginate($request->input('per_page', 10)); + // If we should manually add pagination to the + // query in advance (also used by saved search) + if ($paginate) { + $query->paginate($request->input('per_page', 10)); + } try { $response = $query->get(); From fd625761dda827ff6d6b8ad0c422626361d6cb41 Mon Sep 17 00:00:00 2001 From: Alex Runyan Date: Wed, 1 May 2024 02:01:08 -0400 Subject: [PATCH 3/3] Conditional pagination --- .../Http/Controllers/Api/TaskController.php | 4 +-- .../Traits/TaskControllerIndexMethods.php | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Api/TaskController.php b/ProcessMaker/Http/Controllers/Api/TaskController.php index 29a2ab70e2..e9ea13f025 100644 --- a/ProcessMaker/Http/Controllers/Api/TaskController.php +++ b/ProcessMaker/Http/Controllers/Api/TaskController.php @@ -98,7 +98,7 @@ class TaskController extends Controller * ), * ) */ - public function index(Request $request, $getTotal = false, User $user = null, $paginate = null) + public function index(Request $request, $getTotal = false, User $user = null) { // If a specific user is specified, use it; otherwise use the authorized user // This is necessary to produce accurate counts for Saved Searches @@ -132,7 +132,7 @@ public function index(Request $request, $getTotal = false, User $user = null, $p // If we should manually add pagination to the // query in advance (also used by saved search) - if ($paginate) { + if ($this->isPaginationEnabled()) { $query->paginate($request->input('per_page', 10)); } diff --git a/ProcessMaker/Traits/TaskControllerIndexMethods.php b/ProcessMaker/Traits/TaskControllerIndexMethods.php index 81990fd4a9..5196dd2836 100644 --- a/ProcessMaker/Traits/TaskControllerIndexMethods.php +++ b/ProcessMaker/Traits/TaskControllerIndexMethods.php @@ -15,6 +15,36 @@ trait TaskControllerIndexMethods { + /** + * Manually enable paginated results from the + * index method() + * + * @return void + */ + public function enableIndexPagination(): void + { + static::$paginate = true; + } + + /** + * Determine if pagination was manually set for the + * index() method results + * + * @return bool + */ + public function isPaginationEnabled(): bool + { + return static::$paginate === true; + } + + /** + * Used by saved search to paginate the results + * from the index() method + * + * @var bool + */ + protected static bool $paginate = false; + private function indexBaseQuery($request) { $query = ProcessRequestToken::with(['processRequest', 'user', 'draft']);