Optimize user count queries by performing filtering directly in the database

This commit is contained in:
Julian Prieber 2024-12-13 17:52:29 +01:00
parent ee27f6c5ab
commit 61f17c3a3e

View File

@ -36,27 +36,28 @@ class AdminController extends Controller
{
$userId = Auth::user()->id;
$littlelink_name = Auth::user()->littlelink_name;
$links = Link::where('user_id', $userId)->select('link')->count();
$links = Link::where('user_id', $userId)->count();
$clicks = Link::where('user_id', $userId)->sum('click_number');
$userNumber = User::count();
$siteLinks = Link::count();
$siteClicks = Link::sum('click_number');
$users = User::select('id', 'name', 'email', 'created_at', 'updated_at')->get();
$lastMonthCount = $users->where('created_at', '>=', Carbon::now()->subDays(30))->count();
$lastWeekCount = $users->where('created_at', '>=', Carbon::now()->subDays(7))->count();
$last24HrsCount = $users->where('created_at', '>=', Carbon::now()->subHours(24))->count();
$updatedLast30DaysCount = $users->where('updated_at', '>=', Carbon::now()->subDays(30))->count();
$updatedLast7DaysCount = $users->where('updated_at', '>=', Carbon::now()->subDays(7))->count();
$updatedLast24HrsCount = $users->where('updated_at', '>=', Carbon::now()->subHours(24))->count();
$links = Link::where('user_id', $userId)->select('link')->count();
$clicks = Link::where('user_id', $userId)->sum('click_number');
$topLinks = Link::where('user_id', $userId)->orderby('click_number', 'desc')
->whereNotNull('link')->where('link', '<>', '')
->take(5)->get();
$lastMonthCount = User::where('created_at', '>=', Carbon::now()->subDays(30))->count();
$lastWeekCount = User::where('created_at', '>=', Carbon::now()->subDays(7))->count();
$last24HrsCount = User::where('created_at', '>=', Carbon::now()->subHours(24))->count();
$updatedLast30DaysCount = User::where('updated_at', '>=', Carbon::now()->subDays(30))->count();
$updatedLast7DaysCount = User::where('updated_at', '>=', Carbon::now()->subDays(7))->count();
$updatedLast24HrsCount = User::where('updated_at', '>=', Carbon::now()->subHours(24))->count();
$topLinks = Link::where('user_id', $userId)
->whereNotNull('link')
->where('link', '<>', '')
->orderBy('click_number', 'desc')
->take(5)
->get();
$pageStats = [
'visitors' => [
'all' => visits('App\Models\User', $littlelink_name)->count(),
@ -69,8 +70,23 @@ class AdminController extends Controller
'referers' => visits('App\Models\User', $littlelink_name)->refs(),
'countries' => visits('App\Models\User', $littlelink_name)->countries(),
];
return view('panel/index', ['lastMonthCount' => $lastMonthCount,'lastWeekCount' => $lastWeekCount,'last24HrsCount' => $last24HrsCount,'updatedLast30DaysCount' => $updatedLast30DaysCount,'updatedLast7DaysCount' => $updatedLast7DaysCount,'updatedLast24HrsCount' => $updatedLast24HrsCount,'toplinks' => $topLinks, 'links' => $links, 'clicks' => $clicks, 'pageStats' => $pageStats, 'littlelink_name' => $littlelink_name, 'links' => $links, 'clicks' => $clicks, 'siteLinks' => $siteLinks, 'siteClicks' => $siteClicks, 'userNumber' => $userNumber]);
return view('panel/index', [
'lastMonthCount' => $lastMonthCount,
'lastWeekCount' => $lastWeekCount,
'last24HrsCount' => $last24HrsCount,
'updatedLast30DaysCount' => $updatedLast30DaysCount,
'updatedLast7DaysCount' => $updatedLast7DaysCount,
'updatedLast24HrsCount' => $updatedLast24HrsCount,
'toplinks' => $topLinks,
'links' => $links,
'clicks' => $clicks,
'pageStats' => $pageStats,
'littlelink_name' => $littlelink_name,
'siteLinks' => $siteLinks,
'siteClicks' => $siteClicks,
'userNumber' => $userNumber
]);
}
// Users page