Skip to content

Commit

Permalink
Merge pull request #551 from LACMTA/practice-changes
Browse files Browse the repository at this point in the history
Changes for local dev and more documentation
  • Loading branch information
matikin9 authored Oct 3, 2024
2 parents 9a560e4 + 37cb10c commit 53314f4
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 12 deletions.
12 changes: 7 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [


{
"name": "Python: Remote Attach",
"type": "python",
Expand All @@ -19,12 +21,12 @@
},

{
"name": "Python: Module",
"type": "python",
"name": "Launch FastAPI",
"type": "debugpy",
"request": "launch",
"module": "uvicorn",
"args": ["app.main:app", "--reload"],
"cwd": "${workspaceFolder}\\fastapi",
"cwd": "${workspaceFolder}/fastapi",
"justMyCode": true
},
{
Expand All @@ -35,10 +37,10 @@
"console": "integratedTerminal"
},
{
"name": "Python: UNVICORN",
"name": "Python: UVICORN",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/uvicorn app.main:app --reload",
"program": "${workspaceFolder}/fastapi uvicorn app.main:app --reload",
"console": "integratedTerminal"
},
]
Expand Down
7 changes: 5 additions & 2 deletions readme.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ sudo apt install python3 python3-pip

### Set Environment Variables

Get the `secrets.py` files with the necessary credentials and add them to the `\fastapi\app\` and `\data-loading-service\app\` folders.
Get the `app_secrets.py` files with the necessary credentials and add them to the `\fastapi\app\` and `\data-loading-service\app\` folders.

Make sure they are targeted by the `.gitignore` and grayed out, indicating that they will not be tracked or committed to the repository.

Expand Down Expand Up @@ -231,4 +231,7 @@ docker build . -t metro-api-v2:metro-api-v2 .

# runs metro-api-v2 image
docker run --rm -it -p 80:80/tcp metro-api-v2:metro-api-v2
```
```

### Error connection to server at "localhost"

60 changes: 60 additions & 0 deletions documentation/docs/websockets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Websockets

The API has one WebSocket endpoint used to provide a stream of realtime data. The URL is in this format:

```
wss://api.metro.net/ws/{agency_id}/{endpoint}/{route_codes (optional)}
```

## Parameters

__agency_id (str)__

The agencyID is found in the GTFS-schedule `agency.txt` files.

Values:
- `LACMTA` (Metro Bus)
- `LACMTA_Rail` (Metro Rail)

__endpoint (str)__

The type of GTFS-realtime data to send.

Values:
- `vehicle_positions`
- `trip_updates`

__route_codes (str, optional)__

A comma-separated list of route codes to filter updates. If not provided, updates for all routes are sent.

Route codes are the definitive labels for each of the lines, minus HASTUS version numbers and separated from their "sister" lines.

## Response

The WebSocket endpoint sends updates every 3 seconds.

### Success

The WebSocket endpoint sends updates in the following format:

{
"id": "vehicle_id",
"vehicle": {
"trip": {
"route_id": "route_code",
...
},
...
},
"route_code": "route_code",
...
}

Each message from the WebSocket contains data for a single vehicle.

### Error

If an error occurs while processing updates, the WebSocket endpoint sends an error message in the following format:

"Error: error_message"
5 changes: 5 additions & 0 deletions documentation/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const sidebars = {
type: 'doc',
id: 'docs',
label: 'Documentation Site'
},
{
type: 'doc',
id: 'websockets',
label: 'WebSockets'
}
],
openApiSidebar: [
Expand Down
8 changes: 6 additions & 2 deletions fastapi/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def get_pgbouncer_uri(original_uri):

# Replace the hostname and port with the ones for PgBouncer
pgbouncer_host = 'localhost' # PgBouncer is running on the same machine
pgbouncer_port = 6432 # Default PgBouncer port
pgbouncer_port = '6432' # Default PgBouncer port

# Construct the new URI
pgbouncer_uri = urlunparse(
Expand All @@ -63,8 +63,12 @@ class Config:
REDIS_URL = os.environ.get('REDIS_URL', 'redis://redis:6379')
TARGET_DB_SCHEMA = 'metro_api'
# TARGET_DB_SCHEMA = os.environ.get('TARGET_DB_SCHEMA')

API_DB_URI = os.environ.get('API_DB_URI')

# get_pgbouncer_uri seems to be for locally hosted postgres DB only?
# API_DB_URI = get_pgbouncer_uri(os.environ.get('API_DB_URI'))

API_DB_URI = get_pgbouncer_uri(os.environ.get('API_DB_URI'))
SECRET_KEY = os.environ.get('HASH_KEY')
ALGORITHM = os.environ.get('HASHING_ALGORITHM')
ACCESS_TOKEN_EXPIRE_MINUTES = 30
Expand Down
9 changes: 6 additions & 3 deletions fastapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,12 @@ async def dummy_websocket_endpoint(agency_id: str, endpoint: str, route_codes: O
This endpoint is used for documentation purposes only. It mirrors the WebSocket endpoint that accepts connections and sends real-time updates about vehicles and trips.
Args:
agency_id (str): The ID of the agency.
endpoint (str): The type of updates to send. Can be "vehicle_positions" or "trip_updates".
route_codes (str, optional): A comma-separated list of route codes to filter updates. If not provided, updates for all routes are sent.
agency_id (str): The ID of the agency.
endpoint (str): The type of updates to send. Can be "vehicle_positions" or "trip_updates".
route_codes (str, optional): A comma-separated list of route codes to filter updates. If not provided, updates for all routes are sent.
The WebSocket endpoint sends updates in the following format:
Expand Down

0 comments on commit 53314f4

Please sign in to comment.