diff --git a/app/location/location_proxy.py b/app/location/location_proxy.py new file mode 100644 index 00000000..b6feb4d7 --- /dev/null +++ b/app/location/location_proxy.py @@ -0,0 +1,45 @@ +from .csbs import CSBSLocation +from .nyt import NYTLocation +from datetime import datetime +from datetime import timedelta + + + +class CSBSLocationProxy: + + def __init__(self, id, state, county, coordinates, last_updated, confirmed, deaths): + if confirmed >= 0 and deaths >= 0 and coordinates not None and last_updated > datetime.now() - timedelta(days=30): + self.location = CSBSLocation( + # General info. + id, + "US", + state, + coordinates, + last_updated, + # Statistics. + confirmed=confirmed, + deaths=deaths, + recovered=0, + ) + else: + self.location = "Invalid information" + + + def getInfo(self): + return self.location + + +class NYTLocationProxy: + + def __init__(self, id, state, county, coordinates, last_updated, timelines): + if coordinates not None and last_updated > datetime.now() - timedelta(days=30): + self.location = NYTLocation( + id, state, county, coordinates, last_updated, timelines + ) + + else: + self.location = "Invalid Information" + + + def getInfo(self): + return self.location diff --git a/app/services/location/csbs.py b/app/services/location/csbs.py index 444ebad6..b82f5a24 100644 --- a/app/services/location/csbs.py +++ b/app/services/location/csbs.py @@ -8,7 +8,7 @@ from ...caches import check_cache, load_cache from ...coordinates import Coordinates -from ...location.csbs import CSBSLocation +from ...location.location_proxy import CSBSLocationProxy from ...utils import httputils from . import LocationService @@ -20,6 +20,21 @@ class CSBSLocationService(LocationService): Service for retrieving locations from csbs """ + __instance = None + + @staticmethod + def getInstance(): + if CSBSLocationService.__instance == None: + CSBSLocationService() + return CSBSLocationService.__instance + + def __init__(self): + """ Virtually private constructor. """ + if Singleton.__instance != None: + raise Exception("This class is a singleton!") + else: + Singleton.__instance = self + async def get_all(self): # Get the locations. locations = await get_locations() @@ -76,7 +91,7 @@ async def get_locations(): # Append to locations. locations.append( - CSBSLocation( + CSBSLocationProxy( # General info. i, state, @@ -88,7 +103,7 @@ async def get_locations(): # Statistics. int(item["Confirmed"] or 0), int(item["Death"] or 0), - ) + ).getInfo() ) LOGGER.info(f"{data_id} Data normalized") # save the results to distributed cache diff --git a/app/services/location/nyt.py b/app/services/location/nyt.py index 1f25ec34..e4186576 100644 --- a/app/services/location/nyt.py +++ b/app/services/location/nyt.py @@ -8,7 +8,7 @@ from ...caches import check_cache, load_cache from ...coordinates import Coordinates -from ...location.nyt import NYTLocation +from ...location.location_proxy import NYTLocationProxy from ...models import Timeline from ...utils import httputils from . import LocationService @@ -21,6 +21,22 @@ class NYTLocationService(LocationService): Service for retrieving locations from New York Times (https://github.com/nytimes/covid-19-data). """ + + __instance = None + + @staticmethod + def getInstance(): + if CSBSLocationService.__instance == None: + CSBSLocationService() + return CSBSLocationService.__instance + + def __init__(self): + """ Virtually private constructor. """ + if Singleton.__instance != None: + raise Exception("This class is a singleton!") + else: + Singleton.__instance = self + async def get_all(self): # Get the locations. locations = await get_locations() @@ -111,7 +127,7 @@ async def get_locations(): # Normalize the item and append to locations. locations.append( - NYTLocation( + NYTLocationProxy( id=idx, state=county_state[1], county=county_state[0], @@ -132,7 +148,7 @@ async def get_locations(): ), "recovered": Timeline(), }, - ) + ).getInfo() ) LOGGER.info(f"{data_id} Data normalized") # save the results to distributed cache