/
server.py
250 lines (202 loc) · 8.67 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from flask import Flask, render_template, request
import json
import os
import ibm_db
#from geopy.distance import great_circle
from datetime import datetime
from dateutil import tz
app = Flask(__name__)
# Get service information if on IBM Cloud Platform
if 'VCAP_SERVICES' in os.environ:
db2info = json.loads(os.environ['VCAP_SERVICES'])['dashDB For Transactions'][0]
db2cred = db2info["credentials"]
appenv = json.loads(os.environ['VCAP_APPLICATION'])
else:
# raise ValueError('Expected cloud environment')
with open('new.json') as new_file:
jd_vcap_new_file = json.load(new_file)
db2info = jd_vcap_new_file['dashDB For Transactions'][0]
db2cred = db2info["credentials"]
# Time zone conversion
def getLocalTime(rows):
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('America/Chicago')
for i in rows:
utc = datetime.strptime(i['time'], '%Y-%m-%d %H:%M:%S')
utc = utc.replace(tzinfo=from_zone)
central = utc.astimezone(to_zone)
i['time'] = central
return rows
@app.route('/displayDB', methods=['GET', 'POST'])
def displayDB():
try:
db2conn = ibm_db.connect("DATABASE=" + db2cred['db'] + ";HOSTNAME=" + db2cred['hostname'] + ";PORT=" + str(
db2cred['port']) + ";UID=" + db2cred['username'] + ";PWD=" + db2cred['password'] + ";", "", "")
sql = "Select * from earth"
stmt = ibm_db.prepare(db2conn, sql)
ibm_db.execute(stmt)
result = ibm_db.fetch_assoc(stmt)
row = []
while result != False:
# print sql
row.append(result.copy())
result = ibm_db.fetch_assoc(stmt)
ibm_db.close(db2conn)
#row = getLocalTime(row)
return render_template("displayDB.html", row=row)
except:
print "Exception Occured in Display Method"
return render_template("home.html")
@app.route('/search',methods=['GET', 'POST'])
def search():
try:
db2conn = ibm_db.connect("DATABASE=" + db2cred['db'] + ";HOSTNAME=" + db2cred['hostname'] + ";PORT=" + str(
db2cred['port']) + ";UID=" + db2cred['username'] + ";PWD=" + db2cred['password'] + ";", "", "")
room = request.form['uname']
sql = "Select * from earth WHERE \"mag\" = ?"
print room
stmt = ibm_db.prepare(db2conn, sql)
ibm_db.bind_param(stmt,1,room)
ibm_db.execute(stmt)
result = ibm_db.fetch_assoc(stmt)
row = []
while result != False:
row.append(result.copy())
result = ibm_db.fetch_assoc(stmt)
ibm_db.close(db2conn)
return render_template("search.html", row=row)
except:
print "Exception Occured in Display Method"
return render_template("home.html")
@app.route('/searchcount',methods=['GET', 'POST'])
def searchcount():
try:
db2conn = ibm_db.connect("DATABASE=" + db2cred['db'] + ";HOSTNAME=" + db2cred['hostname'] + ";PORT=" + str(
db2cred['port']) + ";UID=" + db2cred['username'] + ";PWD=" + db2cred['password'] + ";", "", "")
room = request.form['uname']
sql = "Select count(*) from earth WHERE \"mag\" > ?"
print room
stmt = ibm_db.prepare(db2conn, sql)
ibm_db.bind_param(stmt,1,room)
ibm_db.execute(stmt)
result = ibm_db.fetch_assoc(stmt)
row = []
while result != False:
row.append(result.copy())
result = ibm_db.fetch_assoc(stmt)
ibm_db.close(db2conn)
return render_template("searchcount.html", row=row)
except:
print "Exception Occured in Display Method"
return render_template("home.html")
@app.route('/searchinrange',methods=['GET', 'POST'])
def searchinrange():
try:
db2conn = ibm_db.connect("DATABASE=" + db2cred['db'] + ";HOSTNAME=" + db2cred['hostname'] + ";PORT=" + str(
db2cred['port']) + ";UID=" + db2cred['username'] + ";PWD=" + db2cred['password'] + ";", "", "")
magnitude1 = request.form['magnitude1']
magnitude2 = request.form['magnitude2']
timeDate1 = request.form['date1']
timeDate2 = request.form['date2']
print magnitude1
print magnitude2
print timeDate1
print timeDate2
sql = "Select * from earth WHERE \"mag\" between ? AND ? AND \"time\" between ? AND ?"
stmt = ibm_db.prepare(db2conn, sql)
ibm_db.bind_param(stmt,1,magnitude1)
ibm_db.bind_param(stmt, 2, magnitude2)
ibm_db.bind_param(stmt, 3, timeDate1)
ibm_db.bind_param(stmt, 4, timeDate2)
ibm_db.execute(stmt)
result = ibm_db.fetch_assoc(stmt)
row = []
while result != False:
row.append(result.copy())
result = ibm_db.fetch_assoc(stmt)
ibm_db.close(db2conn)
return render_template("searchinrange.html", row=row)
except:
print "Exception Occured in Display Method"
return render_template("home.html")
@app.route('/searchinradius',methods=['GET', 'POST'])
def searchinradius():
try:
db2conn = ibm_db.connect("DATABASE=" + db2cred['db'] + ";HOSTNAME=" + db2cred['hostname'] + ";PORT=" + str(
db2cred['port']) + ";UID=" + db2cred['username'] + ";PWD=" + db2cred['password'] + ";", "", "")
latitude = (request.form['latitude'])
longitude =(request.form['longitude'])
radius = (request.form['radius'])
#sql = "select * from earth"
sql ="select * from (select *,(((acos(sin(("+latitude+"*3.14/180))* sin ((\"latitude\"*3.14/180))+cos(("+latitude+\
"*3.14/180))*cos((\"latitude\"*3.14/180))*COS((("+longitude+" - \"longitude\")*3.14/180))))*180/3.14)*60*1.1515*1.609344) " \
"as distance From earth) where distance <= "+radius+""
stmt = ibm_db.prepare(db2conn, sql)
ibm_db.execute(stmt)
result = ibm_db.fetch_assoc(stmt)
row = []
while result != False:
row.append(result.copy())
result = ibm_db.fetch_assoc(stmt)
ibm_db.close(db2conn)
return render_template("searchinradius.html", row=row)
except:
print "Exception Occured in Display Method"
return render_template("home.html")
@app.route('/searchinmagintudeclusters',methods=['GET', 'POST'])
def searchinmagintudeclusters():
try:
db2conn = ibm_db.connect("DATABASE=" + db2cred['db'] + ";HOSTNAME=" + db2cred['hostname'] + ";PORT=" + str(
db2cred['port']) + ";UID=" + db2cred['username'] + ";PWD=" + db2cred['password'] + ";", "", "")
mag = request.form['mag']
mag2 = mag+".99"
print mag2
sql = "select * from earth where (\"mag\" between ? and ?)"
stmt = ibm_db.prepare(db2conn, sql)
ibm_db.bind_param(stmt,1,mag)
ibm_db.bind_param(stmt, 2, mag2)
ibm_db.execute(stmt)
result = ibm_db.fetch_assoc(stmt)
row = []
while result != False:
row.append(result.copy())
result = ibm_db.fetch_assoc(stmt)
ibm_db.close(db2conn)
return render_template("searchinmagintudeclusters.html", row=row)
except:
print "Exception Occured in Display Method"
return render_template("home.html")
@app.route('/midnight', methods=['GET', 'POST'])
def midnight():
try:
db2conn = ibm_db.connect("DATABASE=" + db2cred['db'] + ";HOSTNAME=" + db2cred['hostname'] + ";PORT=" + str(
db2cred['port']) + ";UID=" + db2cred['username'] + ";PWD=" + db2cred['password'] + ";", "", "")
# Night time
sql = "select count(*) from earth where \"mag\" > 5.0 and (EXTRACT(hour FROM \"time\") >= 18 OR EXTRACT(hour FROM \"time\") <= 6)"
stmt = ibm_db.prepare(db2conn, sql)
ibm_db.execute(stmt)
result = ibm_db.fetch_assoc(stmt)
row = []
while result != False:
row.append(result.copy())
result = ibm_db.fetch_assoc(stmt)
# Day time
sql = "select count(*) from earth where \"mag\" > 5.0 and (EXTRACT(hour FROM \"time\") >= 7 OR EXTRACT(hour FROM \"time\") <= 17)"
stmt = ibm_db.prepare(db2conn, sql)
ibm_db.execute(stmt)
result = ibm_db.fetch_assoc(stmt)
while result != False:
row.append(result.copy())
result = ibm_db.fetch_assoc(stmt)
ibm_db.close(db2conn)
return render_template("midnight.html", row=row)
except:
print "Exception Occured in Display Method"
return render_template("home.html")
@app.route('/')
def index():
return render_template('home.html')
port = os.getenv('PORT', '10000')
if __name__ == "__main__":
#app.run(host='127.0.0.1', port=int(port))
app.run(host='0.0.0.0', port=int(port))