Beispiel #1
0
def master_record_audit(
    record_id: int,
    user: UKRDCUser = Security(auth.get_user()),
    jtrace: Session = Depends(get_jtrace),
    ukrdc3: Session = Depends(get_ukrdc3),
    auditdb: Session = Depends(get_auditdb),
    since: Optional[datetime.datetime] = None,
    until: Optional[datetime.datetime] = None,
    sorter: SQLASorter = Depends(
        make_sqla_sorter(
            [AuditEvent.id, AccessEvent.time],
            default_sort_by=AuditEvent.id,
        )
    ),
):
    """
    Retreive a page of audit events related to a particular master record.
    """
    page = paginate(
        sorter.sort(
            get_auditevents_related_to_masterrecord(
                auditdb, ukrdc3, jtrace, record_id, user, since=since, until=until
            )
        )
    )

    for item in page.items:  # type: ignore
        item.populate_identifiers(jtrace, ukrdc3)

    return page
Beispiel #2
0
def workitems_list(
        since: Optional[datetime.datetime] = None,
        until: Optional[datetime.datetime] = None,
        status: Optional[list[int]] = Query([1]),
        facility: Optional[str] = None,
        user: UKRDCUser = Security(auth.get_user()),
        jtrace: Session = Depends(get_jtrace),
        sorter: SQLASorter = Depends(
            make_sqla_sorter(
                [WorkItem.id, WorkItem.last_updated],
                default_sort_by=WorkItem.last_updated,
            )),
        audit: Auditer = Depends(get_auditer),
):
    """Retreive a list of open work items from the EMPI"""
    query = get_workitems(jtrace,
                          user,
                          statuses=status,
                          facility=facility,
                          since=since,
                          until=until)
    page = paginate(sorter.sort(query))

    for item in page.items:  # type: ignore
        audit.add_workitem(item)

    return page
Beispiel #3
0
def code_list(
        ukrdc3: Session = Depends(get_ukrdc3),
        coding_standard: Optional[list[str]] = Query(None),
        search: Optional[str] = Query(None),
):
    """Retreive a list of internal codes"""
    return paginate(get_codes(ukrdc3, coding_standard, search))
Beispiel #4
0
def error_messages(
        facility: Optional[str] = None,
        since: Optional[datetime.datetime] = None,
        until: Optional[datetime.datetime] = None,
        status: Optional[list[str]] = QueryParam(None),
        ni: Optional[list[str]] = QueryParam([]),
        user: UKRDCUser = Security(auth.get_user()),
        errorsdb: Session = Depends(get_errorsdb),
        sorter: SQLASorter = Depends(ERROR_SORTER),
        audit: Auditer = Depends(get_auditer),
):
    """
    Retreive a list of error messages, optionally filtered by NI, facility, or date.
    By default returns message created within the last 365 days.
    """
    audit.add_event(Resource.MESSAGES, None, MessageOperation.READ)
    return paginate(
        sorter.sort(
            get_messages(
                errorsdb,
                user,
                statuses=status,
                nis=ni,
                facility=facility,
                since=since,
                until=until,
            )))
Beispiel #5
0
def master_record_messages(
    record_id: int,
    facility: Optional[str] = None,
    since: Optional[datetime.datetime] = None,
    until: Optional[datetime.datetime] = None,
    status: Optional[list[str]] = QueryParam(None),
    user: UKRDCUser = Security(auth.get_user()),
    jtrace: Session = Depends(get_jtrace),
    errorsdb: Session = Depends(get_errorsdb),
    sorter: SQLASorter = Depends(ERROR_SORTER),
    audit: Auditer = Depends(get_auditer),
):
    """
    Retreive a list of errors related to a particular master record.
    By default returns message created within the last 365 days.
    """
    audit.add_event(
        Resource.MESSAGES,
        None,
        AuditOperation.READ,
        parent=audit.add_event(Resource.MASTER_RECORD, record_id, AuditOperation.READ),
    )
    return paginate(
        sorter.sort(
            get_messages_related_to_masterrecord(
                errorsdb, jtrace, record_id, user, status, facility, since, until
            )
        )
    )
