api service to retreive data from nphl

This commit is contained in:
pcalengratha
2026-04-23 10:43:13 +07:00
parent b021f4c2ba
commit d801964f59
2 changed files with 72 additions and 12 deletions

View File

@@ -2,27 +2,80 @@
namespace App\Services;
use Carbon\Carbon;
use App\Models\Surveillance;
use App\Models\SurveillanceCase;
use App\Models\CaseLabResult;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
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()
{
try{
$toDate = now();
$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($toDate->toDateString(). ' Service Reload Data Successfully Ran');
$lookbackDays = config('app.lookback_days');
$surveillances = DB::connection('mysql')->select("select * from surveillances");
foreach ($surveillances as $surveillance){
$surveillance_data = $this->get('api/labsurveil.php?surveillance_id='.$surveillance->id.'&start_date='.now()->subDays($lookbackDays[$surveillance->code])->toDateString());
$data = json_decode(preg_replace('/^\xEF\xBB\xBF/', '', $surveillance_data));
@$this->insert_surveillance_cases((array)$data->laboratory_cases);
@$this->insert_surveillance_case_lab_results((array)$data->laboratory_results);
}
//$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;
}
catch (\Exception $e){
@@ -35,6 +88,7 @@ class DataRetrievalService
public function getSARICases($dateFrom, $dateTo=NULL)
{
$cond = "";
$cond .= " and patient.patdate >= '".date('Y-m-d', strtotime($dateFrom))."' ";
$cond .= (!empty($dateTo)) ? " and patient.patdate <='".date('Y-m-d', strtotime($dateTo))."' ":"";

View File

@@ -35,4 +35,10 @@ return [
],
],
'nphl_api' => [
'url' => env('NPHL_API_URL'),
'username' => env('NPHL_API_USERNAME'),
'password' => env('NPHL_API_PASSWORD'),
],
];