diff --git a/app/services/location/jhu.py b/app/services/location/jhu.py index ebed3960..98317816 100644 --- a/app/services/location/jhu.py +++ b/app/services/location/jhu.py @@ -16,6 +16,7 @@ from ...utils import date as date_util from ...utils import httputils from . import LocationService +from . import ModelFactory LOGGER = logging.getLogger("services.location.jhu") PID = os.getpid() @@ -155,6 +156,9 @@ async def get_locations(): # incorrect data to consumers. # *************************************************************************** # Go through locations. + modelFactory = ModelFactory() + model_type = "timeline" + for index, location in enumerate(locations_confirmed): # Get the timelines. @@ -183,19 +187,19 @@ async def get_locations(): datetime.utcnow().isoformat() + "Z", # Timelines (parse dates as ISO). { - "confirmed": Timeline( - timeline={ + "confirmed": modelFactory.create_model(model_type, + { datetime.strptime(date, "%m/%d/%y").isoformat() + "Z": amount for date, amount in timelines["confirmed"].items() } ), - "deaths": Timeline( - timeline={ + "deaths": modelFactory.create_model(model_type, + { datetime.strptime(date, "%m/%d/%y").isoformat() + "Z": amount for date, amount in timelines["deaths"].items() } ), - "recovered": Timeline( + "recovered": modelFactory.create_model(model_type, timeline={ datetime.strptime(date, "%m/%d/%y").isoformat() + "Z": amount for date, amount in timelines["recovered"].items() diff --git a/app/services/location/modelfactory.py b/app/services/location/modelfactory.py new file mode 100644 index 00000000..f8b45417 --- /dev/null +++ b/app/services/location/modelfactory.py @@ -0,0 +1,13 @@ +from ...models import * + +class ModelFactory(): + def create_model(self, type, data): + """Factory Method""" + models = { + "timeline": Timeline(timeline = data), + "latest": Latest(confirmed = data.confirmed, deaths = data.deaths, recovered = data.recovered), + "timelines": Timelines(confirmed = data.confirmed, deaths = data.deaths, recovered = data.recovered), + "location": Location(data) + } + + return models[type] diff --git a/app/services/location/nyt.py b/app/services/location/nyt.py index 1f25ec34..e2f82e27 100644 --- a/app/services/location/nyt.py +++ b/app/services/location/nyt.py @@ -12,6 +12,7 @@ from ...models import Timeline from ...utils import httputils from . import LocationService +from . import ModelFactory LOGGER = logging.getLogger("services.location.nyt") @@ -99,6 +100,8 @@ async def get_locations(): # The normalized locations. locations = [] + modelFactory = ModelFactory() + model_type = "timeline" for idx, (county_state, histories) in enumerate(grouped_locations.items()): # Make location history for confirmed and deaths from dates. @@ -118,19 +121,19 @@ async def get_locations(): coordinates=Coordinates(None, None), # NYT does not provide coordinates last_updated=datetime.utcnow().isoformat() + "Z", # since last request timelines={ - "confirmed": Timeline( - timeline={ + "confirmed": modelFactory.create_model(model_type, + { datetime.strptime(date, "%Y-%m-%d").isoformat() + "Z": amount for date, amount in confirmed_history.items() } ), - "deaths": Timeline( - timeline={ + "deaths": modelFactory.create_model(model_type, + { datetime.strptime(date, "%Y-%m-%d").isoformat() + "Z": amount for date, amount in deaths_history.items() } ), - "recovered": Timeline(), + "recovered": modelFactory.create_model(model_type,{}), }, ) )