Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions backend/leaderboard/tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
from django.test import TestCase
from django.utils import timezone
from rest_framework.test import APIClient

from contributions.models import (
Category,
Contribution,
ContributionType,
SubmittedContribution,
)
from leaderboard.models import ReferralPoints
from users.models import User


class LeaderboardStatsTest(TestCase):
def setUp(self):
self.client = APIClient()
self.community_category, _ = Category.objects.get_or_create(
slug='community',
defaults={'name': 'Community'}
)
self.builder_category, _ = Category.objects.get_or_create(
slug='builder',
defaults={'name': 'Builder'}
)
self.community_type = ContributionType.objects.create(
name='Community Post',
slug='community-post',
category=self.community_category
)
self.builder_type = ContributionType.objects.create(
name='Builder Submission',
slug='builder-submission',
category=self.builder_category
)

def _create_user(self, email, address, visible=True):
return User.objects.create_user(
email=email,
password='pass',
address=address,
visible=visible
)

def test_community_member_count_uses_accepted_community_contributions(self):
now = timezone.now()
community_user = self._create_user(
'community@example.com',
'0x0000000000000000000000000000000000000001'
)
repeat_community_user = self._create_user(
'repeat@example.com',
'0x0000000000000000000000000000000000000002'
)
builder_only_user = self._create_user(
'builder@example.com',
'0x0000000000000000000000000000000000000003'
)
hidden_community_user = self._create_user(
'hidden@example.com',
'0x0000000000000000000000000000000000000004',
visible=False
)
referral_only_user = self._create_user(
'referral@example.com',
'0x0000000000000000000000000000000000000005'
)
pending_user = self._create_user(
'pending@example.com',
'0x0000000000000000000000000000000000000006'
)

Contribution.objects.bulk_create([
Contribution(
user=community_user,
contribution_type=self.community_type,
points=10,
frozen_global_points=10,
contribution_date=now
),
Contribution(
user=repeat_community_user,
contribution_type=self.community_type,
points=10,
frozen_global_points=10,
contribution_date=now
),
Contribution(
user=repeat_community_user,
contribution_type=self.community_type,
points=5,
frozen_global_points=5,
contribution_date=now
),
Contribution(
user=builder_only_user,
contribution_type=self.builder_type,
points=10,
frozen_global_points=10,
contribution_date=now
),
Contribution(
user=hidden_community_user,
contribution_type=self.community_type,
points=10,
frozen_global_points=10,
contribution_date=now
),
])
ReferralPoints.objects.create(
user=referral_only_user,
builder_points=100,
validator_points=100
)
SubmittedContribution.objects.create(
user=pending_user,
contribution_type=self.community_type,
contribution_date=now,
state='pending'
)

response = self.client.get('/api/v1/leaderboard/stats/')

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['community_member_count'], 2)
self.assertEqual(response.data['creator_count'], 2)
32 changes: 21 additions & 11 deletions backend/leaderboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,18 @@ def stats(self, request):
user__visible=True
)

participant_count = leaderboard_entries.count()

# Get contribution count for this category
category_map = {
'validator': 'validator',
'builder': 'builder',
'steward': 'steward'
'steward': 'steward',
'community': 'community'
}
category = category_map.get(leaderboard_type)

if category:
category_contributions = Contribution.objects.filter(
user__visible=True,
contribution_type__category__slug=category
).exclude(
contribution_type__slug__in=['builder-welcome', 'builder', 'validator-waitlist', 'validator']
Expand All @@ -304,13 +304,18 @@ def stats(self, request):
new_points_count = category_contributions.filter(
created_at__gte=last_month
).aggregate(total=Sum('frozen_global_points'))['total'] or 0
if category == 'community':
participant_count = category_contributions.values('user_id').distinct().count()
else:
participant_count = leaderboard_entries.count()
else:
contribution_count = 0
new_contributions_count = 0
new_points_count = 0
total_points = leaderboard_entries.aggregate(
total=Sum('total_points')
)['total'] or 0
participant_count = leaderboard_entries.count()

# New participants in the last 30 days for the specific leaderboard type
if leaderboard_type == 'builder':
Expand Down Expand Up @@ -401,21 +406,26 @@ def stats(self, request):
)
validator_count = validator_contribs.values('user_id').distinct().count()

from .models import ReferralPoints
from django.db.models import F
creator_count = ReferralPoints.objects.filter(
user__visible=True
).annotate(
total_pts=F('builder_points') + F('validator_points')
).filter(total_pts__gt=0).count()
community_member_count = Contribution.objects.filter(
user__visible=True,
contribution_type__category__slug='community'
).values('user_id').distinct().count()
new_community_members_count = Contribution.objects.filter(
user__visible=True,
contribution_type__category__slug='community',
created_at__gte=last_month
).values('user_id').distinct().count()

return Response({
'participant_count': participant_count,
'contribution_count': contribution_count,
'total_points': total_points,
'builder_count': builder_count,
'validator_count': validator_count,
'creator_count': creator_count,
'community_member_count': community_member_count,
# Backward-compatible alias for older clients.
'creator_count': community_member_count,
'new_community_members_count': new_community_members_count,
'new_builders_count': new_builders_count,
'new_validators_count': new_validators_count,
'new_contributions_count': new_contributions_count,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
'/leaderboard': Leaderboard,
'/participants': Validators,
'/referrals': Referrals,
'/community': ReferralProgram,
'/community': Dashboard,
'/community/contributions': Contributions,
'/community/all-contributions': AllContributions,
'/community/referrals': Referrals,
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/portal/LiveStats.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
category: 'validator',
},
{
key: 'creator_count',
key: 'community_member_count',
label: 'Community Members',
value: stats ? formatNumber(stats.creator_count ?? stats.participant_count) : '—',
value: stats ? formatNumber(stats.community_member_count ?? stats.creator_count ?? stats.participant_count) : '—',
delta: '+15%',
category: 'community',
},
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/ui/Podium.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
firstGradient: 'linear-gradient(135deg, #6bdc8a 0%, #3eb359 48%, #207b39 100%)',
glow: 'rgba(62, 179, 89, 0.25)',
},
community: {
firstGradient: 'linear-gradient(135deg, #aa8dff 0%, #7f52e1 48%, #4630a3 100%)',
glow: 'rgba(127, 82, 225, 0.25)',
},
referral: {
firstGradient: 'linear-gradient(135deg, #aa8dff 0%, #7f52e1 48%, #4630a3 100%)',
glow: 'rgba(127, 82, 225, 0.25)',
Expand Down
Loading