Beispiel #6
0
def patient_resultitems(
        patient_record: PatientRecord = Depends(_get_patientrecord),
        service_id: Optional[list[str]] = QueryParam([]),
        order_id: Optional[list[str]] = QueryParam([]),
        since: Optional[datetime.datetime] = None,
        until: Optional[datetime.datetime] = None,
        sorter: SQLASorter = Depends(
            make_sqla_sorter(
                [ResultItem.observation_time, ResultItem.entered_on],
                default_sort_by=ResultItem.observation_time,
            )),
        audit: Auditer = Depends(get_auditer),
):
    """Retreive a specific patient's lab orders"""

    query = patient_record.result_items

    if service_id:
        query = query.filter(ResultItem.service_id.in_(service_id))
    if order_id:
        query = query.filter(ResultItem.order_id.in_(order_id))
    if since:
        query = query.filter(ResultItem.observation_time >= since)
    if until:
        query = query.filter(ResultItem.observation_time <= until)

    audit.add_event(
        Resource.RESULTITEMS,
        None,
        RecordOperation.READ,
        parent=audit.add_event(Resource.PATIENT_RECORD, patient_record.pid,
                               RecordOperation.READ),
    )

    return paginate(sorter.sort(query))
Beispiel #7
0
def patient_observations(
        patient_record: PatientRecord = Depends(_get_patientrecord),
        code: Optional[list[str]] = QueryParam([]),
        sorter: SQLASorter = Depends(
            make_sqla_sorter(
                [Observation.observation_time, Observation.updated_on],
                default_sort_by=Observation.observation_time,
            )),
        audit: Auditer = Depends(get_auditer),
):
    """Retreive a specific patient's lab orders"""
    observations = patient_record.observations
    if code:
        observations = observations.filter(
            Observation.observation_code.in_(code))

    audit.add_event(
        Resource.OBSERVATIONS,
        None,
        RecordOperation.READ,
        parent=audit.add_event(Resource.PATIENT_RECORD, patient_record.pid,
                               RecordOperation.READ),
    )

    return paginate(sorter.sort(observations))
Beispiel #8
0
def code_exclusions(
        ukrdc3: Session = Depends(get_ukrdc3),
        coding_standard: Optional[list[str]] = Query(None),
        code: Optional[list[str]] = Query(None),
        system: Optional[list[str]] = Query(None),
):
    """Retreive a list of internal code maps"""
    return paginate(get_code_exclusions(ukrdc3, coding_standard, code, system))
Beispiel #9
0
def code_maps(
    ukrdc3: Session = Depends(get_ukrdc3),
    source_coding_standard: Optional[list[str]] = Query(None),
    destination_coding_standard: Optional[list[str]] = Query(None),
    source_code: Optional[str] = None,
    destination_code: Optional[str] = None,
):
    """Retreive a list of internal code maps"""
    return paginate(
        get_code_maps(
            ukrdc3,
            source_coding_standard,
            destination_coding_standard,
            source_code,
            destination_code,
        ))
Beispiel #10
0
def patient_laborders(
        patient_record: PatientRecord = Depends(_get_patientrecord),
        audit: Auditer = Depends(get_auditer),
):
    """Retreive a specific patient's lab orders"""
    audit.add_event(
        Resource.LABORDERS,
        None,
        RecordOperation.READ,
        parent=audit.add_event(Resource.PATIENT_RECORD, patient_record.pid,
                               RecordOperation.READ),
    )

    return paginate(
        patient_record.lab_orders.order_by(
            LabOrder.specimen_collected_time.desc()))
