|
2 | 2 | from datetime import UTC, datetime |
3 | 3 | from io import StringIO |
4 | 4 | from logging import Logger |
| 5 | +from typing import Literal |
5 | 6 | from uuid import UUID |
6 | 7 |
|
7 | 8 | from fastapi import APIRouter, Depends, HTTPException, Response, status |
| 9 | +from fastapi.responses import StreamingResponse |
8 | 10 |
|
9 | 11 | from app.api.auth.middleware import get_logged_in_user |
10 | 12 | from app.db.conditions.db import ( |
|
13 | 15 | ) |
14 | 16 | from app.db.conditions.model import DbCondition |
15 | 17 | from app.db.configurations.db import get_configuration_by_id_db |
16 | | -from app.db.configurations.model import DbConfiguration |
| 18 | +from app.db.configurations.model import ( |
| 19 | + DbConfiguration, |
| 20 | + DbConfigurationSectionProcessing, |
| 21 | + DbNarrativeAction, |
| 22 | + DbSectionAction, |
| 23 | +) |
17 | 24 | from app.db.pool import AsyncDatabaseConnection, get_db |
18 | 25 | from app.db.users.model import DbUser |
19 | 26 | from app.services.code_systems import get_all_code_systems_by_key |
| 27 | +from app.services.ecr.policy import NARRATIVE_ONLY_SECTIONS |
| 28 | +from app.services.file_io import ZipFileItem, ZipFilePackage |
20 | 29 | from app.services.logger import get_logger |
21 | 30 |
|
22 | 31 | router = APIRouter(prefix="/{configuration_id}/export") |
@@ -52,25 +61,122 @@ async def get_configuration_export( |
52 | 61 | included_conditions=config.included_conditions, db=db |
53 | 62 | ) |
54 | 63 |
|
55 | | - csv_bytes = await _build_config_csv( |
| 64 | + codes_csv_content = await _build_config_csv( |
56 | 65 | config=config, conditions=included_conditions, logger=logger, db=db |
57 | 66 | ) |
| 67 | + sections_csv_content = _build_sections_csv(sections=config.section_processing) |
58 | 68 |
|
59 | | - filename = _build_export_filename(config_name=config.name) |
| 69 | + timestamp = _get_timestamp() |
60 | 70 |
|
61 | | - return Response( |
62 | | - content=csv_bytes, |
63 | | - media_type="text/csv; charset=utf-8", |
64 | | - headers={"Content-Disposition": f'attachment; filename="{filename}"'}, |
| 71 | + zip_package = ZipFilePackage( |
| 72 | + name=_build_export_filename( |
| 73 | + filename="Configuration_Export", |
| 74 | + extension="zip", |
| 75 | + config_name=config.name, |
| 76 | + config_version=config.version, |
| 77 | + timestamp=timestamp, |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + zip_package.add( |
| 82 | + ZipFileItem( |
| 83 | + file_name=_build_export_filename( |
| 84 | + filename="Code_Export", |
| 85 | + extension="csv", |
| 86 | + config_name=config.name, |
| 87 | + config_version=config.version, |
| 88 | + timestamp=timestamp, |
| 89 | + ), |
| 90 | + file_content=codes_csv_content, |
| 91 | + ) |
65 | 92 | ) |
66 | 93 |
|
| 94 | + zip_package.add( |
| 95 | + ZipFileItem( |
| 96 | + file_name=_build_export_filename( |
| 97 | + filename="Section_Export", |
| 98 | + extension="csv", |
| 99 | + config_name=config.name, |
| 100 | + config_version=config.version, |
| 101 | + timestamp=timestamp, |
| 102 | + ), |
| 103 | + file_content=sections_csv_content, |
| 104 | + ) |
| 105 | + ) |
| 106 | + |
| 107 | + return StreamingResponse( |
| 108 | + content=zip_package.iter_chunks(), |
| 109 | + media_type="application/zip", |
| 110 | + headers={ |
| 111 | + "Content-Disposition": f'attachment; filename="{zip_package.get_name()}"' |
| 112 | + }, |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +def _build_sections_csv( |
| 117 | + sections: list[DbConfigurationSectionProcessing], |
| 118 | +) -> str: |
| 119 | + """Build a CSV summarizing configuration section information.""" |
| 120 | + with StringIO() as csv_text: |
| 121 | + writer = csv.writer(csv_text) |
| 122 | + writer.writerow( |
| 123 | + ["Section Name", "LOINC", "Include", "Coded Data", "Narrative Data"] |
| 124 | + ) |
| 125 | + |
| 126 | + for section in sorted(sections, key=lambda r: r.name.lower()): |
| 127 | + writer.writerow( |
| 128 | + [ |
| 129 | + section.name, |
| 130 | + section.code, |
| 131 | + "Yes" if section.include else "No", |
| 132 | + _get_coded_data_value( |
| 133 | + loinc=section.code, |
| 134 | + action=section.action, |
| 135 | + included=section.include, |
| 136 | + ), |
| 137 | + _get_narrative_data_value( |
| 138 | + narrative=section.narrative, included=section.include |
| 139 | + ), |
| 140 | + ] |
| 141 | + ) |
| 142 | + return csv_text.getvalue() |
| 143 | + |
| 144 | + |
| 145 | +def _get_coded_data_value(loinc: str, action: DbSectionAction, included: bool) -> str: |
| 146 | + if not included: |
| 147 | + return "N/A" |
| 148 | + |
| 149 | + if loinc in NARRATIVE_ONLY_SECTIONS: |
| 150 | + return "N/A" |
| 151 | + |
| 152 | + if action == "retain": |
| 153 | + return "Keep original" |
| 154 | + |
| 155 | + if action == "refine": |
| 156 | + return "Refine" |
| 157 | + |
| 158 | + return "N/A" |
| 159 | + |
| 160 | + |
| 161 | +def _get_narrative_data_value(narrative: DbNarrativeAction, included: bool) -> str: |
| 162 | + if not included: |
| 163 | + return "N/A" |
| 164 | + |
| 165 | + if narrative == "retain": |
| 166 | + return "Keep original" |
| 167 | + |
| 168 | + if narrative == "remove": |
| 169 | + return "Exclude" |
| 170 | + |
| 171 | + return "Reconstruct" |
| 172 | + |
67 | 173 |
|
68 | 174 | async def _build_config_csv( |
69 | 175 | config: DbConfiguration, |
70 | 176 | conditions: list[DbCondition], |
71 | 177 | logger: Logger, |
72 | 178 | db: AsyncDatabaseConnection, |
73 | | -) -> bytes: |
| 179 | +) -> str: |
74 | 180 | """Build the CSV export content for a configuration.""" |
75 | 181 |
|
76 | 182 | code_systems = await get_all_code_systems_by_key(db=db) |
@@ -113,11 +219,22 @@ async def _build_config_csv( |
113 | 219 | ] |
114 | 220 | ) |
115 | 221 |
|
116 | | - return csv_text.getvalue().encode("utf-8") |
| 222 | + return csv_text.getvalue() |
| 223 | + |
| 224 | + |
| 225 | +def _get_timestamp() -> str: |
| 226 | + now = datetime.now(UTC) |
| 227 | + timestamp = now.strftime("%m%d%y_%H_%M_%S") |
| 228 | + return timestamp |
117 | 229 |
|
118 | 230 |
|
119 | | -def _build_export_filename(config_name: str) -> str: |
| 231 | +def _build_export_filename( |
| 232 | + filename: str, |
| 233 | + extension: Literal["csv", "zip"], |
| 234 | + config_name: str, |
| 235 | + config_version: int, |
| 236 | + timestamp: str, |
| 237 | +) -> str: |
120 | 238 | """Build a timestamped filename for a configuration export.""" |
121 | | - safe_name = config_name.replace(" ", "_") |
122 | | - timestamp = datetime.now(UTC).strftime("%m%d%y_%H_%M_%S") |
123 | | - return f"{safe_name}_Code_Export_{timestamp}.csv" |
| 239 | + condition_grouper = config_name.replace(" ", "_") |
| 240 | + return f"{condition_grouper}_v{config_version}_{filename}_{timestamp}.{extension}" |
0 commit comments