asd

asdasd
 avatar
unknown
plain_text
3 years ago
6.4 kB
2
Indexable
from django.core.management.base import BaseCommand
from django.db.utils import IntegrityError
from django.core.exceptions import ObjectDoesNotExist

from decouple import config
from genexpert.models import ResultCovid, Machine, ResultProcessedCovid, Site, Modules, MachineSerialNo
import datetime
import psycopg2
import hl7


date_format = '%Y%m%d%H%M%S'


def parse_row(row):
    msg = row[0].strip("'")
    msg = msg.replace("\\r", "\r")
    if msg[:3] == "b'M":
        msg = msg[2:-1]
    a = hl7.parse(msg)

    # print(obx[4], obx[5])

    print(str(a.segments('OBR')[0]).split('|')[4])

    if str(a.segments('OBR')[0]).split('|')[4] == 'SARSCOV2':
        covid_result = str(a.segments('OBX')[0][5][0]).strip("^")
        instrument_serial_number = a.segment('OBX')[18][4]
        module_serial_number = a.segment('OBX')[18][3]
        start_test_date = a.segment('TQ1')[7]
        sample_id = str(a.segment('SPM')[2]).strip("^")
        notes = ''

        try:
            notes = a.segments('NTE')[0][0][0]
        except:
            pass

        machine_code = str(a.segments('MSH')[0][5]).strip("^")
        date = a.segments('MSH')[0][7]
        # result_id = str(a.segments('MSH')[0][10])
        result_id = str(start_test_date) + \
            str(instrument_serial_number) + machine_code
        patient_id = a.segments('PID')[0][3][0]

        try:
            error = a.segments('NTE')[0][3][0][3][0]
        except (KeyError, IndexError):
            error = None
        data = {
            'covid_result': covid_result,
            'machine_code': machine_code,
            'patient_id': patient_id,
            'date': datetime.datetime.strptime(str(start_test_date), date_format),
            'error': error or '',
            'id': result_id,
            'sample_id': sample_id,
            'module_serial_number': module_serial_number,
            'instrument_serial_number': instrument_serial_number,
            'notes': notes
        }

        return data
    else:
        return None


class Command(BaseCommand):
    help = 'Smth Smth'

    def handle(self, *args, **kwargs):

        last_processed_id = ResultProcessedCovid.load().last_id
        # last_processed_id = 177648

        connection = psycopg2.connect(user=config('DB_USER_GENX'),
                                      password=config('DB_PASSWORD_GENX'),
                                      host=config('DB_HOST_GENX'),
                                      port=config('DB_PORT_GENX'),
                                      database=config('DB_NAME_GENX'))
        cursor = connection.cursor('cursor')
        cursor.itersize = 100
        count = cursor.execute("SELECT data, id FROM hl7_raw_data WHERE id >= %(id)s", {
                               'id': last_processed_id})
        while True:
            rows = cursor.fetchmany(100)
            if not rows:
                break

            for row in rows:
                print("Id:", row[1])
                try:
                    parsed = parse_row(row)
                    if parsed != None:
                        # machine, created = Machine.objects.get_or_create(code=parsed.pop('machine_code'),defaults = {
                        # # 'modules': [],
                        # 'serial': None
                        # })
                        machine, created = Machine.objects.get_or_create(code=parsed['machine_code'])

                        machine_serial_no = parsed.pop(
                            'instrument_serial_number')
                        msn = parsed.pop('module_serial_number')

                        if (machine_serial_no):

                            # module , module_created = Modules.objects.get_or_create(serial_no = str(msn))
                            # if module_created:
                            #     module.machine = machine
                            #     module.save()
                            machineSerialNo, machine_serial_created = MachineSerialNo.objects.get_or_create(serial_no=str(machine_serial_no), defaults={
                                'machine': machine,
                                'updated_at': parsed['date']
                            })
                            print("module_created", machine_serial_created)

                            if not machine_serial_created:
                                machineSerialNo.updated_at = parsed['date']
                                machineSerialNo.save()

                            if (msn):

                                # module , module_created = Modules.objects.get_or_create(serial_no = str(msn))
                                # if module_created:
                                #     module.machine = machine
                                #     module.save()
                                print('SERIAL:::', machineSerialNo)
                                module, module_created = Modules.objects.get_or_create(serial_no=str(msn), defaults={
                                    'serial_id': machineSerialNo.id,
                                    'updated_at': parsed['date']
                                })
                                module_present = True
                                print("module_created", module_created)

                                if not module_created:
                                    module.updated_at = parsed['date']
                                    module.save()
                        parsed.pop('machine_code')
                        try:
                            ResultCovid.objects.create(
                                **parsed, module_serial_number=msn, instrument_serial_number=machine_serial_no, machine_id=machine.id, module_id=module_present and module.id or None)
                        except IntegrityError as e:
                            if e.__cause__.pgcode == '23505':
                                print("Result already exists, skipping")
                            else:
                                raise
                        processed = ResultProcessedCovid.load()
                        # save id of last result_id
                        processed.last_id = rows[-1][1]
                        processed.save()
                except Exception as e:
                    print(e)
                    print(row)
                    print(e)

        cursor.close()
        connection.close()
Editor is loading...