From 50314214775b89ca88291d897940087e7b114328 Mon Sep 17 00:00:00 2001 From: guillaume91 Date: Mon, 24 Jun 2024 09:19:35 +0200 Subject: [PATCH] wip --- laravel_app/app/Models/Project.php | 105 +++++++++++++---------------- 1 file changed, 48 insertions(+), 57 deletions(-) diff --git a/laravel_app/app/Models/Project.php b/laravel_app/app/Models/Project.php index a2c719e..8e3eb53 100644 --- a/laravel_app/app/Models/Project.php +++ b/laravel_app/app/Models/Project.php @@ -13,6 +13,7 @@ use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; +use function Livewire\off; class Project extends Model { @@ -74,13 +75,7 @@ public function getMosaicPath() public function getMosaicList(): Collection { return collect(Storage::files($this->getMosaicPath())) - ->filter(fn($file) => Str::endsWith($file, '.tif')) - ->sortByDesc(function ($file) { - $parts = explode('_', str_replace('.tif', '', $file)); - $week = $parts[1]; - $year = $parts[2]; - return $year.sprintf('%02d', $week); - }) + ->filter(fn($filename) => Str::endsWith($filename, '.tif')) ->values(); } @@ -135,16 +130,9 @@ public function allMosaicsPresent(Carbon $endDate): bool { // end date is in the future 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->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)); - }); - }); + $mosaicsNotPresentInFilesystem = $this->getMissingMosaicsInFileSystem($endDate); if ($mosaicsNotPresentInFilesystem->count() === 0) { return true; } @@ -155,21 +143,21 @@ public function allMosaicsPresent(Carbon $endDate): bool 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++) { - $week = $endDate->weekOfYear; - $year = $endDate->year; - if ($week === 53) { - $year--; - } - - $result->add(sprintf('week_%02d_%04d.tif', $week, $year)); - - $endDate = $endDate->subWeek(); + yield(ProjectMosaic::getFilenameByPeriod($endDate->copy()->subDays($offset*$i),$offset)); } - return $result; } public function getMosiacList() @@ -258,6 +246,7 @@ public function startDownload(Carbon $date) public function schedule() { + //TODO check the ranges. $this->scheduleReport(); } @@ -269,11 +258,13 @@ public function shouldSchedule(): bool 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; - $min_updated_at_date = now() - ->setISODate($year, $week) + $min_updated_at_date = $endDate ->startOfWeek() ->addDays($dayOfWeekIso - 1) ->format('Y-m-d'); @@ -281,50 +272,46 @@ public function hasInvalidMosaicFor($year, $week): bool return $this->mosaics() ->where('updated_at', '>=', $min_updated_at_date) ->statusSuccess() - ->where(['year' => $year, 'week' => $week]) + ->where(['end_date' => $endDate, 'offset' => $offset]) ->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 (now()->year < $year || (now()->year === $year && now()->weekOfYear < $week)) { - $year = null; - $week = null; - logger('year and week is in the future default to now'); + if($endDate->isFuture() || $endDate->isToday() || $offset <= 0){ + logger('EndDate is today or in the future.'); + $endDate = null; + $offset = null; } - $year = $year ?? now()->year; - $week = $week ?? now()->weekOfYear; - + $endDate ??= Carbon::yesterday(); + $offset ??= 7; Bus::chain([ - Bus::batch($this->getFileDownloadsFor($year, $week)), - Bus::batch($this->getMosaicsFor($year, $week)), - Bus::batch([$this->getReportFor($year, $week,true)]), + Bus::batch($this->getFileDownloadsFor($endDate, $offset)), + Bus::batch($this->getMosaicsFor($endDate, $offset)), + Bus::batch([$this->getReportFor($endDate, $offset,true)]), ]) ->dispatch(); return "done"; } - public function getReportFor($year, $week, $sendMail = false) + public function getReportFor(Carbon $endDate, int $offset = 7, $sendMail = false): ProjectReportGeneratorJob { $report = $this->reports()->create([ - 'name' => 'Report for week '.$week.' of '.$year, - 'week' => $week, - 'year' => $year, - 'path' => 'reports/week_'.$week.'_'.$year.'.docx', + 'name' => 'Report of the '.$endDate->format('d-m-Y').' from the past '.$offset.' days', + 'end_date' => $endDate, + 'offset' => $offset, + 'path' => 'reports/'.ProjectReport::getFileName($endDate,$offset).'.docx', ]); 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 $endOfRange)->subWeeks(3)->startOfWeek(); - - $dateRange = CarbonPeriod::create($startOfRange, $endOfRange); + $startOfRange = (clone $endDate)->subdays(4*$offset-1); + $dateRange = CarbonPeriod::create($startOfRange, $endDate); return collect($dateRange) ->map(fn($date) => ProjectDownloadTiffJob::handleForDate($this, $date)) @@ -332,11 +319,15 @@ public function getFileDownloadsFor($year, $startWeekNumber):array ->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)) - ->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() ->toArray(); }