added log download to user interface

This commit is contained in:
Martin Folkerts 2024-08-30 17:13:48 +02:00
parent 4beca30d2e
commit 5c1a320b17
6 changed files with 104 additions and 1 deletions

View file

@ -15,7 +15,7 @@ public function index()
public function show(string $projectName,?string $currentTab = null)
{
if($project = Project::firstWhere([['name',$projectName]])) {
$availableTabs = ['downloads', 'mosaics', 'reports', 'mailings', 'settings', 'exports'];
$availableTabs = ['downloads', 'mosaics', 'reports', 'mailings', 'settings', 'exports', 'logs'];
return in_array($currentTab, $availableTabs) ? view('projects.show', compact(['project', 'currentTab'])) : redirect(route('project.show', [$projectName,'downloads']));
}
return abort(404);

View file

@ -0,0 +1,33 @@
<?php
namespace App\Livewire\Projects\Tabs;
use App\Jobs\ProjectMosiacGeneratorJob;
use App\Models\Project;
use App\Models\ProjectMosaic;
use App\ProjectLogger;
use Carbon\Carbon;
use Illuminate\Support\Facades\Response;
use Livewire\Component;
use Livewire\WithPagination;
class Logs extends Component
{
use WithPagination;
public Project $project;
public function download($file_name) {
return Response::download(ProjectLogger::getAsList($this->project)->where('name', $file_name)->first()->path);
}
public function render()
{
$logs = ProjectLogger::getAsList($this->project);
return view('livewire.projects.tabs.logs', [
'logs' => $logs,
]);
}
}

View file

@ -3,6 +3,7 @@
namespace App;
use App\Models\Project;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
class ProjectLogger
@ -13,4 +14,20 @@ public static function log(Project $project, $message)
Storage::makeDirectory(dirname($filePath));
file_put_contents(Storage::path($filePath), now()->format('Y-m-d H:i:s') . ': ' . $message . PHP_EOL, FILE_APPEND);
}
public static function getAsList(Project $project)
{
$logs = [];
$logPath = $project->download_path . '/logs';
if(Storage::exists($logPath)) {
$files = Storage::files($logPath);
foreach ($files as $file) {
$logs[] = (object) [
'name' => basename($file),
'path' => Storage::path( $project->download_path . '/logs/'. basename($file)),
];
}
}
return collect($logs);
}
}

View file

@ -5,4 +5,5 @@
<a class=" capitalize font-semibold text-md hover:rounded-lg hover:bg-gray-50 hover:text-indigo-600 hover:border-0 py-2 pl-2 pr-3 leading-6 whitespace-nowrap gap-2 flex items-baseline group text-gray-700 {{!($activeTab == 'mailings') ?: 'rounded-lg bg-gray-50 text-indigo-600 border-0'}}" href="{{route('project.show',[$projectName,'mailings'])}}"><i class="group-hover:text-indigo-600 w-6 text-gray-400 fa-regular fa-envelope"></i>Mailings</a>
<a class=" capitalize font-semibold text-md hover:rounded-lg hover:bg-gray-50 hover:text-indigo-600 hover:border-0 py-2 pl-2 pr-3 leading-6 whitespace-nowrap gap-2 flex items-baseline group text-gray-700 {{!($activeTab == 'settings') ?: 'rounded-lg bg-gray-50 text-indigo-600 border-0'}}" href="{{route('project.show',[$projectName,'settings'])}}"><i class="group-hover:text-indigo-600 w-6 text-gray-400 fa-solid fa-gear"></i>Settings</a>
<a class=" capitalize font-semibold text-md hover:rounded-lg hover:bg-gray-50 hover:text-indigo-600 hover:border-0 py-2 pl-2 pr-3 leading-6 whitespace-nowrap gap-2 flex items-baseline group text-gray-700 {{!($activeTab == 'exports') ?: 'rounded-lg bg-gray-50 text-indigo-600 border-0'}}" href="{{route('project.show',[$projectName,'exports'])}}"><i class="group-hover:text-indigo-600 w-6 text-gray-400 fa-solid fa-download"></i>Exports</a>
<a class=" capitalize font-semibold text-md hover:rounded-lg hover:bg-gray-50 hover:text-indigo-600 hover:border-0 py-2 pl-2 pr-3 leading-6 whitespace-nowrap gap-2 flex items-baseline group text-gray-700 {{!($activeTab == 'exports') ?: 'rounded-lg bg-gray-50 text-indigo-600 border-0'}}" href="{{route('project.show',[$projectName,'logs'])}}"><i class="group-hover:text-indigo-600 w-6 text-gray-400 fa-solid fa-regular fa-file"></i>Logs</a>
</div>

View file

@ -0,0 +1,49 @@
<div>
<div class="px-4">
<div class="sm:flex sm:flex-col sm:items-center">
<div class="flex flex-col md:flex-row justify-between w-full my-4">
<h1 class="text-base font-semibold leading-6 text-gray-900">Logs</h1>
</div>
<div class="flex flex-col md:flex-row mt-4 items-center md:justify-between w-full gap-2">
</div>
</div>
<div class="mt-8 flow-root">
<div class="">
<div class="relative">
<div class="inline-block min-w-full py-2 align-middle">
<table class="min-w-full divide-y divide-gray-300">
<thead>
<tr>
<th scope="col"
class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6 lg:pl-8">
{{__('Date')}}
</th>
<th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">
{{__('Open')}}
</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200 bg-white">
@foreach($logs as $log)
<tr>
<td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6 lg:pl-8">{{ $log->name }}</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
<button wire:click="download('{{ $log->name }}')">Open</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="pt-4 flex justify-between items-center">
<div class="text-gray-700 text-sm">
Results: {{ $logs->count() }}
</div>
</div>
</div>
</div>
</div>
</div>
</div>

View file

@ -27,6 +27,9 @@
@case('exports')
<livewire:projects.tabs.exports :project="$project"></livewire:projects.tabs.exports>
@break
@case('logs')
<livewire:projects.tabs.logs :project="$project"></livewire:projects.tabs.logs>
@break
@default
<div class="flex w-full h-screen justify-center items-center"> Menu Component not found.</div>
@endswitch