Skip to content

Commit

Permalink
Adding table for storing relation between profile and his grant and r…
Browse files Browse the repository at this point in the history
…ound_num contributors + command to compute that table (#10759)

* Adding table for string relation between profile and his grant and round_num contributors + command to compute that table

* fixing linter complaints
  • Loading branch information
nutrina authored Aug 17, 2022
1 parent 02fc886 commit fd8fd4c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/assets/v2/js/grants/_new.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ if (document.getElementById('gc-new-grant')) {
},
grantTagOptions() {
const sorted_tags = this.grant_tags.sort((a, b) => a.id - b.id);
const next_id = sorted_tags[sorted_tags.length-1].id + 1;
const next_id = sorted_tags[sorted_tags.length - 1].id + 1;
const all_tags = this.grant_tags.sort((a, b) => b.is_eligibility_tag - a.is_eligibility_tag);
const first_discovery = (tag) => tag.is_eligibility_tag === 0;

Expand Down
48 changes: 48 additions & 0 deletions app/grants/management/commands/compute_grant_contribution_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import datetime as dt
from datetime import timezone
from time import sleep

from django.core.management.base import BaseCommand

import requests
from grants.models import Contribution, GrantContributionIndex


class Command(BaseCommand):

help = "rebuilds the table GrantContributionIndex"

def handle(self, *args, **kwargs):
contributions = (
Contribution.objects.filter(
success=True,
subscription__network="mainnet",
subscription__grant__clr_calculations__latest=True,
)
.order_by(
"subscription__contributor_profile__id",
"subscription__grant__clr_calculations__grantclr__round_num",
"subscription__grant__id",
)
.distinct(
"subscription__contributor_profile__id",
"subscription__grant__clr_calculations__grantclr__round_num",
"subscription__grant__id",
)
.values_list(
"subscription__contributor_profile__id",
"subscription__grant__clr_calculations__grantclr__round_num",
"subscription__grant__id",
)
)

contribIndexList = [
GrantContributionIndex(
profile_id=contribInfo[0],
grant_id=contribInfo[1],
round_num=contribInfo[2],
)
for contribInfo in contributions
]

GrantContributionIndex.objects.bulk_create(contribIndexList, batch_size=100)
30 changes: 30 additions & 0 deletions app/grants/migrations/0147_grantcontributionindex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 2.2.24 on 2022-08-16 12:10

from django.db import migrations, models
import django.db.models.deletion
import economy.models


class Migration(migrations.Migration):

dependencies = [
('dashboard', '0210_auto_20220718_1306'),
('grants', '0146_auto_20220809_1134'),
]

operations = [
migrations.CreateModel(
name='GrantContributionIndex',
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)),
('round_num', models.IntegerField(help_text='The round number a user contributed to')),
('grant', models.ForeignKey(help_text='The grant a user contributed to', on_delete=django.db.models.deletion.CASCADE, to='grants.Grant')),
('profile', models.ForeignKey(help_text='Contributor', on_delete=django.db.models.deletion.CASCADE, to='dashboard.Profile')),
],
options={
'abstract': False,
},
),
]
1 change: 1 addition & 0 deletions app/grants/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@
from .hall_of_fame import GrantHallOfFame, GrantHallOfFameGrantee
from .phantom_funding import PhantomFunding
from .subscription import Subscription
from .grant_contribution_index import GrantContributionIndex
14 changes: 14 additions & 0 deletions app/grants/models/grant_contribution_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from economy.models import SuperModel


class GrantContributionIndex(SuperModel):
"""Stores the grants and round number to shich a user contributed to.
The purpose of this table is to allow a a fast query. This will be used from
the `contributor_statistics`API """

profile = models.ForeignKey('dashboard.Profile', help_text=_('Contributor'), on_delete=models.CASCADE, db_index=True)
grant = models.ForeignKey('Grant', help_text=_('The grant a user contributed to'), on_delete=models.CASCADE)
round_num = models.IntegerField(help_text=_('The round number a user contributed to'))

0 comments on commit fd8fd4c

Please sign in to comment.