Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TM-1417] add delayed job and cache to export project endpoint #549

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace App\Http\Controllers\V2\Exports;

use App\Exports\V2\EntityExport;
use App\Http\Controllers\Controller;
use App\Http\Resources\DelayedJobResource;
use App\Jobs\ExportAllProjectDataAsProjectDeveloperJob;
use App\Models\DelayedJob;
use App\Models\V2\Forms\Form;
use App\Models\V2\Nurseries\Nursery;
use App\Models\V2\Nurseries\NurseryReport;
use App\Models\V2\Projects\Project;
use App\Models\V2\Projects\ProjectReport;
use App\Models\V2\Sites\Site;
use App\Models\V2\Sites\SiteReport;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Excel;

class ExportAllProjectDataAsProjectDeveloperController extends Controller
{
Expand All @@ -23,124 +21,32 @@ public function __invoke(Request $request, Project $project)
$form = $this->getForm(Project::class, $project->framework_key);
$this->authorize('export', [Project::class, $form, $project]);

$filename = storage_path('./'.Str::of($project->name)->replace(['/', '\\'], '-') . ' full export - ' . now() . '.zip');
$zip = new \ZipArchive();
$zip->open($filename, \ZipArchive::CREATE);
try {
$binary_data = Redis::get('exports:project:'.$project->id);

rescue(function () use ($project, $zip, $form) {
$this->addProjectExports($project, $zip, $form);
});
rescue(function () use ($project, $zip) {
$this->addProjectReportExports($project, $zip);
});
rescue(function () use ($project, $zip) {
$this->addSitesExports($project, $zip);
});
rescue(function () use ($project, $zip) {
$this->addSiteReportsExports($project, $zip);
});
rescue(function () use ($project, $zip) {
$this->addSiteShapefiles($project, $zip);
});
rescue(function () use ($project, $zip) {
$this->addNurseriesExports($project, $zip);
});

rescue(function () use ($project, $zip) {
$this->addNurseryReportsExports($project, $zip);
});

$zip->close();

return response()->download($filename)->deleteFileAfterSend();
}

private function addSiteReportsExports(Project $project, \ZipArchive $mainZip): void
{
$form = $this->getForm(SiteReport::class, $project->framework_key);

foreach ($project->sites as $site) {
$filename = 'site reports/'.Str::of($project->name . ' - '.$site->name)->replace(['/', '\\'], '-') . ' - site reports.csv';
$mainZip
->addFromString(
$filename,
(new EntityExport(SiteReport::parentId($site->id), $form))->raw(Excel::CSV)
if (! $binary_data) {
$delayedJob = DelayedJob::create();
$job = new ExportAllProjectDataAsProjectDeveloperJob(
$delayedJob->id,
$form->uuid,
$project->id
);
}
}

private function addSiteShapefiles(Project $project, \ZipArchive $mainZip): void
{
$shapefilesFolder = 'Sites Shapefiles/';
$mainZip->addEmptyDir($shapefilesFolder);
dispatch($job);

foreach ($project->sites as $site) {
$filename = $shapefilesFolder . Str::of($site->name)->replace(['/', '\\'], '-') . '.geojson';
$mainZip->addFromString($filename, $site->boundary_geojson);
}
}
return (new DelayedJobResource($delayedJob))->additional(['message' => "Export for project $project->id is being processed"]);
} else {
$filename = storage_path('./'.Str::of($project->name)->replace(['/', '\\'], '-') . ' full export - ' . now() . '.zip');
file_put_contents($filename, $binary_data);

private function addNurseryReportsExports(Project $project, \ZipArchive $mainZip): void
{
$form = $this->getForm(NurseryReport::class, $project->framework_key);
return response()->download($filename)->deleteFileAfterSend();
}
} catch (\Exception $e) {
Log::error('Error during export for single project : ' . $e->getMessage());

foreach ($project->nurseries as $nursery) {
$filename = 'nursery reports/'.Str::of($project->name . ' - '.$nursery->name)->replace(['/', '\\'], '-') . ' - nursery reports.csv';
$mainZip
->addFromString(
$filename,
(new EntityExport(NurseryReport::parentId($nursery->id), $form))->raw(Excel::CSV)
);
return response()->json(['error' => 'An error occurred during single project export'], 500);
}
}

private function addProjectExports(Project $project, \ZipArchive $mainZip, Form $form): void
{
$filename = Str::of($project->name)->replace(['/', '\\'], '-') . ' - project establishment data.csv';

$mainZip
->addFromString(
$filename,
(new EntityExport(Project::query()->where('id', $project->id), $form))->raw(Excel::CSV)
);
}

private function addProjectReportExports(Project $project, \ZipArchive $mainZip): void
{
$form = $this->getForm(ProjectReport::class, $project->framework_key);
$filename = Str::of($project->name)->replace(['/', '\\'], '-') . ' - project reports.csv';

$mainZip
->addFromString(
$filename,
(new EntityExport($project->reports()->getQuery(), $form))->raw(Excel::CSV)
);
}

private function addSitesExports(Project $project, \ZipArchive $mainZip): void
{
$form = $this->getForm(Site::class, $project->framework_key);
$filename = Str::of($project->name)->replace(['/', '\\'], '-') . ' - site establishment data.csv';
$mainZip
->addFromString(
$filename,
(new EntityExport($project->sites()->getQuery(), $form))->raw(Excel::CSV)
);
}

private function addNurseriesExports(Project $project, \ZipArchive $mainZip): void
{
$form = $this->getForm(Nursery::class, $project->framework_key);
$filename = Str::of($project->name)->replace(['/', '\\'], '-') . ' - nursery establishment data.csv';


$mainZip
->addFromString(
$filename,
(new EntityExport($project->nurseries()->getQuery(), $form))->raw(Excel::CSV)
);
}

private function getForm(string $modelClass, string $framework)
{
return Form::where('model', $modelClass)
Expand Down
69 changes: 69 additions & 0 deletions app/Jobs/ExportAllProjectDataAsProjectDeveloperJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Jobs;

use App\Models\DelayedJob;
use App\Models\V2\Forms\Form;
use App\Models\V2\Projects\Project;
use App\Services\ExportAllProjectDataAsProjectDeveloperService;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Response;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class ExportAllProjectDataAsProjectDeveloperJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

public $timeout = 0;

protected $uuid;

protected $delayed_job_id;

protected $form_uuid;

protected $project_id;

public function __construct(string $delayed_job_id, string $form_uuid, string $project_id)
{
$this->delayed_job_id = $delayed_job_id;
$this->form_uuid = $form_uuid;
$this->project_id = $project_id;
}

public function handle(ExportAllProjectDataAsProjectDeveloperService $exportAllProjectDataAsProjectDeveloperService)
{
try {
$delayedJob = DelayedJob::findOrFail($this->delayed_job_id);
$project = Project::findOrFail($this->project_id);
$form = Form::isUuid($this->form_uuid)->firstOrFail();

$binary_data = $exportAllProjectDataAsProjectDeveloperService->run($form, $project);
Redis::set('exports:project:'.$project->id, $binary_data, 'EX', 7200);

$delayedJob->update([
'status' => DelayedJob::STATUS_SUCCEEDED,
'payload' => ['message' => 'Total Header Calculation completed'],
'status_code' => Response::HTTP_OK,
]);

} catch (Exception $e) {
Log::error('Error in ExportAllProjectDataAsProjectDeveloperJob: ' . $e->getMessage());

DelayedJob::where('id', $this->delayed_job_id)->update([
'status' => DelayedJob::STATUS_FAILED,
'payload' => json_encode(['error' => $e->getMessage()]),
'status_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
]);
}
}
}
145 changes: 145 additions & 0 deletions app/Services/ExportAllProjectDataAsProjectDeveloperService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace App\Services;

use App\Exports\V2\EntityExport;
use App\Models\V2\Forms\Form;
use App\Models\V2\Nurseries\Nursery;
use App\Models\V2\Nurseries\NurseryReport;
use App\Models\V2\Projects\Project;
use App\Models\V2\Projects\ProjectReport;
use App\Models\V2\Sites\Site;
use App\Models\V2\Sites\SiteReport;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Excel;

class ExportAllProjectDataAsProjectDeveloperService
{
public function run($form, $project)
{
$filename = storage_path('./'.Str::of($project->name)->replace(['/', '\\'], '-') . ' full export - ' . now() . '.zip');

$zip = new \ZipArchive();
$zip->open($filename, \ZipArchive::CREATE);

rescue(function () use ($project, $zip, $form) {
$this->addProjectExports($project, $zip, $form);
});
rescue(function () use ($project, $zip) {
$this->addProjectReportExports($project, $zip);
});
rescue(function () use ($project, $zip) {
$this->addSitesExports($project, $zip);
});
rescue(function () use ($project, $zip) {
$this->addSiteReportsExports($project, $zip);
});
rescue(function () use ($project, $zip) {
$this->addSiteShapefiles($project, $zip);
});
rescue(function () use ($project, $zip) {
$this->addNurseriesExports($project, $zip);
});

rescue(function () use ($project, $zip) {
$this->addNurseryReportsExports($project, $zip);
});

$zip->close();

return file_get_contents($filename);
}

private function addSiteReportsExports(Project $project, \ZipArchive $mainZip): void
{
$form = $this->getForm(SiteReport::class, $project->framework_key);

foreach ($project->sites as $site) {
$filename = 'site reports/'.Str::of($project->name . ' - '.$site->name)->replace(['/', '\\'], '-') . ' - site reports.csv';
$mainZip
->addFromString(
$filename,
(new EntityExport(SiteReport::parentId($site->id), $form))->raw(Excel::CSV)
);
}
}

private function addSiteShapefiles(Project $project, \ZipArchive $mainZip): void
{
$shapefilesFolder = 'Sites Shapefiles/';
$mainZip->addEmptyDir($shapefilesFolder);

foreach ($project->sites as $site) {
$filename = $shapefilesFolder . Str::of($site->name)->replace(['/', '\\'], '-') . '.geojson';
$mainZip->addFromString($filename, $site->boundary_geojson);
}
}

private function addNurseryReportsExports(Project $project, \ZipArchive $mainZip): void
{
$form = $this->getForm(NurseryReport::class, $project->framework_key);

foreach ($project->nurseries as $nursery) {
$filename = 'nursery reports/'.Str::of($project->name . ' - '.$nursery->name)->replace(['/', '\\'], '-') . ' - nursery reports.csv';
$mainZip
->addFromString(
$filename,
(new EntityExport(NurseryReport::parentId($nursery->id), $form))->raw(Excel::CSV)
);
}
}

private function addProjectExports(Project $project, \ZipArchive $mainZip, Form $form): void
{
$filename = Str::of($project->name)->replace(['/', '\\'], '-') . ' - project establishment data.csv';

$mainZip
->addFromString(
$filename,
(new EntityExport(Project::query()->where('id', $project->id), $form))->raw(Excel::CSV)
);
}

private function addProjectReportExports(Project $project, \ZipArchive $mainZip): void
{
$form = $this->getForm(ProjectReport::class, $project->framework_key);
$filename = Str::of($project->name)->replace(['/', '\\'], '-') . ' - project reports.csv';

$mainZip
->addFromString(
$filename,
(new EntityExport($project->reports()->getQuery(), $form))->raw(Excel::CSV)
);
}

private function addSitesExports(Project $project, \ZipArchive $mainZip): void
{
$form = $this->getForm(Site::class, $project->framework_key);
$filename = Str::of($project->name)->replace(['/', '\\'], '-') . ' - site establishment data.csv';
$mainZip
->addFromString(
$filename,
(new EntityExport($project->sites()->getQuery(), $form))->raw(Excel::CSV)
);
}

private function addNurseriesExports(Project $project, \ZipArchive $mainZip): void
{
$form = $this->getForm(Nursery::class, $project->framework_key);
$filename = Str::of($project->name)->replace(['/', '\\'], '-') . ' - nursery establishment data.csv';


$mainZip
->addFromString(
$filename,
(new EntityExport($project->nurseries()->getQuery(), $form))->raw(Excel::CSV)
);
}

private function getForm(string $modelClass, string $framework)
{
return Form::where('model', $modelClass)
->where('framework_key', $framework)
->firstOrFail();
}
}
Loading
Loading