1
1
from django .shortcuts import render
2
2
from django .contrib .auth .decorators import login_required
3
3
4
+ from .models import APICalls
5
+
4
6
import requests
7
+ import threading
8
+ import string
9
+ import math
5
10
6
11
# Create your views here.
7
12
@@ -10,39 +15,85 @@ def HomePage(request) :
10
15
return render (request , 'home.html' )
11
16
12
17
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 ()
24
20
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 )
27
29
response = requests .get (url ).json ()
28
- patient_objects = []
29
30
for i in range (len (response )) :
30
31
current_data = response [i ]
31
32
current_data ["hospital" ] = "Plasm_Hospital 1"
32
33
data_arr = [
33
34
current_data ["blood_abo_type" ],
34
35
current_data ["blood_rh_type" ],
35
- "Plasm_Hospital 1" ,
36
+ hospital . hospital_name ,
36
37
"Recovered" ,
37
38
current_data ["age_bracket" ],
38
39
current_data ["gender" ],
39
40
current_data ["nationality" ],
40
41
current_data ["city" ]
41
42
]
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
+
43
85
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 ()
44
95
45
96
# Return the values to the web page
46
97
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 )}
48
99
return render (request , 'search.html' , context )
0 commit comments