Skip to content

Commit

Permalink
[TM-1461] waiting load for data polygons (#582)
Browse files Browse the repository at this point in the history
* add basic endpoints

* move endpoints outside admin

* fix duplicate

* [TM-1461] add entity for site and project endpoint

* [TM-1461] fix distinct responses

* [TM-1461] lint

---------

Co-authored-by: Jose Carlos Laura Ramirez <[email protected]>
  • Loading branch information
egrojMonroy and pachonjcl authored Nov 21, 2024
1 parent 49ca423 commit dbf01a1
Show file tree
Hide file tree
Showing 12 changed files with 595 additions and 2 deletions.
45 changes: 45 additions & 0 deletions app/Http/Controllers/V2/Sites/AdminSitesPolygonController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Http\Controllers\V2\Sites;

use App\Http\Controllers\Controller;
use App\Models\V2\Projects\Project;
use App\Models\V2\Sites\SitePolygon;
use App\Services\PolygonService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;

class AdminSitesPolygonController extends Controller
{
public function __invoke(Request $request): JsonResponse
{
try {
$uuid = $request->input('uuid');
$type = $request->input('type');
$offset = $request->input('offset', 0);
$limit = $request->input('limit', 10);
$request = request();


if ($type === 'projects') {
$project = Project::where('uuid', $uuid)->firstOrFail();
$finalEntityQuery = App::make(PolygonService::class)->getSitePolygonsWithFiltersAndSorts($project->sitePolygons(), $request);
} elseif ($type === 'sites') {
$sitePolygonsQuery = SitePolygon::active()->where('site_id', $uuid);
$finalEntityQuery = App::make(PolygonService::class)->getSitePolygonsWithFiltersAndSorts($sitePolygonsQuery, $request);
}
$sitePolygons = $finalEntityQuery
->offset($offset)
->limit($limit)
->get();

return response()->json($sitePolygons);
} catch (\Exception $e) {
Log::error($e->getMessage());

return response()->json(['error' => 'An error occurred while fetching site polygons'], 500);
}
}
}
41 changes: 41 additions & 0 deletions app/Http/Controllers/V2/Sites/AdminSitesPolygonCountController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Http\Controllers\V2\Sites;

use App\Http\Controllers\Controller;
use App\Models\V2\Projects\Project;
use App\Models\V2\Sites\SitePolygon;
use App\Services\PolygonService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;

class AdminSitesPolygonCountController extends Controller
{
public function __invoke(Request $request): JsonResponse
{
try {
$uuid = $request->input('uuid');
$type = $request->input('type');
$request = request();
if ($type === 'projects') {
$project = Project::where('uuid', $uuid)->firstOrFail();
$countSitePolygons = App::make(PolygonService::class)->getSitePolygonsWithFiltersAndSorts($project->sitePolygons(), $request);
} elseif ($type === 'sites') {
$sitePolygonsQuery = SitePolygon::active()->where('site_id', $uuid);
$countSitePolygons = App::make(PolygonService::class)->getSitePolygonsWithFiltersAndSorts($sitePolygonsQuery, $request);
}

$totalCount = $countSitePolygons->count();

return response()->json([
'count' => $totalCount,
]);
} catch (\Exception $e) {
Log::error($e->getMessage());

return response()->json(['error' => 'An error occurred while fetching site polygons'], 500);
}
}
}
22 changes: 22 additions & 0 deletions app/Services/PolygonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\Validators\SitePolygonValidator;
use DateTime;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
Expand Down Expand Up @@ -582,4 +583,25 @@ public function processClippedPolygons(array $polygonUuids)

return $updatedPolygons;
}

