Skip to content

Commit b44f5e9

Browse files
author
jakub-przepiora
committed
Merge branch 'develop'
2 parents 57f9287 + b1da9d1 commit b44f5e9

4 files changed

Lines changed: 33 additions & 2 deletions

File tree

backend/app/Http/Controllers/Web/Admin/DashboardController.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ public function index(Request $request)
5959
->limit(5)
6060
->get();
6161

62-
// OEE data for today/yesterday per line
62+
// Auto-calculate and fetch OEE for today/yesterday
63+
\Illuminate\Support\Facades\Cache::remember('oee_calculated_'.today()->toDateString(), 900, function () {
64+
$svc = app(\App\Services\Production\OeeCalculationService::class);
65+
$svc->calculateAll(today());
66+
$svc->calculateAll(\Carbon\Carbon::yesterday());
67+
68+
return true;
69+
});
70+
6371
$oeeRecords = \App\Models\OeeRecord::whereDate('record_date', today())
6472
->orWhereDate('record_date', today()->subDay())
6573
->orderByDesc('record_date')

backend/app/Http/Controllers/Web/Admin/OeeController.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
use App\Models\Line;
77
use App\Models\OeeRecord;
88
use App\Services\Production\DowntimeService;
9+
use App\Services\Production\OeeCalculationService;
910
use Carbon\Carbon;
1011
use Illuminate\Http\Request;
12+
use Illuminate\Support\Facades\Cache;
1113

1214
class OeeController extends Controller
1315
{
1416
public function __construct(
1517
protected DowntimeService $downtimeService,
18+
protected OeeCalculationService $oeeService,
1619
) {}
1720

1821
public function index(Request $request)
@@ -22,6 +25,9 @@ public function index(Request $request)
2225
$dateFrom = $request->query('date_from', today()->subDays(7)->toDateString());
2326
$dateTo = $request->query('date_to', today()->toDateString());
2427

28+
// Auto-calculate OEE for today if not cached
29+
$this->ensureOeeCalculated();
30+
2531
$query = OeeRecord::with(['line', 'shift'])
2632
->whereBetween('record_date', [$dateFrom, $dateTo])
2733
->orderByDesc('record_date');
@@ -68,6 +74,8 @@ public function show(Line $line, Request $request)
6874
$dateFrom = $request->query('date_from', today()->subDays(7)->toDateString());
6975
$dateTo = $request->query('date_to', today()->toDateString());
7076

77+
$this->ensureOeeCalculated();
78+
7179
$records = OeeRecord::where('line_id', $line->id)
7280
->whereBetween('record_date', [$dateFrom, $dateTo])
7381
->with('shift')
@@ -82,4 +90,18 @@ public function show(Line $line, Request $request)
8290

8391
return view('admin.oee.show', compact('line', 'records', 'downtimeByReason', 'dateFrom', 'dateTo'));
8492
}
93+
94+
/**
95+
* Auto-calculate OEE for today and yesterday if not already done.
96+
* Cached for 15 minutes to avoid recalculating on every page load.
97+
*/
98+
private function ensureOeeCalculated(): void
99+
{
100+
Cache::remember('oee_calculated_'.today()->toDateString(), 900, function () {
101+
$this->oeeService->calculateAll(today());
102+
$this->oeeService->calculateAll(Carbon::yesterday());
103+
104+
return true;
105+
});
106+
}
85107
}

backend/resources/views/admin/oee/index.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class="card hover:shadow-lg transition-shadow">
156156
@else
157157
<div class="card text-center py-12">
158158
<p class="text-gray-500 text-lg mb-2">No OEE data available</p>
159-
<p class="text-sm text-gray-400">Run <code class="bg-gray-100 dark:bg-gray-700 px-2 py-1 rounded">php artisan oee:calculate</code> to generate records.</p>
159+
<p class="text-sm text-gray-400">OEE data will appear once production batches are completed and downtimes are reported.</p>
160160
</div>
161161
@endif
162162
</div>

backend/routes/console.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
})->purpose('Display an inspiring quote');
1010

1111
Schedule::command('tenants:prune')->everyMinute();
12+
Schedule::command('oee:calculate')->dailyAt('01:00');

0 commit comments

Comments
 (0)