wip;
This commit is contained in:
parent
da9d94880f
commit
a1bba242cb
|
|
@ -65,10 +65,11 @@ public function handle(): void
|
|||
|
||||
public static function handleForDate(Project $project, Carbon $date)
|
||||
{
|
||||
if ($project->downloads()->where(['status' => 'completed', 'date' => $date])->count() > 0) {
|
||||
$filename = sprintf('%s.tif', $date->format('Y-m-d'));
|
||||
if ($project->downloads()->where(['status' => 'completed', 'name' => $filename])->count() > 0) {
|
||||
return;
|
||||
}
|
||||
$filename = sprintf('%s.tif', $date->format('Y-m-d'));
|
||||
|
||||
$path = sprintf('%s/%s/%s', $project->download_path, 'merged_final_tif', $filename);
|
||||
return new self(
|
||||
$project->downloads()->create([
|
||||
|
|
|
|||
|
|
@ -74,6 +74,8 @@ public static function handleFor(Project $project, $year, $startWeekNumber) {
|
|||
return;
|
||||
}
|
||||
$mosaic = $project->mosaics()->create([
|
||||
'name' => sprintf('Week %d, %d', $startWeekNumber, $year),
|
||||
'path' => sprintf('%s/%s/%s', $project->download_path, 'mosaics', sprintf('week_%d_%d.tif', $startWeekNumber, $year)),
|
||||
'year' => $year,
|
||||
'week' => $startWeekNumber,
|
||||
'status' => 'pending',
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ public function render()
|
|||
]);
|
||||
}
|
||||
|
||||
public function saveMosiac(){
|
||||
public function saveMosaic(){
|
||||
$this->validate([
|
||||
'formData.year' => 'required',
|
||||
'formData.week' => 'required',
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
namespace App\Models;
|
||||
|
||||
use App\Jobs\ProjectDownloadTiffJob;
|
||||
use App\Jobs\ProjectMosiacGeneratorJob;
|
||||
use Carbon\CarbonPeriod;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Carbon\Carbon;
|
||||
|
|
@ -261,4 +263,48 @@ public function startDownload(Carbon $date)
|
|||
);
|
||||
ProjectDownloadTiffJob::dispatch($downloadRequest, $date);
|
||||
}
|
||||
|
||||
public function scheduleReport($year, $week)
|
||||
{
|
||||
if ($this->reports()->where(['year' => $year, 'week' => $week])->count() > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
Bus::chain([
|
||||
Bus::batch(self::getFileDownloadsFor($year, $week)),
|
||||
Bus::batch(self::getMosiacsFor($year, $week)),
|
||||
Bus::batch(self::getReport($year, $week)),
|
||||
])->dispatch();
|
||||
}
|
||||
|
||||
public function getReport($year, $week)
|
||||
{
|
||||
|
||||
$report = $this->reports()->create([
|
||||
'name' => 'Report for week '.$week.' of '.$year,
|
||||
'week' => $week,
|
||||
'year' => $year,
|
||||
'path' => 'reports/week_'.$week.'_'.$year.'.docx',
|
||||
]);
|
||||
|
||||
return new ProjectReportGeneratorJob($report);
|
||||
}
|
||||
|
||||
public function getFileDownloadsFor($year, $startWeekNumber): Collection
|
||||
{
|
||||
$endOfRange = \Illuminate\Support\Carbon::now()->setISODate($year, $startWeekNumber)->endOfWeek();
|
||||
$startOfRange = (clone $endOfRange)->subWeeks(2)->startOfWeek();
|
||||
|
||||
$dateRange = CarbonPeriod::create($startOfRange, $endOfRange);
|
||||
|
||||
return collect($dateRange)
|
||||
->map(fn($date) => ProjectDownloadTiffJob::handleForDate($this, $date))
|
||||
->filter();
|
||||
}
|
||||
|
||||
public function getMosaicsFor($year, $startWeekNumber)
|
||||
{
|
||||
return collect(range(0, 2))
|
||||
->map(fn($weekDiff) => ProjectMosiacGeneratorJob::handleFor($this, $year, $startWeekNumber - $weekDiff));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,38 +82,5 @@ public function deleteMe()
|
|||
$this->delete();
|
||||
}
|
||||
|
||||
public static function schedule($year, $week)
|
||||
{
|
||||
Bus::chain([
|
||||
Bus::batch(self::getFileDownloadsFor($year, $week)),
|
||||
Bus::batch(self::getMosiacsFor($year, $week)),
|
||||
Bus::batch(self::getReport($year, $week)),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getFileDownloadsFor($project, $year, $startWeekNumber)
|
||||
{
|
||||
$endOfRange = Carbon::now()->setISODate($year, $startWeekNumber)->endOfWeek();
|
||||
$startOfRange = (clone $endOfRange)->subWeeks(2)->startOfWeek();
|
||||
|
||||
$dateRange = CarbonPeriod::create($startOfRange, $endOfRange);
|
||||
|
||||
return collect($dateRange)
|
||||
->map(fn ($date) => ProjectDownloadTiffJob::handleForDate($project, $date))
|
||||
->filter();
|
||||
}
|
||||
|
||||
public static function getMosaicsFor($project, $year, $startWeekNumber)
|
||||
{
|
||||
$jobs = [];
|
||||
|
||||
foreach (range(0,2) as $weekDiff) {
|
||||
$job = ProjectMosiacGeneratorJob::handleFor($project, $year, $startWeekNumber - $weekDiff);
|
||||
if ($job) {
|
||||
$jobs[] = $job;
|
||||
}
|
||||
}
|
||||
|
||||
return $jobs;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,13 +5,13 @@
|
|||
])
|
||||
|
||||
<x-modal wire:model.live="showCreateModal" {{ $attributes }} >
|
||||
<x-form-modal submit="saveMosiac" wire:loading.class="opacity-50">
|
||||
<x-form-modal submit="saveMosaic" wire:loading.class="opacity-50">
|
||||
<x-slot name="title">
|
||||
{{ __('Project') }}
|
||||
{{ __('Mosaic') }}
|
||||
</x-slot>
|
||||
|
||||
<x-slot name="description">
|
||||
{{ __('Report generator for generating reports') }}
|
||||
{{ __('Mosaic generator for generating reports') }}
|
||||
</x-slot>
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -98,28 +98,8 @@ public static function reportDateProvider()
|
|||
];
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_calculate_mosaic_dependencies()
|
||||
{
|
||||
$project = Project::create([
|
||||
'name' => 'project_name',
|
||||
'download_path' => 'project_download_path',
|
||||
]);
|
||||
$projectReport = $project->reports()->create([
|
||||
'name' => 'name',
|
||||
'year' => 2021,
|
||||
'week' => 25,
|
||||
'path' => 'path/doc.pdf',
|
||||
]);
|
||||
|
||||
Bus::fake();
|
||||
$projectReport->schedule();
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use App\Jobs\ProjectDownloadTiffJob;
|
||||
use App\Jobs\ProjectMosiacGeneratorJob;
|
||||
use App\Models\Project;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
|
@ -81,6 +83,7 @@ public function when_not_all_mosaics_are_present_it_should_return_an_exception()
|
|||
//$lastDate->getWeekOfYear();
|
||||
$project->allMosaicsPresent($lastDate);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function when_all_mosaics_are_present_it_should_return_true()
|
||||
{
|
||||
|
|
@ -144,4 +147,45 @@ public function getMosiacFileListByEndDate_should_return_four_filenames()
|
|||
"week_50_2020.tif",
|
||||
], $list->toArray());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function when_getFileDownloadsFor_is_called_it_returns_a_collection_of_seven_downloads_jobs()
|
||||
{
|
||||
$project = Project::create([
|
||||
'name' => 'project_name',
|
||||
'download_path' => 'project_download_path',
|
||||
]);
|
||||
|
||||
$downloads = $project->getFileDownloadsFor(2023, 2);
|
||||
$this->assertCount(3 * 7, $downloads);
|
||||
$downloads->each(fn($job) => $this->assertInstanceOf(ProjectDownloadTiffJob::class, $job));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function when_getMosaicsFor_is_called_it_returns_a_collection_of_seven_downloads_jobs()
|
||||
{
|
||||
$project = Project::create([
|
||||
'name' => 'project_name',
|
||||
'download_path' => 'project_download_path',
|
||||
]);
|
||||
|
||||
$mosaics = $project->getMosaicsFor(2023, 2);
|
||||
$this->assertCount(3, $mosaics);
|
||||
$mosaics->each(fn($job) => $this->assertInstanceOf(ProjectMosiacGeneratorJob::class, $job));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_create_a_chain_of_batches_that_result_in_a_report()
|
||||
{
|
||||
$project = Project::create([
|
||||
'name' => 'project_name',
|
||||
'download_path' => 'project_download_path',
|
||||
]);
|
||||
|
||||
|
||||
Bus::fake();
|
||||
$project->scheduleReport(2023, 50);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue