Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions app/Classes/Repositories/ApplicationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function allDesc($columns = ['*'])
return $this->applicationModel->orderBy('id', 'desc')->get($columns);
}

public function findIn($ids = [], $columns = ['*'])
{
return $this->applicationModel->whereIn('id', $ids)->get($columns);
}

/**
* @param array $attributes
* @return Application
Expand Down
2 changes: 2 additions & 0 deletions app/Classes/Repositories/ApplicationRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ interface ApplicationRepositoryInterface extends RepositoryInterface
public function findForUserId($tenantId, $userId);

public function allDesc($columns);

public function findIn($ids = [], $columns = ['*']);
}
40 changes: 33 additions & 7 deletions app/Http/Controllers/UsageLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use League\Fractal\Manager;
use App\Models\UsageLog;

class UsageLogController extends Controller
{
Expand Down Expand Up @@ -203,28 +204,53 @@ public function getForApplication(int $applicationId)
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function getTotals()
public function getTotals(Request $request)
{
$this->validate($request, [
'society' => 'sometimes|string',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can "society" be optional?

'region' => 'sometimes|int',
'hazard' => 'sometimes|string',
'date' => 'sometimes|date',
'language' => 'sometimes|string',
]);
try {
// Cache to be enabled in production
//$totals = Cache::remember('usage.totals', 3600 * 24, function () {
$applications = $this->applicationRepo->all();
$usageLogs = $this->usageLogRepo->all();
$usageLog = new UsageLog;
$query = $usageLog->query();

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

$uniqueApplicationIds = $usageLogs->pluck('application_id')->unique();

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

// Calculate total estimated users
$totalEstimatedUsers = $applications->map(function ($application) {
return $application->estimated_users_count;
})->sum();

$totals = [
'applications' => count($applications),
'applications' => count($uniqueApplicationIds),
'estimatedUsers' => $totalEstimatedUsers,
'hits' => count($usageLogs),
];
//});
} catch (\Exception $e) {
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 Down
Loading