Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Fix downloads badges #567

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 34 additions & 11 deletions etc/downloads_badges.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ def millify(n):
return f'{final_output}{millnames[millidx]}'


def downloads_per_day(downloads_per_version):
"""Find the total number of downloads for a given day

Parameters
----------
downloads_per_version: dict
A dict of versions and downloads for that version

Returns
-------
total : int
The total number of downloads on a given day across all versions.
"""
total = 0
for download_stat in downloads_per_version.values():
total += download_stat

return total


def get_default_value(downloads):
"""Find the default value (one day's worth of downloads) for a given input

Expand All @@ -58,7 +78,7 @@ def get_default_value(downloads):
that are contained in the input dictionary.
"""
last_7_keys = sorted(downloads.keys())[-7:]
default_value = int(mean([downloads[key] for key in last_7_keys]))
default_value = int(mean([downloads_per_day(downloads[key]) for key in last_7_keys]))
return default_value


Expand All @@ -69,27 +89,30 @@ def get_default_value(downloads):

# Open a session to save time
session = requests.Session()
session.headers.update({"X-Api-Key": os.getenv("PEPY_API_KEY")})

# Get the data for both the legacy namespace and our current one
pyramid_arima = json.loads(session.get('https://api.pepy.tech/api/projects/pyramid-arima').text)
pmdarima = json.loads(session.get('https://api.pepy.tech/api/projects/pmdarima').text)
pyramid_arima = json.loads(session.get('https://api.pepy.tech/api/v2/projects/pyramid-arima').text)
pmdarima = json.loads(session.get('https://api.pepy.tech/api/v2/projects/pmdarima').text)

# Sum up pmdarima and pyramid-arima downloads to the past week
pmdarima_downloads = 0
default_pmdarima_value = get_default_value(pmdarima['downloads'])
for i in range(7):
pmdarima_downloads += pmdarima['downloads'].get(
(last_week + timedelta(days=i)).strftime(DATE_FORMAT),
default_pmdarima_value
)
new_downloads = pmdarima['downloads'].get((last_week + timedelta(days=i)).strftime(DATE_FORMAT))
if new_downloads is not None:
pmdarima_downloads += downloads_per_day(new_downloads)
else:
pmdarima_downloads += default_pmdarima_value

pyramid_arima_downloads = 0
default_pyramid_arima_value = get_default_value(pyramid_arima['downloads'])
for i in range(7):
pyramid_arima_downloads += pyramid_arima['downloads'].get(
(last_week + timedelta(days=i)).strftime(DATE_FORMAT),
default_pyramid_arima_value
)
new_downloads = pyramid_arima['downloads'].get((last_week + timedelta(days=i)).strftime(DATE_FORMAT))
if new_downloads is not None:
pyramid_arima_downloads += downloads_per_day(new_downloads)
else:
pyramid_arima_downloads += default_pyramid_arima_value

# Millify the totals
total_downloads = millify(pyramid_arima['total_downloads'] + pmdarima['total_downloads'])
Expand Down