Skip to content

Commit cc73208

Browse files
committed
Sort minor versions properly in plots (#51)
1 parent bb0da77 commit cc73208

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

pypistats/views/general.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,15 @@ def package_page(package):
139139
else:
140140
metrics = ["downloads", "percentages"]
141141

142+
if model == PythonMinorDownloadCount:
143+
category_key = python_minor_key
144+
else:
145+
category_key = None
146+
142147
for metric in metrics:
143-
model_data.append({"metric": metric, "name": model.__tablename__, "data": data_function[metric](records)})
148+
model_data.append(
149+
{"metric": metric, "name": model.__tablename__, "data": data_function[metric](records, category_key)}
150+
)
144151

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

193200

194-
def get_download_data(records):
201+
def python_minor_key(version):
202+
try:
203+
key = [""] + [int(p) for p in version.split(".")]
204+
except ValueError:
205+
key = [version]
206+
207+
return key
208+
209+
210+
def get_download_data(records, category_key=None):
195211
"""Organize the data for the absolute plots."""
196212
data = defaultdict(lambda: {"x": [], "y": []})
197213

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

207-
all_categories = sorted(all_categories)
223+
all_categories = sorted(all_categories, key=category_key)
208224
for category in all_categories:
209225
data[category] # set the dict value (keeps it ordered)
210226

@@ -244,7 +260,7 @@ def get_download_data(records):
244260
return data
245261

246262

247-
def get_proportion_data(records):
263+
def get_proportion_data(records, category_key=None):
248264
"""Organize the data for the fill plots."""
249265
data = defaultdict(lambda: {"x": [], "y": [], "text": []})
250266

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

260-
all_categories = sorted(all_categories)
276+
all_categories = sorted(all_categories, key=category_key)
261277
for category in all_categories:
262278
data[category] # set the dict value (keeps it ordered)
263279

0 commit comments

Comments
 (0)