-
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 #550 from hngprojects/dev
Update staging
- Loading branch information
Showing
30 changed files
with
1,502 additions
and
929 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
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
Oops, something went wrong.