Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GITC-245: Implements the CLR calc in SQL #9348

Merged
merged 11 commits into from
Aug 20, 2021
46 changes: 46 additions & 0 deletions app/dashboard/management/commands/update_trust_bonus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'''
Copyright (C) 2021 Gitcoin Core

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

'''

from django.core.management.base import BaseCommand

from dashboard.models import Profile
from dashboard.tasks import update_trust_bonus


class Command(BaseCommand):

help = 'Update every users trust_bonus score'

def add_arguments(self, parser):
parser.add_argument(
'--call-now',
type=int,
help="disable execution on celery and call now"
)

def handle(self, *args, **options):
profiles = Profile.objects.all()
print(profiles.count())
for profile in profiles.iterator():
if (options['call_now']):
params = profile.as_dict
params['trust_bonus'] = profile.trust_bonus
print("Saving - %s - %s" % (profile.handle, params['trust_bonus']))
profile.save()
else:
update_trust_bonus.delay(profile.pk)
1 change: 1 addition & 0 deletions app/dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4400,6 +4400,7 @@ def to_dict(self):
'card_title': f'@{self.handle} | Gitcoin',
'org_works_with': org_works_with,
'card_desc': desc,
'trust_bonus': self.trust_bonus,
'avatar_url': self.avatar_url_with_gitcoin_logo,
'count_bounties_completed': total_fulfilled,
'works_with_collected': works_with_collected,
Expand Down
15 changes: 15 additions & 0 deletions app/dashboard/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,21 @@ def profile_dict(self, pk, retry: bool = True) -> None:
profile.save()


@app.shared_task(bind=True, max_retries=3)
def update_trust_bonus(self, pk):
"""
:param self:
:param pk:
:return:
"""
profile = Profile.objects.get(pk=pk)
params = profile.as_dict
if profile.trust_bonus != params.get('trust_bonus', None):
params['trust_bonus'] = profile.trust_bonus
print("Saving - %s - %s" % (profile.handle, params['trust_bonus']))
profile.save()


@app.shared_task(bind=True)
def maybe_market_to_user_slack(self, bounty_pk, event_name, retry: bool = True) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion app/grants/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class GrantAdmin(GeneralAdmin):
'active', 'visible', 'is_clr_eligible',
'migrated_to', 'region',
'grant_type', 'categories', 'description', 'description_rich', 'github_project_url', 'reference_url', 'admin_address',
'amount_received', 'amount_received_in_round', 'monthly_amount_subscribed',
'amount_received', 'amount_received_in_round', 'monthly_amount_subscribed', 'defer_clr_to',
'deploy_tx_id', 'cancel_tx_id', 'admin_profile', 'token_symbol',
'token_address', 'contract_address', 'contract_version', 'network', 'required_gas_price', 'logo_svg_asset',
'logo_asset', 'created_on', 'modified_on', 'team_member_list',
Expand Down
12 changes: 11 additions & 1 deletion app/grants/clr.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,12 @@ def predict_clr(save_to_db=False, from_date=None, clr_round=None, network='mainn
print(f"- starting slim grant calc at {round(time.time(),1)}")
grants_clr = run_clr_calcs(grant_contributions_curr, v_threshold, uv_threshold, total_pot)
print(f"- saving slim grant calc at {round(time.time(),1)}")
total_count = len(grants_clr.items())
for grant_calc in grants_clr:
counter += 1
if counter % 10 == 0 or True:
print(f"- {counter}/{total_count} grants iter, pk:{grant_id}, at {round(time.time(),1)}")

pk = grant_calc['id']
grant = clr_round.grants.using('default').get(pk=pk)
latest_calc = grant.clr_calculations.using('default').filter(latest=True, grantclr=clr_round).order_by('-pk').first()
Expand All @@ -424,10 +429,12 @@ def predict_clr(save_to_db=False, from_date=None, clr_round=None, network='mainn
continue
clr_prediction_curve = copy.deepcopy(latest_calc.clr_prediction_curve)
clr_prediction_curve[0][1] = grant_calc['clr_amount'] # update only the existing match estimate
print(clr_prediction_curve)
clr_round.record_clr_prediction_curve(grant, clr_prediction_curve)
grant.save()
# if we are only calculating slim CLR calculations, return here and save 97% compute power
print(f"- done calculating at {round(time.time(),1)}")
print(f"\nTotal execution time: {(timezone.now() - clr_calc_start_time)}")
return

print(f"- starting grants iter at {round(time.time(),1)}")
Expand Down Expand Up @@ -460,7 +467,6 @@ def predict_clr(save_to_db=False, from_date=None, clr_round=None, network='mainn
else:
for amount in potential_donations:
# calculate clr with each additional donation and save to grants model
# print(f'using {total_pot_close}')
predicted_clr, grants_clr, _, _ = calculate_clr_for_donation(
grant, amount, grant_contributions_curr, total_pot, v_threshold, uv_threshold
)
Expand All @@ -480,11 +486,15 @@ def predict_clr(save_to_db=False, from_date=None, clr_round=None, network='mainn
else:
clr_prediction_curve = [[0.0, 0.0, 0.0] for x in range(0, 6)]

print(clr_prediction_curve)

clr_round.record_clr_prediction_curve(_grant, clr_prediction_curve)

if from_date > (clr_calc_start_time - timezone.timedelta(hours=1)):
_grant.save()

debug_output.append({'grant': grant.id, "clr_prediction_curve": (potential_donations, potential_clr), "grants_clr": grants_clr})

print(f"\nTotal execution time: {(timezone.now() - clr_calc_start_time)}")

return debug_output
Loading