@@ -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,44 +372,68 @@ public function getTotals(Request $request)
372372 'date ' => 'sometimes|date ' ,
373373 'language ' => 'sometimes|string ' ,
374374 ]);
375+
375376 try {
376- // Cache to be enabled in production
377377 $ usageLog = new UsageLog ;
378378 $ query = $ usageLog ->query ();
379379
380- if (isset ( $ request ->society )) {
381- $ query ->where ('endpoint ' , config ( ' app.api_version ' ) . ' /org/ ' .$ request ->society . '/whatnow ' );
380+ if ($ request ->has ( ' society ' )) {
381+ $ query ->where ('endpoint ' , ' v2 /org/ ' . $ request ->society . '/whatnow ' );
382382 }
383- if (isset ( $ request ->subnational )) {
383+ if ($ request ->has ( ' subnational ' )) {
384384 $ query ->where ('subnational ' , $ request ->subnational );
385385 }
386- if (isset ( $ request ->hazard )) {
386+ if ($ request ->has ( ' hazard ' )) {
387387 $ query ->where ('event_type ' , 'like ' , '% ' . $ request ->hazard . '% ' );
388388 }
389- if (isset ( $ request ->date )) {
389+ if ($ request ->has ( ' date ' )) {
390390 $ query ->whereDate ('timestamp ' , $ request ->date );
391391 }
392- if (isset ( $ request ->language )) {
392+ if ($ request ->has ( ' language ' )) {
393393 $ query ->where ('language ' , $ request ->language );
394394 }
395- $ usageLogs = $ query ->get ();
396395
397- $ uniqueApplicationIds = $ usageLogs ->pluck ('application_id ' )->unique ();
396+ $ stats = $ query ->selectRaw ('COUNT(*) as hits, COUNT(DISTINCT application_id) as unique_apps ' )
397+ ->first ();
398+
399+
400+ $ applicationQuery = $ usageLog ->query ();
401+ if ($ request ->has ('society ' )) {
402+ $ applicationQuery ->where ('endpoint ' , 'v2/org/ ' . $ request ->society . '/whatnow ' );
403+ }
404+ if ($ request ->has ('subnational ' )) {
405+ $ applicationQuery ->where ('subnational ' , $ request ->subnational );
406+ }
407+ if ($ request ->has ('hazard ' )) {
408+ $ applicationQuery ->where ('event_type ' , 'like ' , '% ' . $ request ->hazard . '% ' );
409+ }
410+ if ($ request ->has ('date ' )) {
411+ $ applicationQuery ->whereDate ('timestamp ' , $ request ->date );
412+ }
413+ if ($ request ->has ('language ' )) {
414+ $ applicationQuery ->where ('language ' , $ request ->language );
415+ }
398416
399- $ applications = $ this ->applicationRepo ->findIn ($ uniqueApplicationIds ->toArray ());
417+ $ uniqueApplicationIds = $ applicationQuery ->select ('application_id ' )
418+ ->distinct ()
419+ ->pluck ('application_id ' )
420+ ->toArray ();
400421
401- // Calculate total estimated users
402- $ totalEstimatedUsers = $ applications ->map (function ($ application ) {
403- return $ application ->estimated_users_count ;
404- })->sum ();
422+ $ totalEstimatedUsers = 0 ;
423+ if (!empty ($ uniqueApplicationIds )) {
424+ $ totalEstimatedUsers = $ this ->applicationRepo ->findIn ($ uniqueApplicationIds )
425+ ->sum ('estimated_users_count ' );
426+ }
405427
406428 $ totals = [
407- 'applications ' => count ( $ uniqueApplicationIds ) ,
429+ 'applications ' => $ stats -> unique_apps ,
408430 'estimatedUsers ' => $ totalEstimatedUsers ,
409- 'hits ' => count ( $ usageLogs ) ,
431+ 'hits ' => $ stats -> hits ,
410432 ];
433+
434+
411435 } catch (\Exception $ e ) {
412- Log::error ('Could not get Usage Log totals ' , ['message ' => $ e ->getMessage ()]);
436+ \ Log::error ('Could not get Usage Log totals ' , ['message ' => $ e ->getMessage ()]);
413437 return response ()->json ([
414438 'status ' => 500 ,
415439 'error_message ' => 'Could not get Usage Log totals ' ,
@@ -422,3 +446,4 @@ public function getTotals(Request $request)
422446 ], 200 );
423447 }
424448}
449+
0 commit comments