Changed GetMosaicFor and all the functions call inside

This commit is contained in:
guillaume91 2024-06-18 15:17:57 +02:00
parent 318ea27642
commit 1317c20390
5 changed files with 93 additions and 26 deletions

View file

@ -2,18 +2,12 @@
namespace App\Jobs; namespace App\Jobs;
use App\Models\Project;
use App\Models\ProjectDownload;
use App\Models\ProjectMosaic;
use App\Models\ProjectReport;
use Illuminate\Bus\Batchable; use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Symfony\Component\Process\Process;
class NullJob implements ShouldQueue class NullJob implements ShouldQueue
{ {
@ -25,7 +19,7 @@ class NullJob implements ShouldQueue
*/ */
public function __construct() public function __construct()
{ {
logger('NullJob construct called'); logger('NullJob construct called');
} }
/** /**

View file

@ -8,6 +8,7 @@
use App\Models\ProjectDownload; use App\Models\ProjectDownload;
use App\Models\ProjectMosaic; use App\Models\ProjectMosaic;
use App\Models\ProjectReport; use App\Models\ProjectReport;
use Carbon\Carbon;
use Illuminate\Bus\Batchable; use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldBeUnique;
@ -18,6 +19,9 @@
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
/**
*
*/
class ProjectMosiacGeneratorJob implements ShouldQueue class ProjectMosiacGeneratorJob implements ShouldQueue
{ {
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@ -41,6 +45,8 @@ public function handle(): void
{ {
$weeksAgo = ProjectReport::weeksAgoForYearAndWeek($this->mosaic->year, $this->mosaic->week); $weeksAgo = ProjectReport::weeksAgoForYearAndWeek($this->mosaic->year, $this->mosaic->week);
//TODO change to end_date and offset
$projectFolder = base_path('../'); $projectFolder = base_path('../');
$command = [ $command = [
@ -74,28 +80,59 @@ public function handle(): void
} }
} }
public static function handleFor(Project $project, $year, $startWeekNumber) // public static function handleFor(Project $project, $year, $startWeekNumber)
// {
// logger("ProjectMosiacGeneratorJob::handleFor(week $startWeekNumber, year $year)");
// if ($project->hasInvalidMosaicFor($year, $startWeekNumber)) {
// logger("ProjecMosaicGeneratorJob::handleFor(week $startWeekNumber, year $year): InvalidMosaic.");
// return new NullJob();
// }
//
// $mosaic = $project->mosaics()->updateOrCreate(
// [
// 'year' => $year,
// 'week' => $startWeekNumber,
// ],
// [
// 'name' => sprintf('Week %d, %d', $startWeekNumber, $year),
// 'path' => sprintf('%s/%s/%s',
// $project->download_path,
// 'mosaics',
// ProjectMosaic::getFilenameForYearAndWeek($year, $startWeekNumber)
// ),
// 'year' => $year,
// 'week' => $startWeekNumber,
// ]);
// return new self($mosaic);
// }
/**
* Check if the project has a mosaic for given period.
*/
public static function handleFor(Project $project,Carbon $endDate, int $offset): NullJob|ProjectMosiacGeneratorJob
{ {
logger("ProjectMosiacGeneratorJob::handleFor(week $startWeekNumber, year $year)"); logger("ProjectMosiacGeneratorJob::handleFor($endDate, $offset)");
if ($project->hasInvalidMosaicFor($year, $startWeekNumber)) { if ($project->hasInvalidMosaicFor($endDate,$offset)) {
logger("ProjecMosaicGeneratorJob::handleFor(week $startWeekNumber, year $year): InvalidMosaic."); logger("ProjecMosaicGeneratorJob::handleFor(end_date: $endDate, offset: $offset): InvalidMosaic.");
return new NullJob(); return new NullJob();
} }
/**
* @var ProjectMosaic $mosaic
*/
$mosaic = $project->mosaics()->updateOrCreate( $mosaic = $project->mosaics()->updateOrCreate(
[ [
'year' => $year, 'end_date' => $endDate,
'week' => $startWeekNumber, 'offset' => $offset,
], ],
[ [
'name' => sprintf('Week %d, %d', $startWeekNumber, $year), 'name' => sprintf('Period %s - %s', $endDate->format('Y-m-d'), $endDate->copy()->subDays($offset)->format('Y-m-d')),
'path' => sprintf('%s/%s/%s', 'path' => sprintf('%s/%s/%s',
$project->download_path, $project->download_path,
'mosaics', 'mosaics',
ProjectMosaic::getFilenameForYearAndWeek($year, $startWeekNumber) ProjectMosaic::getFilenameByPeriod($endDate,$offset)
), ),
'year' => $year, 'end_date' => $endDate->format('Y-m-d'),
'week' => $startWeekNumber, 'offset' => $offset,
]); ]);
return new self($mosaic); return new self($mosaic);
} }

View file

@ -269,11 +269,33 @@ public function shouldSchedule(): bool
return false; return false;
} }
public function hasInvalidMosaicFor($year, $week): bool // TODO remove function when it's done
// public function hasInvalidMosaicFor($year, $week): bool
// {
//
// $dayOfWeekIso = Carbon::parse($this->mail_day)->dayOfWeekIso;
// $min_updated_at_date = now()
// ->setISODate($year, $week)
// ->startOfWeek()
// ->addDays($dayOfWeekIso - 1)
// ->format('Y-m-d');
//
// return $this->mosaics()
// ->where('updated_at', '>=', $min_updated_at_date)
// ->statusSuccess()
// ->where(['year' => $year, 'week' => $week])
// ->exists();
//
// }
public function hasInvalidMosaicFor(Carbon $endDate,int $offset): bool
{ {
// parameters : $
// check if the mail day happens the day before mosaic -> good
//TODO use end_date and offset
$dayOfWeekIso = Carbon::parse($this->mail_day)->dayOfWeekIso; $dayOfWeekIso = Carbon::parse($this->mail_day)->dayOfWeekIso;
$min_updated_at_date = now() $min_updated_at_date = $endDate
->setISODate($year, $week)
->startOfWeek() ->startOfWeek()
->addDays($dayOfWeekIso - 1) ->addDays($dayOfWeekIso - 1)
->format('Y-m-d'); ->format('Y-m-d');
@ -281,7 +303,7 @@ public function hasInvalidMosaicFor($year, $week): bool
return $this->mosaics() return $this->mosaics()
->where('updated_at', '>=', $min_updated_at_date) ->where('updated_at', '>=', $min_updated_at_date)
->statusSuccess() ->statusSuccess()
->where(['year' => $year, 'week' => $week]) ->where(['end_date' => $endDate, 'offset' => $offset])
->exists(); ->exists();
} }
@ -332,11 +354,15 @@ public function getFileDownloadsFor($year, $startWeekNumber):array
->toArray(); ->toArray();
} }
public function getMosaicsFor($year, $startWeekNumber) public function getMosaicsFor(Carbon $endDate, $offset= 7)
{ {
logger(sprintf('in Get Mosaics for year %s week %d', $year, $startWeekNumber)); logger(sprintf('in Get Mosaics for Period %s with %d offset', $endDate->format('Y-m-d'),$offset));
return collect(range(0, 3)) return collect(range(0, 3))
->map(fn($weekDiff) => ProjectMosiacGeneratorJob::handleFor($this, $year, $startWeekNumber - $weekDiff)) ->map(function ($periodDiff) use ($endDate, $offset) {
$periodEndDate = $endDate->copy()->subDays($offset*$periodDiff);
// $periodStartDate = $periodEndDate->copy()->subDays($offset-1);
return ProjectMosiacGeneratorJob::handleFor($this, $periodEndDate, $offset);
})
->filter() ->filter()
->toArray(); ->toArray();
} }

