Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
country: fetch_covid_active_cases: Don't retry dates from the beginni…
Browse files Browse the repository at this point in the history
…ng of the pandemic
  • Loading branch information
jpmckinney committed Apr 1, 2021
1 parent 19b76d3 commit aba3b64
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 8 additions & 1 deletion country/management/commands/fetch_covid_active_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def handle(self, *args, **kwargs):
break
dates_in_period.add(date)

# If a country has no dates, the list will be `[None]`.
dates_with_data = dict(
Country.objects.exclude(country_code_alpha_2="gl")
.prefetch_related("covid_monthly_active_cases")
Expand All @@ -33,9 +34,15 @@ def handle(self, *args, **kwargs):
countries = Country.objects.all().exclude(country_code_alpha_2="gl").order_by("country_code")
for country in countries:
country_code = country.country_code
dates_to_query = dates_in_period - set(dates_with_data.get(country_code, []))
country_dates = set(dates_with_data[country_code])
dates_to_query = dates_in_period - country_dates
minimum_date = min(country_dates)

for date in sorted(dates_to_query):
# Don't retry dates from the beginning of the pandemic.
if minimum_date and date < minimum_date:
continue

url = f"https://covid-api.com/api/reports?iso={country_code}&date={date}"

response = requests.get(url)
Expand Down
8 changes: 1 addition & 7 deletions country/tests/commands/test_fetch_covid_active_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ def test_command(self):
],
)

self.assertCommand(
"fetch_covid_active_cases",
expected_out="""\
Fetching https://covid-api.com/api/reports?iso=KEN&date=2020-01-31... NO DATA
Fetching https://covid-api.com/api/reports?iso=KEN&date=2020-02-29... NO DATA
""",
)
self.assertCommand("fetch_covid_active_cases")

self.assertEqual(CovidMonthlyActiveCases.objects.count(), 2)

0 comments on commit aba3b64

Please sign in to comment.