Skip to content

Commit

Permalink
Merge pull request #27 from airavata-courses/feature/24-datasource-ca…
Browse files Browse the repository at this point in the history
…ching

#24-milestone2: cache the nexrad object and added logging
  • Loading branch information
vinayakasg18 authored Apr 6, 2022
2 parents 6eda99b + ff03e87 commit cbe19a2
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 118 deletions.
2 changes: 1 addition & 1 deletion datasource_service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.9-slim-buster
FROM python:3.9.10-slim-buster

LABEL maintainer="[email protected]"

Expand Down
1 change: 1 addition & 0 deletions datasource_service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fetches the radar objects from nexradaws cloud storage
- Plots the graph for the requested radar station
- Returns the json as response with queried information and encoded image
- [ENHANCEMENT] caching added to speed up the download of nexrad object

### The project is built using poetry packaging manager
- Docs: [poetry](https://python-poetry.org/docs/)
Expand Down
19 changes: 17 additions & 2 deletions datasource_service/datasource_service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
import pyart
from matplotlib import pyplot as plt
import base64

from cachetools import cached, TTLCache
import logging
from datasource_service.response_model import ResponseModel

logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)

desc = """ ### Datasource api helps you fetch the radar object from a specified radar station \n
API uses a Py-ART library, an open source library developed by
- JJ Helmus and SM Collis, JORS 2016, doi: 10.5334/jors.119
Expand All @@ -19,14 +24,20 @@
app = FastAPI(title="Datasource-api",
description=desc)

# set caching to one day
ONE_DAY = 60 * 60 * 24


@app.get("/api/v1/{year}/{month}/{day}/{radar}", response_model=ResponseModel, response_model_exclude_unset=True)
@cached(cache=TTLCache(maxsize=5, ttl=ONE_DAY))
def nexrad_data(year: int, month: int, day: int, radar: str):
""" GET api to fetch data from nexradaws """

json_object = {}
encoded_image = ""
try:
logger.info(f"Trying to fetch the {radar} radar station information, for year: {year}, month: {month}, "
f"day: {day}")
radar_object, scans = download_radar_object(year, month, day, radar)
radar_file = scans[-1].filename.rstrip('.gz')
for i, scan in enumerate(radar_object.iter_success(), start=1):
Expand All @@ -51,11 +62,13 @@ def nexrad_data(year: int, month: int, day: int, radar: str):
"radar": radar,
"encoded_image": encoded_image
}
logger.info(f"Finished plotting the weather data for radar station: {radar}")
return json_object
except Exception as ex:
print(f"Error while fetching the data or plotting the graph: {ex}")
logger.error(f"Error while fetching the data or plotting the graph: {ex}", exc_info=True)
raise HTTPException(status_code=404, detail="Radar station is not found")
finally:
logger.debug("Removing the dangling files")
dirs = os.listdir()
cur_dir = os.getcwd()
for file in dirs:
Expand All @@ -72,6 +85,7 @@ def download_radar_object(year: int, month: int, day: int, radar: str):
""" Downloading the radar object """

# nexradaws connection object
logger.info(f"Downloading the nexrad object for radar station: {radar}")
ncon = nexradaws.NexradAwsInterface()
scans = ncon.get_avail_scans(year, month, day, radar)
download_dir = os.getcwd()
Expand All @@ -81,4 +95,5 @@ def download_radar_object(year: int, month: int, day: int, radar: str):

def start():
""" Start fastAPI using uvicorn """
logger.info("Starting the datasource api...")
uvicorn.run("datasource_service.main:app", port=8000, host="0.0.0.0")
Loading

0 comments on commit cbe19a2

Please sign in to comment.