modify dashboard and services fetch source data

This commit is contained in:
pcalengratha
2026-04-08 14:48:42 +07:00
parent f0a5079b15
commit 4534f502e2
9 changed files with 1322 additions and 241 deletions

View File

@@ -139,6 +139,87 @@ class DashboardController extends Controller
return response()->json($data);
}
/*
|--------------------------------------------------------------------------
| Influenza subtype distribution (Overview)
|--------------------------------------------------------------------------
*/
public function influenzaSubtypeDetected(Request $request)
{
$range = $this->getEpiRange($request);
if (!$range) {
return response()->json(['error' => 'Missing epiweek range'], 400);
}
$data = $this->service->influenzaSubtypeDetected(
$range['startYear'],
$range['startWeek'],
$range['endYear'],
$range['endWeek']
);
return response()->json($data);
}
public function covidDistributedByAgeGroup(Request $request)
{
$range = $this->getEpiRange($request);
if (!$range) {
return response()->json(['error' => 'Missing epiweek range'], 400);
}
$data = $this->service->covidDistributedByAgeGroup(
$range['startYear'],
$range['startWeek'],
$range['endYear'],
$range['endWeek']
);
return response()->json($data);
}
public function covidLineageRelativeOverTime(Request $request)
{
$range = $this->getEpiRange($request);
if (!$range) {
return response()->json(['error' => 'Missing epiweek range'], 400);
}
$data = $this->service->covidLineageRelativeOverTime(
$range['startYear'],
$range['startWeek'],
$range['endYear'],
$range['endWeek']
);
return response()->json($data);
}
public function influenzaRelativeOverTime(Request $request)
{
$range = $this->getEpiRange($request);
if (!$range) {
return response()->json(['error' => 'Missing epiweek range'], 400);
}
$data = $this->service->influenzaRelativeOverTime(
$range['startYear'],
$range['startWeek'],
$range['endYear'],
$range['endWeek']
);
return response()->json($data);
}
/*
|--------------------------------------------------------------------------

View File

@@ -575,6 +575,33 @@ class DashboardService
*/
public function provinceCircles($startYear, $startWeek, $endYear, $endWeek)
{
return SurveillanceCase::join('case_lab_results', function ($join) {
$join->on('surveillance_cases.lab_code', '=', 'case_lab_results.lab_code')
->on('surveillance_cases.surveillance_id', '=', 'case_lab_results.surveillance_id');
})
->where(function ($q) use ($startYear, $startWeek, $endYear, $endWeek) {
$q->whereRaw(
"(surveillance_cases.year_data * 100 + surveillance_cases.week_data) BETWEEN ? AND ?",
[
$startYear * 100 + $startWeek,
$endYear * 100 + $endWeek
]
);
})
->whereRaw('case_lab_results.is_positive = 1 and surveillance_cases.surveillance_id not in(6) and case_lab_results.indicator="Influenza"')
->selectRaw("
surveillance_cases.patient_province,
case_lab_results.subtype as pathogen_name,
COUNT(DISTINCT surveillance_cases.lab_code) as total
")
->groupBy(
'surveillance_cases.patient_province',
'case_lab_results.subtype'
)
->get();
return SurveillanceCase::leftJoin(
'case_lab_results',
'surveillance_cases.lab_code',
@@ -590,28 +617,156 @@ class DashboardService
->whereRaw(
"(surveillance_cases.year_data < ? OR (surveillance_cases.year_data = ? AND surveillance_cases.week_data <= ?))",
[$endYear, $endYear, $endWeek]
);
)
->whereRaw('case_lab_results.is_positive = 1 and surveillance_cases.surveillance_id not in(6)');
})
->selectRaw("
surveillance_cases.patient_province,
surveillance_cases.surveillance_id,
case_lab_results.pathogen_name,
COUNT(DISTINCT surveillance_cases.lab_code) as total,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
THEN surveillance_cases.lab_code
END) as positive
COUNT(DISTINCT surveillance_cases.lab_code) as total
")
->groupBy(
'surveillance_cases.patient_province',
'surveillance_cases.surveillance_id'
'case_lab_results.pathogen_name'
)
->get();
}
/* Start Overview Section */
public function influenzaSubtypeDetected($startYear, $startWeek, $endYear, $endWeek)
{
return SurveillanceCase::join('case_lab_results', function ($join) {
$join->on('surveillance_cases.lab_code', '=', 'case_lab_results.lab_code')
->on('surveillance_cases.surveillance_id', '=', 'case_lab_results.surveillance_id');
})
->where(function ($q) use ($startYear, $startWeek, $endYear, $endWeek) {
$q->whereRaw(
"(surveillance_cases.year_data * 100 + surveillance_cases.week_data) BETWEEN ? AND ?",
[
$startYear * 100 + $startWeek,
$endYear * 100 + $endWeek
]
);
})
->whereRaw('case_lab_results.is_positive = 1 and surveillance_cases.surveillance_id not in(6) and case_lab_results.indicator="Influenza"')
->selectRaw("
case_lab_results.subtype,
COUNT(DISTINCT surveillance_cases.lab_code) as total
")
->groupBy(
'case_lab_results.subtype'
)
->get();
}
public function covidDistributedByAgeGroup($startYear, $startWeek, $endYear, $endWeek)
{
return SurveillanceCase::join('case_lab_results', function ($join) {
$join->on('surveillance_cases.lab_code', '=', 'case_lab_results.lab_code')
->on('surveillance_cases.surveillance_id', '=', 'case_lab_results.surveillance_id');
})
->where(function ($q) use ($startYear, $startWeek, $endYear, $endWeek) {
$q->whereRaw(
"(surveillance_cases.year_data * 100 + surveillance_cases.week_data) BETWEEN ? AND ?",
[
$startYear * 100 + $startWeek,
$endYear * 100 + $endWeek
]
);
})
->whereRaw('case_lab_results.is_positive = 1 and surveillance_cases.surveillance_id not in(6) and case_lab_results.indicator="Covid-19"')
->selectRaw("
CASE
WHEN patient_age_inday <= 28 THEN '028 days'
WHEN patient_age_inday <= 364 THEN '29 days11 months'
WHEN patient_age_inday <= 1460 THEN '14 years'
WHEN patient_age_inday <= 5110 THEN '514 years'
WHEN patient_age_inday <= 8765 THEN '1524 years'
WHEN patient_age_inday <= 18250 THEN '2549 years'
WHEN patient_age_inday <= 23725 THEN '5064 years'
ELSE '65+ years'
END as age_group,
CASE
WHEN patient_age_inday <= 28 THEN 1
WHEN patient_age_inday <= 364 THEN 2
WHEN patient_age_inday <= 1460 THEN 3
WHEN patient_age_inday <= 5110 THEN 4
WHEN patient_age_inday <= 8765 THEN 5
WHEN patient_age_inday <= 18250 THEN 6
WHEN patient_age_inday <= 23725 THEN 7
ELSE 8
END as age_order,
COUNT(*) as total
")
->groupBy('age_group', 'age_order')
->orderBy('age_order')
->get();
}
public function covidLineageRelativeOverTime($startYear, $startWeek, $endYear, $endWeek)
{
return SurveillanceCase::join('case_lab_results', function ($join) {
$join->on('surveillance_cases.lab_code', '=', 'case_lab_results.lab_code')
->on('surveillance_cases.surveillance_id', '=', 'case_lab_results.surveillance_id');
})
->where(function ($q) use ($startYear, $startWeek, $endYear, $endWeek) {
$q->whereRaw(
"(surveillance_cases.year_data * 100 + surveillance_cases.week_data) BETWEEN ? AND ?",
[
$startYear * 100 + $startWeek,
$endYear * 100 + $endWeek
]
);
})
->whereRaw('case_lab_results.is_positive = 1 and surveillance_cases.surveillance_id=6 and case_lab_results.indicator="Covid-19" and case_lab_results.pathogen_name not in("","unable to align","N/A","NA")')
->selectRaw("
case_lab_results.pathogen_name as lineage,
concat(surveillance_cases.year_data,'-',surveillance_cases.week_data) as week,
COUNT(DISTINCT surveillance_cases.lab_code) as total
")
->groupBy(
'case_lab_results.pathogen_name',
'week'
)
->get();
}
public function influenzaRelativeOverTime($startYear, $startWeek, $endYear, $endWeek)
{
return SurveillanceCase::join('case_lab_results', function ($join) {
$join->on('surveillance_cases.lab_code', '=', 'case_lab_results.lab_code')
->on('surveillance_cases.surveillance_id', '=', 'case_lab_results.surveillance_id');
})
->where(function ($q) use ($startYear, $startWeek, $endYear, $endWeek) {
$q->whereRaw(
"(surveillance_cases.year_data * 100 + surveillance_cases.week_data) BETWEEN ? AND ?",
[
$startYear * 100 + $startWeek,
$endYear * 100 + $endWeek
]
);
})
->whereRaw('case_lab_results.is_positive = 1 and surveillance_cases.surveillance_id not in(6) and case_lab_results.indicator="Influenza"')
->selectRaw("
case_lab_results.subtype as lineage,
concat(surveillance_cases.year_data,'-',surveillance_cases.week_data) as week,
COUNT(DISTINCT surveillance_cases.lab_code) as total
")
->groupBy(
'case_lab_results.subtype',
'week'
)
->get();
}
/* End Overview Section */
public function provinceProgram($surveillanceId, $startYear, $startWeek, $endYear, $endWeek)
{
return SurveillanceCase::selectRaw("
@@ -699,7 +854,7 @@ class DashboardService
->selectRaw("
CASE
WHEN LOWER(case_lab_results.pathogen_name) LIKE '%influenza%'
WHEN LOWER(case_lab_results.pathogen_name) LIKE '%influenza%'
THEN 'Influenza'
ELSE case_lab_results.pathogen_name
END as pathogen,
@@ -740,7 +895,7 @@ class DashboardService
->where('case_lab_results.subtype', '!=', 'Positive')
->groupBy('subtype')
->groupBy('subtype')
->orderByDesc('total')
->get();
@@ -874,4 +1029,4 @@ class DashboardService
->orderBy('period')
->get();
}
}
}

View File

@@ -20,7 +20,7 @@ class DataRetrievalService
$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->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;
@@ -42,24 +42,35 @@ class DataRetrievalService
->select("
SELECT
patient.labcode,
patient.patdate,
min(if(year(tps.collected)=0,patient.patdate, tps.collected)) as 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,
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);
".$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);
@@ -71,7 +82,7 @@ class DataRetrievalService
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
'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
@@ -93,7 +104,7 @@ class DataRetrievalService
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
'Covid-19' as indicator
FROM `labopatients` patient
inner join sari_patients sr_p
on sr_p.patid = patient.patid
@@ -126,24 +137,33 @@ class DataRetrievalService
->select("
SELECT
patient.labcode,
patient.patdate,
min(if(year(tps.collected)=0,patient.patdate, tps.collected)) as 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,
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
".$cond . "
group by
patient.labcode,
patient.isnewcase,
l.labname_en,
l.labaddress_en,
patient.patage,
patient.patsex,
p.proname_en;"
);
@@ -158,7 +178,7 @@ class DataRetrievalService
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
'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
@@ -182,7 +202,7 @@ class DataRetrievalService
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
'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
@@ -211,24 +231,34 @@ class DataRetrievalService
->select("
SELECT
patient.labcode,
patient.patdate,
min(if(year(tps.collected)=0,patient.patdate, tps.collected)) as 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,
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);
".$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);
@@ -241,7 +271,7 @@ class DataRetrievalService
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
'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
@@ -264,7 +294,7 @@ class DataRetrievalService
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
'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
@@ -287,19 +317,19 @@ class DataRetrievalService
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))."' ":"";
$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_lab_result.results_date as patdate,
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_lab_result.results_date, 'Y') as year_data,
niphc0_nphl.get_epi_period(afi_lab_result.results_date, 'W') as week_data,
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,
@@ -309,7 +339,8 @@ class DataRetrievalService
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);
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);
@@ -321,13 +352,14 @@ class DataRetrievalService
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
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
union
@@ -337,13 +369,15 @@ class DataRetrievalService
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
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null
".$cond."
union
@@ -351,15 +385,16 @@ class DataRetrievalService
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,
if(afi_lab_result.dengue_pcr='Positive', 'Dengue Virus','') pathogen_name,
'' as subtype,
'AFI Dengue PCR Test' as indicator
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
union
@@ -367,15 +402,16 @@ class DataRetrievalService
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,
if(afi_lab_result.chikungunya_pcr='Positive', 'Chikungunya Virus','') pathogen_name,
'' as subtype,
'AFI Chikungunya PCR Test' as indicator
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
union
@@ -383,15 +419,16 @@ class DataRetrievalService
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,
if(afi_lab_result.leptospira_pcr='Positive', 'Leptospira spp.','') pathogen_name,
'' as subtype,
'AFI Leptospira PCR Test' as indicator
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
union
@@ -399,15 +436,16 @@ class DataRetrievalService
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,
if(afi_lab_result.plasmodium_pcr='Positive', 'Plasmodium spp.','') pathogen_name,
'' as subtype,
'AFI Plasmodium PCR Test' as indicator
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
UNION
@@ -415,15 +453,16 @@ class DataRetrievalService
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,
if(afi_lab_result.rickettsia_pcr='Positive', 'Rickettsia spp.','') pathogen_name,
'' as subtype,
'AFI Rickettsia PCR Test' as indicator
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
UNION
@@ -431,15 +470,16 @@ class DataRetrievalService
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,
if(afi_lab_result.salmonella_pcr='Positive', 'Salmonella spp.','') pathogen_name,
'' as subtype,
'AFI Salmonella PCR Test' as indicator
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
UNION
@@ -447,15 +487,16 @@ class DataRetrievalService
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,
if(afi_lab_result.westnile_pcr='Positive', 'West Nile Virus','') pathogen_name,
'' as subtype,
'AFI Westnile PCR Test' as indicator
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
UNION
@@ -463,15 +504,16 @@ class DataRetrievalService
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,
if(afi_lab_result.zika_pcr='Positive', 'ZIKA Virus','') pathogen_name,
'' as subtype,
'AFI ZIKA PCR Test' as indicator
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
UNION
@@ -481,15 +523,16 @@ class DataRetrievalService
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,
if(afi_lab_result.chikungunya_serum='Positive', 'Chikungunya Virus','') pathogen_name,
'' as subtype,
'AFI Chikungunya Serum Test' as indicator
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
UNION
@@ -497,15 +540,16 @@ class DataRetrievalService
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,
if(afi_lab_result.dengue_serum='Positive', 'Dengue Virus','') pathogen_name,
'' as subtype,
'AFI Dengue Serum Test' as indicator
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
UNION
@@ -513,15 +557,16 @@ class DataRetrievalService
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,
if(afi_lab_result.westnile_serum='Positive', 'West Nile Virus','') pathogen_name,
'' as subtype,
'AFI Westnile Serum Test' as indicator
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
UNION
@@ -529,15 +574,16 @@ class DataRetrievalService
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,
if(afi_lab_result.zika_serum='Positive', 'ZIKA Virus','') pathogen_name,
'' as subtype,
'AFI ZIKA Serum Test' as indicator
'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_lab_result.results_date is not null ".$cond."
and afi_case.date_of_interview is not null
and afi_lab_result.sample_code is not null ".$cond."
UNION
@@ -545,15 +591,16 @@ class DataRetrievalService
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,
if(afi_lab_result.je_virus='Positive', 'Japanese Encephalitis Virus','') pathogen_name,
'' as subtype,
'AFI Japanese Encephalitis Test' as indicator
'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_lab_result.results_date is not null ".$cond
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);
@@ -561,62 +608,179 @@ class DataRetrievalService
public function getNSDCases($dateFrom, $dateTo = NULL)
{
return [];
$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 patient.patdate >= '".date('Y-m-d', strtotime($dateFrom))."' ";
$cond .= (!empty($dateTo)) ? " and patient.patdate <='".date('Y-m-d', strtotime($dateTo))."' ":"";
$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
patient.labcode,
patient.patdate,
patient.isnewcase,
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,
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,
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 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.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 = patient.frompro
where patient.status = 1
and tpst.labtestid IN ( 659, 662, 666, 667, 668 )
" . $cond);
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 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);
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);