Example #1
0
def get_db_connection():
    global connection
    global fb
    if not connection:
        connection = psycopg2.connect(dbname='fhirbase',
                                      user='******',
                                      host='fhirbase',
                                      port='5432')
        fb = fhirbase.FHIRBase(connection)
    return fb
Example #2
0
def get_resource():
    connection = psycopg2.connect(
        database='fhirbase',
        user='******',
        host='fhirbasedb-service.globalhivteam30.svc.cluster.local',
        port='5432',
        password='******')
    fb = fhirbase.FHIRBase(connection)
    #patientData = fb.read(resource_type='Patient', id='patientId')
    with fb.execute('SELECT * FROM patient') as cursor:
        respons = (cursor.fetchall())
        return jsonify(respons)
Example #3
0
def get_patient_resource(id):
    connection = psycopg2.connect(database='fhirbase',
                                  user='******',
                                  host='172.17.0.3',
                                  port='5432',
                                  password='******')
    fb = fhirbase.FHIRBase(connection)
    #patientData = fb.read(resource_type='Patient', id='patientId')
    #with fb.execute('SELECT * FROM patient WHERE id=%s', [id]) as cursor:
    #return(cursor.fetchall())
    print('\nID of Patient:  ', [id])
    getPatient = fb.read({'resourceType': 'Patient', 'id': [id]})
    print('\nGot Patient:  ', getPatient)
Example #4
0
def save_in_fhirbase(instances):
    """
    Save instances of FHIR resources in the fhirbase
    :param instances: list of instances
    """
    config = Config("fhirbase")
    with psycopg2.connect(dbname=config.database,
                          user=config.user,
                          host=config.host,
                          port=config.port) as connection:
        fb = fhirbase.FHIRBase(connection)
        for instance in instances:
            fb.create(instance)
Example #5
0
def fb(db):
    instance = fhirbase.FHIRBase(db)
    instance.execute_without_result('TRUNCATE TABLE transaction')
    instance.execute_without_result('TRUNCATE TABLE patient')

    return instance
Example #6
0
def POST_resource():
    #have new test
    connection = psycopg2.connect(database='fhirbase',
                                  user='******',
                                  host='172.17.0.3',
                                  port='5432',
                                  password='******')
    fb = fhirbase.FHIRBase(connection)
    input_form = request.get_json()
    #transform
    #form_obj = json.loads(input_form, object_hook=testFormDecoder)
    #return some sample values for display
    testForm = input_form['testForm']
    print('\nfirst name : ', testForm['name']['firstName'])
    fb = fhirbase.FHIRBase(connection)
    resultOfPatientCreation = fb.create({
        'resourceType':
        'Patient',
        'name': [{
            'use':
            'official',
            'family':
            testForm['name']['lastName'],
            'given':
            [testForm['name']['firstName'], testForm['name']['middleName']]
        }],
        'gender':
        testForm['sex'],
        'birthDate':
        testForm['dob'],
    })
    print('\nresult of create : ', resultOfPatientCreation)
    mapOfTests = {}
    for i in testForm['testData']['tests']:
        print("\n loping over map:  ", i)
        if i['testNumber'] == 1:
            mapOfTests[0] = i
        elif i['testNumber'] == 2:
            mapOfTests[1] = i
        elif i['testNumber'] == 3:
            mapOfTests[2] = i

    print("\nwhat is in the map of tests: ", len(mapOfTests))

    firstTest = returnTestTypeData(
        testForm['testData']['tests'][0]['testAssay'])
    secondTest = returnTestTypeData(
        testForm['testData']['tests'][1]['testAssay'])
    thirdTest = returnTestTypeData(
        testForm['testData']['tests'][2]['testAssay'])

    idOfPatient = resultOfPatientCreation['id']

    print("\nID of patient: ", idOfPatient)

    resultOfHIVObservationCreation = fb.create({
        'resourceType':
        'Observation',
        'code': {
            'coding': [{
                'system': 'http://loinc.org',
                'code': '69668-2',
                'display': 'HIV 1 and 2 Ab IA.rapid Nom'
            }]
        },
        'subject': {
            'reference': 'Patient/' + str(idOfPatient)
        },
        'effectiveDateTime':
        testForm['testData']['testDate'],
        'valueString':
        testForm['resultReceived'],
        'component': [{
            'code': {
                'coding': [{
                    'system': firstTest.system,
                    'code': firstTest.code,
                    'display': firstTest.display
                }]
            },
            'valueString': mapOfTests[0]['testResult']
        }, {
            'code': {
                'coding': [{
                    'system': secondTest.system,
                    'code': secondTest.code,
                    'display': secondTest.display
                }]
            },
            'valueString': mapOfTests[1]['testResult']
        }, {
            'code': {
                'coding': [{
                    'system': thirdTest.system,
                    'code': thirdTest.code,
                    'display': thirdTest.display
                }]
            },
            'valueString': mapOfTests[2]['testResult']
        }]
    })
    print('\nresult of create : ', resultOfHIVObservationCreation)
    sampleAns = str(resultOfPatientCreation) + "\n" + str(
        resultOfHIVObservationCreation)
    return sampleAns
        )
        with open("benchmark/examples.zip", "wb") as f:
            for data in r.iter_content(block_size):
                t.update(len(data))
                f.write(data)
        t.close()
    else:
        print("Using cached resources")


download_resources()
connection = psycopg2.connect(dbname="fb",
                              user="******",
                              host="localhost",
                              port="5432")
fb = fhirbase.FHIRBase(connection)

examples = tqdm(iter_examples(),
                total=count_examples(),
                desc="Running write benchmark")
stats = {}
inserted = []
for example, data in examples:
    if not data.get("id"):
        data["id"] = str(uuid4())
    start = timer()
    res = fb.create(data)
    end = timer()
    stats[example] = end - start
    inserted.append(res)
Example #8
0
def db_connect():
    pguser = os.getenv('PGUSER', 'postgres')
    pgpassword = os.getenv('PGPASSWORD', 'postgres')
    pghost = os.getenv('PGHOST', 'localhost')
    pgport = os.getenv('PGPORT', '5432')
    dbname = os.getenv('PGDATABASE', 'fhirbase')

    return psycopg2.connect(dbname=dbname, user=pguser,
                            password=pgpassword, host=pghost, port=pgport)


if __name__ == '__main__':
    conn = db_connect()
    try:
        fb = fhirbase.FHIRBase(conn)

        print('Create patient')
        patient = fb.create({'resourceType': 'Patient'})
        print(patient)

        print()
        print('Update patient')

        patient.update({'name': [{'text': 'John'}]})
        updated_patient = fb.update(patient)
        print(updated_patient)

        print()
        print('List of patients')