2022-01-10 00:00:14 +08:00
|
|
|
|
import logging
|
2021-12-23 16:53:53 +08:00
|
|
|
|
from datetime import datetime
|
2021-05-30 22:32:13 +08:00
|
|
|
|
from typing import Optional
|
2021-12-23 16:53:53 +08:00
|
|
|
|
|
2024-01-27 01:08:02 +08:00
|
|
|
|
from felicity.apps.analysis import utils
|
2024-06-02 19:02:39 +08:00
|
|
|
|
from felicity.apps.analysis.models.analysis import (Analysis, analysis_profile, AnalysisCategory,
|
2024-02-16 23:48:19 +08:00
|
|
|
|
CodingStandard, Profile,
|
|
|
|
|
RejectionReason,
|
|
|
|
|
SampleType)
|
2024-01-27 01:08:02 +08:00
|
|
|
|
from felicity.apps.analysis.models.qc import QCLevel
|
2024-02-16 23:48:19 +08:00
|
|
|
|
from felicity.apps.analysis.schemas import (AnalysisCategoryCreate,
|
|
|
|
|
AnalysisCreate, ProfileCreate,
|
|
|
|
|
QCLevelCreate,
|
|
|
|
|
RejectionReasonCreate,
|
|
|
|
|
SampleTypeCreate)
|
2024-01-27 01:08:02 +08:00
|
|
|
|
from felicity.apps.common.models import IdSequence
|
2024-06-02 19:02:39 +08:00
|
|
|
|
from felicity.apps.setup.models.setup import Department
|
|
|
|
|
from felicity.apps.setup.models.setup import Unit
|
|
|
|
|
from felicity.apps.setup.schemas import UnitCreate
|
2024-01-27 01:08:02 +08:00
|
|
|
|
from felicity.core.config import get_settings
|
2024-01-28 21:17:16 +08:00
|
|
|
|
from .data import get_seeds
|
2021-12-23 16:53:53 +08:00
|
|
|
|
|
2024-01-26 01:52:03 +08:00
|
|
|
|
settings = get_settings()
|
2021-12-23 16:53:53 +08:00
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2021-05-30 22:32:13 +08:00
|
|
|
|
|
2024-06-02 19:02:39 +08:00
|
|
|
|
units = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def unit_resolver(name: str, description=""):
|
|
|
|
|
if not name or name is None:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
if name in units:
|
|
|
|
|
return units[name]
|
|
|
|
|
|
|
|
|
|
unit = await Unit.get(name=name)
|
|
|
|
|
if not unit:
|
|
|
|
|
unit_in = UnitCreate(name=name, descritpion=description)
|
|
|
|
|
unit = await Unit.create(unit_in)
|
|
|
|
|
units[name] = unit
|
|
|
|
|
return unit
|
|
|
|
|
|
2021-05-30 22:32:13 +08:00
|
|
|
|
|
2024-01-28 21:17:16 +08:00
|
|
|
|
async def seed_categories():
|
2023-10-02 11:38:21 +08:00
|
|
|
|
logger.info("Setting up analyses categories .....")
|
2024-01-28 21:17:16 +08:00
|
|
|
|
data = get_seeds("analyses")
|
2021-12-23 16:53:53 +08:00
|
|
|
|
|
2021-11-08 01:01:12 +08:00
|
|
|
|
categories = data.get("categories", [])
|
|
|
|
|
|
|
|
|
|
for _cat in categories:
|
|
|
|
|
category = await AnalysisCategory.get(name=_cat)
|
|
|
|
|
if not category:
|
2022-01-10 00:00:14 +08:00
|
|
|
|
cat_in = AnalysisCategoryCreate(name=_cat, description=_cat)
|
2021-11-08 01:01:12 +08:00
|
|
|
|
await AnalysisCategory.create(cat_in)
|
2021-05-30 22:32:13 +08:00
|
|
|
|
|
|
|
|
|
|
2024-01-28 21:17:16 +08:00
|
|
|
|
async def seed_qc_levels() -> None:
|
2023-10-28 17:34:50 +08:00
|
|
|
|
logger.info("Setting QC Levels .....")
|
2024-01-28 21:17:16 +08:00
|
|
|
|
data = get_seeds("analyses")
|
2021-12-23 16:53:53 +08:00
|
|
|
|
|
2021-11-08 01:01:12 +08:00
|
|
|
|
qc_levels = data.get("qc_levels", [])
|
|
|
|
|
|
|
|
|
|
for _lvl in qc_levels:
|
|
|
|
|
qc_level = await QCLevel.get(level=_lvl)
|
2021-06-10 04:56:54 +08:00
|
|
|
|
if not qc_level:
|
2021-11-08 01:01:12 +08:00
|
|
|
|
lvl_in = QCLevelCreate(level=_lvl)
|
|
|
|
|
await QCLevel.create(lvl_in)
|
2021-10-01 15:37:34 +08:00
|
|
|
|
|
|
|
|
|
|
2024-01-28 21:17:16 +08:00
|
|
|
|
async def seed_id_sequence() -> None:
|
2023-10-28 17:34:50 +08:00
|
|
|
|
logger.info("Setting up id sequence .....")
|
2024-01-28 21:17:16 +08:00
|
|
|
|
data = get_seeds("analyses")
|
2021-12-23 16:53:53 +08:00
|
|
|
|
|
|
|
|
|
sample_types = data.get("sample_types", [])
|
|
|
|
|
|
|
|
|
|
prefix_keys = ["WS", "P", "AR"]
|
|
|
|
|
|
|
|
|
|
for _st in sample_types:
|
|
|
|
|
st_abbr = _st.get("abbr")
|
|
|
|
|
if st_abbr not in prefix_keys:
|
|
|
|
|
prefix_keys.append(st_abbr)
|
|
|
|
|
|
|
|
|
|
prefix_year = str(datetime.now().year)[2:]
|
|
|
|
|
id_prefixes = [f"{prefix_key}{prefix_year}" for prefix_key in prefix_keys]
|
|
|
|
|
|
|
|
|
|
for prefix in id_prefixes:
|
|
|
|
|
id_seq = await IdSequence.get(prefix=prefix)
|
|
|
|
|
if id_seq is None:
|
|
|
|
|
await IdSequence.create(**{"prefix": prefix, "number": 0})
|
|
|
|
|
|
|
|
|
|
|
2024-01-28 21:17:16 +08:00
|
|
|
|
async def seed_coding_standards() -> None:
|
2023-12-20 02:01:46 +08:00
|
|
|
|
# Coding for clinical laboratory information
|
|
|
|
|
logger.info("Setting up coding standard .....")
|
|
|
|
|
|
|
|
|
|
standards = [
|
2023-12-30 15:51:11 +08:00
|
|
|
|
{
|
|
|
|
|
"name": "LOINC",
|
|
|
|
|
"description": "Logical Observation Identifiers Names and Codes",
|
|
|
|
|
},
|
2023-12-20 02:01:46 +08:00
|
|
|
|
# {"name": "SNOMED CT", "description": "Systemized Nomenclature of Medicine – Clinical Terms"}
|
|
|
|
|
]
|
|
|
|
|
for stand in standards:
|
|
|
|
|
standard = await CodingStandard.get(name=stand.get("name"))
|
|
|
|
|
if standard is None:
|
|
|
|
|
await CodingStandard.create(stand)
|
|
|
|
|
|
|
|
|
|
|
2024-01-28 21:17:16 +08:00
|
|
|
|
async def seed_sample_types() -> None:
|
2023-10-28 17:34:50 +08:00
|
|
|
|
logger.info("Setting up sample types .....")
|
2024-01-28 21:17:16 +08:00
|
|
|
|
data = get_seeds("analyses")
|
2021-12-23 16:53:53 +08:00
|
|
|
|
|
2021-11-08 01:01:12 +08:00
|
|
|
|
sample_types = data.get("sample_types", [])
|
|
|
|
|
|
|
|
|
|
for _st in sample_types:
|
|
|
|
|
st_name = _st.get("name")
|
|
|
|
|
st_description = _st.get("description")
|
|
|
|
|
st_abbr = _st.get("abbr")
|
|
|
|
|
st_active = _st.get("active", 0)
|
|
|
|
|
st = await SampleType.get(name=st_name, abbr=st_abbr)
|
2021-10-01 15:37:34 +08:00
|
|
|
|
if not st:
|
2021-11-08 01:01:12 +08:00
|
|
|
|
st_in = SampleTypeCreate(
|
|
|
|
|
name=st_name,
|
|
|
|
|
description=st_description,
|
|
|
|
|
abbr=st_abbr.upper(),
|
2022-01-10 00:00:14 +08:00
|
|
|
|
active=bool(st_active),
|
2021-11-08 01:01:12 +08:00
|
|
|
|
)
|
2021-10-01 15:37:34 +08:00
|
|
|
|
await SampleType.create(st_in)
|
|
|
|
|
|
|
|
|
|
|
2024-01-28 21:17:16 +08:00
|
|
|
|
async def seed_analyses_services_and_profiles() -> None:
|
2023-10-28 17:34:50 +08:00
|
|
|
|
logger.info("Setting up analysis services and profiles .....")
|
2024-01-28 21:17:16 +08:00
|
|
|
|
data = get_seeds("analyses")
|
2021-11-08 01:01:12 +08:00
|
|
|
|
|
|
|
|
|
analyses = data.get("analyses", [])
|
|
|
|
|
|
|
|
|
|
for _anal in analyses:
|
2022-01-10 00:00:14 +08:00
|
|
|
|
analyte: Optional[Analysis] = await Analysis.get(name=_anal.get("name"))
|
2021-11-08 01:01:12 +08:00
|
|
|
|
if not analyte:
|
2024-06-02 19:02:39 +08:00
|
|
|
|
category = await AnalysisCategory.get(name=_anal.get("category"))
|
|
|
|
|
unit = await unit_resolver(name=_anal.get("ucuum"))
|
2021-11-08 01:01:12 +08:00
|
|
|
|
an_in = AnalysisCreate(
|
2022-01-10 00:00:14 +08:00
|
|
|
|
name=_anal.get("name"),
|
|
|
|
|
description=_anal.get("description"),
|
2024-06-01 21:07:35 +08:00
|
|
|
|
category_uid=category.uid if category else None,
|
2024-06-02 19:02:39 +08:00
|
|
|
|
unit_uid=unit.uid if unit else None,
|
2022-01-10 00:00:14 +08:00
|
|
|
|
keyword=_anal.get("keyword"),
|
|
|
|
|
sort_key=_anal.get("sort_key"),
|
|
|
|
|
active=bool(_anal.get("active")),
|
2021-11-08 01:01:12 +08:00
|
|
|
|
)
|
2023-12-04 18:04:30 +08:00
|
|
|
|
analyte = await Analysis.create(an_in)
|
|
|
|
|
await utils.billing_setup_analysis([analyte.uid])
|
2021-10-01 15:37:34 +08:00
|
|
|
|
|
2021-11-08 01:01:12 +08:00
|
|
|
|
profiles = data.get("analyses_profiles", [])
|
|
|
|
|
|
|
|
|
|
for _prf in profiles:
|
|
|
|
|
a_profile: Optional[Profile] = await Profile.get(name=_prf.get("name"))
|
2021-10-01 15:37:34 +08:00
|
|
|
|
if not a_profile:
|
2024-06-02 19:02:39 +08:00
|
|
|
|
department = await Department.get(name=_prf.get("department"))
|
2021-11-08 01:01:12 +08:00
|
|
|
|
prof_in = ProfileCreate(
|
2024-06-01 21:07:35 +08:00
|
|
|
|
name=_prf.get("name"),
|
|
|
|
|
description=_prf.get("description"),
|
2024-06-02 19:02:39 +08:00
|
|
|
|
department_uid=department.uid if department else None
|
2021-11-08 01:01:12 +08:00
|
|
|
|
)
|
2021-10-01 15:37:34 +08:00
|
|
|
|
a_profile = await Profile.create(prof_in)
|
|
|
|
|
|
2021-11-08 01:01:12 +08:00
|
|
|
|
analyses_names = _prf.get("analyses_names", [])
|
|
|
|
|
for _a_name in analyses_names:
|
2024-06-02 19:02:39 +08:00
|
|
|
|
anal = await Analysis.get(name=_a_name)
|
|
|
|
|
if anal and anal.uid not in [a.uid for a in a_profile.analyses]:
|
|
|
|
|
a_profile.analyses.append(anal)
|
|
|
|
|
await Profile.table_insert(
|
|
|
|
|
analysis_profile,
|
|
|
|
|
mappings={
|
|
|
|
|
"analysis_uid": anal.uid,
|
|
|
|
|
"profile_uid": a_profile.uid
|
|
|
|
|
}
|
|
|
|
|
)
|
2023-12-20 02:01:46 +08:00
|
|
|
|
|
2023-12-04 18:04:30 +08:00
|
|
|
|
await utils.billing_setup_profiles([a_profile.uid])
|
2022-12-28 05:29:14 +08:00
|
|
|
|
|
|
|
|
|
|
2024-01-28 21:17:16 +08:00
|
|
|
|
async def seed_rejection_reasons() -> None:
|
2023-10-28 17:34:50 +08:00
|
|
|
|
logger.info("Setting up rejection reasons .....")
|
2024-01-28 21:17:16 +08:00
|
|
|
|
data = get_seeds("analyses")
|
2022-12-28 05:29:14 +08:00
|
|
|
|
|
|
|
|
|
rejection_reasons = data.get("rejection_reasons", [])
|
|
|
|
|
|
|
|
|
|
for _rr in rejection_reasons:
|
|
|
|
|
reason = await RejectionReason.get(reason=_rr)
|
|
|
|
|
if not reason:
|
|
|
|
|
rr_in = RejectionReasonCreate(reason=_rr)
|
|
|
|
|
await RejectionReason.create(rr_in)
|