Skip to content

Commit fd8fd4c

Browse files
authored
Adding table for storing relation between profile and his grant and round_num contributors + command to compute that table (gitcoinco#10759)
* Adding table for string relation between profile and his grant and round_num contributors + command to compute that table * fixing linter complaints
1 parent 02fc886 commit fd8fd4c

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

app/assets/v2/js/grants/_new.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ if (document.getElementById('gc-new-grant')) {
483483
},
484484
grantTagOptions() {
485485
const sorted_tags = this.grant_tags.sort((a, b) => a.id - b.id);
486-
const next_id = sorted_tags[sorted_tags.length-1].id + 1;
486+
const next_id = sorted_tags[sorted_tags.length - 1].id + 1;
487487
const all_tags = this.grant_tags.sort((a, b) => b.is_eligibility_tag - a.is_eligibility_tag);
488488
const first_discovery = (tag) => tag.is_eligibility_tag === 0;
489489

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import datetime as dt
2+
from datetime import timezone
3+
from time import sleep
4+
5+
from django.core.management.base import BaseCommand
6+
7+
import requests
8+
from grants.models import Contribution, GrantContributionIndex
9+
10+
11+
class Command(BaseCommand):
12+
13+
help = "rebuilds the table GrantContributionIndex"
14+
15+
def handle(self, *args, **kwargs):
16+
contributions = (
17+
Contribution.objects.filter(
18+
success=True,
19+
subscription__network="mainnet",
20+
subscription__grant__clr_calculations__latest=True,
21+
)
22+
.order_by(
23+
"subscription__contributor_profile__id",
24+
"subscription__grant__clr_calculations__grantclr__round_num",
25+
"subscription__grant__id",
26+
)
27+
.distinct(
28+
"subscription__contributor_profile__id",
29+
"subscription__grant__clr_calculations__grantclr__round_num",
30+
"subscription__grant__id",
31+
)
32+
.values_list(
33+
"subscription__contributor_profile__id",
34+
"subscription__grant__clr_calculations__grantclr__round_num",
35+
"subscription__grant__id",
36+
)
37+
)
38+
39+
contribIndexList = [
40+
GrantContributionIndex(
41+
profile_id=contribInfo[0],
42+
grant_id=contribInfo[1],
43+
round_num=contribInfo[2],
44+
)
45+
for contribInfo in contributions
46+
]
47+
48+
GrantContributionIndex.objects.bulk_create(contribIndexList, batch_size=100)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Generated by Django 2.2.24 on 2022-08-16 12:10
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
import economy.models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('dashboard', '0210_auto_20220718_1306'),
12+
('grants', '0146_auto_20220809_1134'),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='GrantContributionIndex',
18+
fields=[
19+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('created_on', models.DateTimeField(db_index=True, default=economy.models.get_time)),
21+
('modified_on', models.DateTimeField(default=economy.models.get_time)),
22+
('round_num', models.IntegerField(help_text='The round number a user contributed to')),
23+
('grant', models.ForeignKey(help_text='The grant a user contributed to', on_delete=django.db.models.deletion.CASCADE, to='grants.Grant')),
24+
('profile', models.ForeignKey(help_text='Contributor', on_delete=django.db.models.deletion.CASCADE, to='dashboard.Profile')),
25+
],
26+
options={
27+
'abstract': False,
28+
},
29+
),
30+
]

app/grants/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@
3535
from .hall_of_fame import GrantHallOfFame, GrantHallOfFameGrantee
3636
from .phantom_funding import PhantomFunding
3737
from .subscription import Subscription
38+
from .grant_contribution_index import GrantContributionIndex
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.db import models
2+
from django.utils.translation import gettext_lazy as _
3+
4+
from economy.models import SuperModel
5+
6+
7+
class GrantContributionIndex(SuperModel):
8+
"""Stores the grants and round number to shich a user contributed to.
9+
The purpose of this table is to allow a a fast query. This will be used from
10+
the `contributor_statistics`API """
11+
12+
profile = models.ForeignKey('dashboard.Profile', help_text=_('Contributor'), on_delete=models.CASCADE, db_index=True)
13+
grant = models.ForeignKey('Grant', help_text=_('The grant a user contributed to'), on_delete=models.CASCADE)
14+
round_num = models.IntegerField(help_text=_('The round number a user contributed to'))

0 commit comments

Comments
 (0)