Skip to content

Commit a9432fe

Browse files
committed
adds an api table and functioning thread level parallelism
1 parent 3f1153b commit a9432fe

File tree

5 files changed

+83
-19
lines changed

5 files changed

+83
-19
lines changed

DonorMatch/admin.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
from django.contrib import admin
22

3+
from .models import APICalls
4+
35
# Register your models here.
6+
7+
admin.site.register(APICalls)

DonorMatch/models.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
from django.db import models
22

33
# Create your models here.
4+
5+
class APICalls(models.Model) :
6+
hospital_name = models.CharField(max_length=100, null=True, blank=True)
7+
address = models.TextField(null=True, blank=True)
8+
api_url = models.CharField(null=False, blank=False, max_length=500)
9+
10+
def __str__(self):
11+
return (self.hospital_name)

DonorMatch/views.py

+68-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
from django.shortcuts import render
22
from django.contrib.auth.decorators import login_required
33

4+
from .models import APICalls
5+
46
import requests
7+
import threading
8+
import string
9+
import math
510

611
# Create your views here.
712

@@ -10,39 +15,85 @@ def HomePage(request) :
1015
return render(request, 'home.html')
1116

1217

13-
#@login_required
14-
def DonorSearch(request) :
15-
if request.method == 'GET' :
16-
return render(request, 'search.html', {'display_data':False})
17-
else :
18-
# Recieve data from the form
19-
blood_abo = request.POST['abo']
20-
blood_rh = request.POST['rh']
21-
city = request.POST['city']
22-
district = request.POST['district']
23-
state = request.POST['state']
18+
patient_data = []
19+
patient_data_lock = threading.Lock()
2420

25-
# Perform api calls here
26-
url = "http://127.0.0.1:8000/api/patient_details/abo=" + blood_abo + "&rh=" + blood_rh + "&city=" + (" " if city == "" else city) + "&district=" + (" " if district == "" else district) + "&state=" + (" " if state == "" else state)
21+
22+
def serial_task(hospitals, blood_requirement) :
23+
""" Performs the serial task of making the api calls and storing the information in an array. """
24+
global patient_data
25+
patient_data_thread_local = []
26+
for hospital in hospitals :
27+
url = hospital.api_url
28+
url = string.Template(url).substitute(**blood_requirement)
2729
response = requests.get(url).json()
28-
patient_objects = []
2930
for i in range(len(response)) :
3031
current_data = response[i]
3132
current_data["hospital"] = "Plasm_Hospital 1"
3233
data_arr = [
3334
current_data["blood_abo_type"],
3435
current_data["blood_rh_type"],
35-
"Plasm_Hospital 1",
36+
hospital.hospital_name,
3637
"Recovered",
3738
current_data["age_bracket"],
3839
current_data["gender"],
3940
current_data["nationality"],
4041
current_data["city"]
4142
]
42-
patient_objects.append(data_arr)
43+
patient_data_thread_local.append(data_arr)
44+
45+
# Acquire the lock
46+
patient_data_lock.acquire()
47+
# Make the additions to the main list of patient data
48+
patient_data = patient_data + patient_data_thread_local
49+
# Release the lock
50+
patient_data_lock.release()
51+
52+
53+
#@login_required
54+
def DonorSearch(request) :
55+
if request.method == 'GET' :
56+
return render(request, 'search.html', {'display_data':False})
57+
else :
58+
global patient_data
59+
60+
# Recieve data from the html-form
61+
blood_requirement = {
62+
'blood_abo' : request.POST['abo'],
63+
'blood_rh' : request.POST['rh'],
64+
'city' : " " if request.POST['city'] == "" else request.POST['city'],
65+
'district' : " " if request.POST['district'] == "" else request.POST['district'],
66+
'state' : " " if request.POST['district'] == "" else request.POST['district']
67+
}
68+
69+
70+
# Perform api calls here
71+
72+
# Get the hospital details from the database
73+
hospital_details = APICalls.objects.all()
74+
num_threads = 2
75+
thread_job_pool = []
76+
thread_max_num_jobs = math.ceil(len(hospital_details) / num_threads)
77+
for i in range(num_threads) :
78+
thread_job_pool.append(hospital_details[(i * thread_max_num_jobs) : min(((i+1) * thread_max_num_jobs), len(hospital_details))])
79+
print(thread_job_pool)
80+
81+
# Sample URL Pattern
82+
#url = "http://127.0.0.1:8000/api/patient_details/abo=$blood_abo&rh=$blood_rh&city=$city&district=$district&state=$state"
83+
#url = "http://127.0.0.1:8000/api/patient_details/abo=" + blood_abo + "&rh=" + blood_rh + "&city=" + (" " if city == "" else city) + "&district=" + (" " if district == "" else district) + "&state=" + (" " if state == "" else state)
84+
4385

86+
# Threading setup
87+
threads = []
88+
for i in range(num_threads-1) :
89+
t = threading.Thread(target=serial_task, args=(thread_job_pool[i], blood_requirement))
90+
threads.append(t)
91+
t.start()
92+
serial_task(thread_job_pool[-1], blood_requirement)
93+
for i in range(num_threads-1) :
94+
threads[i].join()
4495

4596
# Return the values to the web page
4697

47-
context = {'display_data':True, 'patient_data':patient_objects, 'num_patients':len(patient_objects)}
98+
context = {'display_data':True, 'patient_data':patient_data, 'num_patients':len(patient_data)}
4899
return render(request, 'search.html', context)

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
requests
2+
tbb

templates/base.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ <h1 style="color: black; font-size: 35px; margin: 0em; font-family: forte; paddi
3434
{% if user.is_authenticated %}
3535
{% if user.is_superuser %}
3636
<li style="float: right; width: 110px;">
37-
<a href="admin">My Account</a>
37+
<a href="/admin">My Account</a>
3838
</li>
3939
{% endif %}
4040
<li style="float: right; width: 110px;">
41-
<a href="/donor-match">Find Donors</a>
41+
<a href="/donor-search">Find Donors</a>
4242
</li>
4343
{% endif %}
4444
</ul >

0 commit comments

Comments
 (0)