8000 Kevin fix multigrant by thelostone-mc · Pull Request #7870 · gitcoinco/web · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Kevin fix multigrant #7870

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

Merged
merged 8 commits into from
Nov 18, 2020
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
20 changes: 11 additions & 9 deletions app/grants/clr.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,37 +413,39 @@ def predict_clr(save_to_db=False, from_date=None, clr_round=None, network='mainn

if save_to_db:
_grant = Grant.objects.get(pk=grant.pk)
_grant.clr_prediction_curve = list(zip(potential_donations, potential_clr))
base = _grant.clr_prediction_curve[0][1]
clr_prediction_curve = list(zip(potential_donations, potential_clr))
base = clr_prediction_curve[0][1]
_grant.last_clr_calc_date = timezone.now()
_grant.next_clr_calc_date = timezone.now() + timezone.timedelta(minutes=20)

can_estimate = True if base or _grant.clr_prediction_curve[1][1] or _grant.clr_prediction_curve[2][1] or _grant.clr_prediction_curve[3][1] else False
can_estimate = True if base or clr_prediction_curve[1][1] or clr_prediction_curve[2][1] or clr_prediction_curve[3][1] else False

if can_estimate :
_grant.clr_prediction_curve = [[ele[0], ele[1], ele[1] - base] for ele in _grant.clr_prediction_curve ]
clr_prediction_curve = [[ele[0], ele[1], ele[1] - base] for ele in clr_prediction_curve ]
else:
_grant.clr_prediction_curve = [[0.0, 0.0, 0.0] for x in range(0, 6)]
clr_prediction_curve = [[0.0, 0.0, 0.0] for x in range(0, 6)]

JSONStore.objects.create(
created_on=from_date,
view='clr_contribution',
key=f'{grant.id}',
data=_grant.clr_prediction_curve,
data=clr_prediction_curve,
)
clr_round.record_clr_prediction_curve(_grant, clr_prediction_curve)

try:
if _grant.clr_prediction_curve[0][1]:
if clr_prediction_curve[0][1]:
Stat.objects.create(
created_on=from_date,
key=_grant.title[0:43] + "_match",
val=_grant.clr_prediction_curve[0][1],
val=clr_prediction_curve[0][1],
)
max_twitter_followers = max(_grant.twitter_handle_1_follower_count, _grant.twitter_handle_2_follower_count)
if max_twitter_followers:
Stat.objects.create(
created_on=from_date,
key=_grant.title[0:43] + "_admt1",
val=int(100 * _grant.clr_prediction_curve[0][1]/max_twitter_followers),
val=int(100 * clr_prediction_curve[0][1]/max_twitter_followers),
)

if _grant.positive_round_contributor_count:
Expand Down
35 changes: 35 additions & 0 deletions app/grants/migrations/0093_auto_20201113_0559.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 2.2.4 on 2020-11-13 05:59

import django.contrib.postgres.fields
from django.db import migrations, models
import django.db.models.deletion
import economy.models


class Migration(migrations.Migration):

dependencies = [
('grants', '0092_auto_20201111_1301'),
]

operations = [
migrations.RemoveField(
model_name='grant',
name='backup_clr_prediction_curve',
),
migrations.CreateModel(
name='GrantCLRCalculation',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_on', models.DateTimeField(db_index=True, default=economy.models.get_time)),
('modified_on', models.DateTimeField(default=economy.models.get_time)),
('latest', models.BooleanField(db_index=True, default=False, help_text='Is this calc the latest?')),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh its already there! nice!

('clr_prediction_curve', django.contrib.postgres.fields.ArrayField(base_field=django.contrib.postgres.fields.ArrayField(base_field=models.FloatField(), size=2), blank=True, default=list, help_text='5 point curve to predict CLR donations.', size=None)),
('grant', models.ForeignKey(help_text='The grant', on_delete=django.db.models.deletion.CASCADE, related_name='clr_calculations', to='grants.Grant')),
('grantclr', models.ForeignKey(help_text='The grant CLR Round', on_delete=django.db.models.deletion.CASCADE, related_name='clr_calculations', to='grants.GrantCLR')),
],
options={
'abstract': False,
},
),
]
90 changes: 76 additions & 14 deletions app/grants/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,29 @@ def __str__(self):

