Skip to content

Commit

Permalink
have location service send routemodel id to mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-lam committed Nov 7, 2023
1 parent 6bc3c9b commit 29122dd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 44 deletions.
8 changes: 0 additions & 8 deletions location_service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
FROM python:3.11.4-buster
RUN pip install --upgrade cython pip

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

COPY requirements.txt ./
Expand Down
50 changes: 16 additions & 34 deletions location_service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,6 @@

This service acts as an entrypoint for the car to the microservices. The car will send it's coordinates and this service will map the car's coordinates to the routemodel coordinates. Then this service will send a request containing the car's raw data and routemodel coordinates to the historical car/MongoDB service.

**TODO: API request from location service to historical car/MongoDB service**

---

## Docker Commands

### Build image

Make sure you have the docker daemon running and add your `DATABASE_URI` string as an environment variable in the `Dockerfile`. Make sure you're in the `location_service` directory and run:

```
docker build . -t location_service
```

### Run image

To run the service, run:

```
docker run -p 5000:5000 location_service
```

and the service will be running on [localhost:5000](http://localhost:5000/)

---

## Usage (Endpoints)
Expand Down Expand Up @@ -57,16 +33,22 @@ and returns the coordinate (row) in the `location_service` database that is clos

---

## Other: Command Line Args
## Docker Commands

### Build image

Make sure you have the docker daemon running. Make sure you're in the `location_service` directory and run:

```
docker build . -t location_service
```

### Run image

To run the service, run:

```
arg="--create-table",
type=bool,
help="If you want a table to be created in the database",
required=False,
arg="--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,
docker run -p 5000:5000 location_service
```

and the service will be running on [localhost:5000](http://localhost:5000/)
33 changes: 31 additions & 2 deletions location_service/src/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import json
import os
import time

import pymongo
from dotenv import load_dotenv
from flask import Flask, request
from psycopg2 import pool as psycopg2_pool


# Env vars
load_dotenv()
REQUIRE_AUTH = os.getenv("REQUIRE_AUTH", "False").lower() in ("true", "1", "t")
AUTH_KEY = os.environ.get("AUTH_KEY")
Expand All @@ -13,8 +17,12 @@
DB_NAME = os.environ.get("DB_NAME")
DB_USER = os.environ.get("DB_USER")
DB_PASSWORD = os.environ.get("DB_PASSWORD")
MONGODB_CONNECTION_STRING = os.environ.get("MONGODB_CONNECTION_STRING")
MONGODB_NAME = os.environ.get("MONGODB_NAME")
MONGODB_COLLECTION = os.environ.get("MONGODB_COLLECTION")


# Create dbv connections
app = Flask(__name__)
postgres_pool = psycopg2_pool.SimpleConnectionPool(
minconn=1,
Expand All @@ -25,6 +33,9 @@
user=DB_USER,
password=DB_PASSWORD,
)
client = pymongo.MongoClient(MONGODB_CONNECTION_STRING)
db = client[MONGODB_NAME]
collection = db[MONGODB_COLLECTION]


def authorized(auth_key):
Expand Down Expand Up @@ -66,18 +77,36 @@ def closest_location():
if lat is None or lon is None:
return "Invalid request body", 400

query = f"""
query_location = f"""
SELECT *
FROM location_service
ORDER BY location_service.geo <-> ST_SetSRID(ST_MakePoint({lon}, {lat}), 4326)
LIMIT 1;
"""
insert_location_logs = f"""
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
"""
conn = postgres_pool.getconn()
cur = conn.cursor()
cur.execute(query)
cur.execute(query_location)
location = dict(zip(("id", "lat", "lon"), cur.fetchone()))
cur.close()
postgres_pool.putconn(conn)

res = collection.update_one(
{"config_type": "location_service"},
{
"$set": {
"routemodel_id": location["id"],
"location_service_log_id": 0,
"updated_at": int(time.time()),
"active": True,
}
},
upsert=True,
)
print(res)
return location


Expand Down

0 comments on commit 29122dd

Please sign in to comment.