Skip to content

Commit 3fbe03d

Browse files
authored
Fixed API handler for grants contribution statistics, to use GrantContributionIndex + updating the command compute_grant_contribution_index (#10760)
1 parent fd8fd4c commit 3fbe03d

File tree

3 files changed

+76
-28
lines changed

3 files changed

+76
-28
lines changed

app/grants/management/commands/compute_grant_contribution_index.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
import datetime as dt
2-
from datetime import timezone
2+
from datetime import datetime, timezone
33
from time import sleep
44

55
from django.core.management.base import BaseCommand
6+
from django.db.models import F
67

78
import requests
8-
from grants.models import Contribution, GrantContributionIndex
9+
from grants.models import Contribution, Grant, GrantContributionIndex
910

1011

1112
class Command(BaseCommand):
1213

1314
help = "rebuilds the table GrantContributionIndex"
1415

1516
def handle(self, *args, **kwargs):
17+
self.stdout.write(f"{datetime.now()} Building query for contributions ...")
1618
contributions = (
1719
Contribution.objects.filter(
1820
success=True,
1921
subscription__network="mainnet",
2022
subscription__grant__clr_calculations__latest=True,
23+
subscription__contributor_profile__isnull=False,
24+
created_on__gt=F(
25+
"subscription__grant__clr_calculations__grantclr__start_date"
26+
),
27+
created_on__lt=F(
28+
"subscription__grant__clr_calculations__grantclr__end_date"
29+
),
2130
)
2231
.order_by(
2332
"subscription__contributor_profile__id",
@@ -36,13 +45,49 @@ def handle(self, *args, **kwargs):
3645
)
3746
)
3847

48+
self.stdout.write(f"{datetime.now()} Building contribIndexList ...")
3949
contribIndexList = [
4050
GrantContributionIndex(
4151
profile_id=contribInfo[0],
42-
grant_id=contribInfo[1],
43-
round_num=contribInfo[2],
52+
round_num=contribInfo[1],
53+
grant_id=contribInfo[2],
4454
)
4555
for contribInfo in contributions
4656
]
4757

48-
GrantContributionIndex.objects.bulk_create(contribIndexList, batch_size=100)
58+
count = len(contribIndexList)
59+
self.stdout.write(f"{datetime.now()} Length of contribIndexList: {count}")
60+
self.stdout.write(f"{datetime.now()} Clearing GrantContributionIndex ...")
61+
62+
first_id = 0
63+
last_id = 0
64+
65+
try:
66+
first_id = GrantContributionIndex.objects.all().order_by("id")[0].id
67+
last_id = GrantContributionIndex.objects.all().order_by("-id")[0].id
68+
except:
69+
pass
70+
71+
self.stdout.write(
72+
f"{datetime.now()} ... deleting {last_id - first_id + 1} records"
73+
)
74+
75+
count_to_delete = last_id - first_id
76+
count_deleted = 0
77+
batch_size = 50000
78+
for i in range(first_id, last_id, batch_size):
79+
count_deleted += batch_size
80+
self.stdout.write(
81+
f"{datetime.now()} ... {(count_deleted / count_to_delete * 100):.2f}% deleting {count} records up to id {i + batch_size}"
82+
)
83+
GrantContributionIndex.objects.filter(id__lt=i + batch_size).delete()
84+
85+
self.stdout.write(f"{datetime.now()} Saving to GrantContributionIndex ...")
86+
# GrantContributionIndex.objects.bulk_create(contribIndexList, batch_size=1000, ignore_conflicts=True)
87+
for i in range(0, count, batch_size):
88+
self.stdout.write(
89+
f"{datetime.now()} {(i / count * 100):.2f}% Saving to GrantContributionIndex ..."
90+
)
91+
GrantContributionIndex.objects.bulk_create(
92+
contribIndexList[i : i + batch_size], ignore_conflicts=True
93+
)

app/grants/models/grant_contribution_index.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
class GrantContributionIndex(SuperModel):
88
"""Stores the grants and round number to shich a user contributed to.
99
The purpose of this table is to allow a a fast query. This will be used from
10-
the `contributor_statistics`API """
10+
the `contributor_statistics` API """
1111

1212
profile = models.ForeignKey('dashboard.Profile', help_text=_('Contributor'), on_delete=models.CASCADE, db_index=True)
13-
grant = models.ForeignKey('Grant', help_text=_('The grant a user contributed to'), on_delete=models.CASCADE)
13+
grant = models.ForeignKey('grants.Grant', help_text=_('The grant a user contributed to'), on_delete=models.CASCADE)
1414
round_num = models.IntegerField(help_text=_('The round number a user contributed to'))

app/grants/views_api_vc.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
1919
"""
2020
import logging
21+
from datetime import datetime
2122

2223
from django.conf import settings
2324
from django.db.models import Sum
2425
from django.http import JsonResponse
2526
from django.utils.translation import gettext_lazy as _
2627

27-
from grants.models import Contribution, Grant
28+
from grants.models import Contribution, Grant, GrantContributionIndex
2829
from perftools.models import StaticJsonEnv
2930
from townsquare.models import SquelchProfile
3031

@@ -75,46 +76,48 @@ def contributor_statistics(request):
7576

7677
# Get number of grants the user contributed to
7778
num_grants_contribute_to = (
78-
Contribution.objects.filter(profile_for_clr__handle=handle, success=True)
79+
GrantContributionIndex.objects.filter(profile__handle=handle)
7980
.order_by("grant_id")
8081
.distinct("grant_id")
8182
.count()
8283
)
8384

84-
# Get the number of grants the user contributed to
85+
# Get number of rounds the user contributed to
8586
num_rounds_contribute_to = (
87+
GrantContributionIndex.objects.filter(profile__handle=handle)
88+
.order_by("round_num")
89+
.distinct("round_num")
90+
.count()
91+
)
92+
93+
# Get the total number of contributions
94+
contribution_list = list(
8695
Contribution.objects.filter(
96+
profile_for_clr__handle=handle,
8797
success=True,
88-
subscription__contributor_profile__handle=handle,
8998
subscription__network="mainnet",
9099
subscription__grant__clr_calculations__latest=True,
91100
)
92-
.order_by("subscription__grant__clr_calculations__grantclr__round_num")
93-
.distinct("subscription__grant__clr_calculations__grantclr__round_num")
94-
.count()
101+
.order_by(
102+
"id",
103+
)
104+
.distinct(
105+
"id",
106+
)
107+
.values_list("id", "amount_per_period_usdt")
95108
)
96109

97-
total_contribution_amount = Contribution.objects.filter(
98-
profile_for_clr__handle=handle, success=True
99-
).aggregate(Sum("amount_per_period_usdt"))["amount_per_period_usdt__sum"]
100-
total_contribution_amount = (
101-
total_contribution_amount if total_contribution_amount is not None else 0
102-
)
110+
total_contribution_amount = sum([i[1] for i in contribution_list])
103111

104112
# GR14 contributor (and not squelched by FDD)
113+
start = datetime.now()
105114
profile_squelch = SquelchProfile.objects.filter(
106115
profile__handle=handle, active=True
107116
).values_list("profile_id", flat=True)
108117

109118
num_gr14_contributions = (
110-
Contribution.objects.filter(
111-
success=True,
112-
subscription__contributor_profile__handle=handle,
113-
subscription__network="mainnet",
114-
subscription__grant__clr_calculations__latest=True,
115-
subscription__grant__clr_calculations__grantclr__round_num=14,
116-
)
117-
.exclude(subscription__contributor_profile_id__in=profile_squelch)
119+
GrantContributionIndex.objects.filter(profile__handle=handle, round_num=14)
120+
.exclude(profile_id__in=profile_squelch)
118121
.count()
119122
)
120123

0 commit comments

Comments
 (0)