Skip to content

Commit 493a37a

Browse files
committed
wn-160
1 parent e1cd4f3 commit 493a37a

13 files changed

+508
-18790
lines changed

.env.produ

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
APP_NAME=Laravel
2+
APP_ENV=local
3+
APP_KEY=base64:8soMxtwXXH4Exq5gpLV52pjrX4AQ4FcIhPyIhXA0GOE=
4+
APP_DEBUG=true
5+
APP_URL=http://app
6+
7+
LOG_CHANNEL=stack
8+
9+
DB_CONNECTION=mysql
10+
DB_HOST=jazusoft-sandbox-instance-1.c1wpzknhukoz.us-east-1.rds.amazonaws.com
11+
DB_PORT=3306
12+
DB_DATABASE=whatnow-api
13+
DB_USERNAME=CbA7SLl8yE9Uw
14+
DB_PASSWORD=G2N98rLdMGLzLx97xA
15+
16+
STATS_DB_HOST=jazusoft-sandbox-instance-1.c1wpzknhukoz.us-east-1.rds.amazonaws.com
17+
STATS_DB_PORT=3306
18+
STATS_DB_DATABASE=whatnow-api
19+
STATS_DB_USERNAME=CbA7SLl8yE9Uw
20+
STATS_DB_PASSWORD=G2N98rLdMGLzLx97xA
21+
22+
BROADCAST_DRIVER=log
23+
CACHE_DRIVER=file
24+
QUEUE_CONNECTION=sync
25+
SESSION_DRIVER=file
26+
SESSION_LIFETIME=120
27+
28+
REDIS_HOST=redis
29+
REDIS_PASSWORD=null
30+
REDIS_PORT=6379
31+
32+
MAIL_DRIVER=smtp
33+
MAIL_HOST=smtp.mailtrap.io
34+
MAIL_PORT=2525
35+
MAIL_USERNAME=null
36+
MAIL_PASSWORD=null
37+
MAIL_ENCRYPTION=null
38+
39+
AWS_ACCESS_KEY_ID=
40+
AWS_SECRET_ACCESS_KEY=
41+
AWS_DEFAULT_REGION=us-east-1
42+
AWS_BUCKET=
43+
44+
PUSHER_APP_ID=
45+
PUSHER_APP_KEY=
46+
PUSHER_APP_SECRET=
47+
PUSHER_APP_CLUSTER=mt1
48+
49+
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
50+
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

.idea/dataSources.xml

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/Classes/Repositories/WhatNowRepository.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
class WhatNowRepository implements WhatNowRepositoryInterface
1010
{
1111
public const EVENT_STAGES = [
12-
'mitigation',
13-
'seasonalForecast',
14-
'watch',
15-
'warning',
1612
'immediate',
17-
'recover',
13+
'warning',
14+
'anticipated',
15+
'assess_and_plan',
16+
'mitigate_risks',
17+
'prepare_to_respond',
18+
'recover'
1819
];
1920

2021
/**
@@ -53,7 +54,7 @@ public function all($columns = ['*'])
5354
return $this->whatNowModel->all($columns);
5455
}
5556

56-
/**
57+
/**
5758
* @param array $attributes
5859
* @return WhatNowEntity
5960
*/

app/Http/Controllers/UsageLogController.php

Lines changed: 77 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,14 @@
88
use Illuminate\Support\Facades\Cache;
99
use Illuminate\Support\Facades\Log;
1010
use League\Fractal\Manager;
11+
use App\Classes\CustomPaginator;
1112

1213
class 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

Comments
 (0)