70 lines
2.2 KiB
PHP
70 lines
2.2 KiB
PHP
<?php
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Diagnosis;
|
|
use Illuminate\Http\Request;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$totalDiagnosis = Diagnosis::where('user_id', auth()->id())->count();
|
|
|
|
$recentDiagnosis = Diagnosis::where('user_id', auth()->id())
|
|
->latest()
|
|
->take(5)
|
|
->get();
|
|
|
|
// Data untuk chart bulanan
|
|
$monthlyData = Diagnosis::where('user_id', auth()->id())
|
|
->whereYear('created_at', date('Y'))
|
|
->selectRaw('MONTH(created_at) as month, COUNT(*) as count')
|
|
->groupBy('month')
|
|
->get();
|
|
|
|
// Data tanggal diagnosa bulan ini
|
|
$diagnosaDates = Diagnosis::where('user_id', auth()->id())
|
|
->whereYear('created_at', date('Y'))
|
|
->whereMonth('created_at', date('n'))
|
|
->selectRaw('DAY(created_at) as day, COUNT(*) as count')
|
|
->groupBy('day')
|
|
->pluck('count', 'day')
|
|
->toArray();
|
|
|
|
// 🔥 FIX UTAMA: AKURASI RATA-RATA
|
|
$avgAccuracy = Diagnosis::where('user_id', auth()->id())
|
|
->whereNotNull('confidence')
|
|
->avg('confidence');
|
|
|
|
// biar ga null & tampil bagus
|
|
$avgAccuracy = $avgAccuracy ? round($avgAccuracy, 1) : 0;
|
|
|
|
$monthlyAccuracyRaw = Diagnosis::where('user_id', auth()->id())
|
|
->whereYear('created_at', date('Y'))
|
|
->whereNotNull('confidence')
|
|
->selectRaw('MONTH(created_at) as month, AVG(confidence) as avg_confidence')
|
|
->groupBy('month')->get()->keyBy('month');
|
|
|
|
$monthlyAccuracy = collect(range(1, 12))->map(function ($m) use ($monthlyAccuracyRaw) {
|
|
return $monthlyAccuracyRaw->has($m) ? round($monthlyAccuracyRaw[$m]->avg_confidence, 1) : null;
|
|
})->values()->toArray();
|
|
|
|
// Lalu di compact() tambahkan 'monthlyAccuracy'
|
|
return view('dashboard', compact(
|
|
'totalDiagnosis',
|
|
'recentDiagnosis',
|
|
'monthlyData',
|
|
'diagnosaDates',
|
|
'avgAccuracy',
|
|
'monthlyAccuracy' // ← tambahkan ini
|
|
));
|
|
|
|
return view('dashboard', compact(
|
|
'totalDiagnosis',
|
|
'recentDiagnosis',
|
|
'monthlyData',
|
|
'diagnosaDates',
|
|
'avgAccuracy'
|
|
));
|
|
}
|
|
} |