update: inline client stats query

The query only takes 6 seconds on my local untuned machine. Caching it for an hour is probably fine, we have worse queries out there, and indexing isn't an option since this table is write-heavy. Reduces overall complexity and prevents blank page after cache is reset.
This commit is contained in:
Roardom
2025-03-06 16:53:06 +00:00
parent 8210e1614a
commit 933efb785b
4 changed files with 16 additions and 96 deletions
+16 -1
View File
@@ -305,7 +305,22 @@ class StatsController extends Controller
*/
public function clients(): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
{
$clients = cache()->get('stats:clients') ?? [];
$clients = cache()->remember(
'stats:clients',
3600,
fn () => Peer::selectRaw('agent, COUNT(*) as user_count, SUM(peer_count) as peer_count')
->fromSub(
Peer::query()
->select(['agent', 'user_id', DB::raw('COUNT(*) as peer_count')])
->groupBy('agent', 'user_id')
->where('active', '=', true),
'distinct_agent_user'
)
->groupBy('agent')
->orderBy('agent')
->get()
->toArray()
);
$groupedClients = [];