Skip to content

Commit e2b6630

Browse files
author
ExpDev07
committed
cached locations
1 parent c9a4db9 commit e2b6630

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

app/services/location/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def get_all(self):
1111
Gets and returns all of the locations.
1212
1313
:returns: The locations.
14-
:rtype: Location
14+
:rtype: List[Location]
1515
"""
1616
raise NotImplementedError
1717

app/services/location/jhu.py

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,8 @@ class JhuLocationService(LocationService):
99
"""
1010

1111
def get_all(self):
12-
# Get all of the data categories locations.
13-
confirmed = get_category('confirmed')['locations']
14-
deaths = get_category('deaths')['locations']
15-
recovered = get_category('recovered')['locations']
16-
17-
# Final locations to return.
18-
locations = []
19-
20-
# Go through confirmed locations.
21-
for index, location in enumerate(confirmed):
22-
# Grab coordinates.
23-
coordinates = location['coordinates']
24-
25-
# Create location and append.
26-
locations.append(Location(
27-
# General info.
28-
index, location['country'], location['province'], Coordinates(coordinates['lat'], coordinates['long']),
29-
30-
# TODO: date key as ISO format.
31-
# { datetime.strptime(date, '%m/%d/%y').isoformat() + 'Z': int(amount or 0) for date, amount in history.items() }
32-
33-
# Timelines.
34-
Timeline(confirmed[index]['history']),
35-
Timeline(deaths[index]['history']),
36-
Timeline(recovered[index]['history'])
37-
))
38-
39-
# Finally, return the locations.
40-
return locations
12+
# Get the locations.
13+
return get_locations()
4114

4215
def get(self, id):
4316
# Get location at the index equal to provided id.
@@ -58,6 +31,9 @@ def get(self, id):
5831
def get_category(category):
5932
"""
6033
Retrieves the data for the provided category. The data is cached for 1 hour.
34+
35+
:returns: The data for category.
36+
:rtype: dict
6137
"""
6238

6339
# Adhere to category naming standard.
@@ -73,7 +49,7 @@ def get_category(category):
7349
# The normalized locations.
7450
locations = []
7551

76-
for item in data:
52+
for i, item in enumerate(data):
7753
# Filter out all the dates.
7854
dates = dict(filter(lambda element: date_util.is_date(element[0]), item.items()))
7955

@@ -115,4 +91,47 @@ def get_category(category):
11591
'latest': latest,
11692
'last_updated': datetime.utcnow().isoformat() + 'Z',
11793
'source': 'https://github.com/ExpDev07/coronavirus-tracker-api',
118-
}
94+
}
95+
96+
@cached(cache=TTLCache(maxsize=1024, ttl=3600))
97+
def get_locations():
98+
"""
99+
Retrieves the locations from the categories. The locations are cached for 1 hour.
100+
101+
:returns: The locations.
102+
:rtype: List[Location]
103+
"""
104+
# Get all of the data categories locations.
105+
confirmed = get_category('confirmed')['locations']
106+
deaths = get_category('deaths')['locations']
107+
recovered = get_category('recovered')['locations']
108+
109+
# Final locations to return.
110+
locations = []
111+
112+
# Go through locations.
113+
for index, location in enumerate(confirmed):
114+
# Get the timelines.
115+
timelines = {
116+
'confirmed' : confirmed[index]['history'],
117+
'deaths' : deaths[index]['history'],
118+
'recovered' : recovered[index]['history'],
119+
}
120+
121+
# Grab coordinates.
122+
coordinates = location['coordinates']
123+
124+
# Create location and append.
125+
locations.append(Location(
126+
# General info.
127+
index, location['country'], location['province'], Coordinates(coordinates['lat'], coordinates['long']),
128+
129+
# Timelines (parse dates as ISO).
130+
Timeline({ datetime.strptime(date, '%m/%d/%y').isoformat() + 'Z': amount for date, amount in timelines['confirmed'].items() }),
131+
Timeline({ datetime.strptime(date, '%m/%d/%y').isoformat() + 'Z': amount for date, amount in timelines['deaths'].items() }),
132+
Timeline({ datetime.strptime(date, '%m/%d/%y').isoformat() + 'Z': amount for date, amount in timelines['recovered'].items() })
133+
))
134+
135+
# Finally, return the locations.
136+
return locations
137+

0 commit comments

Comments
 (0)