-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'staging' into feat/TM-1531-delayed-job-with-data
- Loading branch information
Showing
39 changed files
with
2,523 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
class RestorationByEcoregionHelper | ||
{ | ||
public static function getCategoryEcoRegion($value, ?bool $isExport = false) | ||
{ | ||
$categoriesFromEcoRegion = [ | ||
'australasian' => [ | ||
'Southeast Australia temperate forests', | ||
'Tocantins/Pindare moist forests', | ||
'Tapajós-Xingu moist forests', | ||
'Mato Grosso seasonal forests', | ||
'Mato Grosso seasonal forests, Xingu-Tocantins-Araguaia moist forests', | ||
'Bahia coastal forests', | ||
'Southern Miombo woodlands', | ||
'Palawan rain forests', | ||
], | ||
'afrotropical' => [ | ||
'Atlantic mixed forests', | ||
'Northern Acacia-Commiphora bushlands and thickets', | ||
'Southern Rift montane forest-grassland mosaic', | ||
'Sierra Madre de Chiapas moist forests', | ||
'Iberian sclerophyllous and semi-deciduous forests', | ||
'Northwest Iberian montane forests', | ||
'Northwestern Congolian lowland forests', | ||
'Albertine Rift montane forests', | ||
'Sahelian Acacia savanna', | ||
'Northern Congolian forest-savanna mosaic', | ||
'Nigerian lowland forests', | ||
'West Sudanian savanna', | ||
'Northern Congolian forest-savanna mosaic, Northwestern Congolian lowland forests', | ||
'Eastern Guinean forests', | ||
'Victoria Basin forest-savanna mosaic', | ||
'Guinean forest-savanna mosaic', | ||
'East Sudanian savanna', | ||
'Central Zambezian Miombo woodlands', | ||
'Ethiopian montane grasslands and woodlands', | ||
'Central African mangroves', | ||
'Southern Acacia-Commiphora bushlands and thickets', | ||
'East African montane forests', | ||
'Eastern Arc forests', | ||
'Guinean mangroves', | ||
'Eastern Zimbabwe montane forest-grassland mosaic', | ||
'Somali Acacia-Commiphora bushlands and thickets', | ||
'Ethiopian montane forests', | ||
'Inner Niger Delta flooded savanna', | ||
'Western Guinean lowland forests', | ||
'Eastern Miombo woodlands', | ||
'Ethiopian montane forests, Ethiopian montane grasslands and woodlands', | ||
'Cross-Sanaga-Bioko coastal forests', | ||
'Zambezian and Mopane woodlands', | ||
'Madagascar lowland forests', | ||
'Madagascar subhumid forests', | ||
'Southern Congolian forest-savanna mosaic', | ||
'East African montane forests', | ||
'East African montane forests, Northern Acacia-Commiphora bushlands and thickets', | ||
'Albertine Rift montane forests, Lake', | ||
], | ||
'paleartic' => [ | ||
'Southwest Iberian Mediterranean sclerophyllous and mixed forests', | ||
'Narmada Valley dry deciduous forests', | ||
'East African montane moorlands', | ||
'Cameroonian Highlands forests', | ||
'Celtic broadleaf forests', | ||
'Atlantic Coast restingas', | ||
], | ||
'neotropical' => [ | ||
'Sinú Valley dry forests', | ||
'Santa Marta montane forests', | ||
'Petén-Veracruz moist forests', | ||
'Central American Atlantic moist forests', | ||
'Petén-Veracruz moist forests, Central American Atlantic moist forests', | ||
'Central American montane forests', | ||
'Central American Atlantic moist forests, Central American montane forests', | ||
'Cross-Niger transition forests', | ||
'Atlantic Coast restingas', | ||
], | ||
|
||
]; | ||
$formatedValue = []; | ||
foreach ($categoriesFromEcoRegion as $category => $values) { | ||
$formatedValue[$category] = 0; | ||
foreach ($value as $key => $val) { | ||
if (in_array($key, $values)) { | ||
$formatedValue[$category] = round((float) $val, 3); | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
$result = array_filter($formatedValue, function ($val) { | ||
return $val !== 0; | ||
}); | ||
|
||
if (empty($result)) { | ||
return $result; | ||
} | ||
if ($isExport) { | ||
return $result; | ||
} else { | ||
return ['data' => $result]; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
app/Http/Controllers/V2/MonitoredData/GetIndicatorPolygonStatusController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\V2\MonitoredData; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\V2\EntityModel; | ||
use App\Models\V2\Projects\Project; | ||
use App\Models\V2\Sites\Site; | ||
use App\Models\V2\Sites\SitePolygon; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class GetIndicatorPolygonStatusController extends Controller | ||
{ | ||
public function __invoke(EntityModel $entity) | ||
{ | ||
try { | ||
$sitePolygonGroupByStatus = SitePolygon::whereHas('site', function ($query) use ($entity) { | ||
if (get_class($entity) == Site::class) { | ||
$query->where('uuid', $entity->uuid); | ||
} elseif (get_class($entity) == Project::class) { | ||
$query->where('project_id', $entity->project->id); | ||
} | ||
}) | ||
->select([ | ||
'id', | ||
'status', | ||
'is_active', | ||
]) | ||
->where('is_active', 1) | ||
->get() | ||
->groupBy('status') | ||
->map(function ($group) { | ||
return $group->count(); | ||
}); | ||
$statuses = ['draft', 'submitted', 'needs-more-information', 'approved']; | ||
$statusesByCount = []; | ||
|
||
foreach ($statuses as $status) { | ||
if (! isset($sitePolygonGroupByStatus[$status])) { | ||
$statusesByCount[$status] = 0; | ||
} else { | ||
$statusesByCount[$status] = $sitePolygonGroupByStatus[$status]; | ||
} | ||
} | ||
|
||
return response()->json($statusesByCount); | ||
} catch (\Exception $e) { | ||
Log::info($e); | ||
} | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
app/Http/Controllers/V2/MonitoredData/GetPolygonsIndicatorAnalysisController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\V2\MonitoredData; | ||
|
||
use App\Helpers\RestorationByEcoregionHelper; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\V2\EntityModel; | ||
use App\Models\V2\Projects\Project; | ||
use App\Models\V2\Sites\Site; | ||
use App\Models\V2\Sites\SitePolygon; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class GetPolygonsIndicatorAnalysisController extends Controller | ||
{ | ||
public function __invoke(EntityModel $entity, string $slug) | ||
{ | ||
$slugMappings = [ | ||
'treeCoverLoss' => [ | ||
'relation_name' => 'treeCoverLossIndicator', | ||
'extra_columns' => '', | ||
], | ||
'treeCoverLossFires' => [ | ||
'relation_name' => 'treeCoverLossIndicator', | ||
], | ||
'restorationByStrategy' => [ | ||
'relation_name' => 'hectaresIndicator', | ||
], | ||
'restorationByLandUse' => [ | ||
'relation_name' => 'hectaresIndicator', | ||
], | ||
'restorationByEcoRegion' => [ | ||
'relation_name' => 'hectaresIndicator', | ||
], | ||
]; | ||
if (! isset($slugMappings[$slug])) { | ||
return response()->json([]); | ||
} | ||
|
||
try { | ||
return SitePolygon::whereHas($slugMappings[$slug]['relation_name'], function ($query) use ($slug) { | ||
$query->where('indicator_slug', $slug) | ||
->where('year_of_analysis', date('Y')); | ||
}) | ||
->whereHas('site', function ($query) use ($entity) { | ||
if (get_class($entity) == Site::class) { | ||
$query->where('uuid', $entity->uuid); | ||
} elseif (get_class($entity) == Project::class) { | ||
$query->where('project_id', $entity->project->id); | ||
} | ||
}) | ||
->select([ | ||
'id', | ||
'poly_name', | ||
'status', | ||
'plantstart', | ||
'site_id', | ||
'is_active', | ||
'poly_id', | ||
'calc_area', | ||
]) | ||
->where('is_active', 1) | ||
->get() | ||
->map(function ($polygon) use ($slugMappings, $slug) { | ||
$indicator = $polygon->{$slugMappings[$slug]['relation_name']}() | ||
->where('indicator_slug', $slug) | ||
->select([ | ||
'indicator_slug', | ||
'year_of_analysis', | ||
'value', | ||
'created_at', | ||
]) | ||
->first(); | ||
$results = [ | ||
'id' => $polygon->id, | ||
'poly_name' => $polygon->poly_name, | ||
'poly_id' => $polygon->poly_id, | ||
'site_id' => $polygon->site_id, | ||
'status' => $polygon->status, | ||
'plantstart' => $polygon->plantstart, | ||
'site_name' => $polygon->site->name ?? '', | ||
'size' => round($polygon->calc_area ?? 0, 3), | ||
'indicator_slug' => $indicator->indicator_slug, | ||
'year_of_analysis' => $indicator->year_of_analysis, | ||
'created_at' => $indicator->created_at, | ||
'base_line' => $indicator->created_at, | ||
'data' => [], | ||
]; | ||
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); | ||
} | ||
|
||
if ($slug == 'restorationByEcoRegion') { | ||
$values = json_decode($indicator->value, true); | ||
$results = array_merge($results, RestorationByEcoregionHelper::getCategoryEcoRegion($values)); | ||
} | ||
|
||
if ($slug == 'restorationByLandUse' || $slug == 'restorationByStrategy') { | ||
$values = json_decode($indicator->value, true); | ||
$results = array_merge($results, $this->processValuesHectares($values)); | ||
} | ||
|
||
return $results; | ||
}); | ||
} catch (\Exception $e) { | ||
Log::info($e); | ||
} | ||
} | ||
|
||
public function processValuesHectares($values) | ||
{ | ||
$separateKeys = []; | ||
foreach ($values as $key => $value) { | ||
$array = explode(',', str_replace('-', '_', $key)); | ||
$arrayTrim = array_map('trim', $array); | ||
foreach ($arrayTrim as $item) { | ||
$separateKeys[$item] = round((float) $value, 3); | ||
} | ||
} | ||
|
||
return ['data' => $separateKeys]; | ||
} | ||
} |
Oops, something went wrong.