Changed GetMosaicFor and all the functions call inside
This commit is contained in:
parent
318ea27642
commit
1317c20390
|
|
@ -2,18 +2,12 @@
|
|||
|
||||
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\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class NullJob implements ShouldQueue
|
||||
{
|
||||
|
|
@ -25,7 +19,7 @@ class NullJob implements ShouldQueue
|
|||
*/
|
||||
public function __construct()
|
||||
{
|
||||
logger('NullJob construct called');
|
||||
logger('NullJob construct called');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
use App\Models\ProjectDownload;
|
||||
use App\Models\ProjectMosaic;
|
||||
use App\Models\ProjectReport;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Bus\Batchable;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
|
|
@ -18,6 +19,9 @@
|
|||
use Illuminate\Queue\SerializesModels;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class ProjectMosiacGeneratorJob implements ShouldQueue
|
||||
{
|
||||
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
|
@ -41,6 +45,8 @@ public function handle(): void
|
|||
{
|
||||
$weeksAgo = ProjectReport::weeksAgoForYearAndWeek($this->mosaic->year, $this->mosaic->week);
|
||||
|
||||
//TODO change to end_date and offset
|
||||
|
||||
$projectFolder = base_path('../');
|
||||
|
||||
$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)");
|
||||
if ($project->hasInvalidMosaicFor($year, $startWeekNumber)) {
|
||||
logger("ProjecMosaicGeneratorJob::handleFor(week $startWeekNumber, year $year): InvalidMosaic.");
|
||||
logger("ProjectMosiacGeneratorJob::handleFor($endDate, $offset)");
|
||||
if ($project->hasInvalidMosaicFor($endDate,$offset)) {
|
||||
logger("ProjecMosaicGeneratorJob::handleFor(end_date: $endDate, offset: $offset): InvalidMosaic.");
|
||||
return new NullJob();
|
||||
}
|
||||
|
||||
/**
|
||||
* @var ProjectMosaic $mosaic
|
||||
*/
|
||||
$mosaic = $project->mosaics()->updateOrCreate(
|
||||
[
|
||||
'year' => $year,
|
||||
'week' => $startWeekNumber,
|
||||
'end_date' => $endDate,
|
||||
'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',
|
||||
$project->download_path,
|
||||
'mosaics',
|
||||
ProjectMosaic::getFilenameForYearAndWeek($year, $startWeekNumber)
|
||||
ProjectMosaic::getFilenameByPeriod($endDate,$offset)
|
||||
),
|
||||
'year' => $year,
|
||||
'week' => $startWeekNumber,
|
||||
'end_date' => $endDate->format('Y-m-d'),
|
||||
'offset' => $offset,
|
||||
]);
|
||||
return new self($mosaic);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -269,11 +269,33 @@ public function shouldSchedule(): bool
|
|||
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;
|
||||
$min_updated_at_date = now()
|
||||
->setISODate($year, $week)
|
||||
$min_updated_at_date = $endDate
|
||||
->startOfWeek()
|
||||
->addDays($dayOfWeekIso - 1)
|
||||
->format('Y-m-d');
|
||||
|
|
@ -281,7 +303,7 @@ 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();
|
||||
|
||||
}
|
||||
|
|
@ -332,11 +354,15 @@ public function getFileDownloadsFor($year, $startWeekNumber):array
|
|||
->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))
|
||||
->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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Models;
|
||||
|
||||
use App\Events\ProjectMosaicStatus;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
|
|
@ -29,11 +30,21 @@ public static function getFilenameForYearAndWeek($year, $startWeekNumber)
|
|||
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()
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
|
||||
public function getStartDateAttribute()
|
||||
{
|
||||
return $this->end_date->subDay($this->offset);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
parent::booted();
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ public function up(): void
|
|||
});
|
||||
ProjectMosaic::all()->each(function (ProjectMosaic $project_mosaic) {
|
||||
$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]);
|
||||
});
|
||||
Schema::table('project_mosaics', function (Blueprint $table) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue