SmartCane/laravel_app/app/Models/Project.php
guillaume91 f5925ed758 okay
2024-06-26 14:45:39 +02:00

391 lines
13 KiB
PHP

<?php
namespace App\Models;
use App\Jobs\ProjectDownloadTiffJob;
use App\Jobs\ProjectMosiacGeneratorJob;
use App\Jobs\ProjectReportGeneratorJob;
use Carbon\CarbonPeriod;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Facades\Excel;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDate;
class Project extends Model
{
use HasFactory;
protected $casts = [
'min_harvest_date' => 'date'
];
protected $fillable = [
'name',
'mail_template',
'mail_subject',
'mail_frequency',
'mail_day',
'mail_scheduled',
'download_path',
'pivot_json_path',
'span_json_path',
'harvest_json_path',
'min_harvest_date'
];
public static function saveWithFormData(mixed $formData)
{
$uniqueIdentifier = ['id' => $formData['id'] ?? null];
/**
* @var Project $project
*/
$project = Project::updateOrCreate($uniqueIdentifier, $formData);
$baseFrom = 'livewire-tmp/';
$baseTo = $project->download_path.'/Data/';
if ($formData['pivot_file']) {
Storage::copy($baseFrom.$formData['pivot_file']['tmpFilename'],$baseTo.'pivot.geojson');
$project->update(['pivot_json_path' => $baseTo.'pivot.geojson']);
}
if ($formData['span_file']) {
Storage::copy($baseFrom.$formData['span_file']['tmpFilename'],$baseTo.'span.geojson');
$project->update(['span_json_path' => $baseTo.'span.geojson']);
}
if ($formData['harvest_file']) {
Storage::copy($baseFrom.$formData['harvest_file']['tmpFilename'],$baseTo.'harvest.'.$formData['harvest_file']['extension']);
if($project->update(['harvest_json_path' => $baseTo.'harvest.'.$formData['harvest_file']['extension']])) {
$project->setMinHarvestDate();
}
}
}
private function upsertMailRecipients($formData)
{
$mailRecipientsData = array_map(function ($mailRecipient) {
$mailRecipient['project_id'] = $this->id;
unset($mailRecipient['created_at']);
unset($mailRecipient['updated_at']);
$mailRecipient['id'] ??= null;
return $mailRecipient;
}, $formData['mail_recipients'] ?? []);
ProjectEmailRecipient::upsert(
$mailRecipientsData,
['id', 'project_id'],
['name', 'email',]
);
}
public function getMosaicPath()
{
return sprintf('%s/%s', $this->download_path, 'weekly_mosaic');
}
public function getMosaicList(): Collection
{
return collect(Storage::files($this->getMosaicPath()))
->filter(fn($filename) => Str::endsWith($filename, '.tif'))
->values();
}
protected static function boot()
{
parent::boot(); // TODO: Change the autogenerated stub
static::deleting(function ($project) {
$project->emailRecipients()->delete();
$project->mailings()->each(function ($mailing) {
$mailing->attachments()->delete();
$mailing->recipients()->delete();
});
$project->mailings()->delete();
});
}
public function reports()
{
return $this->hasMany(ProjectReport::class);
}
public function getAttachmentPathAttribute()
{
return storage_path(sprintf('%s/attachments', $this->download_path));
return '/storage/'.$this->download_path.'/attachments';
}
public function emailRecipients()
{
return $this->hasMany(ProjectEmailRecipient::class);
}
public function mailings()
{
return $this->hasMany(ProjectMailing::class);
}
public function downloads(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(ProjectDownload::class);
}
public function nonFailedDownloads(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(ProjectDownload::class)->where('status','<>','failed');
}
public function mosaics(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(ProjectMosaic::class);
}
public function allMosaicsPresent(Carbon $endDate): bool
{
// end date is in the future
if ($endDate->isFuture()) {
throw new \Exception('Mosaic can\'t be generated for the future. Change the end date.');
}
$mosaicsNotPresentInFilesystem = $this->getMissingMosaicsInFileSystem($endDate);
if ($mosaicsNotPresentInFilesystem->count() === 0) {
return true;
}
$message = sprintf(
'Missing mosaics: %s',
$mosaicsNotPresentInFilesystem->implode(', ')
);
throw new \Exception($message);
}
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
{
for ($i = 0; $i < 4; $i++) {
yield(ProjectMosaic::getFilenameByPeriod($endDate->copy()->subDays($offset*$i),$offset));
}
}
public function getMosiacList()
{
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);
})
->values();
}
public static function getAllDatesOfWeeksInYear($year, $weekNumber): Collection
{
$startOfWeek = Carbon::now()->setISODate($year, $weekNumber)->startOfWeek();
$dates = collect([]);
for ($day = 0; $day < 7; $day++) {
$dates->push((clone $startOfWeek)->addDays($day)->toDateString());
}
return $dates;
}
public function allMergedTiffsPresent(Collection $haystack, Collection $needles)
{
$needlesNotInHaystack = $needles->filter(function ($needle) use ($haystack) {
return !$haystack->contains(function ($item) use ($needle) {
return str_contains($item, $needle);
});
});
if ($needlesNotInHaystack->count() > 0) {
$message = sprintf(
'Missing merged tiffs: %s',
$needlesNotInHaystack->implode(', ')
);
throw new \Exception($message);
}
return true;
}
public function getMergedTiffList()
{
return collect(Storage::files($this->download_path.'/merged_final_tif'))
->filter(fn($file) => Str::endsWith($file, '.tif'))
->sortByDesc(function ($file) {
$parts = explode('_', str_replace('.tif', '', $file));
$date = $parts[1];
return $date;
})
->values();
}
public function hasPendingDownload(): bool
{
return $this->downloads()->statusPending()->count() > 0;
}
public function hasPendingReport(): bool
{
return $this->reports()->statusPending()->count() > 0;
}
public function hasPendingMosaic(): bool
{
return $this->mosaics()->statusPending()->count() > 0;
}
public function startDownload(Carbon $date)
{
$downloadRequest = $this->downloads()->updateOrCreate(
[
'project_id' => $this->id, // of een andere manier om project_id te bepalen
'name' => sprintf('%s.tif', $date->format('Y-m-d')),
],
[
'path' => sprintf('%s/%s/%s.tif', $this->download_path, 'merged_final_tif', $date->format('Y-m-d')),
]
);
ProjectDownloadTiffJob::dispatch($downloadRequest, $date);
}
public function schedule()
{
//TODO check the ranges.
$this->scheduleReport();
}
public function shouldSchedule(): bool
{
if (strtolower($this->mail_day) === strtolower(now()->englishDayOfWeek)) {
return strtolower($this->mail_frequency) === 'weekly' || now()->day <= 7;
}
return false;
}
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 = $endDate
->startOfWeek()
->addDays($dayOfWeekIso - 1)
->format('Y-m-d');
return $this->mosaics()
->where('updated_at', '>=', $min_updated_at_date)
->statusSuccess()
->where(['end_date' => $endDate, 'offset' => $offset])
->exists();
}
public function scheduleReport(?Carbon $endDate = null, ?int $offset = null)
{
if($endDate->isFuture() || $endDate->isToday() || $offset <= 0){
logger('EndDate is today or in the future.');
$endDate = null;
$offset = null;
}
$endDate ??= Carbon::yesterday();
$offset ??= 7;
Bus::chain([
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(Carbon $endDate, int $offset = 7, $sendMail = false): ProjectReportGeneratorJob
{
$report = $this->reports()->create([
'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(Carbon $endDate, int $offset=7):array
{
$startOfRange = (clone $endDate)->subdays(4*$offset-1);
$dateRange = CarbonPeriod::create($startOfRange, $endDate);
return collect($dateRange)
->map(fn($date) => ProjectDownloadTiffJob::handleForDate($this, $date))
->filter()
->toArray();
}
public function getMosaicsFor(Carbon $endDate, int $offset= 7): array
{
logger(sprintf('in Get Mosaics for Period %s with %d offset', $endDate->format('Y-m-d'),$offset));
return collect(range(0, 3))
->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();
}
public function handleMissingDownloads()
{
$this->getMissingDownloads()
->each(function(Carbon $date){
dispatch(ProjectDownloadTiffJob::handleForDate($this,$date));
});
}
public function hasMissingDownloadsDateInHarvestFile():bool
{
return $this->getMissingDownloads()->count() > 0;
}
public function getMissingDownloads():Collection
{
if(!$this->min_harvest_date){
return collect([]);
}
$harvest_dates = $this->nonFailedDownloads()->get()->map(fn($d)=>$d->date);
$all_dates = collect(CarbonPeriod::create($this->min_harvest_date,'1 day',now())->toArray());
return $all_dates->diff($harvest_dates);
}
public function setMinHarvestDate():bool
{
if(!$this->harvest_json_path){
return false;
}
return $this->update(['min_harvest_date'=> $this->getMinimumDateFromHarvestExcelFile()->format('Y-m-d')]);
}
private function getMinimumDateFromHarvestExcelFile():Carbon
{
$value = Storage::disk('local')->path($this->harvest_json_path);
$data = Excel::toCollection(new \App\Imports\ExcelFileImport(),$value);
$season_start_index = $data->first()->first()->search('season_start');
return collect($data->first()->slice(1))->reduce(function($carry,$value,$key) use ($season_start_index){
return min($carry,Carbon::instance(SharedDate::excelToDateTimeObject($value[$season_start_index])));
},now());
}
}