Skip to content

Commit

Permalink
[TM-1536] add delayed job progress (#593)
Browse files Browse the repository at this point in the history
* [TM-1536] add delayed job progress

* remove progress field

* fix lint

* [TM-1536] add optional attribute

* remove comments

* remove comments

* [TM-1536] add proccess message field

* [TM-1536] fix lint
  • Loading branch information
LimberHope authored Dec 2, 2024
1 parent 7873ac9 commit 1c2c749
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Helpers\GeometryHelper;
use App\Http\Resources\DelayedJobResource;
use App\Jobs\FixPolygonOverlapJob;
use App\Models\DelayedJob;
use App\Models\DelayedJobProgress;
use App\Models\V2\Sites\CriteriaSite;
use App\Models\V2\Sites\Site;
use App\Models\V2\Sites\SitePolygon;
Expand All @@ -25,7 +25,9 @@ public function clipOverlappingPolygonsBySite(string $uuid)
ini_set('memory_limit', '-1');
$user = Auth::user();
$polygonUuids = GeometryHelper::getSitePolygonsUuids($uuid)->toArray();
$delayedJob = DelayedJob::create();
$delayedJob = DelayedJobProgress::create([
'processed_content' => 0,
]);
$job = new FixPolygonOverlapJob($delayedJob->id, $polygonUuids, $user->id);
dispatch($job);

Expand Down Expand Up @@ -75,7 +77,10 @@ public function clipOverlappingPolygonsOfProjectBySite(string $uuid)
if (empty($uniquePolygonUuids)) {
return response()->json(['message' => 'No overlapping polygons found for the project.'], 204);
}
$delayedJob = DelayedJob::create();

$delayedJob = DelayedJobProgress::create([
'processed_content' => 0,
]);
$job = new FixPolygonOverlapJob($delayedJob->id, $uniquePolygonUuids, $user->id);
dispatch($job);

Expand Down Expand Up @@ -123,7 +128,9 @@ public function clipOverlappingPolygons(Request $request)
$delayedJob = null;
if (! empty($uniquePolygonUuids)) {
$user = Auth::user();
$delayedJob = DelayedJob::create();
$delayedJob = DelayedJobProgress::create([
'processed_content' => 0,
]);
$job = new FixPolygonOverlapJob($delayedJob->id, $polygonUuids, $user->id);
dispatch($job);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Jobs\InsertGeojsonToDBJob;
use App\Jobs\RunSitePolygonsValidationJob;
use App\Models\DelayedJob;
use App\Models\DelayedJobProgress;
use App\Models\V2\PolygonGeometry;
use App\Models\V2\Sites\Site;
use App\Models\V2\Sites\SitePolygon;
Expand Down Expand Up @@ -1219,7 +1220,10 @@ public function runSiteValidationPolygon(Request $request)
$uuid = $request->input('uuid');

$sitePolygonsUuids = GeometryHelper::getSitePolygonsUuids($uuid)->toArray();
$delayedJob = DelayedJob::create();
$delayedJob = DelayedJobProgress::create([
'total_content' => count($sitePolygonsUuids),
'processed_content' => 0,
]);
$job = new RunSitePolygonsValidationJob($delayedJob->id, $sitePolygonsUuids);
dispatch($job);

Expand All @@ -1235,7 +1239,10 @@ public function runPolygonsValidation(Request $request)
{
try {
$uuids = $request->input('uuids');
$delayedJob = DelayedJob::create();
$delayedJob = DelayedJobProgress::create([
'total_content' => count($uuids),
'processed_content' => 0,
]);
$job = new RunSitePolygonsValidationJob($delayedJob->id, $uuids);
dispatch($job);

Expand Down
25 changes: 25 additions & 0 deletions app/Http/Resources/DelayedJobProgressResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class DelayedJobProgressResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'message' => $this->message ?? 'Job dispatched',
'job_uuid' => $this->uuid,
'proccessed_content' => $this->processed_content,
'total_content' => $this->total_content,
'proccess_message' => $this->proccess_message,
];
}
}
8 changes: 5 additions & 3 deletions app/Jobs/FixPolygonOverlapJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Middleware\SetAuthenticatedUserForJob;
use App\Models\DelayedJob;
use App\Models\DelayedJobProgress;
use App\Services\PolygonService;
use Exception;
use Illuminate\Bus\Queueable;
Expand Down Expand Up @@ -63,14 +64,15 @@ public function handle(): void
{

try {
$delayedJob = DelayedJob::findOrFail($this->delayed_job_id);
$delayedJob = DelayedJobProgress::findOrFail($this->delayed_job_id);
$user = Auth::user();
if ($user) {
$polygonsClipped = App::make(PolygonService::class)->processClippedPolygons($this->polygonUuids);
$polygonsClipped = App::make(PolygonService::class)->processClippedPolygons($this->polygonUuids, $this->delayed_job_id);
$delayedJob->update([
'status' => DelayedJob::STATUS_SUCCEEDED,
'status' => DelayedJobProgress::STATUS_SUCCEEDED,
'payload' => json_encode(['updated_polygons' => $polygonsClipped]),
'status_code' => Response::HTTP_OK,
'progress' => 100,
]);
}
} catch (Exception $e) {
Expand Down
10 changes: 8 additions & 2 deletions app/Jobs/RunSitePolygonsValidationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Jobs;

use App\Models\DelayedJob;
use App\Models\DelayedJobProgress;
use App\Services\PolygonValidationService;
use Exception;
use Illuminate\Bus\Queueable;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function __construct(string $delayed_job_id, array $sitePolygonsUuids)
public function handle(PolygonValidationService $validationService)
{
try {
$delayedJob = DelayedJob::findOrFail($this->delayed_job_id);
$delayedJob = DelayedJobProgress::findOrFail($this->delayed_job_id);
foreach ($this->sitePolygonsUuids as $polygonUuid) {
$request = new Request(['uuid' => $polygonUuid]);
$validationService->validateOverlapping($request);
Expand All @@ -60,12 +61,17 @@ public function handle(PolygonValidationService $validationService)
$validationService->getGeometryType($request);
$validationService->validateEstimatedArea($request);
$validationService->validateDataInDB($request);

$delayedJob->increment('processed_content');
$delayedJob->processMessage();
$delayedJob->save();
}

$delayedJob->update([
'status' => DelayedJob::STATUS_SUCCEEDED,
'status' => DelayedJobProgress::STATUS_SUCCEEDED,
'payload' => ['message' => 'Validation completed for all site polygons'],
'status_code' => Response::HTTP_OK,
'progress' => 100,
]);

} catch (Exception $e) {
Expand Down
36 changes: 36 additions & 0 deletions app/Models/DelayedJobProgress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Models;

class DelayedJobProgress extends DelayedJob
{
public function __construct(array $attributes = [])
{
parent::__construct($attributes);

$this->fillable = array_merge($this->fillable, [
'processed_content',
'total_content',
'proccess_message',
]);

$this->casts = array_merge($this->casts, [
'processed_content' => 'integer',
'total_content' => 'integer',
'proccess_message' => 'string',
]);
}

public function processMessage(): string
{
$progress = 0;
if ($this->total_content > 0) {
$progress = (int)(($this->processed_content / $this->total_content) * 100);
} else {
$progress = 0;
}

return $this->proccess_message = 'Running '. $this->processed_content .' out of '
.$this->total_content. ' polygons ('.$progress.'%)' ;
}
}
11 changes: 10 additions & 1 deletion app/Services/PolygonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Helpers\CreateVersionPolygonGeometryHelper;
use App\Helpers\GeometryHelper;
use App\Helpers\PolygonGeometryHelper;
use App\Models\DelayedJobProgress;
use App\Models\V2\PointGeometry;
use App\Models\V2\PolygonGeometry;
use App\Models\V2\ProjectPitch;
Expand Down Expand Up @@ -562,13 +563,16 @@ public function insertGeojsonToDBFromContent(string $geojsonData, ?string $entit
}
}

public function processClippedPolygons(array $polygonUuids)
public function processClippedPolygons(array $polygonUuids, $delayed_job_id = null)
{
$geojson = GeometryHelper::getPolygonsGeojson($polygonUuids);

$clippedPolygons = App::make(PythonService::class)->clipPolygons($geojson);
$uuids = [];

$delayedJob = DelayedJobProgress::findOrFail($delayed_job_id);

Log::info('test now selected plygons');
if (isset($clippedPolygons['type']) && $clippedPolygons['type'] === 'FeatureCollection' && isset($clippedPolygons['features'])) {
foreach ($clippedPolygons['features'] as $feature) {
if (isset($feature['properties']['poly_id'])) {
Expand All @@ -591,8 +595,13 @@ public function processClippedPolygons(array $polygonUuids)
}

if (! empty($uuids)) {
$delayedJob->total_content = count($newPolygonUuids);
$delayedJob->save();
foreach ($newPolygonUuids as $polygonUuid) {
App::make(PolygonValidationService::class)->runValidationPolygon($polygonUuid);
$delayedJob->increment('processed_content');
$delayedJob->processMessage();
$delayedJob->save();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('delayed_jobs', function (Blueprint $table) {
$table->unsignedInteger('processed_content')->nullable()->after('payload');
$table->unsignedInteger('total_content')->nullable()->after('processed_content');
$table->string('proccess_message')->nullable()->after('total_content');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('delayed_jobs', function (Blueprint $table) {
$table->dropColumn(['proccessed_content', 'total_content', 'proccess_message']);
});
}
};

0 comments on commit 1c2c749

Please sign in to comment.