From aaa90ac88222567aaf10e67e7548791e747f8d6d Mon Sep 17 00:00:00 2001 From: Loc Nguyen Date: Thu, 24 Feb 2022 11:51:31 +0700 Subject: [PATCH 1/7] Fix N+1 frequencies relation at tasks dashboard (#315) * fixbug issue 309 * styleci fix --- src/Http/Controllers/TasksController.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Http/Controllers/TasksController.php b/src/Http/Controllers/TasksController.php index 6e3e48f4..960d1208 100644 --- a/src/Http/Controllers/TasksController.php +++ b/src/Http/Controllers/TasksController.php @@ -18,7 +18,7 @@ class TasksController extends Controller /** * TasksController constructor. * - * @param TaskInterface $tasks + * @param TaskInterface $tasks */ public function __construct(TaskInterface $tasks) { @@ -45,6 +45,7 @@ public function index() ->when(request('q'), function (Builder $query) { $query->where('description', 'LIKE', '%'.request('q').'%'); }) + ->with('frequencies') ->paginate(20), ]); } @@ -71,7 +72,7 @@ public function create() /** * Store a newly created task in storage. * - * @param TaskRequest $request + * @param TaskRequest $request * @return \Illuminate\Http\RedirectResponse */ public function store(TaskRequest $request) @@ -119,7 +120,7 @@ public function edit(Task $task) /** * Update the specified task in storage. * - * @param TaskRequest $request + * @param TaskRequest $request * @param $task * @return \Illuminate\Http\RedirectResponse */ From 9e93d1c275af53c49acf75f04583c258938f2621 Mon Sep 17 00:00:00 2001 From: Jeremy Smith Date: Mon, 7 Mar 2022 13:46:25 +0100 Subject: [PATCH 2/7] Optimisation for AVG run time (#317) Hi ! If you have a task with a lot of results, the average run time calculation take too long... it's now fixed using the relation and not loading all results ! --- resources/views/tasks/view.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/tasks/view.blade.php b/resources/views/tasks/view.blade.php index a30f96e1..7bb535da 100644 --- a/resources/views/tasks/view.blade.php +++ b/resources/views/tasks/view.blade.php @@ -55,7 +55,7 @@
  • Average Run Time - {{$task->results->count() > 0 ? number_format( $task->results->sum('duration') / (1000 * $task->results->count()) , 2) : '0'}} seconds + {{$task->results()->count() > 0 ? number_format( $task->results()->sum('duration') / (1000 * $task->results()->count()) , 2) : '0'}} seconds
  • Next Run Schedule From 7d159dfa02d8b02a5eb464d73e9dcd309e2ba6f2 Mon Sep 17 00:00:00 2001 From: Lucas Graciano Date: Tue, 15 Mar 2022 06:24:17 -0700 Subject: [PATCH 3/7] TaskResults: Improve cleanup & fetching (#320) * Task\autoCleanup: Delete in chunks * TaskResults: Add index to created_at --- ..._results_table_add_index_on_created_at.php | 34 +++++++++++++++++++ src/Task.php | 23 ++++++++----- 2 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 database/migrations/2022_03_14_120000_alter_task_results_table_add_index_on_created_at.php diff --git a/database/migrations/2022_03_14_120000_alter_task_results_table_add_index_on_created_at.php b/database/migrations/2022_03_14_120000_alter_task_results_table_add_index_on_created_at.php new file mode 100644 index 00000000..b7b96278 --- /dev/null +++ b/database/migrations/2022_03_14_120000_alter_task_results_table_add_index_on_created_at.php @@ -0,0 +1,34 @@ +table(TOTEM_TABLE_PREFIX.'task_results', function (Blueprint $table) { + $table->index('created_at'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::connection(TOTEM_DATABASE_CONNECTION) + ->table(TOTEM_TABLE_PREFIX.'task_results', function (Blueprint $table) { + $table->dropIndex('created_at'); + }); + } +} diff --git a/src/Task.php b/src/Task.php index 19b9bb93..4515d2e2 100644 --- a/src/Task.php +++ b/src/Task.php @@ -74,8 +74,7 @@ public function getUpcomingAttribute() /** * Convert a string of command arguments and options to an array. * - * @param bool $console if true will convert arguments to non associative array - * + * @param bool $console if true will convert arguments to non associative array * @return array */ public function compileParameters($console = false) @@ -198,13 +197,21 @@ public function autoCleanup() ->limit($this->auto_cleanup_num) ->get() ->min('id'); - self::results() - ->where('id', '<', $oldest_id) - ->delete(); + do { + $rowsDeleted = self::results() + ->where('id', '<', $oldest_id) + ->limit(500) + ->getQuery() + ->delete(); + } while ($rowsDeleted > 0); } else { - self::results() - ->where('ran_at', '<', Carbon::now()->subDays($this->auto_cleanup_num - 1)) - ->delete(); + do { + $rowsDeleted = self::results() + ->where('ran_at', '<', Carbon::now()->subDays($this->auto_cleanup_num - 1)) + ->limit(500) + ->getQuery() + ->delete(); + } while ($rowsDeleted > 0); } } } From 41c588b9b214a24045311d3155b2ee16f3fc579c Mon Sep 17 00:00:00 2001 From: Quentin Schmick Date: Mon, 21 Mar 2022 21:15:13 -0400 Subject: [PATCH 4/7] Updated to pluck ids and perform delete using the ids (#322) * Updated to pluck ids and perform delete using the ids * fixing style --- src/Task.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Task.php b/src/Task.php index 4515d2e2..12a066e3 100644 --- a/src/Task.php +++ b/src/Task.php @@ -192,26 +192,36 @@ public function autoCleanup() { if ($this->auto_cleanup_num > 0) { if ($this->auto_cleanup_type === 'results') { - $oldest_id = self::results() + $oldest_id = $this->results() ->orderBy('ran_at', 'desc') ->limit($this->auto_cleanup_num) ->get() ->min('id'); do { - $rowsDeleted = self::results() + $rowsToDelete = $this->results() ->where('id', '<', $oldest_id) - ->limit(500) + ->limit(50) ->getQuery() + ->select('id') + ->pluck('id'); + + Result::query() + ->whereIn('id', $rowsToDelete) ->delete(); - } while ($rowsDeleted > 0); + } while ($rowsToDelete > 0); } else { do { - $rowsDeleted = self::results() + $rowsToDelete = $this->results() ->where('ran_at', '<', Carbon::now()->subDays($this->auto_cleanup_num - 1)) - ->limit(500) + ->limit(50) ->getQuery() + ->select('id') + ->pluck('id'); + + Result::query() + ->whereIn('id', $rowsToDelete) ->delete(); - } while ($rowsDeleted > 0); + } while ($rowsToDelete > 0); } } } From 60cb88f1efaed983c9f72680d18e179fe4476a0c Mon Sep 17 00:00:00 2001 From: Quentin Schmick Date: Mon, 21 Mar 2022 22:31:31 -0400 Subject: [PATCH 5/7] Updated to use collection count (#324) * Updated to use collection count * Apply fixes from StyleCI (#325) Co-authored-by: Roshan Gautam Co-authored-by: Roshan Gautam Co-authored-by: Roshan Gautam --- ..._09_25_103421_update_task_results_duration_type.php | 2 +- src/Console/Commands/ListSchedule.php | 2 +- src/Contracts/TaskInterface.php | 6 ++++-- src/Events/Creating.php | 2 +- src/Events/Deleted.php | 4 ++-- src/Events/Executed.php | 4 ++-- src/Events/TaskEvent.php | 2 +- src/Events/Updating.php | 4 ++-- src/Http/Controllers/ActiveTasksController.php | 2 +- src/Http/Controllers/ExecuteTasksController.php | 2 +- src/Http/Controllers/ExportTasksController.php | 3 ++- src/Http/Controllers/ImportTasksController.php | 7 +++++-- src/Http/Requests/ImportRequest.php | 4 +++- src/Listeners/BuildCache.php | 2 +- src/Listeners/BustCache.php | 2 +- src/Listeners/BustCacheImmediately.php | 4 ++-- src/Listeners/Listener.php | 4 ++-- src/Repositories/EloquentTaskRepository.php | 10 +++++----- src/Task.php | 4 ++-- src/Totem.php | 2 -- src/Traits/FrontendSortable.php | 7 +++---- tests/Feature/ViewDashboardTest.php | 4 ++-- 22 files changed, 44 insertions(+), 39 deletions(-) diff --git a/database/migrations/2019_09_25_103421_update_task_results_duration_type.php b/database/migrations/2019_09_25_103421_update_task_results_duration_type.php index eb07d72e..0c91404a 100644 --- a/database/migrations/2019_09_25_103421_update_task_results_duration_type.php +++ b/database/migrations/2019_09_25_103421_update_task_results_duration_type.php @@ -28,7 +28,7 @@ public function down() } /** - * @param bool $toFloat + * @param bool $toFloat */ private function migrateDurationValues(bool $toFloat = true) { diff --git a/src/Console/Commands/ListSchedule.php b/src/Console/Commands/ListSchedule.php index 0be8fafb..2fda418f 100644 --- a/src/Console/Commands/ListSchedule.php +++ b/src/Console/Commands/ListSchedule.php @@ -32,7 +32,7 @@ class ListSchedule extends Command /** * Create a new command instance. * - * @param Schedule $schedule + * @param Schedule $schedule * @return void */ public function __construct(Schedule $schedule) diff --git a/src/Contracts/TaskInterface.php b/src/Contracts/TaskInterface.php index d2b8efb7..e85cb72a 100644 --- a/src/Contracts/TaskInterface.php +++ b/src/Contracts/TaskInterface.php @@ -21,12 +21,14 @@ public function find($id); /** * Returns all tasks. + * * @return \Illuminate\Database\Eloquent\Collection */ public function findAll(); /** * Returns all active tasks. + * * @return \Illuminate\Database\Eloquent\Collection */ public function findAllActive(); @@ -34,7 +36,7 @@ public function findAllActive(); /** * Creates a new task with the given data. * - * @param array $input + * @param array $input * @return \Studio\Totem\Task */ public function store(array $input); @@ -42,7 +44,7 @@ public function store(array $input); /** * Updates the given task with the given data. * - * @param array $input + * @param array $input * @param \Studio\Totem\Task $task * @return \Studio\Totem\Task */ diff --git a/src/Events/Creating.php b/src/Events/Creating.php index b179055d..53d0b6f8 100644 --- a/src/Events/Creating.php +++ b/src/Events/Creating.php @@ -21,7 +21,7 @@ class Creating implements ShouldBroadcast /** * Create a new event instance. * - * @param array $input + * @param array $input */ public function __construct(array $input) { diff --git a/src/Events/Deleted.php b/src/Events/Deleted.php index 0b3d590a..51eefd5f 100644 --- a/src/Events/Deleted.php +++ b/src/Events/Deleted.php @@ -7,8 +7,8 @@ class Deleted extends Event /** * Create a new event instance. * - * @param array $input - * @param Task $task + * @param array $input + * @param Task $task */ public function __construct() { diff --git a/src/Events/Executed.php b/src/Events/Executed.php index 666b7c2a..a0eec41e 100644 --- a/src/Events/Executed.php +++ b/src/Events/Executed.php @@ -10,8 +10,8 @@ class Executed extends BroadcastingEvent /** * Executed constructor. * - * @param Task $task - * @param string $started + * @param Task $task + * @param string $started */ public function __construct(Task $task, $started, $output) { diff --git a/src/Events/TaskEvent.php b/src/Events/TaskEvent.php index c64c85af..4b88cdd0 100644 --- a/src/Events/TaskEvent.php +++ b/src/Events/TaskEvent.php @@ -18,7 +18,7 @@ class TaskEvent extends Event /** * Constructor. * - * @param Task $task + * @param Task $task */ public function __construct(Task $task) { diff --git a/src/Events/Updating.php b/src/Events/Updating.php index 95ff3b8c..d53c9e8c 100644 --- a/src/Events/Updating.php +++ b/src/Events/Updating.php @@ -16,8 +16,8 @@ class Updating extends BroadcastingEvent /** * Create a new event instance. * - * @param array $input - * @param Task $task + * @param array $input + * @param Task $task */ public function __construct(array $input, Task $task) { diff --git a/src/Http/Controllers/ActiveTasksController.php b/src/Http/Controllers/ActiveTasksController.php index 2e93c1eb..779c181e 100644 --- a/src/Http/Controllers/ActiveTasksController.php +++ b/src/Http/Controllers/ActiveTasksController.php @@ -13,7 +13,7 @@ class ActiveTasksController extends Controller private $tasks; /** - * @param TaskInterface $tasks + * @param TaskInterface $tasks */ public function __construct(TaskInterface $tasks) { diff --git a/src/Http/Controllers/ExecuteTasksController.php b/src/Http/Controllers/ExecuteTasksController.php index 291d1e99..1b97e456 100644 --- a/src/Http/Controllers/ExecuteTasksController.php +++ b/src/Http/Controllers/ExecuteTasksController.php @@ -13,7 +13,7 @@ class ExecuteTasksController extends Controller private $tasks; /** - * @param TaskInterface $tasks + * @param TaskInterface $tasks */ public function __construct(TaskInterface $tasks) { diff --git a/src/Http/Controllers/ExportTasksController.php b/src/Http/Controllers/ExportTasksController.php index 3d4373dd..b9e22972 100644 --- a/src/Http/Controllers/ExportTasksController.php +++ b/src/Http/Controllers/ExportTasksController.php @@ -13,7 +13,8 @@ class ExportTasksController extends Controller /** * ExportTasksController constructor. - * @param TaskInterface $tasks + * + * @param TaskInterface $tasks */ public function __construct(TaskInterface $tasks) { diff --git a/src/Http/Controllers/ImportTasksController.php b/src/Http/Controllers/ImportTasksController.php index f6e44d3e..6bfa813b 100644 --- a/src/Http/Controllers/ImportTasksController.php +++ b/src/Http/Controllers/ImportTasksController.php @@ -14,7 +14,8 @@ class ImportTasksController extends Controller /** * ImportTasksController constructor. - * @param TaskInterface $tasks + * + * @param TaskInterface $tasks */ public function __construct(TaskInterface $tasks) { @@ -25,7 +26,9 @@ public function __construct(TaskInterface $tasks) /** * Import tasks from a json file. - * @param \Studio\Totem\Http\Requests\ImportRequest $request + * + * @param \Studio\Totem\Http\Requests\ImportRequest $request + * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function index(ImportRequest $request) diff --git a/src/Http/Requests/ImportRequest.php b/src/Http/Requests/ImportRequest.php index 6fb2a7dd..c5ffd34d 100644 --- a/src/Http/Requests/ImportRequest.php +++ b/src/Http/Requests/ImportRequest.php @@ -51,6 +51,7 @@ public function messages() * * @param array|mixed $keys * @return array + * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function all($keys = null) @@ -68,6 +69,7 @@ public function all($keys = null) * Get the validated data from the request. * * @return array + * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function validated() @@ -84,7 +86,7 @@ public function validated() /** * * Handle a failed validation attempt. * - * @param Validator $validator + * @param Validator $validator */ protected function failedValidation(Validator $validator) { diff --git a/src/Listeners/BuildCache.php b/src/Listeners/BuildCache.php index c34c5368..5ba060d3 100644 --- a/src/Listeners/BuildCache.php +++ b/src/Listeners/BuildCache.php @@ -19,7 +19,7 @@ public function handle(Event $event) /** * Rebuild Cache. * - * @param Event $event + * @param Event $event */ protected function build(Event $event) { diff --git a/src/Listeners/BustCache.php b/src/Listeners/BustCache.php index 12afda2d..13abf5e0 100644 --- a/src/Listeners/BustCache.php +++ b/src/Listeners/BustCache.php @@ -19,7 +19,7 @@ public function handle(Event $event) /** * Clear Cache. * - * @param Event $event + * @param Event $event */ protected function clear(Event $event) { diff --git a/src/Listeners/BustCacheImmediately.php b/src/Listeners/BustCacheImmediately.php index 5f3f17b9..79652a67 100644 --- a/src/Listeners/BustCacheImmediately.php +++ b/src/Listeners/BustCacheImmediately.php @@ -15,7 +15,7 @@ class BustCacheImmediately /** * Create the event listener. * - * @param Container $app + * @param Container $app */ public function __construct(Container $app) { @@ -35,7 +35,7 @@ public function handle(Event $event) /** * Clear Cache. * - * @param Event $event + * @param Event $event */ protected function clear(Event $event) { diff --git a/src/Listeners/Listener.php b/src/Listeners/Listener.php index db3fce85..52e685f4 100644 --- a/src/Listeners/Listener.php +++ b/src/Listeners/Listener.php @@ -21,8 +21,8 @@ class Listener implements ShouldQueue /** * Create the event listener. * - * @param Container $app - * @param TaskInterface $tasks + * @param Container $app + * @param TaskInterface $tasks */ public function __construct(Container $app, TaskInterface $tasks) { diff --git a/src/Repositories/EloquentTaskRepository.php b/src/Repositories/EloquentTaskRepository.php index 3fe9e899..bca6af65 100644 --- a/src/Repositories/EloquentTaskRepository.php +++ b/src/Repositories/EloquentTaskRepository.php @@ -44,7 +44,7 @@ public function builder(): Builder /** * Find a task by id. * - * @param int|Task $id + * @param int|Task $id * @return int|Task */ public function find($id) @@ -87,7 +87,7 @@ public function findAllActive() /** * Create a new task. * - * @param array $input + * @param array $input * @return bool|Task */ public function store(array $input) @@ -108,8 +108,8 @@ public function store(array $input) /** * Update the given task. * - * @param array $input - * @param Task $task + * @param array $input + * @param Task $task * @return bool|int|Task */ public function update(array $input, $task) @@ -130,7 +130,7 @@ public function update(array $input, $task) /** * Delete the given task. * - * @param int|Task $id + * @param int|Task $id * @return bool */ public function destroy($id) diff --git a/src/Task.php b/src/Task.php index 12a066e3..79bc536f 100644 --- a/src/Task.php +++ b/src/Task.php @@ -208,7 +208,7 @@ public function autoCleanup() Result::query() ->whereIn('id', $rowsToDelete) ->delete(); - } while ($rowsToDelete > 0); + } while ($rowsToDelete->count() > 0); } else { do { $rowsToDelete = $this->results() @@ -221,7 +221,7 @@ public function autoCleanup() Result::query() ->whereIn('id', $rowsToDelete) ->delete(); - } while ($rowsToDelete > 0); + } while ($rowsToDelete->count() > 0); } } } diff --git a/src/Totem.php b/src/Totem.php index 82de81ee..d03d3662 100644 --- a/src/Totem.php +++ b/src/Totem.php @@ -22,7 +22,6 @@ class Totem * Determine if the given request can access the Totem dashboard. * * @param \Illuminate\Http\Request $request - * * @return bool */ public static function check($request) @@ -36,7 +35,6 @@ public static function check($request) * Set the callback that should be used to authenticate Totem users. * * @param \Closure $callback - * * @return static */ public static function auth(Closure $callback) diff --git a/src/Traits/FrontendSortable.php b/src/Traits/FrontendSortable.php index d24bd5d7..59823cf5 100644 --- a/src/Traits/FrontendSortable.php +++ b/src/Traits/FrontendSortable.php @@ -7,10 +7,9 @@ trait FrontendSortable { /** - * @param \Illuminate\Database\Eloquent\Builder $builder - * @param array $sortableColumns - * @param array $defaultSort - * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @param array $sortableColumns + * @param array $defaultSort * @return \Illuminate\Database\Eloquent\Builder */ public function scopeSortableBy(Builder $builder, array $sortableColumns, array $defaultSort = ['name' => 'asc']): Builder diff --git a/tests/Feature/ViewDashboardTest.php b/tests/Feature/ViewDashboardTest.php index d9dd8b13..1edb8ba5 100644 --- a/tests/Feature/ViewDashboardTest.php +++ b/tests/Feature/ViewDashboardTest.php @@ -80,8 +80,8 @@ public function view_dashboard_multiple_tasks_with_multiple_results() } /** - * @param int $task_count - * @param int $result_count + * @param int $task_count + * @param int $result_count * @return mixed */ private function _get_task_with_results($task_count = 1, $result_count = 1) From 75e4b2dd84bf90679103cc05d8154d63129e2338 Mon Sep 17 00:00:00 2001 From: Alban Horrocks Date: Wed, 4 May 2022 09:42:58 +0100 Subject: [PATCH 6/7] style ci fixes --- src/Http/Requests/ImportRequest.php | 4 +++- src/Totem.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Http/Requests/ImportRequest.php b/src/Http/Requests/ImportRequest.php index 6fb2a7dd..532e6a4b 100644 --- a/src/Http/Requests/ImportRequest.php +++ b/src/Http/Requests/ImportRequest.php @@ -51,6 +51,7 @@ public function messages() * * @param array|mixed $keys * @return array + * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function all($keys = null) @@ -68,6 +69,7 @@ public function all($keys = null) * Get the validated data from the request. * * @return array + * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function validated() @@ -84,7 +86,7 @@ public function validated() /** * * Handle a failed validation attempt. * - * @param Validator $validator + * @param Validator $validator */ protected function failedValidation(Validator $validator) { diff --git a/src/Totem.php b/src/Totem.php index 82de81ee..6fcfb692 100644 --- a/src/Totem.php +++ b/src/Totem.php @@ -35,7 +35,7 @@ public static function check($request) /** * Set the callback that should be used to authenticate Totem users. * - * @param \Closure $callback + * @param Closure $callback * * @return static */ From d2617b887961abba32441a777d04f6eb1f87f5f6 Mon Sep 17 00:00:00 2001 From: Alban Horrocks Date: Wed, 4 May 2022 09:49:23 +0100 Subject: [PATCH 7/7] more style ci fixes --- src/Http/Requests/ImportRequest.php | 2 +- src/Totem.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Http/Requests/ImportRequest.php b/src/Http/Requests/ImportRequest.php index f72ca898..c5ffd34d 100644 --- a/src/Http/Requests/ImportRequest.php +++ b/src/Http/Requests/ImportRequest.php @@ -69,7 +69,7 @@ public function all($keys = null) * Get the validated data from the request. * * @return array - * + * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function validated() diff --git a/src/Totem.php b/src/Totem.php index 99e39281..6b9f6362 100644 --- a/src/Totem.php +++ b/src/Totem.php @@ -35,7 +35,6 @@ public static function check($request) * Set the callback that should be used to authenticate Totem users. * * @param Closure $callback - * * @return static */ public static function auth(Closure $callback)