From 801f280ff944a5afe012edd2bed29472e8721af7 Mon Sep 17 00:00:00 2001 From: pcalengratha Date: Tue, 24 Mar 2026 16:56:45 +0700 Subject: [PATCH] service_retrieve_data --- .../app/Console/Commands/fetchSourceData.php | 39 + .../Controllers/Api/DashboardController.php | 18 +- .../app/Services/DataRetrievalService.php | 693 ++++++++++++++++++ dashboard/config/app.php | 9 + dashboard/config/database.php | 27 + dashboard/config/logging.php | 7 + .../resources/views/layouts/app.blade.php | 23 +- dashboard/routes/api.php | 3 +- dashboard/routes/console.php | 3 + 9 files changed, 816 insertions(+), 6 deletions(-) create mode 100644 dashboard/app/Console/Commands/fetchSourceData.php create mode 100644 dashboard/app/Services/DataRetrievalService.php diff --git a/dashboard/app/Console/Commands/fetchSourceData.php b/dashboard/app/Console/Commands/fetchSourceData.php new file mode 100644 index 0000000..c83b25e --- /dev/null +++ b/dashboard/app/Console/Commands/fetchSourceData.php @@ -0,0 +1,39 @@ +dataRetrievalService = new DataRetrievalService(); + parent::__construct(); + } + + /** + * Execute the console command. + */ + public function handle() + { + $this->dataRetrievalService->getSurveillanceData(); + } + +} diff --git a/dashboard/app/Http/Controllers/Api/DashboardController.php b/dashboard/app/Http/Controllers/Api/DashboardController.php index bc07fb9..3e6ac58 100644 --- a/dashboard/app/Http/Controllers/Api/DashboardController.php +++ b/dashboard/app/Http/Controllers/Api/DashboardController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Api; +use App\Services\DataRetrievalService; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Services\DashboardService; @@ -10,10 +11,12 @@ use Carbon\Carbon; class DashboardController extends Controller { protected $service; + protected $dataRetrievalService; public function __construct(DashboardService $service) { $this->service = $service; + $this->dataRetrievalService = new DataRetrievalService(); } /* @@ -165,4 +168,17 @@ class DashboardController extends Controller return response()->json($data); } -} \ No newline at end of file + + public function fetchSourceData(){ + try{ + $this->dataRetrievalService->getSurveillanceData(); + return response()->json(['message' => 'Data loaded successfully!'], 200); + } + catch (\Exception $e) + { + return response()->json(['error' => 'Data loaded unsuccessfully!'], 400); + } + } + + +} diff --git a/dashboard/app/Services/DataRetrievalService.php b/dashboard/app/Services/DataRetrievalService.php new file mode 100644 index 0000000..4ac6af9 --- /dev/null +++ b/dashboard/app/Services/DataRetrievalService.php @@ -0,0 +1,693 @@ +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'); + return true; + } + catch (\Exception $e){ + Log::channel('jobs')->error($e->getMessage()); + return false; + } + + } + + + 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))."' ":""; + $sari_cases = DB::connection('mysql_nphl') + ->select(" + SELECT + patient.labcode, + patient.patdate, + patient.isnewcase, + l.labname_en, + l.labaddress_en, + 1 as surveillance_id, + niphc0_nphl.get_epi_period(patient.patdate, 'Y') as year_data, + niphc0_nphl.get_epi_period(patient.patdate, 'W') as week_data, + patient.patage, + patient.patsex, + NULL as is_alive, + p.proname_en + FROM niphc0_nphl.`labopatients` patient + inner join niphc0_nphl.sari_patients sr_p on sr_p.patid = patient.patid + inner join niphc0_nphl.laboratory l on l.labid = patient.patsource + left join niphc0_nphl.province p on p.proid = patient.frompro + WHERE patient.patgroup =2 + and patient.status = 1 + ".$cond); + + $this->insert_surveillance_cases($sari_cases); + + + $sari_case_results = DB::connection('mysql_nphl')->select(" + SELECT + patient.labcode, + 1 as surveillance_id, + if(trs.rtitle='Negatives', 0, 1) as is_positive, + if(trs.rtitle !='Negatives', concat('Influenza ',LEFT(UPPER(trs.rtitle),1)),'') pathogen_name, + if(trs.rtitle !='Negatives', trs.rtitle, '') as subtype, + 'SARI Influenza Test' as indicator + FROM `labopatients` patient + inner join sari_patients sr_p on sr_p.patid = patient.patid + inner join test_patsample tps on tps.patientid = patient.patid + inner join test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid + inner join test_pattestresult tptr on tptr.pstid = tpst.pstid + inner join test_resultselect trs on trs.rid = tptr.rsltid + WHERE patient.patgroup =2 + and patient.status = 1 + and tps.status = 1 + and tpst.status = 1 + and trs.status = 1 + and tpst.labtestid in (775,216,239,238,212,220,340,381,382) + " .$cond. " + union + + SELECT + patient.labcode, + 1 as surveillance_id, + if(trs.rtitle='Negative', 0, 1) as is_positive, + if(trs.rtitle !='Negative', 'SARS-CoV-2', '') as pathogen_name, + '' as subtype, + 'SARI Covid Test' as indicator + FROM `labopatients` patient + inner join sari_patients sr_p + on sr_p.patid = patient.patid + inner join test_patsample tps + on tps.patientid = patient.patid + inner join test_patsamtest tpst + on tpst.patsmid = tps.sampleid + and tpst.patid = tps.patientid + inner join test_pattestresult tptr on tptr.pstid = tpst.pstid + inner join test_resultselect trs on trs.rid = tptr.rsltid + WHERE patient.patgroup =2 + and patient.status = 1 + and tps.status = 1 + and tpst.status = 1 + and trs.status = 1 + and tpst.labtestid in (651,652,653,654,650) + and trs.rtitle !='Pending' + " .$cond); + + $this->insert_surveillance_case_lab_results($sari_case_results); + + } + + public function getILICases($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))."' ":""; + $ili_cases = DB::connection('mysql_nphl') + ->select(" + SELECT + patient.labcode, + patient.patdate, + patient.isnewcase, + l.labname_en, + l.labaddress_en, + 2 as surveillance_id, + niphc0_nphl.get_epi_period(patient.patdate, 'Y') as year_data, + niphc0_nphl.get_epi_period(patient.patdate, 'W') as week_data, + patient.patage, + patient.patsex, + NULL as is_alive, + p.proname_en + FROM niphc0_nphl.`labopatients` patient + inner join niphc0_nphl.ili_patients sr_p on sr_p.patid = patient.patid + inner join niphc0_nphl.laboratory l on l.labid = patient.patsource + left join niphc0_nphl.province p on p.proid = patient.frompro + WHERE patient.patgroup =3 + and patient.status = 1 + ".$cond + ); + + + $this->insert_surveillance_cases($ili_cases); + + $ili_case_results = DB::connection('mysql_nphl') + ->select(" + SELECT + distinct + patient.labcode, + 2 as surveillance_id, + if(trs.rtitle='Negatives', 0, 1) as is_positive, + if(trs.rtitle !='Negatives', concat('Influenza ',LEFT(UPPER(trs.rtitle),1)),'') pathogen_name, + if(trs.rtitle !='Negatives', trs.rtitle, '') as subtype, + 'ILI Influenza Test' as indicator + FROM niphc0_nphl.`labopatients` patient + inner join niphc0_nphl.ili_patients sr_p on sr_p.patid = patient.patid + inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid + inner join niphc0_nphl.test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid + inner join niphc0_nphl.test_pattestresult tptr on tptr.pstid = tpst.pstid + inner join niphc0_nphl.test_resultselect trs on trs.rid = tptr.rsltid + WHERE patient.patgroup = 3 + and patient.status = 1 + and tps.status = 1 + and tpst.status = 1 + and trs.status = 1 + and tpst.labtestid in (775,216,239,238,212,220,340,381,382) + ".$cond." + + union + + SELECT + DISTINCT + patient.labcode, + 2 as surveillance_id, + if(trs.rtitle='Negative', 0, 1) as is_positive, + if(trs.rtitle !='Negative', 'SARS-CoV-2', '') as pathogen_name, + '' as subtype, + 'ILI Covid Test' as indicator + FROM niphc0_nphl.`labopatients` patient + inner join niphc0_nphl.ili_patients sr_p on sr_p.patid = patient.patid + inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid + inner join niphc0_nphl.test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid + inner join niphc0_nphl.test_pattestresult tptr on tptr.pstid = tpst.pstid + inner join niphc0_nphl.test_resultselect trs on trs.rid = tptr.rsltid + WHERE patient.patgroup = 3 + and patient.status = 1 + and tps.status = 1 + and tpst.status = 1 + and trs.status = 1 + and tpst.labtestid in (651,652,653,654,650) + and trs.rtitle !='Pending' + ".$cond); + + $this->insert_surveillance_case_lab_results($ili_case_results); + + } + + public function getLBMCases($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))."' ":""; + $lbm_cases = DB::connection('mysql_nphl') + ->select(" + SELECT + patient.labcode, + patient.patdate, + patient.isnewcase, + l.labname_en, + l.labaddress_en, + 3 as surveillance_id, + niphc0_nphl.get_epi_period(patient.patdate, 'Y') year_data, + niphc0_nphl.get_epi_period(patient.patdate, 'W') week_data, + patient.patage, + patient.patsex, + NULL as is_alive, + p.proname_en + FROM niphc0_nphl.`labopatients` patient + inner join niphc0_nphl.lbms_patients on lbms_patients.patid = patient.patid + left join niphc0_nphl.province p on p.proid = patient.frompro + inner join niphc0_nphl.laboratory l on l.labid = patient.patsource + WHERE patient.patgroup = 17 + and patient.status = 1 + ".$cond); + + $this->insert_surveillance_cases($lbm_cases); + + $lbm_case_results = DB::connection('mysql_nphl') + ->select(" + SELECT + distinct + patient.labcode, + 3 as surveillance_id, + if(trs.rtitle='Negatives', 0, 1) as is_positive, + if(trs.rtitle !='Negatives', concat('Influenza ',LEFT(UPPER(trs.rtitle),1)),'') pathogen_name, + if(trs.rtitle !='Negatives', trs.rtitle, '') as subtype, + 'LBM Influenza Test' as indicator + FROM niphc0_nphl.`labopatients` patient + inner join niphc0_nphl.lbms_patients sr_p on sr_p.patid = patient.patid + inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid + inner join niphc0_nphl.test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid + inner join niphc0_nphl.test_pattestresult tptr on tptr.pstid = tpst.pstid + inner join niphc0_nphl.test_resultselect trs on trs.rid = tptr.rsltid + WHERE patient.patgroup = 17 + and patient.status = 1 + and tps.status = 1 + and tpst.status = 1 + and trs.status = 1 + and tpst.labtestid in (775,216,239,238,212,220,340,381,382) + ".$cond." + union + + SELECT + DISTINCT + patient.labcode, + 3 as surveillance_id, + if(trs.rtitle='Negative', 0, 1) as is_positive, + if(trs.rtitle !='Negative', 'SARS-CoV-2', '') as pathogen_name, + '' as subtype, + 'LBM Covid Test' as indicator + FROM niphc0_nphl.`labopatients` patient + inner join niphc0_nphl.lbms_patients sr_p on sr_p.patid = patient.patid + inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid + inner join niphc0_nphl.test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid + inner join niphc0_nphl.test_pattestresult tptr on tptr.pstid = tpst.pstid + inner join niphc0_nphl.test_resultselect trs on trs.rid = tptr.rsltid + WHERE patient.patgroup = 17 + and patient.status = 1 + and tps.status = 1 + and tpst.status = 1 + and trs.status = 1 + and tpst.labtestid in (651,652,653,654,650) + and trs.rtitle !='Pending' + ".$cond); + + $this->insert_surveillance_case_lab_results($lbm_case_results); + + } + + public function getAFICases($dateFrom, $dateTo = NULL) + { + $cond = ""; + $cond .= " and afi_lab_result.results_date >= '".date('Y-m-d', strtotime($dateFrom))."' "; + $cond .= (!empty($dateTo)) ? " and afi_lab_result.results_date <='".date('Y-m-d', strtotime($dateTo))."' ":""; + $afi_cases = DB::connection('mysql_afi') + ->select(" + SELECT + afi_lab_result.sample_code as labcode, + afi_lab_result.results_date as patdate, + 1 as isnewcase, + afi_case.sentinel_site as labname_en, + '' as labaddress_en, + 4 as surveillance_id, + niphc0_nphl.get_epi_period(afi_lab_result.results_date, 'Y') as year_data, + niphc0_nphl.get_epi_period(afi_lab_result.results_date, 'W') as week_data, + ifnull(DATEDIFF(afi_case.date_of_interview, date_of_birth),180) as patage, + if(afi_case.sex='Male','M','F') as patsex, + NULL as is_alive, + p.proname_en as proname_en + FROM afi_db.`afi_tbl_afiform` as afi_case + left join afi_db.afi_tbl_result afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + left join niphc0_nphl.province p on p.proid = afi_case.residence_province + where + afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null " .$cond); + + $this->insert_surveillance_cases($afi_cases); + + $afi_lab_results = DB::connection('mysql_afi') + ->select( + "SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.influenza_result='Positive',1,0) as is_positive, + if(afi_lab_result.influenza_result='Positive', concat('Influenza ',LEFT(UPPER(JSON_UNQUOTE(JSON_EXTRACT(afi_lab_result.influenza_subtypes, '$[0].id'))),1)),'') pathogen_name, + if(afi_lab_result.influenza_result='Positive', REPLACE(UPPER(JSON_UNQUOTE(JSON_EXTRACT(afi_lab_result.influenza_subtypes, '$[0].id'))),'_','/'),'') as subtype, + 'AFI Influenza Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.influenza_result in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + union + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.sarscov2_result='Positive',1,0) as is_positive, + if(afi_lab_result.sarscov2_result='Positive', 'SARS-CoV-2','') pathogen_name, + '' as subtype, + 'AFI Covid Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.sarscov2_result in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + union + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.dengue_pcr='Positive',1,0) as is_positive, + if(afi_lab_result.dengue_pcr='Positive', 'Dengue','') pathogen_name, + '' as subtype, + 'AFI Dengue PCR Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.dengue_pcr in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + union + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.chikungunya_pcr='Positive',1,0) as is_positive, + if(afi_lab_result.chikungunya_pcr='Positive', 'Chikungunya','') pathogen_name, + '' as subtype, + 'AFI Chikungunya PCR Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.chikungunya_pcr in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + union + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.leptospira_pcr='Positive',1,0) as is_positive, + if(afi_lab_result.leptospira_pcr='Positive', 'Leptospira','') pathogen_name, + '' as subtype, + 'AFI Leptospira PCR Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.leptospira_pcr in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + union + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.plasmodium_pcr='Positive',1,0) as is_positive, + if(afi_lab_result.plasmodium_pcr='Positive', 'Plasmodium','') pathogen_name, + '' as subtype, + 'AFI Plasmodium PCR Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.plasmodium_pcr in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + UNION + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.rickettsia_pcr='Positive',1,0) as is_positive, + if(afi_lab_result.rickettsia_pcr='Positive', 'Rickettsia','') pathogen_name, + '' as subtype, + 'AFI Rickettsia PCR Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.rickettsia_pcr in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + UNION + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.salmonella_pcr='Positive',1,0) as is_positive, + if(afi_lab_result.salmonella_pcr='Positive', 'Salmonella','') pathogen_name, + '' as subtype, + 'AFI Salmonella PCR Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.salmonella_pcr in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + UNION + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.westnile_pcr='Positive',1,0) as is_positive, + if(afi_lab_result.westnile_pcr='Positive', 'Westnile Nile','') pathogen_name, + '' as subtype, + 'AFI Westnile PCR Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.westnile_pcr in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + UNION + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.zika_pcr='Positive',1,0) as is_positive, + if(afi_lab_result.zika_pcr='Positive', 'ZIKA','') pathogen_name, + '' as subtype, + 'AFI ZIKA PCR Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.zika_pcr in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + UNION + + # SERUM + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.chikungunya_serum='Positive',1,0) as is_positive, + if(afi_lab_result.chikungunya_serum='Positive', 'Chikungunya','') pathogen_name, + '' as subtype, + 'AFI Chikungunya Serum Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.chikungunya_serum in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + UNION + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.dengue_serum='Positive',1,0) as is_positive, + if(afi_lab_result.dengue_serum='Positive', 'Dengue','') pathogen_name, + '' as subtype, + 'AFI Dengue Serum Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.dengue_serum in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + UNION + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.westnile_serum='Positive',1,0) as is_positive, + if(afi_lab_result.westnile_serum='Positive', 'Westnile','') pathogen_name, + '' as subtype, + 'AFI Westnile Serum Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.westnile_serum in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + UNION + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.zika_serum='Positive',1,0) as is_positive, + if(afi_lab_result.zika_serum='Positive', 'ZIKA','') pathogen_name, + '' as subtype, + 'AFI ZIKA Serum Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.zika_serum in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond." + + UNION + + SELECT + afi_lab_result.sample_code as labcode, + 4 as surveillance_id, + if(afi_lab_result.je_virus='Positive',1,0) as is_positive, + if(afi_lab_result.je_virus='Positive', 'Japanese Encephalitis','') pathogen_name, + '' as subtype, + 'AFI Japanese Encephalitis Test' as indicator + FROM afi_db.`afi_tbl_afiform` as afi_case + INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id + where afi_lab_result.sample_code is not null + and afi_lab_result.je_virus in('Negative', 'Positive') + and afi_case.afi_event is NOT null + and afi_lab_result.results_date is not null ".$cond + ); + + $this->insert_surveillance_case_lab_results($afi_lab_results); + } + + public function getNSDCases($dateFrom, $dateTo = NULL) + { + return []; + } + + public function getSEQCases($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))."' ":""; + $seq_cases = DB::connection('mysql_nphl') + ->select(" + SELECT DISTINCT + patient.labcode, + patient.patdate, + patient.isnewcase, + l.labname_en, + l.labaddress_en, + 6 as surveillance_id, + niphc0_nphl.get_epi_period(patient.patdate, 'Y') as year_data, + niphc0_nphl.get_epi_period(patient.patdate, 'W') as week_data, + patient.patage, + patient.patsex, + NULL as is_alive, + pro.proname_en + from niphc0_nphl.sq_batch sqb + inner join niphc0_nphl.sq_sub_batch sqsb on sqsb.bt_id = sqb.bt_id + inner join niphc0_nphl.labopatients patient on patient.patid = sqsb.patid + inner join niphc0_nphl.laboratory l on l.labid = patient.patsource + inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid + inner join niphc0_nphl.test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid + left join niphc0_nphl.province pro on pro.proid = patient.frompro + where patient.status = 1 + and tpst.labtestid IN ( 659, 662, 666, 667, 668 ) + " . $cond); + + $this->insert_surveillance_cases($seq_cases); + + $seq_lab_results = DB::connection('mysql_nphl') + ->select(" + SELECT DISTINCT + patient.labcode, + 6 as surveillance_id, + 1 is_positive, + ifnull(sqsb.seqlineage,'N/A') as pathogen_name, + trs.rtitle as subtype, + concat('Sequencing Test ', case when patgroup=2 then 'SARI' when patgroup=3 then 'ILI' when patgroup=19 then 'COVID-19' else '' end) as indicator + from niphc0_nphl.sq_batch sqb + inner join niphc0_nphl.sq_sub_batch sqsb on sqsb.bt_id = sqb.bt_id + inner join niphc0_nphl.labopatients patient on patient.patid = sqsb.patid + inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid + inner join niphc0_nphl.test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid + inner join niphc0_nphl.test_pattestresult tptr on tptr.pstid = tpst.pstid + inner join niphc0_nphl.test_resultselect trs on trs.rid = tptr.rsltid + where patient.status = 1 + and tpst.labtestid IN ( 659, 662, 666, 667, 668 ) + and trs.rtitle not in ('Inconclusive', 'In Progress') + " . $cond); + + $this->insert_surveillance_case_lab_results($seq_lab_results); + + } + + private function insert_surveillance_cases($cases){ + + $case_data = []; + foreach ($cases as $case) { + $case_data[] = [ + 'lab_code' => $case->labcode, + 'case_date' => $case->patdate, + 'is_newcase' => $case->isnewcase, + 'sentinel_site_name' => $case->labname_en, + 'site_province_name' => $case->labaddress_en, + 'surveillance_id' => $case->surveillance_id, + 'year_data' => $case->year_data, + 'week_data' => $case->week_data, + 'patient_age_inday' => $case->patage, + 'patient_sex' => $case->patsex, + 'is_alive' => $case->is_alive, + 'patient_province' => $case->proname_en + ]; + } + + $case_chunks = array_chunk($case_data, 100); + foreach ($case_chunks as $chunk) { + DB::connection('mysql')->table('tmp_surveillance_cases')->upsert($chunk, [ + 'lab_code', + 'surveillance_id' + ], [ + 'case_date', + 'is_newcase', + 'sentinel_site_name', + 'site_province_name', + 'year_data', + 'week_data', + 'patient_age_inday', + 'patient_sex', + 'is_alive', + 'patient_province' + ]); + } + } + + private function insert_surveillance_case_lab_results($lab_results){ + $result_data = []; + foreach ($lab_results as $lab_result) { + $result_data[] = [ + 'lab_code' => $lab_result->labcode, + 'surveillance_id' => $lab_result->surveillance_id, + 'is_positive' => $lab_result->is_positive, + 'pathogen_name' => $lab_result->pathogen_name, + 'subtype' => $lab_result->subtype, + 'indicator' => $lab_result->indicator + ]; + } + $result_chunks = array_chunk($result_data, 100); + foreach ($result_chunks as $chunk) { + DB::connection('mysql')->table('tmp_case_lab_results')->upsert($chunk, + [ + 'lab_code', + 'surveillance_id', + 'indicator' + ], + [ + 'is_positive', + 'pathogen_name', + 'subtype' + ]); + } + } + +} diff --git a/dashboard/config/app.php b/dashboard/config/app.php index 423eed5..9aa6d51 100644 --- a/dashboard/config/app.php +++ b/dashboard/config/app.php @@ -15,6 +15,15 @@ return [ 'name' => env('APP_NAME', 'Laravel'), + 'lookback_days' => [ + 'SARI' => 777, // + 'ILI' => 777, + 'LBM' => 777, + 'AFI' => 777, + 'NDS' => 777, + 'SEQ' => 200 + ], + /* |-------------------------------------------------------------------------- | Application Environment diff --git a/dashboard/config/database.php b/dashboard/config/database.php index df933e7..bdf0a92 100644 --- a/dashboard/config/database.php +++ b/dashboard/config/database.php @@ -113,6 +113,33 @@ return [ // 'trust_server_certificate' => env('DB_TRUST_SERVER_CERTIFICATE', 'false'), ], + 'mysql_nphl' => [ + 'driver' => 'mariadb', + 'host' => env('NPHL_DB_HOST', '127.0.0.1'), + 'port' => env('NPHL_DB_PORT', '3306'), + 'database' => env('NPHL_DB_DATABASE', 'laravel'), + 'username' => env('NPHL_DB_USERNAME', 'root'), + 'password' => env('NPHL_DB_PASSWORD', '') + ], + + 'mysql_afi' => [ + 'driver' => 'mysql', + 'host' => env('AFI_DB_HOST', '127.0.0.1'), + 'port' => env('AFI_DB_PORT', '3306'), + 'database' => env('AFI_DB_DATABASE', 'laravel'), + 'username' => env('AFI_DB_USERNAME', 'root'), + 'password' => env('AFI_DB_PASSWORD', '') + ], + + 'mysql_nds' => [ + 'driver' => 'mysql', + 'host' => env('NDS_DB_HOST', '127.0.0.1'), + 'port' => env('NDS_DB_PORT', '3306'), + 'database' => env('NDS_DB_DATABASE', 'laravel'), + 'username' => env('NDS_DB_USERNAME', 'root'), + 'password' => env('NDS_DB_PASSWORD', '') + ], + ], /* diff --git a/dashboard/config/logging.php b/dashboard/config/logging.php index 9e998a4..75ddaac 100644 --- a/dashboard/config/logging.php +++ b/dashboard/config/logging.php @@ -94,6 +94,13 @@ return [ 'processors' => [PsrLogMessageProcessor::class], ], + 'jobs' => [ + 'driver' => 'daily', + 'path' => storage_path('logs/job_run.log'), + 'level' => 'debug', + 'days' => 60, + ], + 'stderr' => [ 'driver' => 'monolog', 'level' => env('LOG_LEVEL', 'debug'), diff --git a/dashboard/resources/views/layouts/app.blade.php b/dashboard/resources/views/layouts/app.blade.php index e5e5662..dc9ba5b 100644 --- a/dashboard/resources/views/layouts/app.blade.php +++ b/dashboard/resources/views/layouts/app.blade.php @@ -7,10 +7,10 @@ - + - - + + @@ -130,6 +130,10 @@ @endforeach + + @@ -143,6 +147,17 @@ @yield('scripts') + + + - \ No newline at end of file + diff --git a/dashboard/routes/api.php b/dashboard/routes/api.php index f720e3a..92a8006 100644 --- a/dashboard/routes/api.php +++ b/dashboard/routes/api.php @@ -7,4 +7,5 @@ Route::get('/dashboard/summary', [DashboardController::class, 'summary']); Route::get('/dashboard/trend', [DashboardController::class, 'trend']); Route::get('/dashboard/program', [DashboardController::class, 'program']); Route::get('/dashboard/province-circles', [DashboardController::class, 'provinceCircles']); -Route::get('/dashboard/sentinel-map', [DashboardController::class, 'sentinelMap']); \ No newline at end of file +Route::get('/dashboard/sentinel-map', [DashboardController::class, 'sentinelMap']); +Route::get('/dashboard/reload', [DashboardController::class, 'fetchSourceData']); diff --git a/dashboard/routes/console.php b/dashboard/routes/console.php index 3c9adf1..7b1d12b 100644 --- a/dashboard/routes/console.php +++ b/dashboard/routes/console.php @@ -2,7 +2,10 @@ use Illuminate\Foundation\Inspiring; use Illuminate\Support\Facades\Artisan; +use Illuminate\Support\Facades\Schedule; Artisan::command('inspire', function () { $this->comment(Inspiring::quote()); })->purpose('Display an inspiring quote'); + +Schedule::command('app:fetch-source-data')->everyTwoHours();