Skip to content

Commit

Permalink
Sort minor versions properly in plots (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrobst committed Sep 3, 2022
1 parent bb0da77 commit 56842b2
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions pypistats/views/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,15 @@ def package_page(package):
else:
metrics = ["downloads", "percentages"]

if model == PythonMinorDownloadCount:
category_key = python_minor_key
else:
category_key = None

for metric in metrics:
model_data.append({"metric": metric, "name": model.__tablename__, "data": data_function[metric](records)})
model_data.append(
{"metric": metric, "name": model.__tablename__, "data": data_function[metric](records, category_key)}
)

# Build the plots
plots = []
Expand Down Expand Up @@ -191,7 +198,16 @@ def package_page(package):
return render_template("package.html", package=package, plots=plots, metadata=metadata, recent=recent, user=g.user)


def get_download_data(records):
def python_minor_key(version):
try:
key = [""] + [int(p) for p in version.split(".")]
except ValueError:
key = [version]

return key


def get_download_data(records, category_key=None):
"""Organize the data for the absolute plots."""
data = defaultdict(lambda: {"x": [], "y": []})

Expand All @@ -204,7 +220,7 @@ def get_download_data(records):
if record.category not in all_categories:
all_categories.append(record.category)

all_categories = sorted(all_categories)
all_categories = sorted(all_categories, key=category_key)
for category in all_categories:
data[category] # set the dict value (keeps it ordered)

Expand Down Expand Up @@ -244,7 +260,7 @@ def get_download_data(records):
return data


def get_proportion_data(records):
def get_proportion_data(records, category_key=None):
"""Organize the data for the fill plots."""
data = defaultdict(lambda: {"x": [], "y": [], "text": []})

Expand All @@ -257,7 +273,7 @@ def get_proportion_data(records):
if record.category not in all_categories:
all_categories.append(record.category)

all_categories = sorted(all_categories)
all_categories = sorted(all_categories, key=category_key)
for category in all_categories:
data[category] # set the dict value (keeps it ordered)

Expand Down

0 comments on commit 56842b2

Please sign in to comment.