Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions app/location/location_proxy.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 18 additions & 3 deletions app/services/location/csbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -76,7 +91,7 @@ async def get_locations():

# Append to locations.
locations.append(
CSBSLocation(
CSBSLocationProxy(
# General info.
i,
state,
Expand All @@ -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
Expand Down
22 changes: 19 additions & 3 deletions app/services/location/nyt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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],
Expand All @@ -132,7 +148,7 @@ async def get_locations():
),
"recovered": Timeline(),
},
)
).getInfo()
)
LOGGER.info(f"{data_id} Data normalized")
# save the results to distributed cache
Expand Down