This commit is contained in:
guillaume91 2024-06-24 09:19:35 +02:00
parent 3c3c110005
commit 5031421477

View file

@ -13,6 +13,7 @@
use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use function Livewire\off;
class Project extends Model class Project extends Model
{ {
@ -74,13 +75,7 @@ public function getMosaicPath()
public function getMosaicList(): Collection public function getMosaicList(): Collection
{ {
return collect(Storage::files($this->getMosaicPath())) return collect(Storage::files($this->getMosaicPath()))
->filter(fn($file) => Str::endsWith($file, '.tif')) ->filter(fn($filename) => Str::endsWith($filename, '.tif'))
->sortByDesc(function ($file) {
$parts = explode('_', str_replace('.tif', '', $file));
$week = $parts[1];
$year = $parts[2];
return $year.sprintf('%02d', $week);
})
->values(); ->values();
} }
@ -135,16 +130,9 @@ public function allMosaicsPresent(Carbon $endDate): bool
{ {
// end date is in the future // end date is in the future
if ($endDate->isFuture()) { if ($endDate->isFuture()) {
throw new \Exception('Mosaic can be generated for the future. Change the end date.'); throw new \Exception('Mosaic can\'t be generated for the future. Change the end date.');
} }
$mosaicsNotPresentInFilesystem = $this->getMissingMosaicsInFileSystem($endDate);
$mosaicsNotPresentInFilesystem = $this->getMosaicFilenameListByEndDate($endDate)
->filter(function ($filename) {
return !$this->getMosaicList()->contains(function ($mosaicFilename) use ($filename) {
// TODO check the value of the week leading 0
return Str::endsWith($mosaicFilename, substr($filename, -16));
});
});
if ($mosaicsNotPresentInFilesystem->count() === 0) { if ($mosaicsNotPresentInFilesystem->count() === 0) {
return true; return true;
} }
@ -155,21 +143,21 @@ public function allMosaicsPresent(Carbon $endDate): bool
throw new \Exception($message); throw new \Exception($message);
} }
public static function getMosaicFilenameListByEndDate(Carbon $endDate): Collection public function getMissingMosaicsInFileSystem(Carbon $endDate)
{
return collect($this->getMosaicFilenameListByEndDate($endDate))
->filter(function ($filename) {
return !$this->getMosaicList()->contains(function ($mosaicFilename) use ($filename) {
return Str::endsWith($mosaicFilename, substr($filename, -16));
});
});
}
public static function getMosaicFilenameListByEndDate(Carbon $endDate, int $offset=7): \Generator
{ {
$result = collect([]);
for ($i = 0; $i < 4; $i++) { for ($i = 0; $i < 4; $i++) {
$week = $endDate->weekOfYear; yield(ProjectMosaic::getFilenameByPeriod($endDate->copy()->subDays($offset*$i),$offset));
$year = $endDate->year;
if ($week === 53) {
$year--;
}
$result->add(sprintf('week_%02d_%04d.tif', $week, $year));
$endDate = $endDate->subWeek();
} }
return $result;
} }
public function getMosiacList() public function getMosiacList()
@ -258,6 +246,7 @@ public function startDownload(Carbon $date)
public function schedule() public function schedule()
{ {
//TODO check the ranges.
$this->scheduleReport(); $this->scheduleReport();
} }
@ -269,11 +258,13 @@ public function shouldSchedule(): bool
return false; return false;
} }
public function hasInvalidMosaicFor($year, $week): bool
public function hasInvalidMosaicFor(Carbon $endDate,int $offset): bool
{ {
// parameters : $
// check if the mail day happens the day before mosaic -> good
$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,50 +272,46 @@ 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();
} }
public function scheduleReport($year = null, $week = null) public function scheduleReport(?Carbon $endDate = null, ?int $offset = null)
{ {
// if year, week is in the future set year and week to null; if($endDate->isFuture() || $endDate->isToday() || $offset <= 0){
if (now()->year < $year || (now()->year === $year && now()->weekOfYear < $week)) { logger('EndDate is today or in the future.');
$year = null; $endDate = null;
$week = null; $offset = null;
logger('year and week is in the future default to now');
} }
$year = $year ?? now()->year; $endDate ??= Carbon::yesterday();
$week = $week ?? now()->weekOfYear; $offset ??= 7;
Bus::chain([ Bus::chain([
Bus::batch($this->getFileDownloadsFor($year, $week)), Bus::batch($this->getFileDownloadsFor($endDate, $offset)),
Bus::batch($this->getMosaicsFor($year, $week)), Bus::batch($this->getMosaicsFor($endDate, $offset)),
Bus::batch([$this->getReportFor($year, $week,true)]), Bus::batch([$this->getReportFor($endDate, $offset,true)]),
]) ])
->dispatch(); ->dispatch();
return "done"; return "done";
} }
public function getReportFor($year, $week, $sendMail = false) public function getReportFor(Carbon $endDate, int $offset = 7, $sendMail = false): ProjectReportGeneratorJob
{ {
$report = $this->reports()->create([ $report = $this->reports()->create([
'name' => 'Report for week '.$week.' of '.$year, 'name' => 'Report of the '.$endDate->format('d-m-Y').' from the past '.$offset.' days',
'week' => $week, 'end_date' => $endDate,
'year' => $year, 'offset' => $offset,
'path' => 'reports/week_'.$week.'_'.$year.'.docx', 'path' => 'reports/'.ProjectReport::getFileName($endDate,$offset).'.docx',
]); ]);
return (new ProjectReportGeneratorJob($report, $sendMail)); return (new ProjectReportGeneratorJob($report, $sendMail));
} }
public function getFileDownloadsFor($year, $startWeekNumber):array public function getFileDownloadsFor(Carbon $endDate, int $offset=7):array
{ {
$endOfRange = now()->setISODate($year, $startWeekNumber)->endOfWeek(); $startOfRange = (clone $endDate)->subdays(4*$offset-1);
$startOfRange = (clone $endOfRange)->subWeeks(3)->startOfWeek(); $dateRange = CarbonPeriod::create($startOfRange, $endDate);
$dateRange = CarbonPeriod::create($startOfRange, $endOfRange);
return collect($dateRange) return collect($dateRange)
->map(fn($date) => ProjectDownloadTiffJob::handleForDate($this, $date)) ->map(fn($date) => ProjectDownloadTiffJob::handleForDate($this, $date))
@ -332,11 +319,15 @@ public function getFileDownloadsFor($year, $startWeekNumber):array
->toArray(); ->toArray();
} }
public function getMosaicsFor($year, $startWeekNumber) public function getMosaicsFor(Carbon $endDate, int $offset= 7): array
{ {
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();
} }