View file

@ -3,6 +3,7 @@
namespace App\Models; namespace App\Models;
use App\Events\ProjectMosaicStatus; use App\Events\ProjectMosaicStatus;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
@ -29,11 +30,21 @@ public static function getFilenameForYearAndWeek($year, $startWeekNumber)
return sprintf('week_%02d_%d.tif', $startWeekNumber, $year); return sprintf('week_%02d_%d.tif', $startWeekNumber, $year);
} }
public static function getFilenameByPeriod(Carbon $endDate,int $offset)
{
return sprintf('Period_%s_%s.tif', $endDate->format('Y-m-d'), $endDate->copy()->subDays($offset)->format('Y-m-d'));
}
public function project() public function project()
{ {
return $this->belongsTo(Project::class); return $this->belongsTo(Project::class);
} }
public function getStartDateAttribute()
{
return $this->end_date->subDay($this->offset);
}
protected static function booted(): void protected static function booted(): void
{ {
parent::booted(); parent::booted();

View file

@ -20,7 +20,6 @@ public function up(): void
}); });
ProjectMosaic::all()->each(function (ProjectMosaic $project_mosaic) { ProjectMosaic::all()->each(function (ProjectMosaic $project_mosaic) {
$end_date = Carbon::parse(sprintf('%s-W%02d',$project_mosaic->year,$project_mosaic->week))->addDay(Carbon::THURSDAY); $end_date = Carbon::parse(sprintf('%s-W%02d',$project_mosaic->year,$project_mosaic->week))->addDay(Carbon::THURSDAY);
logger('');
$project_mosaic->update(['end_date' => $end_date]); $project_mosaic->update(['end_date' => $end_date]);
}); });
Schema::table('project_mosaics', function (Blueprint $table) { Schema::table('project_mosaics', function (Blueprint $table) {