@property
def grants(self):
return Grant.objects.filter(**self.grant_filters).filter(is_clr_eligible=True)

grants = Grant.objects.filter(hidden=False, active=True, is_clr_eligible=True, link_to_new_grant=None)
if self.grant_filters:
grants = grants.filter(**self.grant_filters)
if self.subscription_filters:
grants = grants.filter(**self.subscription_filters)
if self.collection_filters:
grants = grants.filter(**self.collection_filters)

return grants


def record_clr_prediction_curve(self, grant, clr_prediction_curve):
for obj in self.clr_calculations.filter(grant=grant):
obj.latest = False
obj.save()

GrantCLRCalculation.objects.create(
grantclr=self,
grant=grant,
clr_prediction_curve=clr_prediction_curve,
latest=True,
)


class Grant(SuperModel):
Expand Down Expand Up @@ -331,18 +353,6 @@ class Meta:
max_digits=20,
help_text=_('The fundingamount across all rounds with phantom funding'),
)
# TODO-CROSS-GRANT: [{round: fk1, value: time}]
clr_prediction_curve = ArrayField(
ArrayField(
models.FloatField(),
size=2,
), blank=True, default=list, help_text=_('5 point curve to predict CLR donations.'))
# TODO: REMOVE
backup_clr_prediction_curve = ArrayField(
ArrayField(
models.FloatField(),
size=2,
), blank=True, default=list, help_text=_('backup 5 point curve to predict CLR donations - used to store a secondary backup of the clr prediction curve, in the case a new identity mechanism is used'))
activeSubscriptions = ArrayField(models.CharField(max_length=200), blank=True, default=list)
hidden = models.BooleanField(default=False, help_text=_('Hide the grant from the /grants page?'), db_index=True)
weighted_shuffle = models.PositiveIntegerField(blank=True, null=True)
Expand Down Expand Up @@ -392,6 +402,12 @@ class Meta:

funding_info = models.CharField(default='', blank=True, null=True, max_length=255, help_text=_('Is this grant VC funded?'))

clr_prediction_curve = ArrayField(
ArrayField(
models.FloatField(),
size=2,
), blank=True, default=list, help_text=_('5 point curve to predict CLR donations.'))

