diff --git a/app/assets/v2/js/pages/leaderboard.js b/app/assets/v2/js/pages/leaderboard.js index 7667b6d678f..800661cbdc8 100644 --- a/app/assets/v2/js/pages/leaderboard.js +++ b/app/assets/v2/js/pages/leaderboard.js @@ -35,13 +35,18 @@ $(document).ready(function() { $('#key').change(function() { const val = $(this).val(); - document.location.href = `/leaderboard/${val}?cadence=` + $('#cadence').val() + '&keyword=' + $('#tech-keyword').val(); + document.location.href = `/leaderboard/${val}?cadence=` + $('#cadence').val() + '&keyword=' + $('#tech-keyword').val() + '&product=' + $('#product').val(); }); $('#cadence').change(function() { - document.location.href = document.location.pathname + '?cadence=' + $('#cadence').val() + '&keyword=' + $('#tech-keyword').val(); + document.location.href = document.location.pathname + '?cadence=' + $('#cadence').val() + '&keyword=' + $('#tech-keyword').val() + '&product=' + $('#product').val(); + }); + + $('#product').change(function() { + + document.location.href = document.location.pathname + '?cadence=' + $('#cadence').val() + '&keyword=' + $('#tech-keyword').val() + '&product=' + $('#product').val(); }); @@ -49,7 +54,7 @@ $(document).ready(function() { const keyword = $(this).val(); if (keyword === 'all') { - document.location.href = document.location.pathname + '?cadence=' + $('#cadence').val() + '&keyword='; + document.location.href = document.location.pathname + '?cadence=' + $('#cadence').val() + '&keyword=&product=' + $('#product').val(); window.location.href = new_location; } else { @@ -59,7 +64,7 @@ $(document).ready(function() { base_url = window.location.href.split('?')[0]; } - document.location.href = document.location.pathname + '?cadence=' + $('#cadence').val() + '&keyword=' + $('#tech-keyword').val(); + document.location.href = document.location.pathname + '?cadence=' + $('#cadence').val() + '&keyword=' + $('#tech-keyword').val() + '&product=' + $('#product').val(); } }); }); diff --git a/app/marketing/management/commands/assemble_leaderboards.py b/app/marketing/management/commands/assemble_leaderboards.py index ad6bc46be64..f1f07b0f087 100644 --- a/app/marketing/management/commands/assemble_leaderboards.py +++ b/app/marketing/management/commands/assemble_leaderboards.py @@ -290,6 +290,8 @@ def sum_tip_helper(t, time, index_term, val_usd): def sum_kudos(kt): val_usd = kt.value_in_usdt_now + if not kt.kudos_token_cloned_from: + return index_terms = [kt.kudos_token_cloned_from.url] for index_term in index_terms: sum_kudos_helper(kt, ALL, index_term, val_usd) @@ -372,70 +374,78 @@ class Command(BaseCommand): def handle(self, *args, **options): - # get grants - grants = Contribution.objects.filter(subscription__network='mainnet') - # iterate - for gc in grants: - index_terms = grant_index_terms(gc) - sum_grants(gc, index_terms) - - # get bounties - bounties = Bounty.objects.current().filter(network='mainnet') - - # iterate - for b in bounties: - if not b._val_usd_db: - continue - - index_terms = bounty_index_terms(b) - sum_bounties(b, index_terms) - - # get tips - tips = Tip.objects.send_success().filter(network='mainnet') - - # iterate - for t in tips: - if not t.value_in_usdt_now: - continue - index_terms = tip_index_terms(t) - sum_tips(t, index_terms) - - # kudos' - for kt in KudosTransfer.objects.send_success().filter(network='mainnet'): - sum_kudos(kt) - - # set old LR as inactive - with transaction.atomic(): - lrs = LeaderboardRank.objects.active() - lrs.update(active=False) - - # save new LR in DB - for key, rankings in ranks.items(): - rank = 1 - for index_term, amount in sorted(rankings.items(), key=lambda x: x[1], reverse=True): - count = counts[key][index_term] - lbr_kwargs = { - 'count': count, - 'active': True, - 'amount': amount, - 'rank': rank, - 'leaderboard': key, - 'github_username': index_term - } - - try: - profile = Profile.objects.get(handle__iexact=index_term) - lbr_kwargs['profile'] = profile - lbr_kwargs['tech_keywords'] = profile.keywords - except Profile.MultipleObjectsReturned: - profile = Profile.objects.filter(handle__iexact=index_term).latest('id') - lbr_kwargs['profile'] = profile - lbr_kwargs['tech_keywords'] = profile.keywords - print(f'Multiple profiles found for username: {index_term}') - except Profile.DoesNotExist: - print(f'No profiles found for username: {index_term}') - - # TODO: Bucket LeaderboardRank objects and .bulk_create - LeaderboardRank.objects.create(**lbr_kwargs) - rank += 1 - print(key, index_term, amount, count, rank) + products = ['kudos', 'grants', 'bounties', 'tips', 'all'] + for product in products: + + if product in ['all', 'grants']: + # get grants + grants = Contribution.objects.filter(subscription__network='mainnet') + # iterate + for gc in grants: + index_terms = grant_index_terms(gc) + sum_grants(gc, index_terms) + + if product in ['all', 'bounties']: + # get bounties + bounties = Bounty.objects.current().filter(network='mainnet') + + # iterate + for b in bounties: + if not b._val_usd_db: + continue + + index_terms = bounty_index_terms(b) + sum_bounties(b, index_terms) + + if product in ['all', 'tips']: + # get tips + tips = Tip.objects.send_success().filter(network='mainnet') + + # iterate + for t in tips: + if not t.value_in_usdt_now: + continue + index_terms = tip_index_terms(t) + sum_tips(t, index_terms) + + if product in ['all', 'kudos']: + # kudos' + for kt in KudosTransfer.objects.send_success().filter(network='mainnet'): + sum_kudos(kt) + + # set old LR as inactive + with transaction.atomic(): + lrs = LeaderboardRank.objects.active() + lrs.update(active=False) + + # save new LR in DB + for key, rankings in ranks.items(): + rank = 1 + for index_term, amount in sorted(rankings.items(), key=lambda x: x[1], reverse=True): + count = counts[key][index_term] + lbr_kwargs = { + 'count': count, + 'active': True, + 'amount': amount, + 'rank': rank, + 'leaderboard': key, + 'github_username': index_term, + 'product': product, + } + + try: + profile = Profile.objects.get(handle__iexact=index_term) + lbr_kwargs['profile'] = profile + lbr_kwargs['tech_keywords'] = profile.keywords + except Profile.MultipleObjectsReturned: + profile = Profile.objects.filter(handle__iexact=index_term).latest('id') + lbr_kwargs['profile'] = profile + lbr_kwargs['tech_keywords'] = profile.keywords + print(f'Multiple profiles found for username: {index_term}') + except Profile.DoesNotExist: + print(f'No profiles found for username: {index_term}') + + # TODO: Bucket LeaderboardRank objects and .bulk_create + LeaderboardRank.objects.create(**lbr_kwargs) + rank += 1 + print(key, index_term, amount, count, rank, product) diff --git a/app/marketing/migrations/0008_leaderboardrank_product.py b/app/marketing/migrations/0008_leaderboardrank_product.py new file mode 100644 index 00000000000..d63f5532317 --- /dev/null +++ b/app/marketing/migrations/0008_leaderboardrank_product.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.3 on 2019-11-17 00:44 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('marketing', '0007_job'), + ] + + operations = [ + migrations.AddField( + model_name='leaderboardrank', + name='product', + field=models.CharField(db_index=True, default='all', max_length=255), + preserve_default=False, + ), + ] diff --git a/app/marketing/models.py b/app/marketing/models.py index 97707fc281b..03a7de204da 100644 --- a/app/marketing/models.py +++ b/app/marketing/models.py @@ -204,6 +204,7 @@ class LeaderboardRank(SuperModel): active = models.BooleanField(db_index=True) count = models.IntegerField(default=0) rank = models.IntegerField(default=0) + product = models.CharField(max_length=255, db_index=True) tech_keywords = ArrayField(models.CharField(max_length=50), blank=True, default=list) objects = LeaderboardRankQuerySet.as_manager() diff --git a/app/marketing/tests/management/commands/test_assemble_leaderboards.py b/app/marketing/tests/management/commands/test_assemble_leaderboards.py index 10408192891..4979029813e 100644 --- a/app/marketing/tests/management/commands/test_assemble_leaderboards.py +++ b/app/marketing/tests/management/commands/test_assemble_leaderboards.py @@ -289,11 +289,11 @@ def test_command_handle(self): """Test command assemble leaderboards.""" Command().handle() - assert LeaderboardRank.objects.all().count() == 225 - assert LeaderboardRank.objects.filter(leaderboard="all_all").count() == 15 - assert LeaderboardRank.objects.filter(leaderboard="all_fulfilled").count() == 15 - assert LeaderboardRank.objects.filter(leaderboard="all_earners").count() == 1 - assert LeaderboardRank.objects.filter(leaderboard="all_payers").count() == 1 - assert LeaderboardRank.objects.filter(leaderboard="all_tokens").count() == 1 - assert LeaderboardRank.objects.filter(leaderboard="all_countries").count() == 3 - assert LeaderboardRank.objects.filter(leaderboard="all_keywords").count() == 2 + assert LeaderboardRank.objects.filter(product='all').all().count() == 225 + assert LeaderboardRank.objects.filter(product='all').filter(leaderboard="all_all").count() == 15 + assert LeaderboardRank.objects.filter(product='all').filter(leaderboard="all_fulfilled").count() == 15 + assert LeaderboardRank.objects.filter(product='all').filter(leaderboard="all_earners").count() == 1 + assert LeaderboardRank.objects.filter(product='all').filter(leaderboard="all_payers").count() == 1 + assert LeaderboardRank.objects.filter(product='all').filter(leaderboard="all_tokens").count() == 1 + assert LeaderboardRank.objects.filter(product='all').filter(leaderboard="all_countries").count() == 3 + assert LeaderboardRank.objects.filter(product='all').filter(leaderboard="all_keywords").count() == 2 diff --git a/app/marketing/views.py b/app/marketing/views.py index 2c72d94f9ae..f9f125014c5 100644 --- a/app/marketing/views.py +++ b/app/marketing/views.py @@ -749,6 +749,7 @@ def leaderboard(request, key=''): cadences = ['all', 'weekly', 'monthly', 'quarterly', 'yearly'] + product = request.GET.get('product', 'all') keyword_search = request.GET.get('keyword', '') keyword_search = '' if keyword_search == 'all' else keyword_search limit = request.GET.get('limit', 50) @@ -778,7 +779,7 @@ def leaderboard(request, key=''): title = titles[key] which_leaderboard = f"{cadence}_{key}" - ranks = LeaderboardRank.objects.filter(active=True, leaderboard=which_leaderboard) + ranks = LeaderboardRank.objects.filter(active=True, leaderboard=which_leaderboard, product=product) if keyword_search: ranks = ranks.filter(tech_keywords__icontains=keyword_search) @@ -810,6 +811,8 @@ def leaderboard(request, key=''): 'nav': 'home', 'titles': titles, 'cadence': cadence, + 'product': product, + 'products': ['kudos', 'grants', 'bounties', 'tips', 'all'], 'selected': title, 'is_linked_to_profile': is_linked_to_profile, 'title': page_title, diff --git a/app/retail/templates/leaderboard.html b/app/retail/templates/leaderboard.html index 50b9c7d1a41..f0a28a9fc94 100644 --- a/app/retail/templates/leaderboard.html +++ b/app/retail/templates/leaderboard.html @@ -89,6 +89,17 @@
Product :
+ +Key :