Skip to content
Merged
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
6 changes: 0 additions & 6 deletions .env.bitbucket-pipelines
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ DB_DATABASE=redcross_local
DB_USERNAME=preparecenter
DB_PASSWORD=preparecenter

STATS_DB_HOST=0.0.0.0
STATS_DB_PORT=3307
STATS_DB_DATABASE=redcross_logs
STATS_DB_USERNAME=preparecenter
STATS_DB_PASSWORD=preparecenter

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
Expand Down
6 changes: 0 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ DB_DATABASE=preparecenter
DB_USERNAME=preparecenter
DB_PASSWORD=preparecenter

STATS_DB_HOST=mysql_stats
STATS_DB_PORT=3306
STATS_DB_DATABASE=usage_logs
STATS_DB_USERNAME=preparecenter
STATS_DB_PASSWORD=preparecenter

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
Expand Down
2 changes: 1 addition & 1 deletion app/Classes/Repositories/RegionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function findBySlug($orgId, $slug)
{
$slug = strtolower($slug);
$slug = str_replace(' ', '-', $slug);
return $this->regModel->where('slug', $slug)->where('organisation_id', $orgId)->first();
return $this->regModel->where('slug', $slug)->where('organisation_id', $orgId)->firstOrFail();
}

public function mapTranslationInput($regionId, $language_code, $data)
Expand Down
2 changes: 1 addition & 1 deletion app/Classes/Repositories/UsageLogRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function getForEndpoint($endpoint, $fromDate = null, $toDate = null)

public function listByEndpoint($fromDate = null, $toDate = null)
{
$query = DB::connection('stats_mysql')->table('usage_logs')
$query = DB::table('usage_logs')
->select('endpoint', 'application_id', DB::raw('count(*) as hit_count'))
->groupBy('application_id', 'endpoint')
->orderBy('hit_count', 'desc');
Expand Down
65 changes: 41 additions & 24 deletions app/Http/Controllers/WhatNowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,37 +179,54 @@ public function deleteById($id)
public function getFeed(WhatNowFeed $feed, $code)
{
try {
$org = $this->orgRepo->findByCountryCode(strtoupper($code));
} catch (\Exception $e) {
Log::error('Organisation not found', ['message' => $e->getMessage()]);

return response(null, 404);
}
$feed->setOrganisation($org);

$regName = $this->request->query('region', null);
if ($regName) {
try {
$reg = $this->regionRepo->findBySlug($org->id, $regName);
$feed->setRegion($reg);
$org = $this->orgRepo->findByCountryCode(strtoupper($code));
} catch (\Exception $e) {
Log::error('Region not found', ['message' => $e->getMessage()]);
Log::error('Organisation not found', ['message' => $e->getMessage()]);
$this->changeLogStatus(404);
return response()->json(['message' => 'Organisation not found'], 404);
}
$feed->setOrganisation($org);

$regName = $this->request->query('region', null);
if ($regName) {
try {
$reg = $this->regionRepo->findBySlug($org->id, $regName);
$feed->setRegion($reg);
} catch (\Exception $e) {
Log::error('Region not found', ['message' => $e->getMessage()]);
$this->changeLogStatus(404);
return response()->json(['message' => 'Region not found'], 404);
}
}
}

$langParam = $this->request->query('language', null);
$langHeader = $this->request->header('Accept-Language', null);
$langParam = $this->request->query('language', null);
$langHeader = $this->request->header('Accept-Language', null);

if ($langParam) {
$feed->setLanguage($langParam);
} elseif ($langHeader) {
$feed->setLanguage(locale_accept_from_http($langHeader));
}
if ($langParam) {
$feed->setLanguage($langParam);
} elseif ($langHeader) {
$feed->setLanguage(locale_accept_from_http($langHeader));
}

$feed->setEventTypeFilter($this->request->query('eventType', null));
$feed->loadData();
$feed->setEventTypeFilter($this->request->query('eventType', null));
$feed->loadData();
$data = $feed->getResponseData();
if(empty($data)){
$this->changeLogStatus(204);
}
return response()->json(['data' => $data]);
}catch(\Exception $e){
$this->changeLogStatus(500);
return response()->json(['message' => $e], 500);
}
}

