Skip to content
Closed
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
50 changes: 50 additions & 0 deletions .env.produ
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:8soMxtwXXH4Exq5gpLV52pjrX4AQ4FcIhPyIhXA0GOE=
APP_DEBUG=true
APP_URL=http://app

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=jazusoft-sandbox-instance-1.c1wpzknhukoz.us-east-1.rds.amazonaws.com
DB_PORT=3306
DB_DATABASE=whatnow-api
DB_USERNAME=CbA7SLl8yE9Uw
DB_PASSWORD=G2N98rLdMGLzLx97xA

STATS_DB_HOST=jazusoft-sandbox-instance-1.c1wpzknhukoz.us-east-1.rds.amazonaws.com
STATS_DB_PORT=3306
STATS_DB_DATABASE=whatnow-api
STATS_DB_USERNAME=CbA7SLl8yE9Uw
STATS_DB_PASSWORD=G2N98rLdMGLzLx97xA

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
23 changes: 23 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions app/Classes/Repositories/WhatNowRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
class WhatNowRepository implements WhatNowRepositoryInterface
{
public const EVENT_STAGES = [
'mitigation',
'seasonalForecast',
'watch',
'warning',
'immediate',
'recover',
'warning',
'anticipated',
'assess_and_plan',
'mitigate_risks',
'prepare_to_respond',
'recover'
];

/**
Expand Down Expand Up @@ -53,7 +54,7 @@ public function all($columns = ['*'])
return $this->whatNowModel->all($columns);
}

/**
/**
* @param array $attributes
* @return WhatNowEntity
*/
Expand Down
183 changes: 77 additions & 106 deletions app/Http/Controllers/UsageLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,14 @@
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use League\Fractal\Manager;
use App\Classes\CustomPaginator;

class UsageLogController extends Controller
{
/**
* @var ApplicationRepositoryInterface
*/
protected $applicationRepo;

/**
* @var UsageLogRepositoryInterface
*/
protected $usageLogRepo;

/**
* @var Manager
*/
protected $manager;

/**
* Create a new controller instance.
*
* @param UsageLogRepositoryInterface $usageLogRepo
* @param Manager $manager
*/
public function __construct(ApplicationRepositoryInterface $applicationRepo, UsageLogRepositoryInterface $usageLogRepo, Manager $manager)
{
$this->applicationRepo = $applicationRepo;
Expand Down Expand Up @@ -60,6 +44,18 @@ public function getApplicationLogs(Request $request)
'requestCount' => count($this->usageLogRepo->getForApplication($app->id, $request->fromDate, $request->toDate)),
]);
}

$paginated = new CustomPaginator(
$usageLogs->forPage($request->page, 10),
$usageLogs->count(),
10,
$request->page
);

return response()->json([
'data' => $paginated->toArray(),
], 200);

} catch (\Exception $e) {
Log::error('Could not get Usage Log', ['message' => $e->getMessage()]);

Expand All @@ -69,18 +65,6 @@ public function getApplicationLogs(Request $request)
'errors' => [],
], 500);
}

$paginated = $usageLogs->paginate(10)->toArray();

// TODO: Create custom paginator class
unset($paginated['first_page_url']);
unset($paginated['last_page_url']);
unset($paginated['next_page_url']);
unset($paginated['prev_page_url']);

return response()->json([
'data' => $paginated,
], 200);
}

public function getEndpointLogs(Request $request)
Expand All @@ -93,16 +77,26 @@ public function getEndpointLogs(Request $request)
try {
$usageLogs = $this->usageLogRepo->listByEndpoint($request->fromDate, $request->toDate);

$usageLogs = $usageLogs->map(function ($usageLog) {
$usageLogs->transform(function ($usageLog) {
$application = $this->applicationRepo->find($usageLog->application_id);

$usageLog->application_name = $application['name'] ?? 'Unknown';
$usageLog->application_tenant_user_id = $application['tenant_user_id'] ?? null;

//Log::info(json_encode($application));

return $usageLog;
});

$paginated = new CustomPaginator(
$usageLogs->forPage($request->page, 10),
$usageLogs->count(),
10,
$request->page
);

return response()->json([
'data' => $paginated->toArray(),
], 200);

} catch (\Exception $e) {
Log::error('Could not get Usage Log', ['message' => $e->getMessage()]);

Expand All @@ -112,17 +106,6 @@ public function getEndpointLogs(Request $request)
'errors' => [],
], 500);
}

