Skip to content

Commit

Permalink
Merge pull request #323 from LACMTA/dev
Browse files Browse the repository at this point in the history
`rider usage code` added to api
  • Loading branch information
albertkun authored Oct 5, 2023
2 parents 95cfc64 + abe4e96 commit 17bf58d
Show file tree
Hide file tree
Showing 11 changed files with 13,846 additions and 118 deletions.
6 changes: 5 additions & 1 deletion data-loading-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:1.3
FROM python:3.6
FROM python:3.11

#
# WORKDIR /code
Expand All @@ -11,6 +11,10 @@ COPY ./requirements.txt requirements.txt
# RUN --mount=type=secret,id=ftp_server ./use-secret.sh
# RUN --mount=type=secret,id=ftp_server ./use_secret.sh
# RUN pip install --upgrade cython

RUN apt-get update && apt-get install -y \
libgeos-dev

RUN pip install --no-cache-dir --upgrade -r requirements.txt
#

Expand Down
8 changes: 4 additions & 4 deletions data-loading-service/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# fastapi==0.63.0
pandas
## Requirements for Python 3.11
pydantic
# starlette==0.13.6
# typing-extensions==3.7.4.3
requests
python-dotenv
# pytz
# python-multipart
SQLAlchemy
psycopg2
schedule
geopandas
GeoAlchemy2==0.10.2
GeoAlchemy2
pandas==1.1.5
SQLAlchemy==1.4.45
# python-jose[cryptography]
# passlib[bcrypt]
# # GeoAlchemy2
Expand Down
9 changes: 8 additions & 1 deletion fastapi/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:1.3
FROM python:3
FROM python:3.11

#
# WORKDIR /code
Expand All @@ -10,9 +10,16 @@ COPY ./requirements.txt requirements.txt
# COPY use-secret.sh .
# RUN --mount=type=secret,id=ftp_server ./use-secret.sh
# RUN --mount=type=secret,id=ftp_server ./use_secret.sh

RUN apt-get update && apt-get install -y \
libgeos-dev


RUN pip install --no-cache-dir --upgrade -r requirements.txt
#



COPY ./app /app
# COPY ../appdata ../appdata
# COPY .git /code/.git
Expand Down
2 changes: 1 addition & 1 deletion fastapi/app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from . import models, schemas,gtfs_models
from .config import Config
from .database import Session,get_db,get_async_db
from .database import Session,get_db
from .utils.log_helper import *
from .utils.email_helper import *
from .utils.db_helper import *
Expand Down
2 changes: 0 additions & 2 deletions fastapi/app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ def create_async_uri(uri):


engine = create_engine(Config.API_DB_URI, echo=False, pool_pre_ping=True, pool_size=20, max_overflow=0)
async_engine = create_async_engine(create_async_uri(Config.API_DB_URI), echo=False)
async_session = sessionmaker(async_engine, expire_on_commit=False, class_=AsyncSession)
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# LocalSession = sessionmaker(autocommit=False, autoflush=True, bind=async_engine, expire_on_commit=True)
Expand Down
82 changes: 1 addition & 81 deletions fastapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from datetime import timedelta, date, datetime

from fastapi import FastAPI, Request, Response, Depends, HTTPException, status, WebSocket, WebSocketDisconnect
from fastapi import FastAPI, Request, Response, Depends, HTTPException, status
# from fastapi import FastAPI, Request, Response, Depends, HTTPException, status
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse, RedirectResponse, HTMLResponse,PlainTextResponse
Expand Down Expand Up @@ -303,43 +303,6 @@ async def get_canceled_trip_summary(db: Session = Depends(get_db)):
"total_canceled_trips":total_canceled_trips,
"last_updated":update_time}


# WebSockets
import random
@app.websocket("/live/get_time")
async def live_time_updates(websocket: WebSocket):
await websocket.accept()
while True:
payload = {"time": str(datetime.now()), "message": "Hello World!","value":random.randint(1,100)}
await websocket.send_json(payload)
await asyncio.sleep(10)