public function getSitePolygonsWithFiltersAndSorts($sitePolygonsQuery, Request $request)
{
if ($request->has('status') && $request->input('status')) {
$statusValues = explode(',', $request->input('status'));
$sitePolygonsQuery->whereIn('site_polygon.status', $statusValues);
}

$sortFields = $request->input('sort', []);
foreach ($sortFields as $field => $direction) {
if ($field === 'status') {
$sitePolygonsQuery->orderByRaw('FIELD(site_polygon.status, "draft", "submitted", "needs-more-information", "approved") ' . $direction);
} elseif ($field === 'poly_name') {
$sitePolygonsQuery->orderByRaw('site_polygon.poly_name IS NULL, site_polygon.poly_name ' . $direction);
} else {
$sitePolygonsQuery->orderBy($field, $direction);
}
}

return $sitePolygonsQuery;
}
}
13 changes: 13 additions & 0 deletions openapi-src/V2/definitions/EntityPolygonResponse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type: object
properties:
type:
type: string
description: Type of the entity ('project', 'site', 'unknown')
uuid:
type: string
format: uuid
description: UUID of the entity
polygonsData:
type: array
items:
$ref: './_index.yml#/SitePolygon'
2 changes: 2 additions & 0 deletions openapi-src/V2/definitions/_index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ GeojsonData:
$ref: './GeojsonData.yml'
EntityTypeResponse:
$ref: './EntityTypeResponse.yml'
EntityPolygonResponse:
$ref: './EntityPolygonResponse.yml'
AuditStatusUpdateRequest:
$ref: './AuditStatusUpdateRequest.yml'
SitePolygonResource:
Expand Down
34 changes: 34 additions & 0 deletions openapi-src/V2/paths/Entity/get-v2-entity-count.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
summary: Get Entity Type
description: |
Determine the type of entity based on UUID.
parameters:
- in: query
name: uuid
required: true
description: UUID of the entity
type: string
- in: query
name: type
required: true
description: type of the entity
type: string
- in: query
name: status
required: false
description: Comma-separated list of status values to filter by
type: string
- in: query
name: sort
required: false
description: Sort criteria in the format `sort[poly_name]=asc or sort[status]=desc`
type: string
responses:
'200':
description: Successful response
schema:
type: object
properties:
count:
type: number
'500':
description: Internal server error
39 changes: 39 additions & 0 deletions openapi-src/V2/paths/Entity/get-v2-entity-polygon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
summary: Get Entity Type
description: |
Determine the type of entity based on UUID.
parameters:
- in: query
name: uuid
required: true
description: UUID of the entity
type: string
- in: query
name: type
required: true
description: type of the entity
type: string
- in: query
name: status
required: false
description: Comma-separated list of status values to filter by
type: string
- in: query
name: sort
required: false
description: Sort criteria in the format `sort[poly_name]=asc or sort[status]=desc`
type: string
responses:
'200':
description: Successful response
schema:
$ref: '../../definitions/_index.yml#/EntityPolygonResponse'
'400':
description: Bad request, UUID parameter is missing
'500':
description: Internal server error
schema:
type: object
properties:
error:
type: string
description: Error message
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
summary: Get count polygons for a specific site
parameters:
- in: path
name: UUID
required: true
type: string
description: The UUID of the site
responses:
'200':
description: Successful response
schema:
type: object
properties:
count:
type: number
'500':
description: Internal server error
26 changes: 26 additions & 0 deletions openapi-src/V2/paths/Sites/get-v2-admin-sites-uuid-polygons.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
summary: Get polygons for a specific site
parameters:
- in: path
name: UUID
required: true
type: string
description: The UUID of the site
- in: query
name: limit
required: true
description: The maximum number of polygons to return
type: number
- in: query
name: offset
required: true
description: The number of polygons to skip
type: string
responses:
'200':
description: Successful response
schema:
type: array
items:
$ref: '../../definitions/_index.yml#/SitePolygon'
'500':
description: Internal server error
16 changes: 14 additions & 2 deletions openapi-src/V2/paths/_index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2497,6 +2497,12 @@
$ref: './Sites/delete-v2-sites-uuid.yml'
get:
$ref: './Sites/get-v2-sites-uuid.yml'
/v2/sites/{UUID}/polygons:
get:
$ref: './Sites/get-v2-admin-sites-uuid-polygons.yml'
/v2/sites/{UUID}/polygons/count:
get:
$ref: './Sites/get-v2-admin-sites-uuid-polygons-count.yml'
/v2/site-monitorings/{UUID}:
get:
$ref: './Sites/Monitoring/get-v2-site-monitorings-uuid.yml'
Expand Down Expand Up @@ -2741,9 +2747,15 @@
/v2/type-entity:
get:
$ref: './Entity/get-v2-type-entity.yml'
/v2/entity/polygons/count:
get:
$ref: './Entity/get-v2-entity-count.yml'
/v2/entity/polygons:
get:
$ref: './Entity/get-v2-entity-polygon.yml'
/v2/{ENTITY}/{UUID}/status:
put:
$ref: './Entity/put-v2-entity-uuid-status.yml'
put:
$ref: './Entity/put-v2-entity-uuid-status.yml'
/v2/{ENTITY}/{UUID}/{ID}/delete:
delete:
$ref: './AuditStatus/delete-v2-audit-status.yml'
Expand Down
Loading

0 comments on commit dbf01a1

Please sign in to comment.