-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #549 from Muhammad235/feat/store-api-status-command
feat: store API test result data and retrieval endpoint
- Loading branch information
Showing
5 changed files
with
151 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use DateTime; | ||
use DateTimeZone; | ||
use Illuminate\Console\Command; | ||
|
||
class StoreApiStatus extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'store:api-status'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Get test result from result.json file and save to the database'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
|
||
// Get test result from json file | ||
$data = file_get_contents(base_path('../result.json')); | ||
|
||
$data = json_decode($data, true); | ||
|
||
// Get the collections and executions arrays from the data | ||
$collections = $data['collection']['item']; | ||
$executions = $data['run']['executions']; | ||
|
||
// Loop through each collection | ||
foreach ($collections as $collectionIndex => $collection) { | ||
|
||
// Loop through each item within the current collection and match it with the corresponding execution | ||
foreach ($collection['item'] as $itemIndex => $item) { | ||
|
||
$execution = $executions[$itemIndex] ?? null; | ||
$status = null; | ||
$response_time = null; | ||
|
||
if ($execution && isset($execution['assertions']) && is_array($execution['assertions'])) { | ||
// Extract the first assertion and response time if available | ||
$assertions = $execution['assertions']; | ||
$status = $assertions[0]['assertion'] ?? null; | ||
$response_time = $execution['response']['responseTime'] ?? null; | ||
} | ||
|
||
// Get the current date and time | ||
$date = new DateTime("now", new DateTimeZone('Africa/Lagos')); | ||
$last_checked = $date->format('Y-m-d h:i A'); | ||
|
||
// Update the api status record | ||
\App\Models\ApiStatus::updateOrCreate(['api_group' => $item['name']], [ | ||
'api_group' => $item['name'], | ||
'method' => $item['request']['method'], | ||
'status' => $status, | ||
'response_time' => $response_time, | ||
'last_checked' => $last_checked, | ||
'details' => $this->getDetails($execution), | ||
]); | ||
} | ||
} | ||
|
||
$this->info('Api status stored successfully'); | ||
} | ||
|
||
|
||
private function getDetails($execution) | ||
{ | ||
$response_code = $execution['response']['code'] ?? null; | ||
$response_time = $execution['response']['responseTime'] ?? null; | ||
|
||
if ($response_code >= 500 || $response_code === null) { | ||
return 'API not responding (HTTP ' . ($response_code ?? 'Unknown') . ')'; | ||
} elseif ($response_time > 400) { | ||
return 'High response time detected'; | ||
} else { | ||
return 'All tests passed'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class ApiStatus extends Model | ||
{ | ||
use HasFactory, HasUuids; | ||
|
||
protected $fillable = [ | ||
'api_group', | ||
'method', | ||
'status', | ||
'response_time', | ||
'last_checked', | ||
'details' | ||
]; | ||
} |
33 changes: 33 additions & 0 deletions
33
database/migrations/2024_08_24_074917_create_api_statuses_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('api_statuses', function (Blueprint $table) { | ||
$table->uuid('id')->primary(); | ||
$table->string('api_group')->nullable(); | ||
$table->string('method')->nullable(); | ||
$table->string('status')->nullable(); | ||
$table->string('response_time')->nullable(); | ||
$table->string('last_checked')->timestamps(); | ||
$table->string('details')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('api_statuses'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters