From 355dbfe984752dfa0ff4bf5f5e6dc448928fcd17 Mon Sep 17 00:00:00 2001 From: guillaume91 Date: Tue, 25 Jun 2024 16:50:30 +0200 Subject: [PATCH] [done] Notification downloads --- .../Components/DownloadNotification.php | 35 +++++++++++++++++-- .../app/Livewire/Projects/ProjectManager.php | 4 --- .../app/Livewire/Projects/Tabs/Settings.php | 21 ++++++----- laravel_app/app/Models/Project.php | 28 +++++++++++++++ laravel_app/app/Models/ProjectDownload.php | 1 + .../resources/views/layouts/app.blade.php | 2 -- .../download-notification.blade.php | 12 +++---- .../resources/views/projects/show.blade.php | 2 +- 8 files changed, 82 insertions(+), 23 deletions(-) diff --git a/laravel_app/app/Livewire/Components/DownloadNotification.php b/laravel_app/app/Livewire/Components/DownloadNotification.php index 2895ee6..9256d9e 100644 --- a/laravel_app/app/Livewire/Components/DownloadNotification.php +++ b/laravel_app/app/Livewire/Components/DownloadNotification.php @@ -2,14 +2,45 @@ namespace App\Livewire\Components; +use App\Models\Project; +use Livewire\Attributes\On; use Livewire\Component; class DownloadNotification extends Component { public $show_download_notification; + public Project $project; - public function render() + public function mount(Project $project) { - return view('livewire.components.download-notification'); + $this->show_download_notification = session()->get('show-download.'.$project->id,false); + $this->project = $project; + } + + public function getSessionKey() + { + return 'show-download.'.$this->project->id; + } + + #[On('download_notify')] + public function open() + { + $this->show_download_notification = true; + session([$this->getSessionKey()=>true]); + } + public function close(): void + { + $this->show_download_notification = false; + session([$this->getSessionKey()=>false]); + } + + public function startDownloads() + { + $this->project->handleMissingDownloads(); + $this->close(); + $this->dispatch('notify',message:'Downloads added to the queue.',type:'success'); + } + + } diff --git a/laravel_app/app/Livewire/Projects/ProjectManager.php b/laravel_app/app/Livewire/Projects/ProjectManager.php index 4b9ddd1..371f316 100644 --- a/laravel_app/app/Livewire/Projects/ProjectManager.php +++ b/laravel_app/app/Livewire/Projects/ProjectManager.php @@ -5,14 +5,10 @@ use App\Models\Project; use App\Models\ProjectEmailRecipient; use App\Rules\HarvestFile; -use Carbon\Carbon; -use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Validator; use Illuminate\Validation\Rule; use Livewire\Component; use Livewire\WithFileUploads; -use Maatwebsite\Excel\Facades\Excel; -use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDate; class ProjectManager extends Component { diff --git a/laravel_app/app/Livewire/Projects/Tabs/Settings.php b/laravel_app/app/Livewire/Projects/Tabs/Settings.php index ba0a941..cbc66a3 100644 --- a/laravel_app/app/Livewire/Projects/Tabs/Settings.php +++ b/laravel_app/app/Livewire/Projects/Tabs/Settings.php @@ -39,11 +39,6 @@ class Settings extends Component * @return void */ public function mount(Project $project) - { - $this->loadFormData($project); - } - - private function loadFormData(Project $project) { $this->formData = $project->toArray(); $this->formData['mail_recipients'] = $project->emailRecipients->toArray() ?: [ @@ -84,8 +79,10 @@ public function saveSettings() $this->validateEmailSettingsForm(); $this->shouldReload(); Project::saveWithFormData($this->formData); + $this->shouldDownload(); + //check if the project name changed if($this->isDirty) redirect()->route('project.show',[$this->formData['name'],$this->formData['id'],'settings']); - $this->dispatch('download_notify',title:'Download',message:'hallo'); +// $this->dispatch('download_notify',title:'Download',message:'hallo'); $this->dispatch('saved'); } @@ -94,8 +91,16 @@ public function shouldReload() if (Project::find($this->formData['id'])?->name == $this->formData['name']) return; $this->isDirty = true; } - - + public function shouldDownload(){ + /** + * @var Project $project + */ + $project = Project::find($this->formData['id']); + if($project->hasMissingDownloadsDateInHarvestFile()){ + $this->dispatch('download_notify'); + session(['show-download.'.$project->id=>true]); + } + } public function addEmailRecipient() { diff --git a/laravel_app/app/Models/Project.php b/laravel_app/app/Models/Project.php index b6ab522..4bdd8cd 100644 --- a/laravel_app/app/Models/Project.php +++ b/laravel_app/app/Models/Project.php @@ -131,6 +131,11 @@ 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); @@ -343,6 +348,29 @@ public function getMosaicsFor(Carbon $endDate, int $offset= 7): array ->toArray(); } + public function handleMissingDownloads() + { + $this->getMissingDownloadsDateInHarvestFile() + ->each(function(Carbon $date){ + ProjectDownloadTiffJob::handleForDate($this,$date); + }); + } + + public function hasMissingDownloadsDateInHarvestFile():bool + { + return $this->getMissingDownloadsDateInHarvestFile()->count() > 0; + } + + public function getMissingDownloadsDateInHarvestFile() + { + if(!$this->harvest_json_path || !$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){ diff --git a/laravel_app/app/Models/ProjectDownload.php b/laravel_app/app/Models/ProjectDownload.php index fe773bc..003c4b9 100644 --- a/laravel_app/app/Models/ProjectDownload.php +++ b/laravel_app/app/Models/ProjectDownload.php @@ -7,6 +7,7 @@ use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Query\Builder; class ProjectDownload extends Model { diff --git a/laravel_app/resources/views/layouts/app.blade.php b/laravel_app/resources/views/layouts/app.blade.php index 9cdbf0f..e9a554b 100644 --- a/laravel_app/resources/views/layouts/app.blade.php +++ b/laravel_app/resources/views/layouts/app.blade.php @@ -37,8 +37,6 @@ - - @stack('modals') @stack('map') diff --git a/laravel_app/resources/views/livewire/components/download-notification.blade.php b/laravel_app/resources/views/livewire/components/download-notification.blade.php index e77526c..008b2aa 100644 --- a/laravel_app/resources/views/livewire/components/download-notification.blade.php +++ b/laravel_app/resources/views/livewire/components/download-notification.blade.php @@ -1,14 +1,12 @@ -
name)', + message: '@lang("Some TIF images are missing for this dataset, would you like to download them ?")' }" x-show="open" x-cloak - @download_notify.window="message = event.detail.message; title = event.detail.title;open = true;" >

- +
diff --git a/laravel_app/resources/views/projects/show.blade.php b/laravel_app/resources/views/projects/show.blade.php index 5715238..5ecc990 100644 --- a/laravel_app/resources/views/projects/show.blade.php +++ b/laravel_app/resources/views/projects/show.blade.php @@ -28,6 +28,6 @@
Menu Component not found.
@endswitch
- +