@app.websocket("/{agency_id}/live/trip_detail/route_code/{route_code}")
async def live_get_gtfs_rt_trip_details(websocket: WebSocket,agency_id: AgencyIdEnum, route_code: str, geojson:bool=False, db: Session = Depends(get_async_db)):
await websocket.accept()
async with db as session:
try:
while True:
async for result in crud.get_gtfs_rt_vehicle_positions_trip_data_by_route_code_for_async(session,route_code,geojson,agency_id.value):
await websocket.send_json(result)
await session.commit()
await asyncio.sleep(5)
except WebSocketDisconnect:
await websocket.close()

@app.websocket("/{agency_id}/live/line_detail_updates/{route_code}")
async def get_line_detail_updates_for_route_code(websocket: WebSocket,agency_id: AgencyIdEnum, route_code: str, geojson:bool=False, db: Session = Depends(get_async_db)):
await websocket.accept()
async with db as session:
try:
while True:
async for result in crud.get_gtfs_rt_line_detail_updates_for_route_code(session,route_code,geojson,agency_id.value):
await websocket.send_json(result)
await session.commit()
await asyncio.sleep(5)
except WebSocketDisconnect:
await websocket.close()

@app.get("/{agency_id}/trip_detail_route_code/{route_code}",tags=["Real-Time data"])
async def get_trip_detail_and_route_code(agency_id: AgencyIdEnum, route_code: str, geojson:bool=False,db: Session = Depends(get_db)):
result_array = []
Expand Down Expand Up @@ -478,49 +441,6 @@ async def get_time():
# @app.get("/agencies/")
# async def root():

# WebSockets
import random
@app.websocket("/live/get_time")
async def live_time_updates(websocket: WebSocket):
await websocket.accept()
while True:
payload = {"time": str(datetime.now()), "message": "Hello World!","value":random.randint(1,100)}
await websocket.send_json(payload)
await asyncio.sleep(10)

@app.websocket("/{agency_id}/live/trip_detail/route_code/{route_code}")
async def live_get_gtfs_rt_trip_details(websocket: WebSocket,agency_id: AgencyIdEnum, route_code: str, geojson:bool=False, db: Session = Depends(get_async_db)):
await websocket.accept()
async with db as session:
try:
while True:
async for result in crud.get_gtfs_rt_vehicle_positions_trip_data_by_route_code_for_async(session,route_code,geojson,agency_id.value):
await websocket.send_json(result)
await session.commit()
await asyncio.sleep(5)
except WebSocketDisconnect:
await websocket.close()
# result_array = []
# result_array = crud.get_gtfs_rt_vehicle_positions_trip_data_by_route_code(session,route_code,geojson,agency_id.value)
# await websocket.send_json(crud.get_gtfs_rt_vehicle_positions_trip_data_by_route_code(session,route_code,geojson,agency_id.value))
# await websocket.send_json(jsonable_encoder(result_array))
# payload = {"time": str(datetime.now()), "message": "Hello World!","value":random.randint(1,100)}
# await websocket.send_json(payload)
# await asyncio.sleep(10)

# Frontend Routing
@app.get("/websocket_test")
def read_root(request: Request):
return templates.TemplateResponse("ws.html", {"request": request})

# class SPAStaticFiles(StaticFiles):
# async def get_response(self, path: str, scope):
# response = await super().get_response(path, scope)
# if response.status_code == 404:
# response = await super().get_response('.', scope)
# return response

# app.mount('/', SPAStaticFiles(directory='app/documentation', html=True), name='frontend')

@app.get("/",response_class=HTMLResponse)
def index(request:Request):
Expand Down
2 changes: 2 additions & 0 deletions fastapi/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CalendarDates(Base):
date = Column(String)
exception_type = Column(Integer)
agency_id = Column(String)

class StopTimes(Base):
__tablename__ = "stop_times"
arrival_time = Column(String)
Expand All @@ -50,6 +51,7 @@ class StopTimes(Base):
bay_num = Column(Integer)
agency_id = Column(String)
trip_id = Column(Integer, primary_key=True,index=True)
rider_usage_code = Column(Integer)

class Stops(Base):
__tablename__ = "stops"
Expand Down
1 change: 1 addition & 0 deletions fastapi/app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class StopTimes(BaseModel):
bay_num: int
id: int
agency_id: str
rider_usage_code: int

class Stops(BaseModel):
stop_id: int
Expand Down
Loading

0 comments on commit 17bf58d

Please sign in to comment.