From 859f44cc30a72aabc7028b53fbee9881391ad93a Mon Sep 17 00:00:00 2001 From: Owocki Date: Sat, 16 Nov 2019 18:05:04 -0700 Subject: [PATCH 1/2] allows leaderboard to be browsed by product" --- app/assets/v2/js/pages/leaderboard.js | 13 +- .../commands/assemble_leaderboards.py | 144 ++++++++++-------- .../0008_leaderboardrank_product.py | 19 +++ app/marketing/models.py | 1 + app/marketing/views.py | 5 +- app/retail/templates/leaderboard.html | 11 ++ 6 files changed, 121 insertions(+), 72 deletions(-) create mode 100644 app/marketing/migrations/0008_leaderboardrank_product.py diff --git a/app/assets/v2/js/pages/leaderboard.js b/app/assets/v2/js/pages/leaderboard.js index 7667b6d678f..456813d45e4 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 b893ce139ce..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')[0:5] - # 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/views.py b/app/marketing/views.py index 8f5d908879d..bbe5c691fc5 100644 --- a/app/marketing/views.py +++ b/app/marketing/views.py @@ -744,6 +744,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) @@ -773,7 +774,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) @@ -805,6 +806,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 e66c1a3bf33..1ff21d9be40 100644 --- a/app/retail/templates/leaderboard.html +++ b/app/retail/templates/leaderboard.html @@ -89,6 +89,17 @@

{{title}}

+
+

Product :

+ +
+
{% if technologies|length > 0 %}

Key :        

From 9046db2002d0f08b738347c1c2ee30d6daa7fb7c Mon Sep 17 00:00:00 2001 From: Owocki Date: Sat, 16 Nov 2019 18:07:41 -0700 Subject: [PATCH 2/2] linter --- app/assets/v2/js/pages/leaderboard.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/assets/v2/js/pages/leaderboard.js b/app/assets/v2/js/pages/leaderboard.js index 456813d45e4..800661cbdc8 100644 --- a/app/assets/v2/js/pages/leaderboard.js +++ b/app/assets/v2/js/pages/leaderboard.js @@ -54,7 +54,7 @@ $(document).ready(function() { const keyword = $(this).val(); if (keyword === 'all') { - document.location.href = document.location.pathname + '?cadence=' + $('#cadence').val() + '&keyword=' + '&product=' + $('#product').val(); + document.location.href = document.location.pathname + '?cadence=' + $('#cadence').val() + '&keyword=&product=' + $('#product').val(); window.location.href = new_location; } else { @@ -64,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() + '&product=' + $('#product').val(); + document.location.href = document.location.pathname + '?cadence=' + $('#cadence').val() + '&keyword=' + $('#tech-keyword').val() + '&product=' + $('#product').val(); } }); });