Skip to content

Commit

Permalink
[TM-1466] remove cached functinality from Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
LimberHope committed Dec 17, 2024
1 parent d66b705 commit b40c0d7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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') {
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,22 @@
use App\Models\DelayedJob;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class RunIndicatorAnalysisController extends Controller
{
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());

Expand Down
5 changes: 1 addition & 4 deletions app/Jobs/RunIndicatorAnalysisJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions app/Services/RunIndicatorAnalysisService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b40c0d7

Please sign in to comment.