diff --git a/location_service/.env.sample b/location_service/.env.sample index f377940..9a802ad 100644 --- a/location_service/.env.sample +++ b/location_service/.env.sample @@ -1,2 +1,8 @@ DATABASE_URI="postgresql+psycopg2://user:password@host:port/database" +DB_HOST="" +DB_NAME="" +DB_USER="" +DB_PASSWORD="" +DB_PORT="" +REQUIRE_AUTH="" AUTH_KEY="" diff --git a/location_service/Dockerfile b/location_service/Dockerfile index 6726233..da84922 100644 --- a/location_service/Dockerfile +++ b/location_service/Dockerfile @@ -1,7 +1,12 @@ FROM python:3.11.4-buster RUN pip install --upgrade cython pip -ENV DATABASE_URI="" +ENV DB_HOST="" +ENV DB_NAME="" +ENV DB_USER="" +ENV DB_PASSWORD="" +ENV DB_PORT="" +ENV REQUIRE_AUTH="" ENV AUTH_KEY="" WORKDIR /usr/location_service @@ -13,4 +18,4 @@ COPY . . EXPOSE 5000 -CMD [ "python", "src/main.py", "--create-table", "true", "--seed-filename", "./uw_sample_gpx.csv" ] +CMD [ "python", "src/main.py" ] diff --git a/location_service/requirements.txt b/location_service/requirements.txt index 97e0e73..bcd3089 100644 --- a/location_service/requirements.txt +++ b/location_service/requirements.txt @@ -1,22 +1,10 @@ -blinker==1.6.2 -click==8.1.5 +blinker==1.7.0 +click==8.1.7 colorama==0.4.6 -Flask==2.3.2 -Flask-SQLAlchemy==3.0.5 -GeoAlchemy2==0.14.0 -greenlet==2.0.2 +Flask==3.0.0 itsdangerous==2.1.2 Jinja2==3.1.2 MarkupSafe==2.1.3 -numpy==1.25.1 -packaging==23.1 -pandas==2.0.3 -psycopg2==2.9.6 -python-dateutil==2.8.2 +psycopg2==2.9.9 python-dotenv==1.0.0 -pytz==2023.3 -six==1.16.0 -SQLAlchemy==2.0.18 -typing_extensions==4.7.1 -tzdata==2023.3 Werkzeug==3.0.1 diff --git a/location_service/src/main.py b/location_service/src/main.py index dd7b5f4..8cc39c2 100644 --- a/location_service/src/main.py +++ b/location_service/src/main.py @@ -1,81 +1,35 @@ import json import os -from argparse import ArgumentParser -import pandas as pd from dotenv import load_dotenv from flask import Flask, request -from flask_sqlalchemy import SQLAlchemy -from geoalchemy2 import Geometry -from geoalchemy2.comparator import Comparator -from sqlalchemy import asc, func +from psycopg2 import pool as psycopg2_pool load_dotenv() -DATABASE_URI = os.environ.get("DATABASE_URI") +REQUIRE_AUTH = os.getenv("REQUIRE_AUTH", "False").lower() in ("true", "1", "t") AUTH_KEY = os.environ.get("AUTH_KEY") - - -parser = ArgumentParser() -parser.add_argument( - "--create-table", - type=bool, - help="If you want a table to be created in the database", - required=False, -) -parser.add_argument( - "--seed-filename", - type=str, - help="String of the filepath of the csv file if you want to seed the database with the csv file contents", - required=False, -) -args = parser.parse_args() -args = vars(args) +DB_HOST = os.environ.get("DB_HOST") +DB_PORT = os.environ.get("DB_PORT") +DB_NAME = os.environ.get("DB_NAME") +DB_USER = os.environ.get("DB_USER") +DB_PASSWORD = os.environ.get("DB_PASSWORD") app = Flask(__name__) -app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI -app.config["SQLALCHEMY_ECHO"] = False -app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False -db = SQLAlchemy(app) -app.app_context().push() - - -class Location(db.Model): - __tablename__ = "location_test" # Change table name - - id = db.Column(db.Integer, primary_key=True) - lat = db.Column(db.Float) - lon = db.Column(db.Float) - geo = db.Column(Geometry(geometry_type="POINT", srid=4326)) - - def to_dict(self): - return { - "id": self.id, - "lat": self.lat, - "lon": self.lon, - } - - @staticmethod - def create(lat, lon): - geo = "POINT({} {})".format(lon, lat) # Note that postgis is (lon, lat) - location = Location(lat=lat, lon=lon, geo=geo) - db.session.add(location) - db.session.commit() - - @staticmethod - def seed_from_csv(filename): - df = pd.read_csv(filename) - db.session.query(Location).delete() - for row in df.itertuples(): - geo = "POINT({} {})".format( - row.lon, row.lat - ) # Note that postgis is (lon, lat) - location = Location(id=row.Index + 1, lat=row.lat, lon=row.lon, geo=geo) - db.session.add(location) - db.session.commit() +postgres_pool = psycopg2_pool.SimpleConnectionPool( + minconn=1, + maxconn=10, + host=DB_HOST, + port=DB_PORT, + database=DB_NAME, + user=DB_USER, + password=DB_PASSWORD, +) def authorized(auth_key): + if not REQUIRE_AUTH: + return True return auth_key == AUTH_KEY @@ -84,14 +38,21 @@ def index(): return "location service" -@app.route("/all", methods=["GET"]) +@app.route("/all", methods=["GET", "POST"]) def all(): body = json.loads(request.data) auth_key = body.get("auth_key", None) if not authorized(auth_key): return "Not authorized", 401 - locations = db.session.query(Location).order_by(asc(Location.id)) - return [l.to_dict() for l in locations] + + query = "SELECT id, lat, lon FROM location_service;" + conn = postgres_pool.getconn() + cur = conn.cursor() + cur.execute(query) + locations = [dict(zip(("id", "lat", "lon"), values)) for values in cur.fetchall()] + cur.close() + postgres_pool.putconn(conn) + return locations @app.route("/location", methods=["POST"]) @@ -104,27 +65,22 @@ def closest_location(): return "Not authorized", 401 if lat is None or lon is None: return "Invalid request body", 400 - location = ( - db.session.query(Location) - .order_by( - Comparator.distance_centroid( - Location.geo, - func.Geometry( - func.ST_GeographyFromText( - "POINT({} {})".format(lon, lat) - ) # Note that postgis is (lon, lat) - ), - ) - ) - .limit(1) - .first() - ) - return location.to_dict() + + query = f""" + SELECT * + FROM location_service + ORDER BY location_service.geo <-> ST_SetSRID(ST_MakePoint({lon}, {lat}), 4326) + LIMIT 1; + """ + conn = postgres_pool.getconn() + cur = conn.cursor() + cur.execute(query) + location = dict(zip(("id", "lat", "lon"), cur.fetchone())) + print(location) + cur.close() + postgres_pool.putconn(conn) + return location if __name__ == "__main__": - if args["create_table"]: - db.create_all() - if args["seed_filename"]: - Location.seed_from_csv(args["seed_filename"]) - app.run(debug=False, host="0.0.0.0", port=5000) + app.run(debug=True, host="0.0.0.0", port=5000) diff --git a/location_service/uw_sample_gpx.csv b/location_service/uw_sample_gpx.csv deleted file mode 100644 index 0e156be..0000000 --- a/location_service/uw_sample_gpx.csv +++ /dev/null @@ -1,140 +0,0 @@ -,lon,lat,type,step,next_turn,dir,dist_to_next_m,elapsed_dist_m,time_to_next_s -0,-80.53641449999999,43.472216,waypoint,,,Head northwest on Phillip St,605.0,0.0,115.0 -1,-80.53648,43.4723,,,,,,, -2,-80.53657,43.47242,,,,,,, -3,-80.53667,43.47256,,,,,,, -4,-80.53682,43.47276,,,,,,, -5,-80.53699,43.47297,,,,,,, -6,-80.5372,43.47326,,,,,,, -7,-80.53725,43.47332,,,,,,, -8,-80.53755,43.47374,,,,,,, -9,-80.53769,43.47391,,,,,,, -10,-80.53786,43.47413,,,,,,, -11,-80.53794,43.47424,,,,,,, -12,-80.53809,43.47443,,,,,,, -13,-80.53821,43.47461,,,,,,, -14,-80.53845,43.47493,,,,,,, -15,-80.53855,43.47508,,,,,,, -16,-80.53877,43.47537,,,,,,, -17,-80.5389,43.47556,,,,,,, -18,-80.53922,43.47599,,,,,,, -19,-80.53929,43.47608,,,,,,, -20,-80.5393,43.47609,,,,,,, -21,-80.53946,43.47631,,,,,,, -22,-80.53949,43.47635,,,,,,, -23,-80.53972,43.47667,,,,,,, -24,-80.53977,43.47673,,,,,,, -25,-80.53983,43.4768,,,,,,, -26,-80.53984,43.47682,,,,,,, -27,-80.53997,43.477,,,,,,, -28,-80.5399735,43.477,waypoint,turn-left,turn-left,Turn left onto Columbia St W,1629.0,605.0,150.0 -29,-80.54021,43.47692,,,,,,, -30,-80.54146,43.47645,,,,,,, -31,-80.54204,43.47625,,,,,,, -32,-80.54263,43.47605,,,,,,, -33,-80.54275,43.47605,,,,,,, -34,-80.54294,43.47598,,,,,,, -35,-80.54322,43.47589,,,,,,, -36,-80.54412,43.47559,,,,,,, -37,-80.54553,43.47509,,,,,,, -38,-80.54631,43.47482,,,,,,, -39,-80.54653,43.47476,,,,,,, -40,-80.54666,43.47472,,,,,,, -41,-80.54728,43.4745,,,,,,, -42,-80.54811,43.47417,,,,,,, -43,-80.54905,43.47383,,,,,,, -44,-80.54907,43.47382,,,,,,, -45,-80.55024,43.47339,,,,,,, -46,-80.5504,43.47334,,,,,,, -47,-80.55109,43.47309,,,,,,, -48,-80.55123,43.47304,,,,,,, -49,-80.55345,43.47225,,,,,,, -50,-80.55501,43.47169,,,,,,, -51,-80.55631,43.47122,,,,,,, -52,-80.55697,43.47098,,,,,,, -53,-80.5579,43.47064,,,,,,, -54,-80.55792,43.47063,,,,,,, -55,-80.5581,43.47057,,,,,,, -56,-80.558103,43.4705729,waypoint,turn-left,turn-left,Turn left onto Westmount Rd N,1624.0,2234.0,128.0 -57,-80.5583,43.47052,,,,,,, -58,-80.55812,43.47044,,,,,,, -59,-80.55734,43.46999,,,,,,, -60,-80.5569,43.46976,,,,,,, -61,-80.55651,43.46958,,,,,,, -62,-80.55631,43.46949,,,,,,, -63,-80.55606,43.46939,,,,,,, -64,-80.55573,43.46927,,,,,,, -65,-80.55539,43.46917,,,,,,, -66,-80.5551,43.46908,,,,,,, -67,-80.55479,43.46901,,,,,,, -68,-80.55443,43.46895,,,,,,, -69,-80.55414,43.46892,,,,,,, -70,-80.55385,43.4689,,,,,,, -71,-80.55347,43.4689,,,,,,, -72,-80.55309,43.46893,,,,,,, -73,-80.55221,43.46901,,,,,,, -74,-80.55184,43.46906,,,,,,, -75,-80.55146,43.46912,,,,,,, -76,-80.55113,43.46914,,,,,,, -77,-80.5509,43.46915,,,,,,, -78,-80.55067,43.46914,,,,,,, -79,-80.55047,43.46912,,,,,,, -80,-80.55024,43.46909,,,,,,, -81,-80.55002,43.46905,,,,,,, -82,-80.54992,43.46902,,,,,,, -83,-80.54975,43.46897,,,,,,, -84,-80.54959,43.46892,,,,,,, -85,-80.54946,43.46886,,,,,,, -86,-80.54923,43.46872,,,,,,, -87,-80.54909,43.46866,,,,,,, -88,-80.54881,43.46851,,,,,,, -89,-80.54858,43.46834,,,,,,, -90,-80.54797,43.46774,,,,,,, -91,-80.54792,43.4677,,,,,,, -92,-80.54754,43.4673,,,,,,, -93,-80.54728,43.46703,,,,,,, -94,-80.54725,43.467,,,,,,, -95,-80.5471,43.46686,,,,,,, -96,-80.54695,43.46672,,,,,,, -97,-80.54694,43.46672,,,,,,, -98,-80.5468,43.46658,,,,,,, -99,-80.54678,43.46656,,,,,,, -100,-80.5466,43.46643,,,,,,, -101,-80.54635,43.46625,,,,,,, -102,-80.54624,43.46619,,,,,,, -103,-80.54604,43.46607,,,,,,, -104,-80.54587,43.46599,,,,,,, -105,-80.54567,43.46589,,,,,,, -106,-80.54542,43.46578,,,,,,, -107,-80.54484,43.46551,,,,,,, -108,-80.54375,43.465,,,,,,, -109,-80.54367,43.46496,,,,,,, -110,-80.54303,43.46468,,,,,,, -111,-80.54282,43.46459,,,,,,, -112,-80.54193,43.46431,,,,,,, -113,-80.54156,43.46421,,,,,,, -114,-80.5415593,43.4642077,waypoint,turn-left,turn-left,Turn left onto University Ave W,942.0,3858.0,122.0 -115,-80.54145,43.46415,,,,,,, -116,-80.54139,43.46426,,,,,,, -117,-80.54124,43.46442,,,,,,, -118,-80.54065,43.46556,,,,,,, -119,-80.54002,43.46679,,,,,,, -120,-80.53998,43.46686,,,,,,, -121,-80.53943,43.46791,,,,,,, -122,-80.53904,43.46866,,,,,,, -123,-80.53899,43.46876,,,,,,, -124,-80.53856,43.46961,,,,,,, -125,-80.53819,43.47031,,,,,,, -126,-80.53813,43.47043,,,,,,, -127,-80.53797,43.47073,,,,,,, -128,-80.53786,43.4709,,,,,,, -129,-80.53781,43.47098,,,,,,, -130,-80.53774,43.47107,,,,,,, -131,-80.53764,43.4713,,,,,,, -132,-80.53748,43.47143,,,,,,, -133,-80.53741,43.47149,,,,,,, -134,-80.53733,43.47155,,,,,,, -135,-80.53715,43.47167,,,,,,, -136,-80.53701,43.47174,,,,,,, -137,-80.53694,43.47178,,,,,,, -138,-80.5369395,43.4717778,waypoint,,,,,,