1
1
import datetime as dt
2
- from datetime import timezone
2
+ from datetime import datetime , timezone
3
3
from time import sleep
4
4
5
5
from django .core .management .base import BaseCommand
6
+ from django .db .models import F
6
7
7
8
import requests
8
- from grants .models import Contribution , GrantContributionIndex
9
+ from grants .models import Contribution , Grant , GrantContributionIndex
9
10
10
11
11
12
class Command (BaseCommand ):
12
13
13
14
help = "rebuilds the table GrantContributionIndex"
14
15
15
16
def handle (self , * args , ** kwargs ):
17
+ self .stdout .write (f"{ datetime .now ()} Building query for contributions ..." )
16
18
contributions = (
17
19
Contribution .objects .filter (
18
20
success = True ,
19
21
subscription__network = "mainnet" ,
20
22
subscription__grant__clr_calculations__latest = True ,
23
+ subscription__contributor_profile__isnull = False ,
24
+ created_on__gt = F (
25
+ "subscription__grant__clr_calculations__grantclr__start_date"
26
+ ),
27
+ created_on__lt = F (
28
+ "subscription__grant__clr_calculations__grantclr__end_date"
29
+ ),
21
30
)
22
31
.order_by (
23
32
"subscription__contributor_profile__id" ,
@@ -36,13 +45,49 @@ def handle(self, *args, **kwargs):
36
45
)
37
46
)
38
47
48
+ self .stdout .write (f"{ datetime .now ()} Building contribIndexList ..." )
39
49
contribIndexList = [
40
50
GrantContributionIndex (
41
51
profile_id = contribInfo [0 ],
42
- grant_id = contribInfo [1 ],
43
- round_num = contribInfo [2 ],
52
+ round_num = contribInfo [1 ],
53
+ grant_id = contribInfo [2 ],
44
54
)
45
55
for contribInfo in contributions
46
56
]
47
57
48
- GrantContributionIndex .objects .bulk_create (contribIndexList , batch_size = 100 )
58
+ count = len (contribIndexList )
59
+ self .stdout .write (f"{ datetime .now ()} Length of contribIndexList: { count } " )
60
+ self .stdout .write (f"{ datetime .now ()} Clearing GrantContributionIndex ..." )
61
+
62
+ first_id = 0
63
+ last_id = 0
64
+
65
+ try :
66
+ first_id = GrantContributionIndex .objects .all ().order_by ("id" )[0 ].id
67
+ last_id = GrantContributionIndex .objects .all ().order_by ("-id" )[0 ].id
68
+ except :
69
+ pass
70
+
71
+ self .stdout .write (
72
+ f"{ datetime .now ()} ... deleting { last_id - first_id + 1 } records"
73
+ )
74
+
75
+ count_to_delete = last_id - first_id
76
+ count_deleted = 0
77
+ batch_size = 50000
78
+ for i in range (first_id , last_id , batch_size ):
79
+ count_deleted += batch_size
80
+ self .stdout .write (
81
+ f"{ datetime .now ()} ... { (count_deleted / count_to_delete * 100 ):.2f} % deleting { count } records up to id { i + batch_size } "
82
+ )
83
+ GrantContributionIndex .objects .filter (id__lt = i + batch_size ).delete ()
84
+
85
+ self .stdout .write (f"{ datetime .now ()} Saving to GrantContributionIndex ..." )
86
+ # GrantContributionIndex.objects.bulk_create(contribIndexList, batch_size=1000, ignore_conflicts=True)
87
+ for i in range (0 , count , batch_size ):
88
+ self .stdout .write (
89
+ f"{ datetime .now ()} { (i / count * 100 ):.2f} % Saving to GrantContributionIndex ..."
90
+ )
91
+ GrantContributionIndex .objects .bulk_create (
92
+ contribIndexList [i : i + batch_size ], ignore_conflicts = True
93
+ )
0 commit comments