Skip to content

Commit

Permalink
Merge branch 'release/utltimate-ulmus' into feat/TM-1531-delayed-job-…
Browse files Browse the repository at this point in the history
…with-data
  • Loading branch information
egrojMonroy authored Dec 18, 2024
2 parents 1a743a3 + a5b3be3 commit 07d2267
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 34 deletions.
43 changes: 43 additions & 0 deletions app/Console/Commands/OneOff/AssociateExactMatchTrees.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Console\Commands\OneOff;

use App\Models\V2\TreeSpecies\TreeSpecies;
use Illuminate\Console\Command;

class AssociateExactMatchTrees extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'one-off:associate-exact-match-trees';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update tree species rows without a taxon_id but that do have an exact match in the backbone.';

/**
* Execute the console command.
*/
public function handle()
{
TreeSpecies::withoutTimestamps(function () {
$query = TreeSpecies::withTrashed()
->join('tree_species_research', 'v2_tree_species.name', '=', 'tree_species_research.scientific_name')
->where('v2_tree_species.taxon_id', null);
$this->withProgressBar((clone $query)->count(), function ($progressBar) use ($query) {
$query->chunkById(100, function ($trees) use ($progressBar) {
foreach ($trees as $tree) {
TreeSpecies::where('id', $tree->id)->update(['taxon_id' => $tree->taxon_id]);
$progressBar->advance();
}
});
});
});
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ public function runPolygonsValidation(Request $request)
'entity_id' => $entity->id,
'entity_type' => get_class($entity),
'is_acknowledged' => false,
'name' => 'Polygon Validation',
'name' => 'Polygon Validation'
]);
$job = new RunSitePolygonsValidationJob($delayedJob->id, $uuids);
dispatch($job);
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 07d2267

Please sign in to comment.