Skip to content

Commit

Permalink
As a gitcoin admin, I'd like the mailchimp syncing to be refactored, …
Browse files Browse the repository at this point in the history
…so the load on the database is lowered and the site runs faster (#4798)

* sync mail perf

* comment
  • Loading branch information
owocki authored and danlipert committed Jul 24, 2019
1 parent 3910078 commit e670fa8
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions app/marketing/management/commands/sync_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@
from marketing.models import EmailSubscriber
from marketing.utils import get_or_save_email_subscriber as process_email

hours_ago = 12 #if you change, this make sure you change the crontab file to ...
# make it inclusive of all users since last run of this job


def pull_to_db():
# setup
before_timestamp = timezone.now() - timezone.timedelta(hours=hours_ago)

print('- pull_to_db')
print("- profile")
from dashboard.models import Profile
# right now, we only take profiles that've given us an access token
profiles = Profile.objects.exclude(email='').all()
profiles = Profile.objects.exclude(email='').filter(modified_on__gt=before_timestamp).all()
# in the future, though, we could take ALL github profiles in the system and use those
# profiles = Profile.objects.exclude(email='').all()
for profile in profiles:
process_email(profile.email, 'profile_email')
for email in profiles.values_list('email', flat=True):
process_email(email, 'profile_email')

print("- match")
from marketing.models import Match
for match in Match.objects.all():
process_email(match.email, 'match')
for email in Match.objects.filter(modified_on__gt=before_timestamp).values_list('email', flat=True):
process_email(email, 'match')

get_size = 50
client = MailChimp(mc_api=settings.MAILCHIMP_API_KEY, mc_user=settings.MAILCHIMP_USER)
Expand All @@ -58,16 +64,15 @@ def pull_to_db():
print('local')
print("- dashboard_sub")
from dashboard.models import Subscription
for sub in Subscription.objects.all():
email = sub.email
for email in Subscription.objects.filter(modified_on__gt=before_timestamp).values_list('email', flat=True):
process_email(email, 'dashboard_subscription')

print("- tip, Kudos")
from dashboard.models import Tip
from kudos.models import KudosTransfer
objects = [Tip, KudosTransfer]
for obj in objects:
for _this in obj.objects.all():
for _this in obj.objects.filter(modified_on__gt=before_timestamp):
# do not add receive tip emails to the mailing list,
# don't want to spam people at 4 diff email addresses
# for email in tip.emails:
Expand All @@ -79,7 +84,7 @@ def pull_to_db():

print("- bounty")
from dashboard.models import Bounty
for b in Bounty.objects.prefetch_related('fulfillments').all():
for b in Bounty.objects.filter(modified_on__gt=before_timestamp).prefetch_related('fulfillments').all():
email_list = []
if b.bounty_owner_email:
email_list.append(b.bounty_owner_email)
Expand All @@ -91,17 +96,18 @@ def pull_to_db():

print("- tdi")
from tdi.models import WhitepaperAccess, WhitepaperAccessRequest
for wa in WhitepaperAccess.objects.all():
process_email(wa.email, 'whitepaperaccess')
for email in WhitepaperAccess.objects.filter(modified_on__gt=before_timestamp).values_list('email', flat=True):
process_email(email, 'whitepaperaccess')

for wa in WhitepaperAccessRequest.objects.all():
process_email(wa.email, 'whitepaperaccessrequest')
for email in WhitepaperAccessRequest.objects.filter(modified_on__gt=before_timestamp).values_list('email', flat=True):
process_email(email, 'whitepaperaccessrequest')

print('/pull_to_db')

def sync_mailchimp_list(eses, list_id):
print("- {} emails".format(eses.count()))
for es in eses:
client = MailChimp(mc_api=settings.MAILCHIMP_API_KEY, mc_user=settings.MAILCHIMP_USER)
for es in eses.values_list('email', flat=True):
email = es.email
print(email)
try:
Expand Down

0 comments on commit e670fa8

Please sign in to comment.