From b1b6c639b25d6ad9864c48ff7f17804a04274ac6 Mon Sep 17 00:00:00 2001 From: pcalengratha Date: Mon, 4 May 2026 10:15:54 +0700 Subject: [PATCH] modify feature nphl api integration --- .../app/Services/DataRetrievalService.php | 771 +----------------- dashboard/config/app.php | 12 +- 2 files changed, 11 insertions(+), 772 deletions(-) diff --git a/dashboard/app/Services/DataRetrievalService.php b/dashboard/app/Services/DataRetrievalService.php index 733c82f..6373d69 100644 --- a/dashboard/app/Services/DataRetrievalService.php +++ b/dashboard/app/Services/DataRetrievalService.php @@ -63,18 +63,12 @@ class DataRetrievalService $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)); + $data = json_decode(preg_replace('/^\x{FEFF}+/u', '', $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; } @@ -85,761 +79,6 @@ 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))."' ":""; - $sari_cases = DB::connection('mysql_nphl') - ->select(" - SELECT - patient.labcode, - min(if(year(tps.collected)=0,patient.patdate, tps.collected)) as patdate, - patient.isnewcase, - l.labname_en, - l.labaddress_en, - 1 as surveillance_id, - min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,patient.patdate, tps.collected), 'Y')) as year_data, - min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,patient.patdate, tps.collected), '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.test_patsample tps on tps.patientid = patient.patid and tps.status = 1 - 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 . " - group by - patient.labcode, - patient.isnewcase, - l.labname_en, - l.labaddress_en, - patient.patage, - patient.patsex, - p.proname_en;" - - ); - - $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, - 'Influenza' 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, - 'Covid-19' 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, - min(if(year(tps.collected)=0,patient.patdate, tps.collected)) as patdate, - patient.isnewcase, - l.labname_en, - l.labaddress_en, - 2 as surveillance_id, - min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,patient.patdate, tps.collected), 'Y')) as year_data, - min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,patient.patdate, tps.collected), '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.test_patsample tps on tps.patientid = patient.patid and tps.status = 1 - 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 . " - group by - patient.labcode, - patient.isnewcase, - l.labname_en, - l.labaddress_en, - patient.patage, - patient.patsex, - p.proname_en;" - ); - - - $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, - 'Influenza' 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, - 'Covid-19' 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, - min(if(year(tps.collected)=0,patient.patdate, tps.collected)) as patdate, - patient.isnewcase, - l.labname_en, - l.labaddress_en, - 3 as surveillance_id, - min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,patient.patdate, tps.collected), 'Y')) as year_data, - min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,patient.patdate, tps.collected), 'W')) as 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 - inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid and tps.status = 1 - 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 . " - group by - patient.labcode, - patient.isnewcase, - l.labname_en, - l.labaddress_en, - patient.patage, - patient.patsex, - p.proname_en;" - ); - - $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, - 'Influenza' 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, - 'Covid-19' 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_case.date_of_interview >= '".date('Y-m-d', strtotime($dateFrom))."' "; - $cond .= (!empty($dateTo)) ? " and afi_case.date_of_interview <='".date('Y-m-d', strtotime($dateTo))."' ":""; - $afi_cases = DB::connection('mysql_afi') - ->select(" - SELECT - afi_lab_result.sample_code as labcode, - afi_case.date_of_interview 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_case.date_of_interview, 'Y') as year_data, - niphc0_nphl.get_epi_period(afi_case.date_of_interview, '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_case.date_of_interview is not null - and afi_lab_result.sample_code 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, - 'Influenza' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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, - 'Covid-19' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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 Virus','') pathogen_name, - '' as subtype, - 'Dengue Virus|PCR' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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 Virus','') pathogen_name, - '' as subtype, - 'Chikungunya Virus|PCR' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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 spp.','') pathogen_name, - '' as subtype, - 'Leptospira spp.|PCR' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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 spp.','') pathogen_name, - '' as subtype, - 'Plasmodium spp.|PCR' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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 spp.','') pathogen_name, - '' as subtype, - 'Rickettsia spp.|PCR' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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 spp.','') pathogen_name, - '' as subtype, - 'Salmonella spp.|PCR' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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', 'West Nile Virus','') pathogen_name, - '' as subtype, - 'West Nile Virus|PCR' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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 Virus','') pathogen_name, - '' as subtype, - 'ZIKA Virus|PCR' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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 Virus','') pathogen_name, - '' as subtype, - 'Chikungunya Virus|Serum' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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 Virus','') pathogen_name, - '' as subtype, - 'Dengue Virus|Serum' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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', 'West Nile Virus','') pathogen_name, - '' as subtype, - 'West Nile Virus|Serum' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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 Virus','') pathogen_name, - '' as subtype, - 'ZIKA Virus|Serum' 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_case.date_of_interview is not null - and afi_lab_result.sample_code 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 Virus','') pathogen_name, - '' as subtype, - 'Japanese Encephalitis Virus|Serum' 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_case.date_of_interview is not null - and afi_lab_result.sample_code is not null ".$cond - ); - - $this->insert_surveillance_case_lab_results($afi_lab_results); - } - - public function getNSDCases($dateFrom, $dateTo = NULL) - { - $cond = ""; - $cond .= " and sc.sc_date >= '".date('Y-m-d', strtotime($dateFrom))."' "; - $cond .= (!empty($dateTo)) ? " and sc.sc_date <='".date('Y-m-d', strtotime($dateTo))."' ":""; - $nds_cases = DB::connection('mysql_nds') - ->select(" - SELECT - DISTINCT - sc.sc_sample_code as labcode, - sc.sc_date as patdate, - 1 as isnewcase, - site_ad.sn_name as labname_en, - '' as labaddress_en, - 5 as surveillance_id, - niphc0_nphl.get_epi_period(sc.sc_date, 'Y') as year_data, - niphc0_nphl.get_epi_period(sc.sc_date, 'W') as week_data, - ifnull(DATEDIFF(sc.sc_date, patient.pat_dob),180) as patage, - if(patient.pat_gender='Male','M','F') as patsex, - NULL as is_alive, - pro.pro_eng as proname_en - from nds_db.tbl_nds_site_invest case_inv - inner join nds_db.tbl_nds_siteinvet_patient inv_patient - on inv_patient.pksi_id = case_inv.pksi_id - inner join nds_db.tbl_nds_site_address site_ad - on site_ad.pksn_id = case_inv.fksn_id - inner join nds_db.tbl_nds_patient patient - on patient.pkpat_id = inv_patient.fkpat_id - inner join nds_db.tbl_nds_basic_sample bs - on bs.fkpat_id = inv_patient.fkpat_id - inner join nds_db.tbl_nds_sample_collection sc - on sc.fkbs_id = bs.pkbs_id - left join nds_db.tblprovince pro - on pro.pkpro_code = JSON_UNQUOTE(JSON_EXTRACT(patient.pat_address, '$[0].province')) - where - sc.sc_sample_code is not null - and length(sc_sample_code)>0 " .$cond); - - $this->insert_surveillance_cases($nds_cases); - - $nds_lab_results = DB::connection('mysql_nds') - ->select(" - SELECT - DISTINCT - sc.sc_sample_code as labcode, - 5 as surveillance_id, - rt.rt_positive as is_positive, - trt.trt_name pathogen_name, - '' as subtype, - trt.trt_name as indicator - from nds_db.tbl_nds_site_invest case_inv - inner join nds_db.tbl_nds_siteinvet_patient inv_patient - on inv_patient.pksi_id = case_inv.pksi_id - inner join nds_db.tbl_nds_patient patient - on patient.pkpat_id = inv_patient.fkpat_id - inner join nds_db.tbl_nds_basic_sample bs - on bs.fkpat_id = inv_patient.fkpat_id - inner join nds_db.tbl_nds_sample_collection sc - on sc.fkbs_id = bs.pkbs_id - inner join nds_db.tbl_nds_result_test rt - on rt.fkpat_id = bs.fkpat_id - inner join nds_db.tbl_nds_test_result_type trt - on trt.pktrt_id = rt.fktrt_id - - where - sc.sc_sample_code is not null - and length(sc_sample_code)>0 " .$cond); - - $this->insert_surveillance_case_lab_results($nds_lab_results); - - } - - public function getSEQCases($dateFrom, $dateTo = NULL) - { - $cond = ""; - $cond .= " and c.patdate >= '".date('Y-m-d', strtotime($dateFrom))."' "; - $cond .= (!empty($dateTo)) ? " and c.patdate <='".date('Y-m-d', strtotime($dateTo))."' ":""; - $seq_cases = DB::connection('mysql_nphl') - ->select(" - SELECT DISTINCT - c.labcode, - min(if(year(tps.collected)=0,c.patdate, tps.collected)) as patdate, - c.isnewcase, - l.labname_en, - l.labaddress_en, - 6 as surveillance_id, - min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,c.patdate, tps.collected), 'Y')) as year_data, - min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,c.patdate, tps.collected), 'W')) as week_data, - c.patage, - c.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 c on c.patid = sqsb.patid - inner join niphc0_nphl.laboratory l on l.labid = p.patsource - inner join niphc0_nphl.test_patsample tps on tps.patientid = c.patid and tps.status = 1 - 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 = c.frompro - where c.status = 1 - and tpst.labtestid IN ( 659, 662, 666, 667, 668 ) - " . $cond . " - group by - c.labcode, - c.isnewcase, - l.labname_en, - l.labaddress_en, - c.patage, - c.patsex, - pro.proname_en;" - ); - - $this->insert_surveillance_cases($seq_cases); - - $seq_lab_results = DB::connection('mysql_nphl') - ->select(" - SELECT - labcode, - 6 as surveillance_id, - 1 is_positive, - ifnull(seqlineage,'N/A') as pathogen_name, - GROUP_CONCAT(seq_result SEPARATOR '') as subtype, - (case - when (Group_concat(influ_result SEPARATOR '')='Negatives' or Group_concat(influ_result SEPARATOR '')='') and Group_concat(covid_result SEPARATOR '')='Positive' then 'Covid-19' - when (Group_concat(influ_result SEPARATOR '') !='Negatives' and Group_concat(influ_result SEPARATOR '')!='') and Group_concat(covid_result SEPARATOR '')='Negative' then 'Influenza' - when (Group_concat(influ_result SEPARATOR '') !='Negatives' and Group_concat(influ_result SEPARATOR '')!='') and Group_concat(covid_result SEPARATOR '')='Positive' then 'Co-infection' - else 'N/A' - end) as indicator - FROM (SELECT - c.labcode, - ( CASE - WHEN e1.labtestid IN ( 238, 340, 381 ) THEN e3.rtitle - ELSE '' - end ) influ_result, - ( CASE - WHEN e1.labtestid IN ( 650, 651 ) THEN e3.rtitle - ELSE '' - end ) covid_result, - ( CASE - WHEN e1.labtestid IN ( 668, 659, 666, 667, 662 ) THEN - e3.rtitle - ELSE '' - end ) seq_result, - b.seqlineage - FROM niphc0_nphl.sq_batch a - LEFT JOIN niphc0_nphl.sq_sub_batch b - ON a.bt_id = b.bt_id - LEFT JOIN niphc0_nphl.labopatients c - ON b.patid = c.patid - LEFT JOIN niphc0_nphl.laboratory d - ON c.patsource = d.labid - LEFT JOIN niphc0_nphl.test_patsamtest e1 - ON c.patid = e1.patid - LEFT JOIN niphc0_nphl.test_pattestresult e2 - ON e1.pstid = e2.pstid - LEFT JOIN niphc0_nphl.test_resultselect e3 - ON e2.rsltid = e3.rid - LEFT JOIN niphc0_nphl.ncov_patients e4 - ON c.patid = e4.patid - LEFT JOIN niphc0_nphl.test_patsample e5 - ON c.patid = e5.patientid - AND e5.sampleid = e1.patsmid - LEFT JOIN niphc0_nphl.test_patsmpbyunit f - ON e5.sampleid = f.patsamid - AND c.patid = f.patid - WHERE e1.labtestid IN ( 238, 340, 381, 650, - 651, 668, 659, 666, - 667, 662 ) - " . $cond . " - ) tbl_a - GROUP BY labcode, - seqlineage - HAVING - GROUP_CONCAT(seq_result SEPARATOR '') not in ('','Inconclusive', 'In Progress') - "); - - $this->insert_surveillance_case_lab_results($seq_lab_results); - - } - private function insert_surveillance_cases($cases){ $case_data = []; @@ -860,9 +99,9 @@ class DataRetrievalService ]; } - $case_chunks = array_chunk($case_data, 100); + $case_chunks = array_chunk($case_data, 500); foreach ($case_chunks as $chunk) { - DB::connection('mysql')->table('tmp_surveillance_cases')->upsert($chunk, [ + DB::connection('mysql')->table('surveillance_cases')->upsert($chunk, [ 'lab_code', 'surveillance_id' ], [ @@ -892,9 +131,9 @@ class DataRetrievalService 'indicator' => $lab_result->indicator ]; } - $result_chunks = array_chunk($result_data, 100); + $result_chunks = array_chunk($result_data, 500); foreach ($result_chunks as $chunk) { - DB::connection('mysql')->table('tmp_case_lab_results')->upsert($chunk, + DB::connection('mysql')->table('case_lab_results')->upsert($chunk, [ 'lab_code', 'surveillance_id', diff --git a/dashboard/config/app.php b/dashboard/config/app.php index ad5e6bf..6851d50 100644 --- a/dashboard/config/app.php +++ b/dashboard/config/app.php @@ -16,12 +16,12 @@ return [ 'name' => env('APP_NAME', 'Laravel'), 'lookback_days' => [ - 'SARI' => 7, // - 'ILI' => 7, - 'LBM' => 7, - 'AFI' => 7, - 'NDS' => 7, - 'SEQ' => 7 + 'SARI' => 150, + 'ILI' => 150, + 'LBM' => 150, + 'AFI' => 150, + 'NDS' => 150, + 'SEQ' => 150 ], /*