api service to retreive data from nphl
This commit is contained in:
@@ -2,27 +2,80 @@
|
|||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Illuminate\Http\Client\RequestException;
|
||||||
use App\Models\Surveillance;
|
|
||||||
use App\Models\SurveillanceCase;
|
|
||||||
use App\Models\CaseLabResult;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
class DataRetrievalService
|
class DataRetrievalService
|
||||||
{
|
{
|
||||||
|
|
||||||
|
protected $apiBaseUrl;
|
||||||
|
protected $apiUsername;
|
||||||
|
protected $apiPassword;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->apiBaseUrl = config('services.nphl_api.url');
|
||||||
|
$this->apiUsername = config('services.nphl_api.username');
|
||||||
|
$this->apiPassword = config('services.nphl_api.password');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($endpoint)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl . $endpoint);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_ENCODING, '');
|
||||||
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
|
||||||
|
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
|
||||||
|
curl_setopt($ch, CURL_HTTP_VERSION_1_1, 0);
|
||||||
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
|
||||||
|
curl_setopt($ch, CURLOPT_USERPWD, $this->apiUsername . ":" . $this->apiPassword);
|
||||||
|
$resp = curl_exec($ch);
|
||||||
|
$b="";
|
||||||
|
if($e = curl_error($ch)){
|
||||||
|
$b= $e;
|
||||||
|
}else{
|
||||||
|
$b= $resp;
|
||||||
|
}
|
||||||
|
curl_close($ch);
|
||||||
|
return $b;
|
||||||
|
|
||||||
|
} catch (RequestException $e) {
|
||||||
|
return [
|
||||||
|
'error' => true,
|
||||||
|
'message' => $e->getMessage(),
|
||||||
|
'status' => optional($e->response)->status()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function getSurveillanceData()
|
public function getSurveillanceData()
|
||||||
{
|
{
|
||||||
try{
|
try{
|
||||||
$toDate = now();
|
$lookbackDays = config('app.lookback_days');
|
||||||
$this->getSARICases(now()->subDays(config('app.lookback_days.SARI'))->toDateString(), $toDate); // done
|
$surveillances = DB::connection('mysql')->select("select * from surveillances");
|
||||||
$this->getILICases(now()->subDays(config('app.lookback_days.ILI'))->toDateString(), $toDate); // done
|
foreach ($surveillances as $surveillance){
|
||||||
$this->getLBMCases(now()->subDays(config('app.lookback_days.LBM'))->toDateString(), $toDate); // done
|
$surveillance_data = $this->get('api/labsurveil.php?surveillance_id='.$surveillance->id.'&start_date='.now()->subDays($lookbackDays[$surveillance->code])->toDateString());
|
||||||
$this->getAFICases(now()->subDays(config('app.lookback_days.AFI'))->toDateString(), $toDate); // done
|
$data = json_decode(preg_replace('/^\xEF\xBB\xBF/', '', $surveillance_data));
|
||||||
$this->getNSDCases(now()->subDays(config('app.lookback_days.NDS'))->toDateString(), $toDate);
|
@$this->insert_surveillance_cases((array)$data->laboratory_cases);
|
||||||
$this->getSEQCases(now()->subDays(config('app.lookback_days.SEQ'))->toDateString(), $toDate); // done
|
@$this->insert_surveillance_case_lab_results((array)$data->laboratory_results);
|
||||||
Log::channel('jobs')->info($toDate->toDateString(). ' Service Reload Data Successfully Ran');
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//$this->getSARICases(now()->subDays(config('app.lookback_days.SARI'))->toDateString(), $toDate); // done
|
||||||
|
// $this->getILICases(now()->subDays(config('app.lookback_days.ILI'))->toDateString(), $toDate); // done
|
||||||
|
// $this->getLBMCases(now()->subDays(config('app.lookback_days.LBM'))->toDateString(), $toDate); // done
|
||||||
|
// $this->getAFICases(now()->subDays(config('app.lookback_days.AFI'))->toDateString(), $toDate); // done
|
||||||
|
// $this->getNSDCases(now()->subDays(config('app.lookback_days.NDS'))->toDateString(), $toDate);
|
||||||
|
// $this->getSEQCases(now()->subDays(config('app.lookback_days.SEQ'))->toDateString(), $toDate); // done
|
||||||
|
Log::channel('jobs')->info(now()->toDateString(). ' Service Reload Data Successfully Ran');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (\Exception $e){
|
catch (\Exception $e){
|
||||||
@@ -35,6 +88,7 @@ class DataRetrievalService
|
|||||||
|
|
||||||
public function getSARICases($dateFrom, $dateTo=NULL)
|
public function getSARICases($dateFrom, $dateTo=NULL)
|
||||||
{
|
{
|
||||||
|
|
||||||
$cond = "";
|
$cond = "";
|
||||||
$cond .= " and patient.patdate >= '".date('Y-m-d', strtotime($dateFrom))."' ";
|
$cond .= " and patient.patdate >= '".date('Y-m-d', strtotime($dateFrom))."' ";
|
||||||
$cond .= (!empty($dateTo)) ? " and patient.patdate <='".date('Y-m-d', strtotime($dateTo))."' ":"";
|
$cond .= (!empty($dateTo)) ? " and patient.patdate <='".date('Y-m-d', strtotime($dateTo))."' ":"";
|
||||||
|
|||||||
@@ -35,4 +35,10 @@ return [
|
|||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'nphl_api' => [
|
||||||
|
'url' => env('NPHL_API_URL'),
|
||||||
|
'username' => env('NPHL_API_USERNAME'),
|
||||||
|
'password' => env('NPHL_API_PASSWORD'),
|
||||||
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user