Skip to content
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

Email logging #4174

Merged
merged 1 commit into from
Jul 12, 2023
Merged
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
8 changes: 7 additions & 1 deletion uber/tasks/email.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import Mapping
from datetime import timedelta, datetime
from time import sleep
from time import sleep, time

from celery.schedules import crontab
from pockets import groupify, listify
Expand Down Expand Up @@ -159,6 +159,8 @@ def send_automated_emails():
if not (c.DEV_BOX or c.SEND_EMAILS):
return None

quantity_sent = 0
start_time = time()
with Session() as session:
active_automated_emails = session.query(AutomatedEmail) \
.filter(*AutomatedEmail.filters_for_active) \
Expand All @@ -167,6 +169,7 @@ def send_automated_emails():
automated_emails_by_model = groupify(active_automated_emails, 'model')

for model, query_func in AutomatedEmailFixture.queries.items():
log.info("Sending automated emails for " + model.__name__)
automated_emails = automated_emails_by_model.get(model.__name__, [])
for automated_email in automated_emails:
if automated_email.currently_sending:
Expand All @@ -181,13 +184,15 @@ def send_automated_emails():
unapproved_count = 0

model_instances = query_func(session)
log.info("Evaluating " + str(len(model_instances)) " instances of " + model.__name__)
for model_instance in model_instances:
if model_instance.id not in automated_email.emails_by_fk_id:
if automated_email.would_send_if_approved(model_instance):
if automated_email.approved or not automated_email.needs_approval:
if model_instance.active_receipt:
session.refresh_receipt_and_model(model_instance)
automated_email.send_to(model_instance, delay=False)
quantity_sent += 1
else:
unapproved_count += 1
automated_email.last_send_time = datetime.now()
Expand All @@ -199,6 +204,7 @@ def send_automated_emails():
session.add(automated_email)
session.commit()

log.info("Sent " + str(quantity_sent) + " emails in " + str(time() - start_time) + " seconds")
return {e.ident: e.unapproved_count for e in active_automated_emails if e.unapproved_count > 0}

# TODO: Once we finish converting each AutomatedEmailFixture.filter
Expand Down
Loading