$paginated = $usageLogs->paginate(10)->toArray();

unset($paginated['first_page_url']);
unset($paginated['last_page_url']);
unset($paginated['next_page_url']);
unset($paginated['prev_page_url']);

return response()->json([
'data' => $usageLogs,
], 200);
}

public function export(Request $request)
Expand All @@ -134,6 +117,31 @@ public function export(Request $request)

try {
$usageLogs = $this->usageLogRepo->whereBetween($request->fromDate, $request->toDate, ['*']);

$csv = \League\Csv\Writer::createFromFileObject(new \SplTempFileObject());

$csv->insertOne([
'Application Name',
'Endpoint',
'Method',
'Timestamp',
]);

$applications = $this->applicationRepo->all();

foreach ($usageLogs as $usageLog) {
$application = $applications->where('id', $usageLog->application_id)->first();

$csv->insertOne([
$application ? $application->name : 'Unknown',
$usageLog->endpoint,
$usageLog->method,
$usageLog->timestamp,
]);
}

print_r($csv->toString());

} catch (\Exception $e) {
Log::error('Could not export Usage Logs', ['message' => $e->getMessage()]);

Expand All @@ -143,48 +151,19 @@ public function export(Request $request)
'errors' => [],
], 500);
}

// Create CSV
$csv = \League\Csv\Writer::createFromFileObject(new \SplTempFileObject());

$csv->insertOne([
'Application Name',
'Endpoint',
'Method',
'Timestamp',
]);

$applications = $this->applicationRepo->all();

// Insert usage log records
foreach ($usageLogs as $usageLog) {
$application = $applications->where('id', $usageLog->application_id)->first();

$csv->insertOne([
$application ? $application->name : 'Unknown',
$usageLog->endpoint,
$usageLog->method,
$usageLog->timestamp,
]);
}

// Print raw CSV as response
print_r($csv->toString());
}

/**
* @param int $applicationId
* @return \Symfony\Component\HttpFoundation\Response
*/
public function getForApplication(int $applicationId)
{
try {
// Cache to be enabled in production
// $usageLogs = Cache::remember("usage.application.{$applicationId}", 3600, function () use ($applicationId) {
// return $this->usageLogRepo->getForApplication($applicationId);
// });
$usageLogs = Cache::remember("usage.application.{$applicationId}", 3600, function () use ($applicationId) {
return $this->usageLogRepo->getForApplication($applicationId);
});

return response()->json([
'count' => count($usageLogs),
], 200);

$usageLogs = $this->usageLogRepo->getForApplication($applicationId);
} catch (\Exception $e) {
Log::error('Could not get Usage Logs for application', ['message' => $e->getMessage()]);

Expand All @@ -194,34 +173,30 @@ public function getForApplication(int $applicationId)
'errors' => [],
], 500);
}

return response()->json([
'count' => count($usageLogs),
], 200);
}

/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function getTotals()
{
try {
// Cache to be enabled in production
//$totals = Cache::remember('usage.totals', 3600 * 24, function () {
$applications = $this->applicationRepo->all();
$usageLogs = $this->usageLogRepo->all();

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

$totals = [
'applications' => count($applications),
'estimatedUsers' => $totalEstimatedUsers,
'hits' => count($usageLogs),
];
//});
$totals = Cache::remember('usage.totals', 3600 * 24, function () {
$applications = $this->applicationRepo->all();
$usageLogs = $this->usageLogRepo->all();

$totalEstimatedUsers = $applications->map(function ($application) {
return $application->estimated_users_count;
})->sum();

return [
'applications' => count($applications),
'estimatedUsers' => $totalEstimatedUsers,
'hits' => count($usageLogs),
];
});

return response()->json([
'data' => $totals,
], 200);

} catch (\Exception $e) {
Log::error('Could not get Usage Log totals', ['message' => $e->getMessage()]);

Expand All @@ -231,9 +206,5 @@ public function getTotals()
'errors' => [],
], 500);
}

return response()->json([
'data' => $totals,
], 200);
}
}
Loading
Loading