Skip to content

Commit 09b0a13

Browse files
committed
The second aggregate pattern
1 parent 0e3d924 commit 09b0a13

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

app/location/__init__.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
# put confirmed cases, deaths cases, recovered cases into one class
77
# Inseated of using confirmed cases, deaths cases, recovered cases as attributes, we can use CaseNumbers class instance as attribute
88
class CaseNumbers:
9-
def __init__(self, confirmed = 0, deaths = 0, recovered = 0):
9+
def __init__(self, id, confirmed = 0, deaths = 0, recovered = 0):
10+
self.id = id
1011
self.confirmed = confirmed
1112
self.deaths = deaths
1213
self.recovered = recovered
1314

15+
16+
#put all location information into one class
17+
#CaseNumbers, Locationinfo, coordinates and Location forms one aggregate
1418
class Locationinfo:
1519
def __init__(self, id, country, province, coordinates):
1620
self.id = id
@@ -29,7 +33,7 @@ class Location: # pylint: disable=too-many-instance-attributes
2933

3034
# Use instance of class CaseNumbers as attribute
3135
def __init__(
32-
self, locationinfo, last_updated, casenumbers
36+
self, id, locationinfo, last_updated, casenumbers
3337
): # pylint: disable=too-many-arguments
3438
# General info.
3539
self.locationinfo = locationinfo
@@ -48,7 +52,7 @@ def country_code(self):
4852
:returns: The country code.
4953
:rtype: str
5054
"""
51-
return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper()
55+
return (countries.country_code(self.locationinfo.country) or countries.DEFAULT_COUNTRY_CODE).upper()
5256

5357
@property
5458
def country_population(self):
@@ -69,13 +73,13 @@ def serialize(self):
6973
"""
7074
return {
7175
# General info.
72-
"id": self.id,
73-
"country": self.country,
76+
"id": self.locationinfo.id,
77+
"country": self.locationinfo.country,
7478
"country_code": self.country_code,
7579
"country_population": self.country_population,
76-
"province": self.province,
80+
"province": self.locationinfo.province,
7781
# Coordinates.
78-
"coordinates": self.coordinates.serialize(),
82+
"coordinates": self.locationinfo.coordinates.serialize(),
7983
# Last updated.
8084
"last_updated": self.last_updated,
8185
# Latest data (statistics).
@@ -93,13 +97,10 @@ class TimelinedLocation(Location):
9397
"""
9498

9599
# pylint: disable=too-many-arguments
96-
def __init__(self, id, country, province, coordinates, last_updated, timelines, casenumbers):
100+
def __init__(self, locationinfo, last_updated, timelines, casenumbers):
97101
super().__init__(
98102
# General info.
99-
id,
100-
country,
101-
province,
102-
coordinates,
103+
locationinfo,
103104
last_updated,
104105
# Statistics (retrieve latest from timelines).
105106
casenumbers

app/services/location/csbs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from ...utils import httputils
1313
from . import LocationService
1414

15+
from urls import URLs
16+
1517
LOGGER = logging.getLogger("services.location.csbs")
1618

1719

@@ -32,7 +34,7 @@ async def get(self, loc_id): # pylint: disable=arguments-differ
3234

3335

3436
# Base URL for fetching data
35-
BASE_URL = "https://facts.csbs.org/covid-19/covid19_county.csv"
37+
#BASE_URL = "https://facts.csbs.org/covid-19/covid19_county.csv"
3638

3739

3840
@cached(cache=TTLCache(maxsize=1, ttl=1800))
@@ -52,7 +54,7 @@ async def get_locations():
5254
locations = cache_results
5355
else:
5456
LOGGER.info(f"{data_id} shared cache empty")
55-
async with httputils.CLIENT_SESSION.get(BASE_URL) as response:
57+
async with httputils.CLIENT_SESSION.get(URLs.CSBS) as response:
5658
text = await response.text()
5759

5860
LOGGER.debug(f"{data_id} Data received")

app/services/location/jhu.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from ...utils import httputils
1818
from . import LocationService
1919

20+
from urls import URLs
21+
2022
LOGGER = logging.getLogger("services.location.jhu")
2123
PID = os.getpid()
2224

@@ -41,7 +43,7 @@ async def get(self, loc_id): # pylint: disable=arguments-differ
4143

4244

4345
# Base URL for fetching category.
44-
BASE_URL = "https://raw.githubusercontent.com/CSSEGISandData/2019-nCoV/master/csse_covid_19_data/csse_covid_19_time_series/"
46+
# BASE_URL = "https://raw.githubusercontent.com/CSSEGISandData/2019-nCoV/master/csse_covid_19_data/csse_covid_19_time_series/"
4547

4648

4749
@cached(cache=TTLCache(maxsize=4, ttl=1800))
@@ -64,7 +66,7 @@ async def get_category(category):
6466
else:
6567
LOGGER.info(f"{data_id} shared cache empty")
6668
# URL to request data from.
67-
url = BASE_URL + "time_series_covid19_%s_global.csv" % category
69+
url = URLs.JHU + "time_series_covid19_%s_global.csv" % category
6870

6971
# Request the data
7072
LOGGER.info(f"{data_id} Requesting data...")

app/services/location/nyt.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from ...utils import httputils
1414
from . import LocationService
1515

16+
from urls import URLs
17+
1618
LOGGER = logging.getLogger("services.location.nyt")
1719

1820

@@ -36,7 +38,7 @@ async def get(self, loc_id): # pylint: disable=arguments-differ
3638

3739

3840
# Base URL for fetching category.
39-
BASE_URL = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv"
41+
# BASE_URL = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv"
4042

4143

4244
def get_grouped_locations_dict(data):
@@ -85,7 +87,7 @@ async def get_locations():
8587
locations = cache_results
8688
else:
8789
LOGGER.info(f"{data_id} shared cache empty")
88-
async with httputils.CLIENT_SESSION.get(BASE_URL) as response:
90+
async with httputils.CLIENT_SESSION.get(URLs.NYT) as response:
8991
text = await response.text()
9092

9193
LOGGER.debug(f"{data_id} Data received")

app/services/location/urls.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import enum
2+
3+
# Encapsulate urls into one class, clas URLs serve as aggregate root.
4+
class URLs(str, enum.Enum):
5+
6+
JHU = "https://raw.githubusercontent.com/CSSEGISandData/2019-nCoV/master/csse_covid_19_data/csse_covid_19_time_series/"
7+
CSBS = "https://facts.csbs.org/covid-19/covid19_county.csv"
8+
NYT = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv"

0 commit comments

Comments
 (0)