-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
427 lines (356 loc) · 14.6 KB
/
views.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 8 15:51:47 2018
@author: garima.misra
ALL
--START ADDRESS LIST AND EXISTS
--END ADDRESS LIST AND EXISTS
--FROM START TO END WITH DATE
#Below two can be integrated in a single method also: all dates will be taken for a given combination
--FROM START TO END WITHOUT DATE
--TRAFFIC ON A PARTICULAR DATE
current_app context
"""
import numpy as np
import time
import sys
import traceback
import logging
from logging.handlers import RotatingFileHandler
from flask import Flask, jsonify, render_template, abort, request, make_response
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from models import db
from models import DelhiTraffic, MumbaiTraffic, BangloreTraffic
from custom import ValidationError, NotFoundError, ServerError, Validation
#from views import Validation
api = Flask(__name__)
api.config.from_pyfile('Config.py')
@api.route('/v1/Cities/')
def Cities():
data = {'City_List' : ['Delhi', 'Mumbai', 'Banglore']}
return jsonify(data)
@api.route('/v1/check_data_meta/')
def check_Data_Meta():
x = DelhiTraffic.query.all()
print(len(x))
print(x)
print(type(x[1]))
print(type(x[1].distance))
print(type(x[1].duration))
print(type(x[1].duration_traffic))
print(type(x[1].end_address))
print(type(x[1].start_address))
print(type(x[1].t))
print(type(x[1].speed_no_traffic))
print(type(x[1].speed_w_traffic))
print(type(x[1].t_human))
print(type(x[1].route))
return(jsonify({'check_data' : DelhiTraffic.query.all()}))
@api.route('/v2/traffic/', methods = ['GET'])
def traffic2():
#FROM START TO END WITH DATE
try:
if request.args.get('city'):
city = request.args.get('city')
else:
city = "Delhi"
if request.args.get('date_time'):
date_time = request.args.get('date_time') #Check date length and format and existance in database
else:
date_time = "201706130430"
if request.args.get('start_station'):
start_station = request.args.get('start_station')
else:
start_station = "MSIL Internal Rd, Maruti Udyog, Sector 18, Gurugram, Haryana 122008, India"
if request.args.get('end_station'):
end_station = request.args.get('end_station')
else:
end_station = "Civil Lines, Gurugram, Haryana 122022, India"
except ValueError as valueerr:
print("ValueError has been raised." )
print(valueerr)
except TypeError as typeerr:
print("Type error has been raised.")
print(typeerr)
if city == "Delhi":
conn = DelhiTraffic
elif city == "Banglore":
conn = BangloreTraffic
elif city == "Mumbai":
conn = MumbaiTraffic
else:
conn = DelhiTraffic
Validation.date_time_validation(conn, date_time)
Validation.city_validation(conn, city)
Validation.end_station_validation(conn, end_station)
Validation.start_station_validation(conn, start_station)
print(type(date_time))
print(date_time)
print(start_station)
print(end_station)
data_db = conn.query.filter(conn.start_address.like('%'+start_station+'%'),
conn.end_address.like('%'+end_station+'%'), conn.t == date_time).all()
print(len(data_db))
print(data_db)
if(len(data_db) == 0):
raise NotFoundError({'start_station' : start_station, 'date_time' : date_time,
'end_station' : end_station}, message = 'This combination doesn\'t match any record')
else:
start_time = time.time()
data = { i+1 : {'Distance': data_db[i].distance, 'Duration' : data_db[i].duration,
'Duration_Traffic' : data_db[i].duration_traffic, 'End_Address' : data_db[i].end_address,
'Start_Address' : data_db[i].start_address, 'Speed_Without_Traffic' : data_db[i].speed_no_traffic,
'Speed_With_Traffic' : data_db[i].speed_w_traffic, 'Time' : data_db[i].t_human,
'Route' : data_db[i].route}
for i in range(len(data_db))}
traffic_data = {'traffic_data' : data}
print(traffic_data)
del (data)
end_time = time.time()
print("time taken ", end_time - start_time)
return jsonify(traffic_data)
@api.route('/v2/traffic/date/', methods = ['GET'])
def traffic_date():
#TRAFFIC ON A PARTICULAR DATE
try:
if request.args.get('date_time'):
date_time = request.args.get('date_time')
else:
date_time = '201706130430'
if request.args.get('city'):
city = request.args.get('city')
else:
city = 'Delhi'
except ValueError as valueerr:
print("ValueError has been raised." )
print(valueerr)
except TypeError as typeerr:
print("Type error has been raised.")
print(typeerr)
if city == "Delhi":
conn = DelhiTraffic
elif city == "Banglore":
conn = BangloreTraffic
elif city == "Mumbai":
conn = MumbaiTraffic
else:
conn = DelhiTraffic
Validation.date_time_validation(conn, date_time)
Validation.city_validation(conn, city)
data_db = conn.query.filter(conn.t.like('%'+date_time+'%'))
start_time = time.time()
data = {}
for i in range((data_db).count()):
print(i)
data[i + 1] = {'Distance': data_db[i].distance, 'Duration' : data_db[i].duration,
'Duration_Traffic' : data_db[i].duration_traffic,
'End_Address' : data_db[i].end_address, 'Start_Address' : data_db[i].start_address,
'Speed_Without_Traffic' : data_db[i].speed_no_traffic, 'Speed_With_Traffic' : data_db[i].speed_w_traffic,
'Time' : data_db[i].t_human, 'Route' : data_db[i].route}
print(data_db[i])
traffic_data = {'traffic_data' : data}
del (data)
end_time = time.time()
print("time taken ", end_time - start_time)
return jsonify(traffic_data)
@api.route('/v2/traffic/start/end/', methods = ['GET'])
def traffic_start_end():
#FROM START TO END WITHOUT DATE
try:
if request.args.get('city'):
city = request.args.get('city')
else:
city = "Delhi"
if request.args.get('start_station'):
start_station = request.args.get('start_station')
else:
start_station = "MSIL Internal Rd, Maruti Udyog, Sector 18, Gurugram, Haryana 122008, India"
if request.args.get('end_station'):
end_station = request.args.get('end_station')
else:
end_station = "Civil Lines, Gurugram, Haryana 122022, India"
except ValueError as valueerr:
print("ValueError has been raised." )
print(valueerr)
except TypeError as typeerr:
print("Type error has been raised.")
print(typeerr)
if city == "Delhi":
conn = DelhiTraffic
elif city == "Banglore":
conn = BangloreTraffic
elif city == "Mumbai":
conn = MumbaiTraffic
else:
conn = DelhiTraffic
Validation.city_validation(conn, city)
Validation.end_station_validation(conn, end_station)
Validation.start_station_validation(conn, start_station)
print(start_station)
print(end_station)
data_db = conn.query.filter(conn.start_address.like('%'+start_station+'%'), conn.end_address.like('%'+end_station+'%')).all()
print(len(data_db))
print(data_db)
if(len(data_db) == 0):
raise NotFoundError({'start_station' : start_station,'end_station' : end_station},
message = 'This combination doesn\'t match any record')
else:
start_time = time.time()
data = { i+1 : {'Distance': data_db[i].distance, 'Duration' : data_db[i].duration,
'Duration_Traffic' : data_db[i].duration_traffic,
'End_Address' : data_db[i].end_address, 'Start_Address' : data_db[i].start_address,
'Speed_Without_Traffic' : data_db[i].speed_no_traffic,
'Speed_With_Traffic' : data_db[i].speed_w_traffic, 'Time' : data_db[i].t_human,
'Route' : data_db[i].route }
for i in range(len(data_db))}
traffic_data = {'traffic_data' : data}
del (data)
end_time = time.time()
print("time taken ", end_time - start_time)
return jsonify(traffic_data)
@api.route('/v2/traffic/end/', methods = ['GET'])
def traffic_endStations():
#END ADDRESS LIST AND EXISTS
try:
if request.args.get('city'):
city = request.args.get('city')
else:
city = "Delhi"
except ValueError as valueerr:
print("ValueError has been raised." )
print(valueerr)
except TypeError as typeerr:
print("Type error has been raised.")
print(typeerr)
if city == "Delhi":
conn = DelhiTraffic
elif city == "Banglore":
conn = BangloreTraffic
elif city == "Mumbai":
conn = MumbaiTraffic
else:
conn = DelhiTraffic
Validation.city_validation(conn, city)
data_db = conn.query.distinct(conn.end_address).group_by(conn.end_address)
print(type(data_db))
print(data_db.count())
#print(len(data_db))
if(data_db.count() == 0):
raise NotFoundError({'city' : city}, message = 'This city doesn\'t match any record')
else:
start_time = time.time()
data = { i+1 : data_db[i].end_address for i in range(data_db.count())}
traffic_data = {'start_stations' : data}
print(traffic_data)
del (data)
end_time = time.time()
print("time taken ", end_time - start_time)
return jsonify(traffic_data)
@api.route('/v2/traffic/start/', methods = ['GET'])
def traffic_startStations():
#START ADDRESS LIST AND EXISTS
try:
if request.args.get('city'):
city = request.args.get('city')
else:
city = "Delhi"
except ValueError as valueerr:
print("ValueError has been raised." )
print(valueerr)
except TypeError as typeerr:
print("Type error has been raised.")
print(typeerr)
if city == "Delhi":
conn = DelhiTraffic
elif city == "Banglore":
conn = BangloreTraffic
elif city == "Mumbai":
conn = MumbaiTraffic
else:
conn = DelhiTraffic
Validation.city_validation(conn, city)
data_db = conn.query.distinct(conn.start_address).group_by(conn.start_address)
print(type(data_db))
print(data_db.count())
#print(len(data_db))
if(data_db.count() == 0):
raise NotFoundError({'city' : city}, message = 'This city doesn\'t match any record')
else:
start_time = time.time()
data = { i+1 : data_db[i].start_address for i in range(data_db.count())}
traffic_data = {'start_stations' : data}
print(traffic_data)
del (data)
end_time = time.time()
print("time taken ", end_time - start_time)
return jsonify(traffic_data)
@api.route('/v2/traffic/add/', methods = ['POST'])
def traffic_add():
if not request.json or not 'date_time' in request.json or not 'city' in request.json:
abort (400)
if request.args.get('date_time'):
date_time = request.json['date_time']
city = request.json['city']
distance = request.json['distance']
duration = request.json['duration']
duration_traffic = request.json['duration_traffic']
end_address = request.json['end_address']
start_address = request.json['start_address']
speed_no_traffic = request.json['speed_no_traffic']
speed_w_traffic = request.json['speed_w_traffic']
#timestamp and human time stamp is remaining
@api.route('/v1/<string:City>/<string:from_station>/<string:to_station>/<string:date_time>/', methods = ['GET'])
def traffic1(City, from_station, to_station, date_time):
if City == "Delhi":
conn = DelhiTraffic
elif City == "Banglore":
conn = BangloreTraffic
elif City == "Mumbai":
conn = MumbaiTraffic
else :
conn = DelhiTraffic
print(type(date_time))
print(date_time)
data_db = conn.query.filter(conn.start_address.like('%'+from_station+'%'),
conn.end_address.like('%'+to_station+'%'), conn.t == date_time).all()
print(len(data_db))
print(len(data_db))
print((data_db[0]))
data = {}
for i in range(len(data_db)):
print(i)
data[i + 1] = {'Distance': data_db[i].distance, 'Duration' : data_db[i].duration,
'Duration_Traffic' : data_db[i].duration_traffic, 'End_Address' : data_db[i].end_address,
'Start_Address' : data_db[i].start_address, 'Speed_Without_Traffic' : data_db[i].speed_no_traffic,
'Speed_With_Traffic' : data_db[i].speed_w_traffic, 'Time' : data_db[i].t_human,
'Route' : data_db[i].route}
print(data_db[i])
traffic_data = {'traffic_data' : data}
del (data)
return jsonify(traffic_data)
#@api.errorhandler(400)
@api.errorhandler(ValidationError)
def handle_ValidationError(error):
response = jsonify(error.to_dict())
return response
@api.errorhandler(404)
def NotFoundDefault(error):
print(error)
#response = jsonify(error.to_dict())
return error
@api.errorhandler(NotFoundError)
def handle_NotFoundError(error):
print(error)
response = jsonify(error.to_dict())
return response
@api.errorhandler
def default_error_handler(error):
error = ServerError()
return error.to_dict(), getattr(error, 'code', 500)
if __name__ == '__main__':
db.init_app(api)
api.run(debug = True)
#http://127.0.0.1:5000/v1/traffic/?city=Delhi&date_time=201706130430&from_station=MSIL%20Internal%20Rd,%20Maruti%20Udyog,%20Sector%2018,%20Gurugram,%20Haryana%20122008,%20India&to_station=%27Civil%20Lines,%20Gurugram,%20Haryana%20122022,%20India%27
#http://127.0.0.1:5000/v1/Delhi/West%20Delhi,%20New%20Delhi,%20Delhi%20110009,%20India/Outer%20Ring%20Rd,%20Delhi,%20India/201704051500/
#http://127.0.0.1:5000/v2/traffic/?city=Mumbai&date_time=201706130430&start_station=Teen Murti Marg, Teen Murti Bhavan, New Delhi, Delhi 110011, India&end_station=Terminal 1D Departure Rd, Indira Gandhi International Airport, New Delhi, Delhi, India