Skip to content

Commit

Permalink
added more informative error messages for db server,
Browse files Browse the repository at this point in the history
decreased server timeout
updated api to not delete and reinsert duplicate trips
fixed a loggin bug when exception occured
  • Loading branch information
JonnoFTW committed Dec 18, 2017
1 parent 1885cf1 commit b23bf31
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
3 changes: 3 additions & 0 deletions tests/test_json.http
@@ -0,0 +1,3 @@
http://localhost:6767/

###
4 changes: 2 additions & 2 deletions webcan/__init__.py
Expand Up @@ -61,8 +61,8 @@ def main(global_config, **settings):

def add_db(request):
conn = MongoClient(db_url.geturl(),
serverSelectionTimeoutMS=10000,
connectTimeoutMS=10000,
serverSelectionTimeoutMS=3000,
connectTimeoutMS=3000,
socketTimeoutMS=10000,
maxPoolSize=200,
maxIdleTimeMs=30000,
Expand Down
14 changes: 14 additions & 0 deletions webcan/api.py
Expand Up @@ -28,6 +28,7 @@ def upload_vehicle(request):
rows = []
# create a new trip_id based off the first GPS time reading
timestamps = {}
new_trip = True
for row in gzip.decompress(base64.b64decode(data64)).decode('ascii').splitlines():
try:
js = json.loads(row)
Expand All @@ -36,13 +37,22 @@ def upload_vehicle(request):
continue
if 'vid' not in js or 'trip_id' not in js:
return HTTPBadRequest("Please provide a vid and trip_id on every row")

if 'timestamp' in js and js['timestamp'] is not None:
js['timestamp'] = dateutil.parser.parse(js['timestamp'])

if js['trip_id'] not in timestamps:
try:
timestamps[js['trip_id']] = js['timestamp'].astimezone(tz_local).strftime("%Y%m%d_%H%M%S_{}".format('_'.join(js['trip_id'].split('_')[2:])))
# print("Changing {} to {}".format(js['trip_id'], timestamps[js['trip_id']]))
if new_trip:
if request.db.rpi_readings.find_one({'trip_id': timestamps[js['trip_id']]}) is not None:
print("Skipping:", js['trip_id'])
return {'msg': "Already seen this trip, skipping"}
else:
# we can go ahead with adding this trip
print("Adding", js['trip_id'])
new_trip = False
except:
pass
else:
Expand All @@ -56,6 +66,10 @@ def upload_vehicle(request):
return HTTPForbidden('You must provide a valid API key for all Vehicle IDs used')
for row in rows:
row['trip_id'] = timestamps[row['trip_id']]
if len(rows) < 30:
return {
'msg': 'Trip is too short to be of use and was rejected'
}
request.db.rpi_readings.remove({'trip_id': {'$in': list(trips)+list(timestamps.values())}})
if not rows:
return {'inserted': 0}
Expand Down
5 changes: 4 additions & 1 deletion webcan/login.py
Expand Up @@ -13,7 +13,10 @@ def check_logged_in(event):
# if the user is not logged in and tries to access anything but /login,
# redirect to /loging or send ajax error about not being logged in
req = event.request
req.environ['REMOTE_USER'] = req.authenticated_userid
try:
req.environ['REMOTE_USER'] = req.authenticated_userid
except:
pass
if req.path in ('/login', '/logout', '/api/upload', '/reset_password'):
return
if not req.user:
Expand Down
9 changes: 5 additions & 4 deletions webcan/templates/exceptions/503.mako
@@ -1,13 +1,14 @@
<%inherit file="../layout.mako"/>
<%inherit file="../simple/layout.mako"/>
<div class="content">
<div class="row justify-content-md-center">
<div class="col-6 col-">
<div class="card card-inverse card-danger">
<div class="card-header ">
<h3>503: Service Service Unavailable</h3>
<div class="card-header text-center">
<h3>503: Service Unavailable</h3>
</div>
<div class="card-body">
The server is currently unavailable. Please try again at a later time
The server is currently unavailable. Please try again at a later time <br>
<b>Error:</b> ${msg}
</div>
</div>
</div>
Expand Down
11 changes: 10 additions & 1 deletion webcan/views.py
Expand Up @@ -33,6 +33,8 @@ def _get_user_devices(request):

@subscriber(BeforeRender)
def add_device_global(event):
if event['request'].exception:
return
if event['renderer_info'].type == '.mako':
event['_pid'] = os.getpid()
event['_host'] = platform.node()
Expand Down Expand Up @@ -198,7 +200,14 @@ def notfound(request):

@exception_view_config(pymongo.errors.NetworkTimeout, renderer='templates/exceptions/503.mako')
def service_unavailable(request):
return {'msg'}
return {'msg': 'Server too busy or your query took too long'}

@exception_view_config(pymongo.errors.ConnectionFailure, renderer='templates/exceptions/503.mako')
def connection_failure(request):
return {'msg': 'Server connection timeout'}
@exception_view_config(pymongo.errors.ServerSelectionTimeoutError, renderer='templates/exceptions/503.mako')
def service_unselectable(request):
return {'msg': 'Can\'t find server'}


@view_config(context=exc.HTTPBadRequest)
Expand Down

0 comments on commit b23bf31

Please sign in to comment.