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
17 changes: 17 additions & 0 deletions app/location/location_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from csbs import CSBSLocation
from nyt import NYTLocation

class LocationRoot:


def __init__(self, location):
location = NYTLocationService


def set_csbs(self, id, state, county, coordinates, last_updated, confirmed, deaths):
self.location = CSBSLocation(id, state, county, coordinates, last_updated, confirmed, deaths)



def set_nyt(self, id, state, county, coordinates, last_updated, timelines):
self.location = NYTLocation( id, state, county, coordinates, last_updated, timelines)
19 changes: 15 additions & 4 deletions app/routers/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import enum

from fastapi import APIRouter, HTTPException, Request

from ..services import ServiceRoot
from ..data import DATA_SOURCES
from ..models import LatestResponse, LocationResponse, LocationsResponse

Expand All @@ -26,7 +26,11 @@ async def get_latest(
"""
Getting latest amount of total confirmed cases, deaths, and recoveries.
"""
locations = await request.state.source.get_all()

#service root for aggregate pattern
service = ServiceRoot(source)

locations = await service.get_all()
return {
"latest": {
"confirmed": sum(map(lambda location: location.confirmed, locations)),
Expand Down Expand Up @@ -56,8 +60,10 @@ async def get_locations(
params.pop("source", None)
params.pop("timelines", None)

service = ServiceRoot(source)

# Retrieve all the locations.
locations = await request.state.source.get_all()
locations = await service.get_all()

# Attempt to filter out locations with properties matching the provided query params.
for key, value in params.items():
Expand Down Expand Up @@ -98,7 +104,12 @@ async def get_location_by_id(
"""
Getting specific location by id.
"""
location = await request.state.source.get(id)

#aggregate root for service layer
service = ServiceRoot(source)

# Retrieve location.
location = await service.get(id)
return {"location": location.serialize(timelines)}


Expand Down
33 changes: 19 additions & 14 deletions app/services/location/csbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from ...caches import check_cache, load_cache
from ...coordinates import Coordinates
from ...location.csbs import CSBSLocation
from ...location.location_root import LocationRoot
#from ...location.csbs import CSBSLocation
from ...utils import httputils
from . import LocationService

Expand Down Expand Up @@ -74,21 +75,25 @@ async def get_locations():
# Date string without "EDT" at end.
last_update = " ".join(item["Last Update"].split(" ")[0:2])

#access location aggregate root, and append it to locations.
location_csbs = LocationRoot()
location_csbs.set_csbs(
# General info.
i,
state,
county,
# Coordinates.
Coordinates(item["Latitude"], item["Longitude"]),
# Last update (parse as ISO).
datetime.strptime(last_update, "%Y-%m-%d %H:%M").isoformat() + "Z",
# Statistics.
int(item["Confirmed"] or 0),
int(item["Death"] or 0),
)
#location.method(args)
# Append to locations.
locations.append(
CSBSLocation(
# General info.
i,
state,
county,
# Coordinates.
Coordinates(item["Latitude"], item["Longitude"]),
# Last update (parse as ISO).
datetime.strptime(last_update, "%Y-%m-%d %H:%M").isoformat() + "Z",
# Statistics.
int(item["Confirmed"] or 0),
int(item["Death"] or 0),
)
location_csbs.location
)
LOGGER.info(f"{data_id} Data normalized")
# save the results to distributed cache
Expand Down
51 changes: 28 additions & 23 deletions app/services/location/nyt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from ...caches import check_cache, load_cache
from ...coordinates import Coordinates
from ...location.nyt import NYTLocation
#from ...location.nyt import NYTLocation
from ...location.location_root import LocationRoot
from ...models import Timeline
from ...utils import httputils
from . import LocationService
Expand Down Expand Up @@ -109,30 +110,34 @@ async def get_locations():
deaths_list = histories["deaths"]
deaths_history = {date: int(amount or 0) for date, amount in deaths_list}

#access location aggregate root, and append it to locations.
location_nyt = LocationRoot()
location_nyt.set_nyt(
id=idx,
state=county_state[1],
county=county_state[0],
coordinates=Coordinates(None, None), # NYT does not provide coordinates
last_updated=datetime.utcnow().isoformat() + "Z", # since last request
timelines={
"confirmed": Timeline(
timeline={
datetime.strptime(date, "%Y-%m-%d").isoformat() + "Z": amount
for date, amount in confirmed_history.items()
}
),
"deaths": Timeline(
timeline={
datetime.strptime(date, "%Y-%m-%d").isoformat() + "Z": amount
for date, amount in deaths_history.items()
}
),
"recovered": Timeline(),
},
)

# Normalize the item and append to locations.
locations.append(
NYTLocation(
id=idx,
state=county_state[1],
county=county_state[0],
coordinates=Coordinates(None, None), # NYT does not provide coordinates
last_updated=datetime.utcnow().isoformat() + "Z", # since last request
timelines={
"confirmed": Timeline(
timeline={
datetime.strptime(date, "%Y-%m-%d").isoformat() + "Z": amount
for date, amount in confirmed_history.items()
}
),
"deaths": Timeline(
timeline={
datetime.strptime(date, "%Y-%m-%d").isoformat() + "Z": amount
for date, amount in deaths_history.items()
}
),
"recovered": Timeline(),
},
)
location_nyt.location
)
LOGGER.info(f"{data_id} Data normalized")
# save the results to distributed cache
Expand Down
26 changes: 26 additions & 0 deletions app/services/location/service_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from jhu import JHULocationService
from nyt import NYTLocationService
from csbs import CSBSLocationService

class ServiceRoot:

def __init__(self, source, service):
if source == '' or source == None or source == 'jhu':
self.source = 'jhu'
self.service = JHULocationService()
elif source == 'nyt':
self.source = source
self.service = NYTLocationService()
elif source == 'csbs':
self.source = source
self.service = CSBSLocationService()
else
self.source = None

async def get_all(self):
locations = await self.service.get_all()
return locations

async def get(self, id):
location = await self.service.get(id)
return location