Performance Tweaks

This commit is contained in:
JonatanRek 2024-08-07 10:30:51 +02:00
parent b486cad1ad
commit 1254f7015b
2 changed files with 31 additions and 7 deletions

View File

@ -3,7 +3,10 @@
namespace App\Livewire\Maintenance;
use App\Models\MaintenanceHistory;
use App\Models\MaintenanceTaskHistory;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
class ProgressForm extends Component
@ -12,10 +15,12 @@ class ProgressForm extends Component
//INPUTS
public array $maintenance_task_status;
public array $maintenance_task_status_old;
public function mount(int $maintenanceHistory)
{
$this->maintenanceHistory = MaintenanceHistory::with(['historyHosts', 'historyHosts.historyTasks', 'historyHosts.historyTasks.maintenanceTask', 'historyHosts.historyTasks.maintenanceTask.task'])->find($maintenanceHistory);
$this->maintenanceHistory = self::getMaintenanceHistory($maintenanceHistory);
foreach ($this->maintenanceHistory->historyHosts as $maintenanceHost) {
foreach ($maintenanceHost->historyTasks as $maintenanceTask) {
$this->maintenance_task_status[$maintenanceHost->id][$maintenanceTask->id] = (!empty($maintenanceTask->finished_at) ? true : false);
@ -25,12 +30,16 @@ public function mount(int $maintenanceHistory)
public function updatedMaintenanceTaskStatus($value)
{
$hosts = $this->maintenanceHistory->historyHosts()->whereIn('maintenance_host_histories.id', array_keys($this->maintenance_task_status))->get()->keyBy(['id']);
foreach ($this->maintenance_task_status as $host_history_id => $tasks) {
$done = [];
$notDone = [];
foreach ($tasks as $task_history_id => $state) {
if (isset($this->maintenance_task_status_old[$host_history_id][$task_history_id]) && $this->maintenance_task_status_old[$host_history_id][$task_history_id] === $state){
continue;
}
if ($state === true) {
$done[] = $task_history_id;
continue;
@ -40,31 +49,45 @@ public function updatedMaintenanceTaskStatus($value)
}
if (count($done) != 0) {
$hosts[$host_history_id]->historyTasks()
MaintenanceTaskHistory::where('maintenance_task_histories.maintenance_host_history_id', $host_history_id)
->where('maintenance_task_histories.status', '!=', 2)
->whereIn('maintenance_task_histories.id', $done)
->update([
'maintenance_task_histories.maintenance_host_history_id' => $host_history_id,
'maintenance_task_histories.status' => 2,
'maintenance_task_histories.finished_at' => Carbon::now(),
]);
}
if (count($notDone) != 0) {
$this->maintenanceHistory->historyHosts()
->find($host_history_id)->historyTasks()
MaintenanceTaskHistory::where('maintenance_task_histories.maintenance_host_history_id', $host_history_id)
->where('maintenance_task_histories.status', '!=', 1)
->whereIn('maintenance_task_histories.id', $notDone)
->update([
'maintenance_task_histories.maintenance_host_history_id' => $host_history_id,
'maintenance_task_histories.status' => 1,
'maintenance_task_histories.finished_at' => null,
]);
}
}
$this->maintenance_task_status_old = $this->maintenance_task_status;
}
public function render()
{
$this->maintenanceHistory = self::getMaintenanceHistory($this->maintenanceHistory->id);
return view('livewire.maintenance.progress-form');
}
private static function getMaintenanceHistory(int $maintenanceHistoryId){
return Cache::remember("maintenance_history_{$maintenanceHistoryId}", 60, function () use ($maintenanceHistoryId) {
return MaintenanceHistory::with([
'maintenance.tasks',
'historyHosts',
'historyHosts.historyTasks',
'historyHosts.historyTasks.maintenanceTask',
'historyHosts.historyTasks.maintenanceTask.task'
])->find($maintenanceHistoryId);
});
}
}

View File

@ -24,4 +24,5 @@ public function maintenanceTask()
{
return $this->belongsTo(MaintenanceTask::class);
}
}