88use Illuminate \Support \Facades \Cache ;
99use Illuminate \Support \Facades \Log ;
1010use League \Fractal \Manager ;
11+ use App \Classes \CustomPaginator ;
1112
1213class UsageLogController extends Controller
1314{
14- /**
15- * @var ApplicationRepositoryInterface
16- */
1715 protected $ applicationRepo ;
18-
19- /**
20- * @var UsageLogRepositoryInterface
21- */
2216 protected $ usageLogRepo ;
23-
24- /**
25- * @var Manager
26- */
2717 protected $ manager ;
2818
29- /**
30- * Create a new controller instance.
31- *
32- * @param UsageLogRepositoryInterface $usageLogRepo
33- * @param Manager $manager
34- */
3519 public function __construct (ApplicationRepositoryInterface $ applicationRepo , UsageLogRepositoryInterface $ usageLogRepo , Manager $ manager )
3620 {
3721 $ this ->applicationRepo = $ applicationRepo ;
@@ -60,6 +44,18 @@ public function getApplicationLogs(Request $request)
6044 'requestCount ' => count ($ this ->usageLogRepo ->getForApplication ($ app ->id , $ request ->fromDate , $ request ->toDate )),
6145 ]);
6246 }
47+
48+ $ paginated = new CustomPaginator (
49+ $ usageLogs ->forPage ($ request ->page , 10 ),
50+ $ usageLogs ->count (),
51+ 10 ,
52+ $ request ->page
53+ );
54+
55+ return response ()->json ([
56+ 'data ' => $ paginated ->toArray (),
57+ ], 200 );
58+
6359 } catch (\Exception $ e ) {
6460 Log::error ('Could not get Usage Log ' , ['message ' => $ e ->getMessage ()]);
6561
@@ -69,18 +65,6 @@ public function getApplicationLogs(Request $request)
6965 'errors ' => [],
7066 ], 500 );
7167 }
72-
73- $ paginated = $ usageLogs ->paginate (10 )->toArray ();
74-
75- // TODO: Create custom paginator class
76- unset($ paginated ['first_page_url ' ]);
77- unset($ paginated ['last_page_url ' ]);
78- unset($ paginated ['next_page_url ' ]);
79- unset($ paginated ['prev_page_url ' ]);
80-
81- return response ()->json ([
82- 'data ' => $ paginated ,
83- ], 200 );
8468 }
8569
8670 public function getEndpointLogs (Request $ request )
@@ -93,16 +77,26 @@ public function getEndpointLogs(Request $request)
9377 try {
9478 $ usageLogs = $ this ->usageLogRepo ->listByEndpoint ($ request ->fromDate , $ request ->toDate );
9579
96- $ usageLogs = $ usageLogs -> map (function ($ usageLog ) {
80+ $ usageLogs-> transform (function ($ usageLog ) {
9781 $ application = $ this ->applicationRepo ->find ($ usageLog ->application_id );
9882
9983 $ usageLog ->application_name = $ application ['name ' ] ?? 'Unknown ' ;
10084 $ usageLog ->application_tenant_user_id = $ application ['tenant_user_id ' ] ?? null ;
10185
102- //Log::info(json_encode($application));
103-
10486 return $ usageLog ;
10587 });
88+
89+ $ paginated = new CustomPaginator (
90+ $ usageLogs ->forPage ($ request ->page , 10 ),
91+ $ usageLogs ->count (),
92+ 10 ,
93+ $ request ->page
94+ );
95+
96+ return response ()->json ([
97+ 'data ' => $ paginated ->toArray (),
98+ ], 200 );
99+
106100 } catch (\Exception $ e ) {
107101 Log::error ('Could not get Usage Log ' , ['message ' => $ e ->getMessage ()]);
108102
@@ -112,17 +106,6 @@ public function getEndpointLogs(Request $request)
112106 'errors ' => [],
113107 ], 500 );
114108 }
115-
116- $ paginated = $ usageLogs ->paginate (10 )->toArray ();
117-
118- unset($ paginated ['first_page_url ' ]);
119- unset($ paginated ['last_page_url ' ]);
120- unset($ paginated ['next_page_url ' ]);
121- unset($ paginated ['prev_page_url ' ]);
122-
123- return response ()->json ([
124- 'data ' => $ usageLogs ,
125- ], 200 );
126109 }
127110
128111 public function export (Request $ request )
@@ -134,6 +117,31 @@ public function export(Request $request)
134117
135118 try {
136119 $ usageLogs = $ this ->usageLogRepo ->whereBetween ($ request ->fromDate , $ request ->toDate , ['* ' ]);
120+
121+ $ csv = \League \Csv \Writer::createFromFileObject (new \SplTempFileObject ());
122+
123+ $ csv ->insertOne ([
124+ 'Application Name ' ,
125+ 'Endpoint ' ,
126+ 'Method ' ,
127+ 'Timestamp ' ,
128+ ]);
129+
130+ $ applications = $ this ->applicationRepo ->all ();
131+
132+ foreach ($ usageLogs as $ usageLog ) {
133+ $ application = $ applications ->where ('id ' , $ usageLog ->application_id )->first ();
134+
135+ $ csv ->insertOne ([
136+ $ application ? $ application ->name : 'Unknown ' ,
137+ $ usageLog ->endpoint ,
138+ $ usageLog ->method ,
139+ $ usageLog ->timestamp ,
140+ ]);
141+ }
142+
143+ print_r ($ csv ->toString ());
144+
137145 } catch (\Exception $ e ) {
138146 Log::error ('Could not export Usage Logs ' , ['message ' => $ e ->getMessage ()]);
139147
@@ -143,48 +151,19 @@ public function export(Request $request)
143151 'errors ' => [],
144152 ], 500 );
145153 }
146-
147- // Create CSV
148- $ csv = \League \Csv \Writer::createFromFileObject (new \SplTempFileObject ());
149-
150- $ csv ->insertOne ([
151- 'Application Name ' ,
152- 'Endpoint ' ,
153- 'Method ' ,
154- 'Timestamp ' ,
155- ]);
156-
157- $ applications = $ this ->applicationRepo ->all ();
158-
159- // Insert usage log records
160- foreach ($ usageLogs as $ usageLog ) {
161- $ application = $ applications ->where ('id ' , $ usageLog ->application_id )->first ();
162-
163- $ csv ->insertOne ([
164- $ application ? $ application ->name : 'Unknown ' ,
165- $ usageLog ->endpoint ,
166- $ usageLog ->method ,
167- $ usageLog ->timestamp ,
168- ]);
169- }
170-
171- // Print raw CSV as response
172- print_r ($ csv ->toString ());
173154 }
174155
175- /**
176- * @param int $applicationId
177- * @return \Symfony\Component\HttpFoundation\Response
178- */
179156 public function getForApplication (int $ applicationId )
180157 {
181158 try {
182- // Cache to be enabled in production
183- // $usageLogs = Cache::remember("usage.application.{$applicationId}", 3600, function () use ($applicationId) {
184- // return $this->usageLogRepo->getForApplication($applicationId);
185- // });
159+ $ usageLogs = Cache::remember ("usage.application. {$ applicationId }" , 3600 , function () use ($ applicationId ) {
160+ return $ this ->usageLogRepo ->getForApplication ($ applicationId );
161+ });
162+
163+ return response ()->json ([
164+ 'count ' => count ($ usageLogs ),
165+ ], 200 );
186166
187- $ usageLogs = $ this ->usageLogRepo ->getForApplication ($ applicationId );
188167 } catch (\Exception $ e ) {
189168 Log::error ('Could not get Usage Logs for application ' , ['message ' => $ e ->getMessage ()]);
190169
@@ -194,34 +173,30 @@ public function getForApplication(int $applicationId)
194173 'errors ' => [],
195174 ], 500 );
196175 }
197-
198- return response ()->json ([
199- 'count ' => count ($ usageLogs ),
200- ], 200 );
201176 }
202177
203- /**
204- * @return \Symfony\Component\HttpFoundation\Response
205- */
206178 public function getTotals ()
207179 {
208180 try {
209- // 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 ();
213-
214- // Calculate total estimated users
215- $ totalEstimatedUsers = $ applications ->map (function ($ application ) {
216- return $ application ->estimated_users_count ;
217- })->sum ();
218-
219- $ totals = [
220- 'applications ' => count ($ applications ),
221- 'estimatedUsers ' => $ totalEstimatedUsers ,
222- 'hits ' => count ($ usageLogs ),
223- ];
224- //});
181+ $ totals = Cache::remember ('usage.totals ' , 3600 * 24 , function () {
182+ $ applications = $ this ->applicationRepo ->all ();
183+ $ usageLogs = $ this ->usageLogRepo ->all ();
184+
185+ $ totalEstimatedUsers = $ applications ->map (function ($ application ) {
186+ return $ application ->estimated_users_count ;
187+ })->sum ();
188+
189+ return [
190+ 'applications ' => count ($ applications ),
191+ 'estimatedUsers ' => $ totalEstimatedUsers ,
192+ 'hits ' => count ($ usageLogs ),
193+ ];
194+ });
195+
196+ return response ()->json ([
197+ 'data ' => $ totals ,
198+ ], 200 );
199+
225200 } catch (\Exception $ e ) {
226201 Log::error ('Could not get Usage Log totals ' , ['message ' => $ e ->getMessage ()]);
227202
@@ -231,9 +206,5 @@ public function getTotals()
231206 'errors ' => [],
232207 ], 500 );
233208 }
234-
235- return response ()->json ([
236- 'data ' => $ totals ,
237- ], 200 );
238209 }
239210}
0 commit comments