Skip to content

Commit

Permalink
make log format uniform
Browse files Browse the repository at this point in the history
  • Loading branch information
nonnontrivial committed Jun 26, 2024
1 parent 72fa55f commit c892ae7
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ RUN pip install --no-cache-dir --upgrade -r ./requirements.txt

COPY . .

CMD python -m uvicorn api.main:app --host 0.0.0.0 --port 8000
CMD python -m api.main
1 change: 1 addition & 0 deletions api/api/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

api_version = os.getenv("API_VERSION", "v1")
service_port = int(os.getenv("SERVICE_PORT", 8000))
log_level = int(os.getenv("LOG_LEVEL", 20))
17 changes: 15 additions & 2 deletions api/api/main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
from dataclasses import asdict
import logging

import uvicorn
from fastapi import FastAPI, HTTPException, APIRouter

from .config import api_version, log_level
from .config import api_version, log_level, service_port
from .models import PredictionResponse
from .pollution.pollution import ArtificialNightSkyBrightnessMapImage, Coords
from .prediction.prediction import (
Prediction,
predict_sky_brightness,
)

logging.basicConfig(level=log_level)
log_format = "%(asctime)s [%(levelname)s] %(message)s"
logging.basicConfig(level=log_level, format=log_format)


def get_log_config():
config = uvicorn.config.LOGGING_CONFIG
config["formatters"]["access"]["fmt"] = log_format
return config


app = FastAPI()
main_router = APIRouter(prefix=f"/api/{api_version}")

Expand Down Expand Up @@ -49,3 +59,6 @@ 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())
2 changes: 1 addition & 1 deletion api/api/prediction/meteo/open_meteo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, site: ObserverSite) -> None:
self.url_base = f"{PROTOCOL}://{open_meteo_host}:{open_meteo_port}"

async def get_values_at_site(self) -> t.Tuple[int, float]:
"""ask the instance of open meteo for cloud cover and elevation values for the observer site"""
"""ask open meteo for cloud cover and elevation for the observer site"""
import httpx

lat, lon = self.site.latitude.value, self.site.longitude.value
Expand Down
1 change: 1 addition & 0 deletions pp/pp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async def main():
try:
async with httpx.AsyncClient() as client:
while True:
# allow predictions over cells to run in interleaved way
for cell_coords in resolution_zero_cell_coords:
await asyncio.create_task(predict_on_cell_coords(client, cell_coords, channel))
await asyncio.sleep(task_sleep_interval)
Expand Down
13 changes: 9 additions & 4 deletions pp/pp/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,38 @@


async def get_prediction_message_for_lat_lon(client: httpx.AsyncClient, lat: float, lon: float) -> PredictionMessage:
"""create the object that will get published to rabbitmq"""
res = await client.get(prediction_endpoint_url, params={"lat": lat, "lon": lon})
res.raise_for_status()

data = res.json()
if (mpsas := data.get("sky_brightness", None)) is None:
raise ValueError("no sky brightness reading in api response")

return PredictionMessage(
message = PredictionMessage(
lat=lat,
lon=lon,
h3_id=h3.geo_to_h3(lat, lon, 0),
utc=datetime.utcnow().isoformat(),
mpsas=mpsas,
)
return message


# message_store = {}


async def predict_on_cell_coords(client: httpx.AsyncClient, coords: Tuple[float, float], channel: Channel):
async def predict_on_cell_coords(client: httpx.AsyncClient, h3_coords: Tuple[float, float], channel: Channel):
"""retrieve and publish a sky brightness prediction at coords for the h3 cell"""
import json

try:
lat, lon = coords
lat, lon = h3_coords

m = await get_prediction_message_for_lat_lon(client, lat, lon)
message_body = asdict(m)

log.info(f"publishing {message_body} to {prediction_queue}")
channel.basic_publish(exchange="", routing_key=prediction_queue, body=json.dumps(message_body))

# keep track of how many messages are published for each cell
Expand All @@ -53,4 +58,4 @@ async def predict_on_cell_coords(client: httpx.AsyncClient, coords: Tuple[float,
except httpx.HTTPStatusError as e:
log.error(f"got bad status from api server {e}")
except Exception as e:
log.error(f"could not publish prediction at {coords} because {e}")
log.error(f"could not publish prediction at {h3_coords} because {e}")

0 comments on commit c892ae7

Please sign in to comment.