Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions app/Http/Controllers/UsageLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function getApplicationLogs(Request $request)
$orderBy = $request->query('orderBy', 'name');
$sort = strtolower($request->query('sort', 'asc')) === 'desc';
$apps = $this->applicationRepo->allDesc(['id', 'tenant_user_id', 'name', 'estimated_users_count']);

$usageLogs = collect([]);

foreach ($apps as $app) {
Expand Down Expand Up @@ -372,44 +372,68 @@ public function getTotals(Request $request)
'date' => 'sometimes|date',
'language' => 'sometimes|string',
]);

try {
// Cache to be enabled in production
$usageLog = new UsageLog;
$query = $usageLog->query();

if (isset($request->society)) {
$query->where('endpoint', config('app.api_version') . '/org/' .$request->society.'/whatnow');
if ($request->has('society')) {
$query->where('endpoint', 'v2/org/' . $request->society . '/whatnow');
}
if (isset($request->subnational)) {
if ($request->has('subnational')) {
$query->where('subnational', $request->subnational);
}
if (isset($request->hazard)) {
if ($request->has('hazard')) {
$query->where('event_type', 'like', '%' . $request->hazard . '%');
}
if (isset($request->date)) {
if ($request->has('date')) {
$query->whereDate('timestamp', $request->date);
}
if (isset($request->language)) {
if ($request->has('language')) {
$query->where('language', $request->language);
}
$usageLogs = $query->get();

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


$applicationQuery = $usageLog->query();
if ($request->has('society')) {
$applicationQuery->where('endpoint', 'v2/org/' . $request->society . '/whatnow');
}
if ($request->has('subnational')) {
$applicationQuery->where('subnational', $request->subnational);
}
if ($request->has('hazard')) {
$applicationQuery->where('event_type', 'like', '%' . $request->hazard . '%');
}
if ($request->has('date')) {
$applicationQuery->whereDate('timestamp', $request->date);
}
if ($request->has('language')) {
$applicationQuery->where('language', $request->language);
}

$applications = $this->applicationRepo->findIn($uniqueApplicationIds->toArray());
$uniqueApplicationIds = $applicationQuery->select('application_id')
->distinct()
->pluck('application_id')
->toArray();

// Calculate total estimated users
$totalEstimatedUsers = $applications->map(function ($application) {
return $application->estimated_users_count;
})->sum();
$totalEstimatedUsers = 0;
if (!empty($uniqueApplicationIds)) {
$totalEstimatedUsers = $this->applicationRepo->findIn($uniqueApplicationIds)
->sum('estimated_users_count');
}

$totals = [
'applications' => count($uniqueApplicationIds),
'applications' => $stats->unique_apps,
'estimatedUsers' => $totalEstimatedUsers,
'hits' => count($usageLogs),
'hits' => $stats->hits,
];


} catch (\Exception $e) {
Log::error('Could not get Usage Log totals', ['message' => $e->getMessage()]);
\Log::error('Could not get Usage Log totals', ['message' => $e->getMessage()]);
return response()->json([
'status' => 500,
'error_message' => 'Could not get Usage Log totals',
Expand All @@ -422,3 +446,4 @@ public function getTotals(Request $request)
], 200);
}
}

Loading