From b40c0d7c4ed72396d562b4d3d4f2b51c8a0b9d10 Mon Sep 17 00:00:00 2001 From: Limber Mamani Date: Tue, 17 Dec 2024 11:46:13 -0400 Subject: [PATCH] [TM-1466] remove cached functinality from Redis --- ...GetPolygonsIndicatorAnalysisController.php | 24 +++++++++---------- .../RunIndicatorAnalysisController.php | 22 +++++++---------- app/Jobs/RunIndicatorAnalysisJob.php | 5 +--- app/Services/RunIndicatorAnalysisService.php | 6 ++--- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/app/Http/Controllers/V2/MonitoredData/GetPolygonsIndicatorAnalysisController.php b/app/Http/Controllers/V2/MonitoredData/GetPolygonsIndicatorAnalysisController.php index 85d6ee58..73961c48 100644 --- a/app/Http/Controllers/V2/MonitoredData/GetPolygonsIndicatorAnalysisController.php +++ b/app/Http/Controllers/V2/MonitoredData/GetPolygonsIndicatorAnalysisController.php @@ -78,7 +78,7 @@ public function __invoke(EntityModel $entity, string $slug) 'status' => $polygon->status, 'plantstart' => $polygon->plantstart ?? '-', 'site_name' => $polygon->site->name ?? '-', - 'size' => round($polygon->calc_area ?? 0, 3), + 'size' => round($polygon->calc_area ?? 0, 1), 'indicator_slug' => $indicator->indicator_slug, 'year_of_analysis' => $indicator->year_of_analysis, 'created_at' => $indicator->created_at, @@ -87,16 +87,16 @@ public function __invoke(EntityModel $entity, string $slug) ]; if (str_contains($slug, 'treeCoverLoss')) { $valueYears = json_decode($indicator->value, true); - $results['data']['2015'] = round((float) $valueYears['2015'], 3); - $results['data']['2016'] = round((float) $valueYears['2016'], 3); - $results['data']['2017'] = round((float) $valueYears['2017'], 3); - $results['data']['2018'] = round((float) $valueYears['2018'], 3); - $results['data']['2019'] = round((float) $valueYears['2019'], 3); - $results['data']['2020'] = round((float) $valueYears['2020'], 3); - $results['data']['2021'] = round((float) $valueYears['2021'], 3); - $results['data']['2022'] = round((float) $valueYears['2022'], 3); - $results['data']['2023'] = round((float) $valueYears['2023'], 3); - $results['data']['2024'] = round((float) $valueYears['2024'], 3); + $results['data']['2015'] = round((float) $valueYears['2015'], 1); + $results['data']['2016'] = round((float) $valueYears['2016'], 1); + $results['data']['2017'] = round((float) $valueYears['2017'], 1); + $results['data']['2018'] = round((float) $valueYears['2018'], 1); + $results['data']['2019'] = round((float) $valueYears['2019'], 1); + $results['data']['2020'] = round((float) $valueYears['2020'], 1); + $results['data']['2021'] = round((float) $valueYears['2021'], 1); + $results['data']['2022'] = round((float) $valueYears['2022'], 1); + $results['data']['2023'] = round((float) $valueYears['2023'], 1); + $results['data']['2024'] = round((float) $valueYears['2024'], 1); } if ($slug == 'restorationByEcoRegion') { @@ -123,7 +123,7 @@ public function processValuesHectares($values) $array = explode(',', str_replace('-', '_', $key)); $arrayTrim = array_map('trim', $array); foreach ($arrayTrim as $item) { - $separateKeys[$item] = round((float) $value, 3); + $separateKeys[$item] = round((float) $value, 1); } } diff --git a/app/Http/Controllers/V2/MonitoredData/RunIndicatorAnalysisController.php b/app/Http/Controllers/V2/MonitoredData/RunIndicatorAnalysisController.php index 2a55ba64..08307b03 100644 --- a/app/Http/Controllers/V2/MonitoredData/RunIndicatorAnalysisController.php +++ b/app/Http/Controllers/V2/MonitoredData/RunIndicatorAnalysisController.php @@ -8,7 +8,6 @@ use App\Models\DelayedJob; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Redis; class RunIndicatorAnalysisController extends Controller { @@ -16,20 +15,15 @@ public function __invoke(Request $request, string $slug) { try { $requestData = $request->all(); - $binary_data = Redis::get('run:indicator|'.$slug.'|'.json_encode($requestData['uuids'])); - if (! $binary_data) { - $delayedJob = DelayedJob::create(); - $job = new RunIndicatorAnalysisJob( - $delayedJob->id, - $requestData, - $slug - ); - dispatch($job); + $delayedJob = DelayedJob::create(); + $job = new RunIndicatorAnalysisJob( + $delayedJob->id, + $requestData, + $slug + ); + dispatch($job); - return (new DelayedJobResource($delayedJob))->additional(['message' => 'Analysis for '.$slug.' is being processed']); - } else { - return response()->json(['message' => 'Analysis for '.$slug.' is already processed'], 200); - } + return (new DelayedJobResource($delayedJob))->additional(['message' => 'Analysis for '.$slug.' is being processed']); } catch (\Exception $e) { Log::error('Error during analysis for ' . $slug . ' : ' . $e->getMessage()); diff --git a/app/Jobs/RunIndicatorAnalysisJob.php b/app/Jobs/RunIndicatorAnalysisJob.php index aabc716d..b7aa76ef 100644 --- a/app/Jobs/RunIndicatorAnalysisJob.php +++ b/app/Jobs/RunIndicatorAnalysisJob.php @@ -12,7 +12,6 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Redis; class RunIndicatorAnalysisJob implements ShouldQueue { @@ -40,9 +39,7 @@ public function handle(RunIndicatorAnalysisService $runIndicatorAnalysisService) { try { $delayedJob = DelayedJob::findOrFail($this->delayed_job_id); - - $binary_data = $runIndicatorAnalysisService->run($this->request, $this->slug); - Redis::set('run:indicator|'.$this->slug.'|'.json_encode($this->request['uuids']), $binary_data); + $runIndicatorAnalysisService->run($this->request, $this->slug); $delayedJob->update([ 'status' => DelayedJob::STATUS_SUCCEEDED, diff --git a/app/Services/RunIndicatorAnalysisService.php b/app/Services/RunIndicatorAnalysisService.php index 67843e8f..6822441b 100644 --- a/app/Services/RunIndicatorAnalysisService.php +++ b/app/Services/RunIndicatorAnalysisService.php @@ -85,7 +85,7 @@ public function run(array $request, string $slug) $response = $this->sendApiRequestIndicator(getenv('GFW_SECRET_KEY'), $slugMappings[$slug]['query_url'], $slugMappings[$slug]['sql'], $polygonGeometry['geo']); if (str_contains($slug, 'treeCoverLoss')) { - $processedTreeCoverLossValue = $this->processTreeCoverLossValue($response->json()['data']); + $processedTreeCoverLossValue = $this->processTreeCoverLossValue($response->json()['data'], $slugMappings[$slug]['indicator']); } if ($response->successful()) { @@ -142,11 +142,11 @@ public function sendApiRequestIndicator($secret_key, $query_url, $query_sql, $ge ]); } - public function processTreeCoverLossValue($data) + public function processTreeCoverLossValue($data, $indicator) { $processedTreeCoverLossValue = []; foreach ($data as $i) { - $processedTreeCoverLossValue[$i['umd_tree_cover_loss__year']] = $i['area__ha']; + $processedTreeCoverLossValue[$i[$indicator . '__year']] = $i['area__ha']; } return $processedTreeCoverLossValue;