Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6e5bf9f
reduce cache size (#21)
Kilo59 May 2, 2020
da822ba
Update Procfile
Kilo59 May 2, 2020
b085652
Update Procfile
Kilo59 May 3, 2020
a1dcfb4
Update jhu.py
Kilo59 May 3, 2020
cc768a9
update max requests and max jitters
Kilo59 May 4, 2020
ba056ce
Add .deepsource.toml
deepsourcebot May 7, 2020
e54724e
Update Procfile
Kilo59 May 8, 2020
170eb12
Cache mem optimize (#23)
Kilo59 May 9, 2020
7fe38b9
Sentry (#24)
Kilo59 May 9, 2020
96efc92
use official middleware (#25)
Kilo59 May 9, 2020
ecb0a68
cache redis json serialization error
Kilo59 May 9, 2020
464453d
fix nyt redis serialization error
Kilo59 May 9, 2020
aa8b51d
refactor Timeline class to pydantic model
tanwinn May 20, 2020
f98447e
partial recovery fix (#31)
Kilo59 May 20, 2020
4bde047
Formatting & version (#32)
Kilo59 May 20, 2020
a955670
update FastAPI .54.1 -> .64.1 (#34)
Kilo59 Jul 28, 2020
eaebf76
update uvicorn
Kilo59 Aug 1, 2020
3a2975f
2 workers
Kilo59 Aug 1, 2020
d4088c6
Merge branch 'master' into refactor#28
Kilo59 Aug 10, 2020
f53e661
'Refactored by Sourcery' (#35)
sourcery-ai[bot] Aug 10, 2020
ba5050e
Refactored by Sourcery (#33)
sourcery-ai[bot] Aug 10, 2020
a4fd67b
fix Coordinates import
Kilo59 Sep 14, 2020
76014f7
fix jhu timeline init call
Kilo59 Sep 14, 2020
cf65e6e
update requirements
Kilo59 Sep 14, 2020
d954534
Merge pull request #30 from Kilo59/refactor#28
Kilo59 Sep 14, 2020
d66ce54
Merge branch 'master' into master
Kilo59 Sep 14, 2020
239c3bf
fix ValueError
Kilo59 Oct 29, 2020
a9816e9
add StatusCake uptime monitoring
Kilo59 Mar 13, 2021
578b60e
revert to 1 hour cache
Kilo59 Mar 21, 2021
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
Prev Previous commit
Next Next commit
refactor Timeline class to pydantic model
  • Loading branch information
tanwinn committed May 20, 2020
commit aa8b51dd5917d90c8ee66a70ee9c8798a69ab7d6
21 changes: 19 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""app.models.py"""
from typing import Dict, List

from pydantic import BaseModel
from pydantic import BaseModel, validator


class Latest(BaseModel):
Expand All @@ -27,9 +27,26 @@ class Timeline(BaseModel):
Timeline model.
"""

latest: int
timeline: Dict[str, int] = {}

@validator("timeline")
@classmethod
def sort_timeline(cls, value):
"""Sort the timeline history before inserting into the model"""
return dict(sorted(value.items()))

@property
def latest(self):
"""Get latest available history value."""
return list(self.timeline.values())[-1] if self.timeline else 0

def serialize(self):
"""
Serialize the model into dict
TODO: override dict() instead of using serialize
"""
return {**self.dict(), "latest": self.latest}


class Timelines(BaseModel):
"""
Expand Down
2 changes: 1 addition & 1 deletion app/services/location/csbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ async def get_locations():
await load_cache(data_id, locations)
except TypeError as type_err:
LOGGER.error(type_err)

# Return the locations.
return locations
8 changes: 4 additions & 4 deletions app/services/location/jhu.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ...caches import check_cache, load_cache
from ...coordinates import Coordinates
from ...location import TimelinedLocation
from ...timeline import Timeline
from ...models import Timeline
from ...utils import countries
from ...utils import date as date_util
from ...utils import httputils
Expand Down Expand Up @@ -175,18 +175,18 @@ async def get_locations():
# Timelines (parse dates as ISO).
{
"confirmed": Timeline(
{
timeline={
datetime.strptime(date, "%m/%d/%y").isoformat() + "Z": amount
for date, amount in timelines["confirmed"].items()
}
),
"deaths": Timeline(
{
timeline={
datetime.strptime(date, "%m/%d/%y").isoformat() + "Z": amount
for date, amount in timelines["deaths"].items()
}
),
"recovered": Timeline({}),
"recovered": Timeline(),
},
)
)
Expand Down
8 changes: 4 additions & 4 deletions app/services/location/nyt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ...caches import check_cache, load_cache
from ...coordinates import Coordinates
from ...location.nyt import NYTLocation
from ...timeline import Timeline
from ...models import Timeline
from ...utils import httputils
from . import LocationService

Expand Down Expand Up @@ -119,18 +119,18 @@ async def get_locations():
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({}),
"recovered": Timeline(),
},
)
)
Expand Down
42 changes: 0 additions & 42 deletions app/timeline.py

This file was deleted.

10 changes: 5 additions & 5 deletions tests/test_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from app import coordinates, location, timeline
from app import coordinates, location, models


def mocked_timeline(*args, **kwargs):
Expand All @@ -22,7 +22,7 @@ def __init__(self, latest):
(2, "Cruise Ship", "XX", "", 15, 100, 1000, 1111, 22222),
],
)
@mock.patch("app.timeline.Timeline", side_effect=mocked_timeline)
@mock.patch("app.models.Timeline", side_effect=mocked_timeline)
def test_location_class(
mocked_timeline,
test_id,
Expand All @@ -39,9 +39,9 @@ def test_location_class(
coords = coordinates.Coordinates(latitude=latitude, longitude=longitude)

# Timelines
confirmed = timeline.Timeline(confirmed_latest)
deaths = timeline.Timeline(deaths_latest)
recovered = timeline.Timeline(recovered_latest)
confirmed = models.Timeline(confirmed_latest)
deaths = models.Timeline(deaths_latest)
recovered = models.Timeline(recovered_latest)

# Date now.
now = datetime.utcnow().isoformat() + "Z"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from app import timeline
from app import models


def test_timeline_class():
Expand All @@ -15,7 +15,7 @@ def test_timeline_class():
"1/23/20": 3,
}

history_data = timeline.Timeline(history=timeseries)
history_data = models.Timeline(timeline=timeseries)

# validate last value
assert history_data.latest == 7
Expand Down