Files
2026-06-04 14:47:39 +07:00

56 lines
1.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\ActivityLog;
class ActivityLogController extends Controller
{
public function index()
{
$logs = ActivityLog::latest()->limit(200)->get()->map(fn($l) => [
'id' => $l->id,
'user' => $l->user_name ?? 'Sistem',
'action' => $l->action,
'description' => $l->description,
'ip' => $l->ip_address,
'created_at' => $l->created_at?->format('d/m/Y H:i:s'),
]);
return response()->json($logs);
}
public function stats()
{
$today = now()->startOfDay();
$week = now()->startOfWeek();
$month = now()->startOfMonth();
$byType = ActivityLog::selectRaw('action, COUNT(*) as total')
->groupBy('action')
->pluck('total', 'action');
$topUsers = ActivityLog::selectRaw('user_name, COUNT(*) as total')
->whereNotNull('user_name')
->groupBy('user_name')
->orderByDesc('total')
->limit(5)
->get();
$daily = ActivityLog::selectRaw('DATE(created_at) as day, COUNT(*) as total')
->where('created_at', '>=', now()->subDays(13)->startOfDay())
->groupBy('day')
->orderBy('day')
->pluck('total', 'day');
return response()->json([
'logins_today' => ActivityLog::where('action', 'login')->where('created_at', '>=', $today)->count(),
'logins_week' => ActivityLog::where('action', 'login')->where('created_at', '>=', $week)->count(),
'logins_month' => ActivityLog::where('action', 'login')->where('created_at', '>=', $month)->count(),
'actions_today' => ActivityLog::where('created_at', '>=', $today)->count(),
'by_type' => $byType,
'top_users' => $topUsers,
'daily' => $daily,
]);
}
}