Skip to content

Commit cbff37c

Browse files
committed
fix: calculation of percentage increase for trending
1 parent 2d5a299 commit cbff37c

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-17
lines changed

django_wtf/core/models/repository_model.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from datetime import datetime, timedelta
1+
# pylint: disable=redefined-outer-name
2+
from datetime import date, datetime, timedelta
23

34
import hanzidentifier
45
from django.contrib.postgres.fields import ArrayField
@@ -65,17 +66,31 @@ def __repr__(self):
6566
def __str__(self):
6667
return self.__repr__()
6768

68-
def stars_since(self, td: timedelta, date=None):
69+
def stars_since(self, since: timedelta, date=None):
6970
if date is None:
7071
date = datetime.today().date()
71-
previous_date = date - td
72+
previous_date = date - since
73+
return self.stars - self.stars_at(previous_date)
74+
75+
def stars_at(self, date: date):
7276
try:
7377
previous_stars = RepositoryStars.objects.get(
74-
created_at=previous_date, repository=self
78+
created_at=date, repository=self
7579
)
7680
except ObjectDoesNotExist:
7781
return 0
78-
return self.stars - previous_stars.stars
82+
return previous_stars.stars
83+
84+
def stars_relative_increase(self, since: timedelta):
85+
"""
86+
Return the relative increase in stars going back `since` timedelta.
87+
Useful for calculating the percentage increase in stars.
88+
"""
89+
before = datetime.today().date() - since
90+
try:
91+
return (self.stars - self.stars_at(before)) / self.stars_at(before)
92+
except ZeroDivisionError:
93+
return 0
7994

8095
@property
8196
def truncated_description(self):

django_wtf/core/queries.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
def trending_repositories(days_since, **filters):
1818
trending = []
1919
for repo in Repository.valid.filter(stars__gte=20, **filters):
20-
stars_in_the_last_week = repo.stars_since(timedelta(days=days_since))
21-
if stars_in_the_last_week > 0:
22-
setattr(repo, "stars_lately", stars_in_the_last_week)
23-
setattr(repo, "stars_quota", repo.stars_lately / repo.stars) # type: ignore
20+
delta = timedelta(days=days_since)
21+
stars_gained = repo.stars_since(delta)
22+
if stars_gained > 0:
23+
setattr(repo, "stars_gained", stars_gained)
24+
setattr(
25+
repo, "percentage_increase", repo.stars_relative_increase(delta) * 100
26+
)
2427
trending.append(repo)
2528

26-
return sorted(trending, key=lambda e: e.stars_quota, reverse=True)
29+
return sorted(trending, key=lambda e: e.percentage_increase, reverse=True)
2730

2831

2932
@cached_as(Contributor, timeout=60 * 60 * 24)

django_wtf/core/templatetags/wtf_tags.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
@register.filter
77
def to_percent(obj):
88
if obj:
9-
return f"{obj:.2%}"
10-
return obj
9+
if obj > 10:
10+
return f"{obj:.0f}%"
11+
return f"{obj:.2f}%"
12+
return ""

django_wtf/core/views/test_trending_repositories_view.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_trending_by_week(
2424
res = user_client.get(url + "?trending=7")
2525
assert res.context["object_list"][0] == more_popular
2626
assert res.context["object_list"][1] == less_popular
27-
assert res.context["object_list"][0].stars_lately == 30
28-
assert res.context["object_list"][0].stars_quota == 0.9375
29-
assert res.context["object_list"][1].stars_lately == 30
30-
assert res.context["object_list"][1].stars_quota == 0.75
27+
assert res.context["object_list"][0].stars_gained == 30
28+
assert res.context["object_list"][0].percentage_increase == 1500.0
29+
assert res.context["object_list"][1].stars_gained == 30
30+
assert res.context["object_list"][1].percentage_increase == 300

django_wtf/templates/core/trending_repositories.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
</td>
5858
<td class="text-right">
5959
<a href="{{ url }}" target="_blank">
60-
<strong>{{ repo.stars_quota | to_percent }}</strong> ({{ repo.stars_lately }})
60+
<strong>{{ repo.percentage_increase | to_percent }}</strong> ({{ repo.stars_gained }})
6161
</a>
6262
</td>
6363
{% endwith %}

0 commit comments

Comments
 (0)