From f0ef9a5602d98f912beb34cb2e2fcfacf36c8acb Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Sat, 30 Dec 2023 19:13:40 +0800 Subject: [PATCH] Proof of concept - Vue Polling - Performance Improvement and tweaking for better user experience --- .../Processors/FetchesJobResultProcessor.php | 8 +++ .../Processors/UpdateJobResultProcessor.php | 64 +++++++++++++++++++ .../ListPackingListsJobProcessor.php | 46 +++---------- 3 files changed, 80 insertions(+), 38 deletions(-) create mode 100644 app/Classes/Modules/Jobs/Processors/UpdateJobResultProcessor.php diff --git a/app/Classes/Modules/Jobs/Processors/FetchesJobResultProcessor.php b/app/Classes/Modules/Jobs/Processors/FetchesJobResultProcessor.php index dfaea815..e8c02934 100644 --- a/app/Classes/Modules/Jobs/Processors/FetchesJobResultProcessor.php +++ b/app/Classes/Modules/Jobs/Processors/FetchesJobResultProcessor.php @@ -21,6 +21,14 @@ class FetchesJobResultProcessor $this->fetchesJobResult = $fetchesJobResult; } + + /** + * @param Request $request + * @return Model + * @throws \App\Classes\Exceptions\MalformedRequestException + * @throws \App\Classes\Exceptions\JobResourceNotFoundException + * @throws \App\Classes\Exceptions\ResourceNotFoundException + */ public function execute(Request $request){ $res1 = $this->fetchesJobResult->execute(['job_id' => $request->route('job_id')]); diff --git a/app/Classes/Modules/Jobs/Processors/UpdateJobResultProcessor.php b/app/Classes/Modules/Jobs/Processors/UpdateJobResultProcessor.php new file mode 100644 index 00000000..fff83bb0 --- /dev/null +++ b/app/Classes/Modules/Jobs/Processors/UpdateJobResultProcessor.php @@ -0,0 +1,64 @@ +fetchesJobResult = $fetchesJobResult; + $this->updatesJobResult = $updatesJobResult; + } + + /** + * @param ListGenericJobObject $listGenericJobObject + * @param array $resultCurrent + * @return void + * @throws \App\Classes\Exceptions\MalformedRequestException + * @throws \App\Classes\Exceptions\JobResourceNotFoundException + */ + public function execute(ListGenericJobObject $listGenericJobObject, $resultCurrent) { + $jobResultCurrent = $this->fetchesJobResult->execute(['job_id' => $listGenericJobObject->getJobId()]); + $resultCurrentJson = json_encode($resultCurrent); + $resultSignatureCurrent = md5($resultCurrentJson); + + try{ + $jobResultExisting = $this->fetchesJobResult->execute(['request_signature' => $jobResultCurrent->request_signature, 'result_not_null' => true, 'order_by_id_desc' => true]); + $resultSignatureExisting = $jobResultExisting->result_signature; + if($resultSignatureExisting != $resultSignatureCurrent){ + $this->updateJobResult($jobResultCurrent, $resultCurrentJson, $resultSignatureCurrent, $listGenericJobObject->getJobCommandName(), $listGenericJobObject->getJobCommand()); + } + } catch (JobResourceNotFoundException $exception){ + $this->updateJobResult($jobResultCurrent, $resultCurrentJson, $resultSignatureCurrent, $listGenericJobObject->getJobCommandName(), $listGenericJobObject->getJobCommand()); + } + } + + private function updateJobResult($jobResultCurrent, $resultCurrentJson, $resultSignatureCurrent, $jobCommandName, $jobCommand){ + $updateJobResultObject = new UpdateJobResultObject( + $resultCurrentJson, + $resultSignatureCurrent, + $jobCommandName, + $jobCommand + ); + $create = $this->updatesJobResult->execute($jobResultCurrent, $updateJobResultObject); + } +} diff --git a/app/Classes/Modules/PackingLists/Processors/ListPackingListsJobProcessor.php b/app/Classes/Modules/PackingLists/Processors/ListPackingListsJobProcessor.php index 0849fabc..4e67f7a1 100644 --- a/app/Classes/Modules/PackingLists/Processors/ListPackingListsJobProcessor.php +++ b/app/Classes/Modules/PackingLists/Processors/ListPackingListsJobProcessor.php @@ -2,13 +2,11 @@ namespace App\Classes\Modules\PackingLists\Processors; -use App\Classes\Exceptions\JobResourceNotFoundException; + use App\Classes\Modules\PackingLists\Services\ListsPackingLists; -use App\Classes\Modules\Jobs\Services\UpdatesJobResult; -use App\Classes\Modules\Jobs\Services\FetchesJobResult; +use App\Classes\Modules\Jobs\Processors\UpdateJobResultProcessor; use App\Classes\General\Helper; use App\Classes\Modules\Jobs\DataTransferObjects\ListGenericJobObject; -use App\Classes\Modules\Jobs\DataTransferObjects\UpdateJobResultObject; use App\Http\Resources\ListPackingListJobResource; class ListPackingListsJobProcessor @@ -17,23 +15,18 @@ class ListPackingListsJobProcessor /** @var ListsPackingLists */ private $listsPackingLists; - /** @var FetchesJobResult */ - private $fetchesJobResult; - - /** @var UpdatesJobResult */ - private $updatesJobResult; + /** @var UpdateJobResultProcessor */ + private $updateJobResultProcessor; /** * ListPackingListsJobProcessor constructor. * @param ListsPackingLists $listsPackingLists - * @param FetchesJobResult $fetchesJobResult - * @param UpdatesJobResult $updatesJobResult + * @param UpdateJobResultProcessor $updateJobResultProcessor */ - public function __construct(ListsPackingLists $listsPackingLists, FetchesJobResult $fetchesJobResult, UpdatesJobResult $updatesJobResult) + public function __construct(ListsPackingLists $listsPackingLists, UpdateJobResultProcessor $updateJobResultProcessor) { $this->listsPackingLists = $listsPackingLists; - $this->fetchesJobResult = $fetchesJobResult; - $this->updatesJobResult = $updatesJobResult; + $this->updateJobResultProcessor = $updateJobResultProcessor; } /** @@ -49,29 +42,6 @@ class ListPackingListsJobProcessor $item['userInfo'] = $listGenericJobObject->getUserInfo(); } $resultCurrent = Helper::collectionResponse(ListPackingListJobResource::collection($query)); - - $jobResultCurrent = $this->fetchesJobResult->execute(['job_id' => $listGenericJobObject->getJobId()]); - $resultCurrentJson = json_encode($resultCurrent); - $resultSignatureCurrent = md5($resultCurrentJson); - - try{ - $jobResultExisting = $this->fetchesJobResult->execute(['request_signature' => $jobResultCurrent->request_signature, 'result_not_null' => true, 'order_by_id_desc' => true]); - $resultSignatureExisting = $jobResultExisting->result_signature; - if($resultSignatureExisting != $resultSignatureCurrent){ - $this->updateJobResult($jobResultCurrent, $resultCurrentJson, $resultSignatureCurrent, $listGenericJobObject->getJobCommandName(), $listGenericJobObject->getJobCommand()); - } - } catch (JobResourceNotFoundException $exception){ - $this->updateJobResult($jobResultCurrent, $resultCurrentJson, $resultSignatureCurrent, $listGenericJobObject->getJobCommandName(), $listGenericJobObject->getJobCommand()); - } - } - - private function updateJobResult($jobResultCurrent, $resultCurrentJson, $resultSignatureCurrent, $jobCommandName, $jobCommand){ - $updateJobResultObject = new UpdateJobResultObject( - $resultCurrentJson, - $resultSignatureCurrent, - $jobCommandName, - $jobCommand - ); - $create = $this->updatesJobResult->execute($jobResultCurrent, $updateJobResultObject); + $this->updateJobResultProcessor->execute($listGenericJobObject, $resultCurrent); } }