Skip to content

Commit fc44782

Browse files
authored
Merge pull request #7 from IFRCGo/feature/WN-114
Feature/wn 114
2 parents 764f812 + f02b3b6 commit fc44782

3 files changed

Lines changed: 40 additions & 7 deletions

File tree

app/Classes/Repositories/ApplicationRepository.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public function allDesc($columns = ['*'])
4242
return $this->applicationModel->orderBy('id', 'desc')->get($columns);
4343
}
4444

45+
public function findIn($ids = [], $columns = ['*'])
46+
{
47+
return $this->applicationModel->whereIn('id', $ids)->get($columns);
48+
}
49+
4550
/**
4651
* @param array $attributes
4752
* @return Application

app/Classes/Repositories/ApplicationRepositoryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ interface ApplicationRepositoryInterface extends RepositoryInterface
1414
public function findForUserId($tenantId, $userId);
1515

1616
public function allDesc($columns);
17+
18+
public function findIn($ids = [], $columns = ['*']);
1719
}

app/Http/Controllers/UsageLogController.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Facades\Cache;
99
use Illuminate\Support\Facades\Log;
1010
use League\Fractal\Manager;
11+
use App\Models\UsageLog;
1112

1213
class UsageLogController extends Controller
1314
{
@@ -203,28 +204,53 @@ public function getForApplication(int $applicationId)
203204
/**
204205
* @return \Symfony\Component\HttpFoundation\Response
205206
*/
206-
public function getTotals()
207+
public function getTotals(Request $request)
207208
{
209+
$this->validate($request, [
210+
'society' => 'sometimes|string',
211+
'region' => 'sometimes|int',
212+
'hazard' => 'sometimes|string',
213+
'date' => 'sometimes|date',
214+
'language' => 'sometimes|string',
215+
]);
208216
try {
209217
// Cache to be enabled in production
210-
//$totals = Cache::remember('usage.totals', 3600 * 24, function () {
211-
$applications = $this->applicationRepo->all();
212-
$usageLogs = $this->usageLogRepo->all();
218+
$usageLog = new UsageLog;
219+
$query = $usageLog->query();
220+
221+
if (isset($request->society)) {
222+
$query->where('endpoint', 'v1/org/'.$request->society.'/whatnow');
223+
}
224+
if (isset($request->region)) {
225+
$query->where('region', $request->region);
226+
}
227+
if (isset($request->hazard)) {
228+
$query->where('event_type', 'like', '%' . $request->hazard . '%');
229+
}
230+
if (isset($request->date)) {
231+
$query->whereDate('timestamp', $request->date);
232+
}
233+
if (isset($request->language)) {
234+
$query->where('language', $request->language);
235+
}
236+
$usageLogs = $query->get();
237+
238+
$uniqueApplicationIds = $usageLogs->pluck('application_id')->unique();
239+
240+
$applications = $this->applicationRepo->findIn($uniqueApplicationIds->toArray());
213241

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

219247
$totals = [
220-
'applications' => count($applications),
248+
'applications' => count($uniqueApplicationIds),
221249
'estimatedUsers' => $totalEstimatedUsers,
222250
'hits' => count($usageLogs),
223251
];
224-
//});
225252
} catch (\Exception $e) {
226253
Log::error('Could not get Usage Log totals', ['message' => $e->getMessage()]);
227-
228254
return response()->json([
229255
'status' => 500,
230256
'error_message' => 'Could not get Usage Log totals',

0 commit comments

Comments
 (0)