Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ databaseChangeLog:
- sqlFile:
path: db/report/execution/libraries/nbs_sr_02.sql
splitStatements: false
- sqlFile:
path: db/report/execution/libraries/nbs_sr_07.sql
splitStatements: false
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Migrate the NBSCUSTOM.SAS library to the nbs_sr_05 python library
-- Migrate the NBSSR00005.SAS library to the nbs_sr_05 python library

USE [NBS_ODSE]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- Migrate the NBSSR00007.SAS library to the nbs_sr_07 python library

USE [NBS_ODSE]

DECLARE @pyLib VARCHAR(50) = 'nbs_sr_07'
DECLARE @sasLib VARCHAR(50) = 'NBSSR00007.SAS'
DECLARE @desc VARCHAR(300) = 'SR7: Cases of Selected Diseases vs. 5-Year Median for Selected Time Period. Report demonstrates, in table form, Investigation(s) [both Individual and Summary] by year-to-date, and 5-year median irrespective of Case Status'

IF EXISTS (SELECT * FROM [dbo].[Report_Library] WHERE UPPER(library_name) = @sasLib)
BEGIN
UPDATE [dbo].[Report_Library]
SET
library_name = @pyLib,
runner = 'python',
desc_txt = @desc,
last_chg_time = CURRENT_TIMESTAMP,
last_chg_user_id = 99999999
WHERE
UPPER(library_name) = @sasLib;
END
ELSE
BEGIN
-- Create a row for this library
INSERT INTO [dbo].[Report_Library] (
library_name,
desc_txt,
runner,
is_builtin_ind,
add_time,
add_user_id,
last_chg_time,
last_chg_user_id
) VALUES (
@pyLib,
@desc,
'python',
'Y',
CURRENT_TIMESTAMP,
99999999,
CURRENT_TIMESTAMP,
99999999
);
END
73 changes: 73 additions & 0 deletions apps/report-execution/src/libraries/nbs_sr_07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from src.db_transaction import Transaction
from src.libraries.nbs_sr_05 import execute as execute_nbs_sr_05
from src.models import ReportResult, Table


def execute(
trx: Transaction,
subset_query: str,
data_source_name: str,
**kwargs,
):
"""Standard Report 07: Cases of Selected Diseases vs. 5-Year Median for a
specific state.

Each row has columns for the:
* Disease
* type (e.g. Five Year Median YTD or Current YTD)
* Number of Cases

Conversion notes:
* Matched "export format"
* Remove references to "Bar Graph" since export is a table
* Use results of nbs_sr_05.py for the following:
* - subheader
* - content (data is modified to fit expected table format of nbs_sr_07.py)
"""
nbs_sr_05_report_result = execute_nbs_sr_05(
trx, subset_query, data_source_name, **kwargs
)
nbs_sr_05_report_result_rows = nbs_sr_05_report_result.content.data

modified_table = Table(columns=['Disease', 'type', 'Number of Cases'], data=[])

for row in nbs_sr_05_report_result_rows:
rows_to_add = [
(row[3], 'Five Year Median YTD', row[6]),
(row[3], 'Current YTD', row[2]),
]
modified_table.data.extend(rows_to_add)

header = (
'SR7: Cases of Selected Diseases vs. 5-Year Median for Selected Time Period'
)
description = (
'<u>Report content</u>\n'
'Data Source: nbs_ods.PHCDemographic (publichealthcasefact)\n'
'Output: Report demonstrates, in table form, '
'Investigation(s) [both Individual and Summary] '
'by year-to-date, and 5-year median irrespective of Case Status.\n'
'Output:\n'
'1) Does not include Investigation(s) that have been logically deleted\n'
'2) Is filtered based on the state, disease(s) and advanced criteria selected '
'by user\n'
'3) Will not include Investigation(s) that do not have a value for the State '
'selected by the user\n'
'4) Is based on month and year of the calculated Event Date\n'
'Calculations:'
'1) Current Year Totals by disease: Total Investigation(s) [both Individual '
'and Summary] where the Year of the Event Date equal the current Year\n'
'2) 5-Year median: Median number of Investigation(s) [both Individual and '
'Summary] for the past five years\n'
'3) Event Date: Derived using the hierarchy of Onset Date, Diagnosis Date, '
'Report to County, Report to State and Date the Investigation was created in '
'the NBS.\n'
)