Beispiel #11
0
def search_masterrecords(
        pid: list[str] = QueryParam([]),
        mrn_number: list[str] = QueryParam([]),
        ukrdc_number: list[str] = QueryParam([]),
        full_name: list[str] = QueryParam([]),
        dob: list[str] = QueryParam([]),
        facility: list[str] = QueryParam([]),
        search: list[str] = QueryParam([]),
        number_type: list[str] = QueryParam([]),
        include_ukrdc: bool = False,
        user: UKRDCUser = Security(auth.get_user()),
        jtrace: Session = Depends(get_jtrace),
        ukrdc3: Session = Depends(get_ukrdc3),
        audit: Auditer = Depends(get_auditer),
):
    """Search the EMPI for a particular master record"""
    matched_ukrdc_ids = search_masterrecord_ids(mrn_number, ukrdc_number,
                                                full_name, pid, dob, facility,
                                                search, ukrdc3)

    # Matched UKRDC IDs will only give us UKRDC-type Master Records,
    # but we also want the associated NHS/CHI/HSC master records.
    # So, we do a single pass of the link records to expand our selection.
    person_ids = (jtrace.query(LinkRecord.person_id).join(MasterRecord).filter(
        MasterRecord.nationalid.in_(matched_ukrdc_ids)))

    master_ids = (jtrace.query(MasterRecord.id).join(LinkRecord).filter(
        LinkRecord.person_id.in_(person_ids)))

    matched_records = get_masterrecords(jtrace, user).filter(
        MasterRecord.id.in_(master_ids))

    if number_type:
        matched_records = matched_records.filter(
            MasterRecord.nationalid_type.in_(number_type))

    if not include_ukrdc:
        matched_records = matched_records.filter(
            MasterRecord.nationalid_type != "UKRDC")

    page: Page = paginate(matched_records)  # type: ignore

    for record in page.items:
        audit.add_event(Resource.MASTER_RECORD, record.id, AuditOperation.READ)

    return page
Beispiel #12
0
def facility_patients_latest_errors(
        code: str,
        ukrdc3: Session = Depends(get_ukrdc3),
        errorsdb: Session = Depends(get_errorsdb),
        statsdb: Session = Depends(get_statsdb),
        user: UKRDCUser = Security(auth.get_user()),
        sorter: SQLASorter = Depends(ERROR_SORTER),
        audit: Auditer = Depends(get_auditer),
):
    """Retreive time-series new error counts for the last year for a particular facility"""
    query = get_patients_latest_errors(ukrdc3, errorsdb, statsdb, code, user)

    audit.add_event(
        Resource.MESSAGES,
        None,
        AuditOperation.READ,
        parent=audit.add_event(Resource.FACILITY, code, AuditOperation.READ),
    )

    return paginate(sorter.sort(query))
Beispiel #13
0
def patient_documents(
        patient_record: PatientRecord = Depends(_get_patientrecord),
        sorter: SQLASorter = Depends(
            make_sqla_sorter(
                [Document.documenttime, Document.updatedon],
                default_sort_by=Document.documenttime,
            )),
        audit: Auditer = Depends(get_auditer),
):
    """Retreive a specific patient's documents"""
    audit.add_event(
        Resource.DOCUMENTS,
        None,
        RecordOperation.READ,
        parent=audit.add_event(Resource.PATIENT_RECORD, patient_record.pid,
                               RecordOperation.READ),
    )
    # NOTE: We defer the 'stream' column to avoid sending the full PDF file content
    # when we're just querying the list of documents.
    return paginate(
        sorter.sort(patient_record.documents.options(defer("stream"))))
Beispiel #14
0
def workitem_messages(
        workitem_id: int,
        facility: Optional[str] = None,
        since: Optional[datetime.datetime] = None,
        until: Optional[datetime.datetime] = None,
        status: Optional[list[str]] = QueryParam(None),
        user: UKRDCUser = Security(auth.get_user()),
        jtrace: Session = Depends(get_jtrace),
        errorsdb: Session = Depends(get_errorsdb),
        audit: Auditer = Depends(get_auditer),
):
    """Retreive a list of other work items related to a particular work item"""
    workitem = get_extended_workitem(jtrace, workitem_id, user)

    workitem_nis: list[str] = [
        record.nationalid for record in workitem.incoming.master_records
    ]

    if workitem.master_record:
        workitem_nis.append(workitem.master_record.nationalid)

    audit.add_event(
        Resource.MESSAGES,
        None,
        AuditOperation.READ,
        parent=audit.add_event(Resource.WORKITEM, workitem_id,
                               AuditOperation.READ),
    )

    return paginate(
        get_messages(
            errorsdb,
            user,
            statuses=status,
            nis=workitem_nis,
            facility=facility,
            since=since,
            until=until,
        ))