Skip to content

Commit 3359336

Browse files
committed
FIx Usage Log get totals run out of memory
1 parent 23e2ad8 commit 3359336

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

app/Http/Controllers/UsageLogController.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -373,43 +373,57 @@ public function getTotals(Request $request)
373373
'language' => 'sometimes|string',
374374
]);
375375
try {
376-
// Cache to be enabled in production
377376
$usageLog = new UsageLog;
378377
$query = $usageLog->query();
379378

380379
if (isset($request->society)) {
381-
$query->where('endpoint', config('app.api_version') . '/org/' .$request->society.'/whatnow');
382380
}
383-
if (isset($request->subnational)) {
381+
if ($request->has('subnational')) {
384382
$query->where('subnational', $request->subnational);
385383
}
386-
if (isset($request->hazard)) {
384+
if ($request->has('hazard')) {
387385
$query->where('event_type', 'like', '%' . $request->hazard . '%');
388386
}
389-
if (isset($request->date)) {
387+
if ($request->has('date')) {
390388
$query->whereDate('timestamp', $request->date);
391389
}
392-
if (isset($request->language)) {
390+
if ($request->has('language')) {
393391
$query->where('language', $request->language);
394392
}
395-
$usageLogs = $query->get();
396393

397-
$uniqueApplicationIds = $usageLogs->pluck('application_id')->unique();
394+
$stats = $query->selectRaw('COUNT(*) as hits, COUNT(DISTINCT application_id) as unique_apps')
395+
->first();
398396

399-
$applications = $this->applicationRepo->findIn($uniqueApplicationIds->toArray());
400397

401-
// Calculate total estimated users
402-
$totalEstimatedUsers = $applications->map(function ($application) {
398+
$applicationQuery = $usageLog->query();
399+
}
400+
if ($request->has('subnational')) {
401+
$applicationQuery->where('subnational', $request->subnational);
402+
}
403+
if ($request->has('hazard')) {
404+
$applicationQuery->where('event_type', 'like', '%' . $request->hazard . '%');
405+
}
406+
}
407+
if ($request->has('language')) {
408+
$applicationQuery->where('language', $request->language);
409+
}
410+
411+
$uniqueApplicationIds = $applicationQuery->select('application_id')
412+
->pluck('application_id')
413+
403414
return $application->estimated_users_count;
404415
})->sum();
416+
$totalEstimatedUsers = $this->applicationRepo->findIn($uniqueApplicationIds)
405417

406418
$totals = [
407419
'applications' => count($uniqueApplicationIds),
420+
'applications' => $stats->unique_apps,
408421
'estimatedUsers' => $totalEstimatedUsers,
409-
'hits' => count($usageLogs),
410422
];
423+
424+
411425
} catch (\Exception $e) {
412-
Log::error('Could not get Usage Log totals', ['message' => $e->getMessage()]);
426+
\Log::error('Could not get Usage Log totals', ['message' => $e->getMessage()]);
413427
return response()->json([
414428
'status' => 500,
415429
'error_message' => 'Could not get Usage Log totals',
@@ -422,3 +436,4 @@ public function getTotals(Request $request)
422436
], 200);
423437
}
424438
}
439+

0 commit comments

Comments
 (0)