Files
speech2text/app/Http/Controllers/Admin/DashboardController.php
2026-06-02 17:35:45 +08:00

66 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Department;
use App\Models\TranscriptionProject;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
class DashboardController extends Controller
{
public function index(): View
{
$stats = [
'users_active' => User::where('is_active', true)->where('role', 'user')->count(),
'users_inactive' => User::where('is_active', false)->where('role', 'user')->count(),
'users_total' => User::where('role', 'user')->count(),
'projects_total' => TranscriptionProject::count(),
'projects_pending' => TranscriptionProject::where('transcription_status', 'pending')->count(),
'projects_processing' => TranscriptionProject::where('transcription_status', 'processing')->count(),
'projects_completed' => TranscriptionProject::where('transcription_status', 'completed')->count(),
'projects_failed' => TranscriptionProject::where('transcription_status', 'failed')->count(),
'projects_deleted' => TranscriptionProject::onlyTrashed()->count(),
'projects_last_7' => TranscriptionProject::where('created_at', '>=', now()->subDays(7))->count(),
'projects_last_30' => TranscriptionProject::where('created_at', '>=', now()->subDays(30))->count(),
'storage_bytes' => (int) TranscriptionProject::sum('file_size'),
'storage_completed_bytes' => (int) TranscriptionProject::where('transcription_status', 'completed')->sum('file_size'),
'duration_total_seconds' => (int) TranscriptionProject::whereNotNull('duration_seconds')->sum('duration_seconds'),
];
// Top 5 pengguna paling aktif (by project count)
$topUsers = User::where('role', 'user')
->withCount('ownedProjects')
->orderByDesc('owned_projects_count')
->limit(5)
->get(['id', 'name', 'email']);
// Trend projek 30 hari lepas (group by date)
$trendRaw = TranscriptionProject::select(
DB::raw('DATE(created_at) as date'),
DB::raw('COUNT(*) as total'),
DB::raw('SUM(CASE WHEN transcription_status = "completed" THEN 1 ELSE 0 END) as completed'),
DB::raw('SUM(CASE WHEN transcription_status = "failed" THEN 1 ELSE 0 END) as failed')
)
->where('created_at', '>=', now()->subDays(29)->startOfDay())
->groupBy(DB::raw('DATE(created_at)'))
->orderBy('date')
->get();
// Projek mengikut jabatan (metadata sahaja)
$deptStats = Department::select('departments.name')
->selectRaw('COUNT(tp.id) as project_count')
->leftJoin('users', 'users.department_id', '=', 'departments.id')
->leftJoin('transcription_projects as tp', 'tp.owner_user_id', '=', 'users.id')
->where('departments.is_active', true)
->groupBy('departments.id', 'departments.name')
->orderByDesc('project_count')
->limit(8)
->get();
return view('admin.dashboard', compact('stats', 'topUsers', 'trendRaw', 'deptStats'));
}
}