feat: display precise public labour and economic observations in map context
tests / ci (push) Canceled after 0s
tests / ci (push) Canceled after 0s
This commit is contained in:
@@ -9,6 +9,8 @@ use App\Services\CorridorAgribusinessOpportunities;
|
||||
use App\Services\InvestmentCatalogue;
|
||||
use App\Services\InvestmentComplements;
|
||||
use App\Services\InvestmentGeography;
|
||||
use App\Services\InvestmentMapIndicators;
|
||||
use App\Services\InvestmentZoneOperationalDetails;
|
||||
use App\Services\InvestmentZones;
|
||||
use App\Services\OpportunityCorridorExplorer;
|
||||
use App\Services\OpportunityTextSearch;
|
||||
@@ -155,6 +157,7 @@ class InvestmentDiscoveryController extends Controller
|
||||
$filters = $catalogue->filters($request);
|
||||
$context = $this->territorial($filters)->map(fn ($d) => ['id' => 'dossier:'.$d->id, 'title' => $d->title, 'kind' => $d->cluster_id ? 'Cluster' : ($d->corridor_id ? 'Corredor' : 'Projeto âncora'), 'url' => '/carteira/'.$d->id]);
|
||||
$opportunityOnlyFilters = array_filter(Arr::only($filters, ['association_type', 'association_target', 'association_state', 'association_purpose', 'authority_id', 'chain_id', 'subsector_id', 'category', 'publication_state', 'education', 'sector', 'facets', 'project_id', 'cluster_id', 'corridor_id', 'min_amount', 'max_amount', 'currency', 'before', 'maturity_level', 'preparation']), fn ($value) => $value !== null && $value !== '' && $value !== false && $value !== []);
|
||||
$quantitative = ['state' => 'not_included', 'records' => [], 'truncated' => false];
|
||||
if ($opportunityOnlyFilters === [] && ! in_array($filters['dimension'] ?? null, ['cluster', 'corridor'], true)) {
|
||||
$zoneFilters = $filters;
|
||||
if (in_array($filters['dimension'] ?? null, ['zee', 'zfi'], true)) {
|
||||
@@ -166,10 +169,12 @@ class InvestmentDiscoveryController extends Controller
|
||||
$zones->where('id', $filters[$key]);
|
||||
}
|
||||
}
|
||||
$context = $context->concat($zones->orderBy('name')->limit(30)->get(['id', 'name', 'category'])->map(fn ($z) => ['id' => 'zone:'.$z->id, 'title' => $z->name, 'kind' => ['zee' => 'ZEE', 'zfi' => 'ZFI', 'industrial_park' => 'Parque industrial'][$z->category] ?? $z->category, 'url' => '/zonas/'.$z->id]));
|
||||
$zoneRows = $zones->orderBy('name')->orderBy('id')->limit(31)->get(['id', 'name', 'category', 'version_no', ...(app(InvestmentZoneOperationalDetails::class)->installed() ? ['operational_details'] : [])]);
|
||||
$quantitative = app(InvestmentMapIndicators::class)->forZones($zoneRows->take(30)->values(), $zoneRows->count() > 30);
|
||||
$context = $context->concat($zoneRows->take(30)->map(fn ($z) => ['id' => 'zone:'.$z->id, 'title' => $z->name, 'kind' => ['zee' => 'ZEE', 'zfi' => 'ZFI', 'industrial_park' => 'Parque industrial'][$z->category] ?? $z->category, 'url' => '/zonas/'.$z->id]));
|
||||
}
|
||||
|
||||
return response()->json([...$catalogue->map($filters), 'territorial_context' => $context->values(), 'context_limit_per_collection' => 30]);
|
||||
return response()->json([...$catalogue->map($filters), 'territorial_context' => $context->values(), 'quantitative_context' => $quantitative, 'context_limit_per_collection' => 30]);
|
||||
}
|
||||
|
||||
public function compare(Request $request, InvestmentCatalogue $catalogue): JsonResponse
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\InvestmentZone;
|
||||
use App\Support\StructuredData;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/** Public park observations remain individual source records, never regional estimates. */
|
||||
final class InvestmentMapIndicators
|
||||
{
|
||||
public const INDICATORS = ['employment', 'investment', 'production', 'exports', 'local_procurement'];
|
||||
|
||||
/** @param Collection<int, InvestmentZone> $zones
|
||||
* @return array<string,mixed> */
|
||||
public function forZones(Collection $zones, bool $zoneLimitReached = false): array
|
||||
{
|
||||
$zoneLimitReached = $zoneLimitReached || $zones->count() > 30;
|
||||
$zones = $zones->take(30)->values();
|
||||
$details = app(InvestmentZoneOperationalDetails::class);
|
||||
$result = ['state' => $details->installed() ? 'available' : 'unavailable', 'records' => [],
|
||||
'zones_consulted' => $zones->count(), 'zone_limit' => 30, 'record_limit' => 200, 'truncated' => $zoneLimitReached];
|
||||
if ($result['state'] === 'unavailable') {
|
||||
return $result;
|
||||
}
|
||||
foreach ($zones as $zone) {
|
||||
// This is the already selected public envelope; never decrypt the private preparation.
|
||||
$public = $details->publicData($zone);
|
||||
$observations = StructuredData::collectionItems($public['integral']['observations'] ?? []);
|
||||
$values = [];
|
||||
if ($observations !== []) {
|
||||
// Extract decimal text from this exact loaded snapshot, not a newer database version or a PHP float.
|
||||
foreach (DB::select("SELECT item->>'id' AS id, item->>'value' AS value FROM jsonb_array_elements((?::jsonb)->'public'->'integral'->'observations') AS item", [$zone->getRawOriginal('operational_details')]) as $number) {
|
||||
$values[$number->id] = $number->value;
|
||||
}
|
||||
}
|
||||
foreach ($observations as $row) {
|
||||
if (! in_array($row['indicator'] ?? null, self::INDICATORS, true)) {
|
||||
continue;
|
||||
}
|
||||
if (count($result['records']) === 200) {
|
||||
$result['truncated'] = true;
|
||||
|
||||
return $result;
|
||||
}
|
||||
$knowledge = $row['knowledge'] ?? 'unknown';
|
||||
$rawValue = $values[$row['id'] ?? ''] ?? null;
|
||||
$value = $knowledge !== 'unknown' && is_numeric($rawValue) ? $rawValue : null;
|
||||
$missing = [];
|
||||
foreach (['unit', 'period_start', 'period_end', 'scope', 'population', 'methodology', 'source_reference', 'source_date'] as $field) {
|
||||
if (trim((string) ($row[$field] ?? '')) === '') {
|
||||
$missing[] = $field;
|
||||
}
|
||||
}
|
||||
$result['records'][] = [
|
||||
...array_intersect_key($row, array_flip(['id', 'label', 'description', 'indicator', 'unit', 'period_start', 'period_end', 'scope', 'population', 'methodology', 'source_reference', 'source_date', 'phase_id', 'plot_id'])),
|
||||
'zone_id' => $zone->id, 'zone_name' => $zone->name, 'zone_version' => (int) $zone->version_no, 'url' => '/zonas/'.$zone->id,
|
||||
'theme' => $row['indicator'] === 'employment' ? 'workforce' : 'economic_potential',
|
||||
'value' => $value, 'knowledge' => $knowledge, 'missing_metadata' => $missing,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user