This commit is contained in:
2026-06-22 08:47:13 +07:00
parent a6f8551ab8
commit f518d7d184
7 changed files with 346 additions and 263 deletions

View File

@@ -96,21 +96,21 @@ class DashboardService
$q->whereRaw(
"(year_data > ?
OR (
year_data = ?
AND week_data >= ?
)
)",
[$startYear, $startYear, $startWeek]
)
OR (
year_data = ?
AND week_data >= ?
)
)",
[$startYear, $startYear, $startWeek]
)
->whereRaw(
"(year_data < ?
OR (
year_data = ?
AND week_data <= ?
)
)",
OR (
year_data = ?
AND week_data <= ?
)
)",
[$endYear, $endYear, $endWeek]
);
})
@@ -121,38 +121,38 @@ class DashboardService
->selectRaw("
surveillance_cases.year_data as year,
surveillance_cases.week_data as period,
surveillance_cases.year_data as year,
surveillance_cases.week_data as period,
case_lab_results.pathogen_name as pathogen,
case_lab_results.subtype,
case_lab_results.pathogen_name as pathogen,
case_lab_results.subtype,
CASE
CASE
WHEN LOWER(case_lab_results.pathogen_name)
LIKE '%influenza%'
WHEN LOWER(case_lab_results.pathogen_name)
LIKE '%influenza%'
OR LOWER(case_lab_results.pathogen_name)
LIKE '%covid%'
OR LOWER(case_lab_results.pathogen_name)
LIKE '%covid%'
OR LOWER(case_lab_results.pathogen_name)
LIKE '%sars-cov%'
OR LOWER(case_lab_results.pathogen_name)
LIKE '%sars-cov%'
THEN 'section_1'
THEN 'section_1'
WHEN LOWER(case_lab_results.indicator)
LIKE '%serum%'
WHEN LOWER(case_lab_results.indicator)
LIKE '%serum%'
THEN 'section_3'
THEN 'section_3'
ELSE 'section_2'
ELSE 'section_2'
END as afi_section,
END as afi_section,
COUNT(DISTINCT surveillance_cases.lab_code)
as total_positive
COUNT(DISTINCT surveillance_cases.lab_code)
as total_positive
")
")
->groupBy(
'surveillance_cases.year_data',
@@ -184,6 +184,7 @@ class DashboardService
];
}
public function afiCaseTrend(
$surveillanceId,
$startYear,
@@ -194,14 +195,42 @@ class DashboardService
/*
|--------------------------------------------------------------------------
| TOTAL CASES BY SECTION
| AFI SECTION SQL
|--------------------------------------------------------------------------
*/
$afiSectionSql = "
CASE
WHEN LOWER(case_lab_results.indicator)
LIKE '%influenza%'
OR LOWER(case_lab_results.indicator)
LIKE '%covid%'
THEN 'section_1'
WHEN LOWER(case_lab_results.indicator)
LIKE '%serum%'
THEN 'section_3'
ELSE 'section_2'
END
";
/*
|--------------------------------------------------------------------------
| ALL TESTED CASES
|--------------------------------------------------------------------------
|
| Count DISTINCT lab_code PER SECTION
|
| section_1 = Influenza / Covid
| section_2 = PCR
| section_3 = Serum
| THIS MUST INCLUDE:
| - positive
| - negative
| - weeks with no positives
|
*/
@@ -226,7 +255,11 @@ class DashboardService
AND week_data >= ?
)
)",
[$startYear, $startYear, $startWeek]
[
$startYear,
$startYear,
$startWeek
]
)
->whereRaw(
@@ -236,38 +269,27 @@ class DashboardService
AND week_data <= ?
)
)",
[$endYear, $endYear, $endWeek]
[
$endYear,
$endYear,
$endWeek
]
);
})
->selectRaw("
surveillance_cases.year_data as year,
surveillance_cases.week_data as period,
surveillance_cases.year_data as year,
surveillance_cases.week_data as period,
CASE
{$afiSectionSql}
as afi_section,
WHEN LOWER(case_lab_results.indicator)
LIKE '%influenza%'
COUNT(DISTINCT case_lab_results.lab_code)
as total_cases
OR LOWER(case_lab_results.indicator)
LIKE '%covid%'
THEN 'section_1'
WHEN LOWER(case_lab_results.indicator)
LIKE '%serum%'
THEN 'section_3'
ELSE 'section_2'
END as afi_section,
COUNT(DISTINCT case_lab_results.lab_code)
as total_cases
")
")
->groupBy(
'surveillance_cases.year_data',
@@ -275,16 +297,29 @@ class DashboardService
'afi_section'
)
->get()
->orderBy('surveillance_cases.year_data')
->keyBy(
fn($r) =>
$r->year .
'-' .
$r->period .
'-' .
$r->afi_section
);
->orderBy('surveillance_cases.week_data')
->get();
/*
|--------------------------------------------------------------------------
| KEYED TOTALS
|--------------------------------------------------------------------------
*/
$totalCasesKeyed = $totalCases->keyBy(
fn($r) =>
$r->year .
'-' .
$r->period .
'-' .
$r->afi_section
);
/*
|--------------------------------------------------------------------------
@@ -313,7 +348,11 @@ class DashboardService
AND week_data >= ?
)
)",
[$startYear, $startYear, $startWeek]
[
$startYear,
$startYear,
$startWeek
]
)
->whereRaw(
@@ -323,54 +362,52 @@ class DashboardService
AND week_data <= ?
)
)",
[$endYear, $endYear, $endWeek]
[
$endYear,
$endYear,
$endWeek
]
);
})
->where('case_lab_results.is_positive', 1)
->whereNotNull('case_lab_results.pathogen_name')
->whereNotNull(
'case_lab_results.pathogen_name'
)
->where(
'case_lab_results.pathogen_name',
'!=',
''
)
->selectRaw("
surveillance_cases.year_data as year,
surveillance_cases.week_data as period,
surveillance_cases.year_data as year,
surveillance_cases.week_data as period,
CASE
CASE
WHEN LOWER(case_lab_results.pathogen_name)
LIKE '%influenza%'
THEN 'Influenza'
WHEN LOWER(case_lab_results.pathogen_name)
LIKE '%influenza%'
ELSE case_lab_results.pathogen_name
THEN 'Influenza'
END as pathogen,
ELSE case_lab_results.pathogen_name
case_lab_results.subtype,
END as pathogen,
CASE
case_lab_results.subtype,
WHEN LOWER(case_lab_results.indicator)
LIKE '%influenza%'
{$afiSectionSql}
as afi_section,
OR LOWER(case_lab_results.indicator)
LIKE '%covid%'
COUNT(DISTINCT case_lab_results.lab_code)
as total_positive
THEN 'section_1'
WHEN LOWER(case_lab_results.indicator)
LIKE '%serum%'
THEN 'section_3'
ELSE 'section_2'
END as afi_section,
COUNT(DISTINCT case_lab_results.lab_code)
as total_positive
")
")
->groupBy(
'surveillance_cases.year_data',
@@ -386,9 +423,10 @@ class DashboardService
->get()
->map(function ($r) use ($totalCases) {
->map(function ($r) use ($totalCasesKeyed) {
$key =
$r->year .
'-' .
$r->period .
@@ -396,36 +434,92 @@ class DashboardService
$r->afi_section;
$r->total_cases =
$totalCases[$key]->total_cases ?? 0;
$totalCasesKeyed[$key]
->total_cases ?? 0;
$r->positivity_rate =
$r->total_cases > 0
? round(
($r->total_positive / $r->total_cases) * 100,
(
$r->total_positive
/ $r->total_cases
) * 100,
1
)
: 0;
return $r;
});
/*
|--------------------------------------------------------------------------
| RETURN
|--------------------------------------------------------------------------
*/
return [
'section_1' => $rows
->where('afi_section', 'section_1')
->values(),
'section_1' => [
'section_2' => $rows
->where('afi_section', 'section_2')
->values(),
'rows' => $rows
->where(
'afi_section',
'section_1'
)
->values(),
'section_3' => $rows
->where('afi_section', 'section_3')
->values()
'totals' => $totalCases
->where(
'afi_section',
'section_1'
)
->values()
],
'section_2' => [
'rows' => $rows
->where(
'afi_section',
'section_2'
)
->values(),
'totals' => $totalCases
->where(
'afi_section',
'section_2'
)
->values()
],
'section_3' => [
'rows' => $rows
->where(
'afi_section',
'section_3'
)
->values(),
'totals' => $totalCases
->where(
'afi_section',
'section_3'
)
->values()
]
];
}
public function programSummaryFast($surveillanceId, $year = null, $week = null, $dateFrom = null, $dateTo = null)
{
@@ -445,29 +539,29 @@ class DashboardService
}
$row = $query->selectRaw("
COUNT(DISTINCT surveillance_cases.lab_code) as total_cases,
COUNT(DISTINCT surveillance_cases.lab_code) as total_cases,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
THEN surveillance_cases.lab_code
END) as overall_positive,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
THEN surveillance_cases.lab_code
END) as overall_positive,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
AND (
LOWER(case_lab_results.pathogen_name) LIKE '%influenza%'
)
THEN surveillance_cases.lab_code
END) as influenza_positive,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
AND (
LOWER(case_lab_results.pathogen_name) LIKE '%influenza%'
)
THEN surveillance_cases.lab_code
END) as influenza_positive,
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
AND (
LOWER(case_lab_results.pathogen_name) LIKE '%covid%'
)
THEN surveillance_cases.lab_code
END) as covid_positive
")->first();
COUNT(DISTINCT CASE
WHEN case_lab_results.is_positive = 1
AND (
LOWER(case_lab_results.pathogen_name) LIKE '%covid%'
)
THEN surveillance_cases.lab_code
END) as covid_positive
")->first();
if (!$row || $row->total_cases == 0) {
return [