Skip to content

Commit 641c734

Browse files
committed
fix_usage_logs_run_out_of_memory
1 parent 3359336 commit 641c734

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

app/Http/Controllers/UsageLogController.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function getApplicationLogs(Request $request)
9191
$orderBy = $request->query('orderBy', 'name');
9292
$sort = strtolower($request->query('sort', 'asc')) === 'desc';
9393
$apps = $this->applicationRepo->allDesc(['id', 'tenant_user_id', 'name', 'estimated_users_count']);
94-
94+
9595
$usageLogs = collect([]);
9696

9797
foreach ($apps as $app) {
@@ -372,11 +372,13 @@ public function getTotals(Request $request)
372372
'date' => 'sometimes|date',
373373
'language' => 'sometimes|string',
374374
]);
375+
375376
try {
376377
$usageLog = new UsageLog;
377378
$query = $usageLog->query();
378379

379-
if (isset($request->society)) {
380+
if ($request->has('society')) {
381+
$query->where('endpoint', 'v2/org/' . $request->society . '/whatnow');
380382
}
381383
if ($request->has('subnational')) {
382384
$query->where('subnational', $request->subnational);
@@ -396,29 +398,37 @@ public function getTotals(Request $request)
396398

397399

398400
$applicationQuery = $usageLog->query();
401+
if ($request->has('society')) {
402+
$applicationQuery->where('endpoint', 'v2/org/' . $request->society . '/whatnow');
399403
}
400404
if ($request->has('subnational')) {
401405
$applicationQuery->where('subnational', $request->subnational);
402406
}
403407
if ($request->has('hazard')) {
404408
$applicationQuery->where('event_type', 'like', '%' . $request->hazard . '%');
405409
}
410+
if ($request->has('date')) {
411+
$applicationQuery->whereDate('timestamp', $request->date);
406412
}
407413
if ($request->has('language')) {
408414
$applicationQuery->where('language', $request->language);
409415
}
410416

411417
$uniqueApplicationIds = $applicationQuery->select('application_id')
418+
->distinct()
412419
->pluck('application_id')
420+
->toArray();
413421

414-
return $application->estimated_users_count;
415-
})->sum();
422+
$totalEstimatedUsers = 0;
423+
if (!empty($uniqueApplicationIds)) {
416424
$totalEstimatedUsers = $this->applicationRepo->findIn($uniqueApplicationIds)
425+
->sum('estimated_users_count');
426+
}
417427

418428
$totals = [
419-
'applications' => count($uniqueApplicationIds),
420429
'applications' => $stats->unique_apps,
421430
'estimatedUsers' => $totalEstimatedUsers,
431+
'hits' => $stats->hits,
422432
];
423433

424434

0 commit comments

Comments
 (0)