From f1c12d5bcda5d0ee29cf86929aad10ddd78d8748 Mon Sep 17 00:00:00 2001 From: Mason DeMelo Date: Wed, 8 May 2019 16:39:17 -0400 Subject: [PATCH] Updates --- API/apache_conf.conf | 13 +++ API/api.py | 70 ++++++++------ API/db/test.db | Bin 12288 -> 12288 bytes API/mrd14005@sdp40.cse.uconn.edu | 160 +++++++++++++++++++++++++++++++ API/post_test.py | 2 +- 5 files changed, 216 insertions(+), 29 deletions(-) create mode 100644 API/apache_conf.conf create mode 100644 API/mrd14005@sdp40.cse.uconn.edu diff --git a/API/apache_conf.conf b/API/apache_conf.conf new file mode 100644 index 0000000..b94ff50 --- /dev/null +++ b/API/apache_conf.conf @@ -0,0 +1,13 @@ + + ServerName sdp40.cse.uconn.edu + + LoadModule wsgi_module "/var/www/api/env/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so" + + WSGIDaemonProcess HuskyTrackAPI threads=1 + WSGIProcessGroup HuskyTrackAPI + WSGIScriptAlias /api /var/www/api/HuskyTrackAPI.wsgi process-group=HuskyTrackAPI application-group=%{GLOBAL} + + Order allow,deny + Allow from all + + \ No newline at end of file diff --git a/API/api.py b/API/api.py index 98fbba9..a8c170e 100644 --- a/API/api.py +++ b/API/api.py @@ -3,7 +3,7 @@ import sys import json from datetime import datetime -from flask import Flask, request +from flask import Flask, request, g from DataObjects import LocationData from sqlalchemy import create_engine @@ -54,12 +54,20 @@ def get_session(config): # Create session engine = create_engine(url) Session = sessionmaker(bind=engine) - session = Session() + session = getattr(g, '_session', None) + if session is None: + session = Session() return session except Exception as e: print("Unable to configure database connection. Error:", e) sys.exit() + +@app.route('/', methods=('GET', 'POST')) +def test(): + if request.method == 'POST': + print (request.get_json()) + return "Success" @app.route('/api') def api(): @@ -78,27 +86,28 @@ def location_data(): if request.is_json: try: raw_location_data = request.get_json() - location = raw_location_data["location"] - coords = location["coords"] - lat = coords["latitude"] - lng = coords["longitude"] - acc = coords["accuracy"] - spd = coords["speed"] - hed = coords["heading"] - alt = coords["altitude"] - tim = location["timestamp"] - uuid = location["uuid"] - uid = "1" - locationData = LocationData(uuid = uuid, - uid = uid, - latitude = lat, - longitude = lng, - accuracy = acc, - speed = spd, - heading = hed, - altitude = alt, - timestamp = tim) - session.add(locationData) + locations = raw_location_data["location"] + uid = raw_location_data["user_id"] + for location in locations: + coords = location["coords"] + lat = coords["latitude"] + lng = coords["longitude"] + acc = coords["accuracy"] + spd = coords["speed"] + hed = coords["heading"] + alt = coords["altitude"] + tim = location["timestamp"] + uuid = location["uuid"] + locationData = LocationData(uuid = uuid, + uid = uid, + latitude = lat, + longitude = lng, + accuracy = acc, + speed = spd, + heading = hed, + altitude = alt, + timestamp = tim) + session.add(locationData) session.commit() return "Success" except Exception as e: @@ -107,7 +116,7 @@ def location_data(): else: return "NOT JSON" else: - try: + if 'start' and 'end' in request.args: start = request.args.get('start') end = request.args.get('end') @@ -138,9 +147,14 @@ def location_data(): result.append(json_entry) return json.dumps(result) - except Exception as e: - print(e) + else: return json.dumps([[l.uuid, l.latitude, l.longitude, l.timestamp] for l in session.query(LocationData).all()]) - + +@app.teardown_appcontext +def close_connection(exception): + session = getattr(g, '_session', None) + if session is not None: + session.close() + if __name__ == '__main__': - app.run(debug=True) \ No newline at end of file + app.run(host='0.0.0.0', port=44) diff --git a/API/db/test.db b/API/db/test.db index 759bb0b12072deac94e806d0bee0c140298378b7..2b4b9d110113099529bdd5b9327dded490b50ccb 100644 GIT binary patch delta 1099 zcma))J7^S96oxZI*z99w3}O&m1QP@kyz6~G7FGsgp%#LVZ8iwGbLWlSqC@_c*0GjXRn>r8a! zANiAhacRU~@jdUJS9Kq{GnGz-mp_%Sm&Z%bN~elnic6h^W-$r)rB z9s{Vf1ypbhv?LL*h+-jm$fYtUIG!%fzPa(^)!Fgo^{FS*!4cA%G)*x?3P1{wIFD4F zh&rJ)j%n+%x4|eh45XTd0)ohD3}_NV0#u|7ASUA0Pz@!Ia>&(lquo6jBzb!J``chD z?oFBo@s)Skyx-1(xQ;|k5Z1cn_E}?2p%D`p%Q%!kTT20zHp)=a04O6ap|--pMD$qA zm7gDT*4np~U%|mYg8qeq5VqzjJyhnL)6kehq!j}(CJ4~jYM=-;04v6rR!|Abd)D22 ze4vtB_u%B2Tf5UV888i^I+HcQXe%slnC3K?jakT<21ugpI~EuyDhx2xqR=QATh-IC zxb=H*^nO081Hs5TqjgAXg!0y0DcO`DnYKtUp4p=~pcX2iSQG(DDdR+FZ7A7E!Zro{ z?>JKcTVZiiK^TQ9<_ZHtas@EkR7@-gG&7nYBO!+U@B0q5ySYKn$Lw@4^EU~oPFW3e z(dzQ*htWyjT^;eu{%wEF`|2I@Kl{|X=&pJ%{R`e+ukF5e*E+`H}Cu4I^!`-exH y&OlRlEUK3u4pB4V=7)JnlK-`&l3xxJcsV~DCbUw1xDh~gFoCTBbYIj?IKKfaU^6ZN delta 202 zcmZojXh@hK&B#Ad#+i|SW5N=CF%BLx2L5(_KEA1ZGQ5j start, LocationData.timestamp < end).all() + + dataPoints = defaultdict(list) + for result in query_results: + dataPoints[result.uid].append(result) + + result = [] + for user in dataPoints: + print(user) + json_entry = json.loads('{}') + json_entry['user_id'] = user + json_entry['start_time'] = min(dataPoints[user], key=lambda x:x.timestamp).timestamp + json_entry['end_time'] = max(dataPoints[user], key=lambda x:x.timestamp).timestamp + json_entry['locations'] = [] + for location in dataPoints[user]: + json_location = json.loads('{}') + json_location['latitude'] = location.latitude + json_location['longitude'] = location.longitude + json_location['timestamp'] = location.timestamp + json_entry['locations'].append(json_location) + result.append(json_entry) + + return json.dumps(result) + else: + return json.dumps([[l.uuid, l.latitude, l.longitude, l.timestamp] for l in session.query(LocationData).all()]) + +@app.teardown_appcontext +def close_connection(exception): + session = getattr(g, '_session', None) + if session is not None: + session.close() + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=44) diff --git a/API/post_test.py b/API/post_test.py index c2e3836..f0f47a1 100644 --- a/API/post_test.py +++ b/API/post_test.py @@ -36,7 +36,7 @@ data = { } } -req = urllib2.Request('http://127.0.0.1:5000/api/locationdata') +req = urllib2.Request('http://sdp40.cse.uconn.edu/api/locationdata') req.add_header('Content-Type', 'application/json') response = urllib2.urlopen(req, json.dumps(data))