weighted_risk_score = models.DecimalField(
default=0,
decimal_places=4,
Expand All @@ -417,6 +433,7 @@ def __str__(self):
"""Return the string representation of a Grant."""
return f"id: {self.pk}, active: {self.active}, title: {self.title}, type: {self.grant_type}"


def calc_clr_round(self):
clr_round = None

Expand All @@ -439,6 +456,7 @@ def calc_clr_round(self):
self.is_clr_active = False
self.clr_round_num = ''


@property
def tenants(self):
"""returns list of chains the grant can recieve contributions in"""
Expand All @@ -452,6 +470,29 @@ def tenants(self):
return tenants


@property
def calc_clr_round_nums(self):
roudn_nums = [ele for ele in self.in_active_clrs.values_list('round_num', flat=True)]
return ", ".join(roudn_nums)


@property
def calc_clr_prediction_curve(self):
# [amount_donated, match amount, bonus_from_match_amount ], etc..
# [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]
_clr_prediction_curve = []
for insert_clr_calc in self.clr_calculations.filter(latest=True).order_by('-created_on'):
insert_clr_calc = insert_clr_calc.clr_prediction_curve
if not _clr_prediction_curve:
_clr_prediction_curve = insert_clr_calc
else:
for j in [1,2]:
for i in [0,1,2,3,4,5]:
# add the 1 and 2 index of each clr prediction cuve
_clr_prediction_curve[i][j] += insert_clr_calc[i][j]
return _clr_prediction_curve


def updateActiveSubscriptions(self):
"&q 5D39 uot;"updates the active subscriptions list"""
handles = []
Expand Down Expand Up @@ -694,6 +735,10 @@ def favorite(self, user):

def save(self, update=True, *args, **kwargs):
"""Override the Grant save to optionally handle modified_on logic."""

self.clr_prediction_curve = self.calc_clr_prediction_curve
self.clr_round_num = self.calc_clr_round_nums

if self.modified_on < (timezone.now() - timezone.timedelta(minutes=15)):
from grants.tasks import update_grant_metadata
update_grant_metadata.delay(self.pk)
Expand Down Expand Up @@ -1226,7 +1271,6 @@ def create_contribution(self, tx_id, is_successful_contribution=True):
return contribution



class DonationQuerySet(models.QuerySet):
"""Define the Contribution default queryset and manager."""

Expand Down Expand Up @@ -1999,3 +2043,21 @@ class GrantStat(SuperModel):

AE8F def __str__(self):
return f'{self.snapshot_type} {self.created_on} for {self.grant.title}'


class GrantCLRCalculation(SuperModel):

latest = models.BooleanField(default=False, db_index=True, help_text="Is this calc the latest?")
grant = models.ForeignKey(Grant, on_delete=models.CASCADE, related_name='clr_calculations',
help_text=_('The grant'))
grantclr = models.ForeignKey(GrantCLR, on_delete=models.CASCADE, related_name='clr_calculations',
help_text=_('The grant CLR Round'))

clr_prediction_curve = ArrayField(
ArrayField(
models.FloatField(),
size=2,
), blank=True, default=list, help_text=_('5 point curve to predict CLR donations.'))

def __str__(self):
return f'{self.created_on} for g:{self.grant.pk} / gclr:{self.grantclr.pk} : {self.clr_prediction_curve}'
2 changes: 1 addition & 1 deletion app/grants/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from celery.utils.log import get_task_logger
from dashboard.models import Profile
from grants.models import Grant, Subscription
from marketing.mails import new_supporter, thank_you_for_supporting, new_grant_admin, new_grant
from marketing.mails import new_grant, new_grant_admin, new_supporter, thank_you_for_supporting
from townsquare.models import Comment

logger = get_task_logger(__name__)
Expand Down
3 changes: 1 addition & 2 deletions app/grants/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ def add_grant_to_active_clrs(grant):

active_clr_rounds = GrantCLR.objects.filter(is_active=True)
for clr_round in active_clr_rounds:
grants_in_clr = Grant.objects.filter(**clr_round.grant_filters)
if grants_in_clr.filter(pk=grant.pk).count():
if clr_round.grants.filter(pk=grant.pk).exists():
grant.in_active_clrs.add(clr_round)
grant.save()

Expand Down
6 changes: 2 additions & 4 deletions app/grants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
CartActivity, Contribution, Flag, Grant, GrantCategory, GrantCLR, GrantCollection, GrantType, MatchPledge,
PhantomFunding, Subscription,
)
from grants.tasks import update_grant_metadata, process_grant_creation_email
from grants.tasks import process_grant_creation_email, update_grant_metadata
from grants.utils import emoji_codes, get_leaderboard, get_user_code, is_grant_team_member, sync_payout
from inbox.utils import send_notification_to_user_from_gitcoinbot
from kudos.models import BulkTransferCoupon, Token
Expand Down Expand Up @@ -1321,10 +1321,8 @@ def grant_details(request, grant_id, grant_slug):

if clr_round:
is_clr_active = True
clr_round_num = clr_round.round_num
else:
is_clr_active = False
clr_round_num = 'LAST'

is_clr_active = True if clr_round else False
title = grant.title + " | Grants"
Expand Down Expand Up @@ -1354,7 +1352,7 @@ def grant_details(request, grant_id, grant_slug):
'activity_count': activity_count,
'contributors': contributors,
'clr_active': is_clr_active,
'round_num': clr_round_num,
'round_num': grant.clr_round_num,
'is_team_member': is_team_member,
'is_owner': grant.admin_profile.pk == request.user.profile.pk if request.user.is_authenticated else False,
'voucher_fundings': voucher_fundings,
Expand Down
0