return ReportResult(
content_type='table',
content=modified_table,
header=header,
subheader=nbs_sr_05_report_result.subheader,
description=description,
)
85 changes: 85 additions & 0 deletions apps/report-execution/tests/integration/libraries/nbs_sr_07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import datetime

import pytest
import yaml

from src.execute_report import execute_report
from src.models import ReportSpec

db_table = '[NBS_ODSE].[dbo].[PublicHealthCaseFact]'
db_fk_tables = ['[NBS_ODSE].[dbo].[SubjectRaceInfo]']
faker_schema = 'phc_demographic.yaml'


@pytest.mark.usefixtures('setup_containers', 'fake_db_table')
@pytest.mark.integration
class TestIntegrationNbsSr07Library:
"""Integration tests for the nbs_sr_07 library."""

@pytest.fixture(autouse=True)
def set_time(self, time_machine):
time_machine.move_to(datetime.datetime(2024, 6, 24))

def test_execute_report_check_data(self, snapshot):
report_spec = ReportSpec.model_validate(
{
'version': 1,
'is_export': True,
'is_builtin': True,
'report_title': 'SR7',
'library_name': 'nbs_sr_07',
'data_source_name': '[NBS_ODSE].[dbo].[PHCDemographic]',
'subset_query': 'SELECT * FROM [NBS_ODSE].[dbo].[PHCDemographic]',
'time_range': {'start': '2024-01-01', 'end': '2024-12-31'},
}
)

result = execute_report(report_spec)
assert result.content_type == 'table'

data = result.content.data
assert len(data) == 6
assert len(data[0]) == len(result.content.columns)

snapshot.assert_match(yaml.dump(data), 'snapshot.yml')

for disease in ['Pertussis', 'Measles', 'Salmonellosis']:
disease_rows = list()
for row in result.content.data:
if row[0] == disease:
disease_rows.append(row)
# ensure there are 2 rows per disease
assert len(disease_rows) == 2
assert disease_rows[0][1] == 'Five Year Median YTD'
assert disease_rows[0][2] >= 100
assert disease_rows[1][1] == 'Current YTD'
assert disease_rows[1][2] >= 100

def test_execute_report_check_metadata(self):
"""Check the metadata and column names are correct with a frozen date."""
report_spec = ReportSpec.model_validate(
{
'version': 1,
'is_export': True,
'is_builtin': True,
'report_title': 'NBS Custom',
'library_name': 'nbs_sr_07',
'data_source_name': '[NBS_ODSE].[dbo].[PHCDemographic]',
'subset_query': 'SELECT * FROM [NBS_ODSE].[dbo].[PHCDemographic]',
'time_range': {'start': '2024-01-01', 'end': '2024-12-31'},
}
)

result = execute_report(report_spec)
assert (
result.header
== 'SR7: Cases of Selected Diseases vs. 5-Year Median for Selected '
'Time Period'
)
assert result.subheader == 'N/A, Georgia, Tennessee | 06/24/2024'
assert len(result.description) > 100
assert result.content_type == 'table'

assert result.content.columns[0] == 'Disease'
assert result.content.columns[1] == 'type'
assert result.content.columns[2] == 'Number of Cases'
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- !!python/tuple
- Measles
- Five Year Median YTD
- 1016.0
- !!python/tuple
- Measles
- Current YTD
- !!python/object/apply:decimal.Decimal
- '928.00000'
- !!python/tuple
- Pertussis
- Five Year Median YTD
- 834.0
- !!python/tuple
- Pertussis
- Current YTD
- !!python/object/apply:decimal.Decimal
- '1464.00000'
- !!python/tuple
- Salmonellosis
- Five Year Median YTD
- 785.0
- !!python/tuple
- Salmonellosis
- Current YTD
- !!python/object/apply:decimal.Decimal
- '1424.00000'
Loading