Skip to content

Commit

Permalink
store service host in config
Browse files Browse the repository at this point in the history
  • Loading branch information
nonnontrivial committed Aug 10, 2024
1 parent 53c7bba commit 313d9be
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ python -m api.main

### endpoints

#### `/api/v1/lp`
#### `/api/v1/pollution`

Gets the approximate artificial sky brightness
map [RGBA pixel value](https://djlorenz.github.io/astronomy/lp2022/colors.html) for a lat and lon (for the year 2022).

```sh
curl "localhost:8000/api/v1/lp?lat=40.7277478&lon=-74.0000374"
curl "localhost:8000/api/v1/pollution?lat=40.7277478&lon=-74.0000374"
```

```json
Expand Down
3 changes: 3 additions & 0 deletions api/api/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os

api_version = os.getenv("API_VERSION", "v1")

service_port = int(os.getenv("SERVICE_PORT", 8000))
service_host = "0.0.0.0"

log_level = int(os.getenv("LOG_LEVEL", 20))
19 changes: 9 additions & 10 deletions api/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import uvicorn
from fastapi import FastAPI, HTTPException, APIRouter

from .config import api_version, log_level, service_port
from .config import api_version, log_level, service_port, service_host
from .models import PredictionResponse
from .pollution.pollution import ArtificialNightSkyBrightnessMapImage, Coords
from .prediction.prediction import (
Expand All @@ -26,16 +26,15 @@ def get_log_config():
main_router = APIRouter(prefix=f"/api/{api_version}")


def create_prediction_response(prediction_obj: Prediction) -> PredictionResponse:
"""create the object that is ultimately served to clients as the prediction response."""
precision_digits = 4
y = round(float(prediction_obj.y.item()), precision_digits)
return PredictionResponse(mpsas=y)


@main_router.get("/predict")
async def get_prediction(lat, lon):
"""Predict sky brightness in magnitudes per square arcsecond for a lat and lon."""

def create_prediction_response(prediction_obj: Prediction) -> PredictionResponse:
"""create the object that is ultimately served to clients as the prediction response."""
y = round(float(prediction_obj.y.item()), 10)
return PredictionResponse(mpsas=y)

try:
lat, lon = float(lat), float(lon)
prediction = await predict_sky_brightness(lat, lon)
Expand All @@ -44,7 +43,7 @@ async def get_prediction(lat, lon):
raise HTTPException(status_code=500, detail=f"failed to predict because {e}")


@main_router.get("/lp")
@main_router.get("/pollution")
async def get_artificial_light_pollution(lat, lon):
"""Get artificial light pollution at a lat and lon. Source https://djlorenz.github.io/astronomy/lp2022/"""
try:
Expand All @@ -62,4 +61,4 @@ async def get_artificial_light_pollution(lat, lon):
app.include_router(main_router)

if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=service_port, log_config=get_log_config())
uvicorn.run(app, host=service_host, port=service_port, log_config=get_log_config())
4 changes: 2 additions & 2 deletions api/api/tests/test_pollution_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_get_city_pollution(lat, lon):
"b": 255,
"a": 255
}
res = client.get(f"{API_PREFIX}/lp?lat={lat}&lon={lon}")
res = client.get(f"{API_PREFIX}/pollution?lat={lat}&lon={lon}")
assert res.json() == max_channels


Expand All @@ -36,5 +36,5 @@ def test_out_of_bounds(lat, lon):
"b": 0,
"a": 255
}
res = client.get(f"{API_PREFIX}/lp?lat={lat}&lon={lon}")
res = client.get(f"{API_PREFIX}/pollution?lat={lat}&lon={lon}")
assert res.json() == empty_channels
1 change: 0 additions & 1 deletion api/api/tests/test_prediction_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
def test_prediction(coords, lowerbound, upperbound):
lat, lon = coords
response = client.get(f"{API_PREFIX}/predict?lat={lat}&lon={lon}")
# assert response.json() == {}
assert response.status_code == 200
brightness = response.json()["mpsas"]
assert lowerbound <= brightness <= upperbound

0 comments on commit 313d9be

Please sign in to comment.