1818
1919"""
2020import logging
21+ from datetime import datetime
2122
2223from django .conf import settings
2324from django .db .models import Sum
2425from django .http import JsonResponse
2526from django .utils .translation import gettext_lazy as _
2627
27- from grants .models import Contribution , Grant
28+ from grants .models import Contribution , Grant , GrantContributionIndex
2829from perftools .models import StaticJsonEnv
2930from townsquare .models import SquelchProfile
3031
@@ -75,46 +76,39 @@ 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 = (
86- Contribution .objects .filter (
87- success = True ,
88- subscription__contributor_profile__handle = handle ,
89- subscription__network = "mainnet" ,
90- subscription__grant__clr_calculations__latest = True ,
91- )
92- .order_by ("subscription__grant__clr_calculations__grantclr__round_num" )
93- .distinct ("subscription__grant__clr_calculations__grantclr__round_num" )
87+ GrantContributionIndex .objects .filter (profile__handle = handle )
88+ .order_by ("round_num" )
89+ .distinct ("round_num" )
9490 .count ()
9591 )
9692
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- )
93+ # Get the total contribution amount
94+ total_contribution_amount = GrantContributionIndex .objects .filter (
95+ profile__handle = handle
96+ ).aggregate (total_contribution_amount = Sum ("amount" ))["total_contribution_amount" ]
97+
98+ total_contribution_amount
99+
100+ if total_contribution_amount is None :
101+ total_contribution_amount = 0
103102
104103 # GR14 contributor (and not squelched by FDD)
104+ start = datetime .now ()
105105 profile_squelch = SquelchProfile .objects .filter (
106106 profile__handle = handle , active = True
107107 ).values_list ("profile_id" , flat = True )
108108
109109 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 )
110+ GrantContributionIndex .objects .filter (profile__handle = handle , round_num = 14 )
111+ .exclude (profile_id__in = profile_squelch )
118112 .count ()
119113 )
120114
@@ -123,7 +117,7 @@ def contributor_statistics(request):
123117 "num_grants_contribute_to" : num_grants_contribute_to ,
124118 "num_rounds_contribute_to" : num_rounds_contribute_to ,
125119 "total_contribution_amount" : total_contribution_amount ,
126- "is_gr14_contributor " : num_gr14_contributions > 0 ,
120+ "num_gr14_contributions " : num_gr14_contributions ,
127121 }
128122 )
129123
0 commit comments