Skip to content

Commit

Permalink
adds weekly leaderboard ranks to the activity feed
Browse files Browse the repository at this point in the history
  • Loading branch information
owocki committed Dec 6, 2019
1 parent 1407210 commit 0b85d6c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 83 deletions.
185 changes: 102 additions & 83 deletions app/marketing/management/commands/assemble_leaderboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,91 +382,110 @@ def should_suppress_leaderboard(handle):
return False


def do_leaderboard_feed():
from dashboard.models import Activity
max_rank = 25
for _type in [PAYERS, EARNERS, ORGS]:
key = f'{WEEKLY}_{_type}'
lrs = LeaderboardRank.objects.active().filter(leaderboard=key, rank__lte=max_rank)
print(key, lrs.count())
for lr in lrs:
metadata = {
'title': f"was ranked #{lr.rank} on the Gitcoin Weekly {_type.title()} Leaderboard",
'link': f'/leaderboard/{_type}'
}
Activity.objects.create(profile=lr.profile, activity_type='leaderboard_rank', metadata=metadata)


def do_leaderboard():
global ranks
global counts

products = ['kudos', 'grants', 'bounties', 'tips', 'all']
for product in products:

ranks = default_ranks()
counts = default_ranks()
index_terms = []

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().filter(product=product)
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)


class Command(BaseCommand):

help = 'creates leaderboard objects'

def handle(self, *args, **options):

global ranks
global counts

products = ['kudos', 'grants', 'bounties', 'tips', 'all']
for product in products:

ranks = default_ranks()
counts = default_ranks()
index_terms = []

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().filter(product=product)
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)
#do_leaderboard()
do_leaderboard_feed()
4 changes: 4 additions & 0 deletions app/retail/templates/shared/activity.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
{% elif row.activity_type == 'status_update' %}
{% trans "updated status" %}
<b>{{ row.metadata.title }}</b>
{% elif row.activity_type == 'leaderboard_rank' %}
<a href="{{ row.metadata.link }}">{{ row.metadata.title }}</a>
{% elif row.activity_type == 'new_bounty' %}
<span>{% trans "funded a new issue: " %}</span>{{ row.urled_title | safe }}
{% elif row.activity_type == 'start_work' %}
Expand Down Expand Up @@ -159,6 +161,8 @@
<a href="/kudos/{{row.kudos.kudos_token_cloned_from_id}}/{{row.kudos.kudos_token_cloned_from.name}}">
<img src="{% static row.kudos_data.image %}" alt="" class="w-100" style="max-width:100px;">
</a>
{% elif row.activity_type == 'leaderboard_rank' %}
<img src="{% static "/v2/images/kudos/money.svg" %}" alt="" class="w-100" style="max-width:100px;">
{% elif row.metadata.quest_reward %}
<img src="{{row.metadata.quest_reward}}" alt="" class="w-100" style="max-width:100px;">
{% else %}
Expand Down

0 comments on commit 0b85d6c

Please sign in to comment.