return response()->json(['data' => $feed->getResponseData()], 200);
protected function changeLogStatus($status){
if(isset($this->request->usageLog)){
$this->request->usageLog->code_status = $status;
$this->request->usageLog->save();
}
}

/**
Expand Down
26 changes: 12 additions & 14 deletions app/Http/Middleware/ApiAuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Middleware;

use App\Models\Application;
use App\Models\UsageLog;
use Carbon\Carbon;
use Closure;
use Illuminate\Support\Facades\Log;
Expand All @@ -23,20 +24,17 @@ public function handle($request, Closure $next)
if (! $application) {
return parent::handle($request, $next);
}

// Configure payload for logging API usage data
$event = [
'type' => 'APIRequestEvent',
'payload' => [
'app_id' => $application->id,
'endpoint' => $request->path(),
'method' => $request->method(),
'timestamp' => Carbon::now()->toDateTimeString(),
],
];

// Log request to CloudWatch
Log::channel('cloudwatch_access')->info(json_encode($event));
$usageLog = new UsageLog;
$usageLog->application_id = $application->id;
$usageLog->method = $request->method();
$usageLog->endpoint = $request->path();
$usageLog->timestamp = Carbon::now()->toDateTimeString();
$usageLog->code_status = 200;
$usageLog->language = $request->input('language', false) ? $request->input('language', null) : $request->header('Accept-Language', null);
$usageLog->region = $request->input('region', null);
$usageLog->event_type = $request->input('eventType', null);
$usageLog->save();
$request->usageLog=$usageLog;

return $next($request);
}
Expand Down
4 changes: 3 additions & 1 deletion app/Models/UsageLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

class UsageLog extends Model
{
protected $connection = 'stats_mysql'; // This model uses a different database connection for the stats DB

protected $table = 'usage_logs';

public $timestamps = false;

}
20 changes: 0 additions & 20 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,6 @@
]) : [],
],

// This connection is used for the stats database
'stats_mysql' => [
'driver' => 'mysql',
'url' => env('STATS_DATABASE_URL'),
'host' => env('STATS_DB_HOST', '127.0.0.1'),
'port' => env('STATS_DB_PORT', '3306'),
'database' => env('STATS_DB_DATABASE', 'forge'),
'username' => env('STATS_DB_USERNAME', 'forge'),
'password' => env('STATS_DB_PASSWORD', ''),
'unix_socket' => env('STATS_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],

'pgsql' => [
'driver' => 'pgsql',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CreateUsageLogsTable extends Migration
*/
public function up()
{
Schema::connection('stats_mysql')->create('usage_logs', function (Blueprint $table) {
Schema::create('usage_logs', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('application_id');
$table->string('method');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddCodeStatusOnUsageLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('usage_logs', function (Blueprint $table) {
$table->integer('code_status')->nullable();
$table->string('language',10)->nullable();
$table->string('region',45)->nullable();
$table->string('event_type',45)->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('usage_logs', function (Blueprint $table) {
$table->dropColumn('code_status');
$table->dropColumn('language');
$table->dropColumn('region');
$table->dropColumn('event_type');
});
}
}
2 changes: 1 addition & 1 deletion database/seeds/UsageLogTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function run()
$file->setFlags(SplFileObject::SKIP_EMPTY | SplFileObject::READ_AHEAD | SplFileObject::DROP_NEW_LINE | SplFileObject::READ_CSV);

foreach ($file as $row) {
DB::connection('stats_mysql')->table('usage_logs')->insert([
DB::table('usage_logs')->insert([
'id' => $row[0],
'method' => $row[1],
'application_id' => $row[2],
Expand Down
4 changes: 0 additions & 4 deletions helm-chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ secrets:
DB_DATABASE: DB_DATABASE
DB_USERNAME: DB_USERNAME
DB_PASSWORD: DB_PASSWORD
STATS_DB_HOST: STATS_DB_HOST
STATS_DB_DATABASE: STATS_DB_DATABASE
STATS_DB_USERNAME: STATS_DB_USERNAME
STATS_DB_PASSWORD: STATS_DB_PASSWORD
APP_KEY: APP_KEY

# Ingress (if using)
Expand Down
Loading