Skip to content
Open
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
14 changes: 9 additions & 5 deletions app/services/location/jhu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions app/services/location/modelfactory.py
Original file line number Diff line number Diff line change
@@ -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]
13 changes: 8 additions & 5 deletions app/services/location/nyt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ...models import Timeline
from ...utils import httputils
from . import LocationService
from . import ModelFactory

LOGGER = logging.getLogger("services.location.nyt")

Expand Down Expand Up @@ -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.
Expand All @@ -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,{}),
},
)
)
Expand Down