|
8 | 8 | use Illuminate\Support\Facades\Cache; |
9 | 9 | use Illuminate\Support\Facades\Log; |
10 | 10 | use League\Fractal\Manager; |
| 11 | +use App\Models\UsageLog; |
11 | 12 |
|
12 | 13 | class UsageLogController extends Controller |
13 | 14 | { |
@@ -203,28 +204,53 @@ public function getForApplication(int $applicationId) |
203 | 204 | /** |
204 | 205 | * @return \Symfony\Component\HttpFoundation\Response |
205 | 206 | */ |
206 | | - public function getTotals() |
| 207 | + public function getTotals(Request $request) |
207 | 208 | { |
| 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 | + ]); |
208 | 216 | try { |
209 | 217 | // 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()); |
213 | 241 |
|
214 | 242 | // Calculate total estimated users |
215 | 243 | $totalEstimatedUsers = $applications->map(function ($application) { |
216 | 244 | return $application->estimated_users_count; |
217 | 245 | })->sum(); |
218 | 246 |
|
219 | 247 | $totals = [ |
220 | | - 'applications' => count($applications), |
| 248 | + 'applications' => count($uniqueApplicationIds), |
221 | 249 | 'estimatedUsers' => $totalEstimatedUsers, |
222 | 250 | 'hits' => count($usageLogs), |
223 | 251 | ]; |
224 | | - //}); |
225 | 252 | } catch (\Exception $e) { |
226 | 253 | Log::error('Could not get Usage Log totals', ['message' => $e->getMessage()]); |
227 | | - |
228 | 254 | return response()->json([ |
229 | 255 | 'status' => 500, |
230 | 256 | 'error_message' => 'Could not get Usage Log totals', |
|
0 commit comments