Skip to content

Commit

Permalink
Optimized update request statuses job
Browse files Browse the repository at this point in the history
  • Loading branch information
johnyu95 committed Oct 3, 2023
1 parent 3f82981 commit 516e251
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
50 changes: 31 additions & 19 deletions app/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from sqlalchemy.exc import SQLAlchemyError

from app import calendar, sentry, db
from app.constants import OPENRECORDS_DL_EMAIL, request_status
from app.constants import OPENRECORDS_DL_EMAIL, request_status, determination_type
from app.constants.event_type import EMAIL_NOTIFICATION_SENT, REQ_STATUS_CHANGED
from app.constants.response_privacy import PRIVATE
from app.lib.db_utils import create_object, update_object
from app.lib.email_utils import send_email
from app.models import Agencies, Emails, Events, Requests
from app.models import Agencies, Emails, Events, Requests, Responses, Determinations

# NOTE: (For Future Reference)
# If we find ourselves in need of a request context, app.test_request_context() might come in handy.
Expand Down Expand Up @@ -46,6 +46,7 @@ def _update_request_statuses():

agencies = Agencies.query.with_entities(Agencies.ein).filter_by(is_active=True).all()
for agency_ein, in agencies:
# Overdue requests
requests_overdue = Requests.query.filter(
Requests.due_date < now,
Requests.status != request_status.CLOSED,
Expand All @@ -54,6 +55,20 @@ def _update_request_statuses():
Requests.due_date.asc()
).all()

# Query for all acknowledged overdue requests
agency_requests_overdue = Requests.query.join(Responses, Determinations).filter(
Requests.due_date < now,
Requests.status != request_status.CLOSED,
Requests.agency_ein == agency_ein,
Determinations.dtype == determination_type.ACKNOWLEDGMENT
).order_by(
Requests.due_date.asc()
).all()

# Get the difference for all unacknowledged overdue requests
agency_acknowledgments_overdue = list(set(requests_overdue) - set(agency_requests_overdue))

# Due soon requests
requests_due_soon = Requests.query.filter(
Requests.due_date > now,
Requests.due_date <= due_soon_date,
Expand All @@ -63,22 +78,25 @@ def _update_request_statuses():
Requests.due_date.asc()
).all()

# Query for all acknowledged due soon requests
agency_requests_due_soon = Requests.query.join(Responses, Determinations).filter(
Requests.due_date > now,
Requests.due_date <= due_soon_date,
Requests.status != request_status.CLOSED,
Requests.agency_ein == agency_ein,
Determinations.dtype == determination_type.ACKNOWLEDGMENT
).order_by(
Requests.due_date.asc()
).all()

# Get the difference for all unacknowledged due soon requests
agency_acknowledgments_due_soon = list(set(requests_due_soon) - set(agency_requests_due_soon))

if not requests_overdue and not requests_due_soon:
continue

agency_requests_overdue = []
agency_acknowledgments_overdue = []
agency_requests_due_soon = []
agency_acknowledgments_due_soon = []

# OVERDUE
for request in requests_overdue:

if request.was_acknowledged:
agency_requests_overdue.append(request)
else:
agency_acknowledgments_overdue.append(request)

if request.status != request_status.OVERDUE:
create_object(
Events(
Expand All @@ -97,12 +115,6 @@ def _update_request_statuses():

# DUE SOON
for request in requests_due_soon:

if request.was_acknowledged:
agency_requests_due_soon.append(request)
else:
agency_acknowledgments_due_soon.append(request)

if request.status != request_status.DUE_SOON:
create_object(
Events(
Expand Down
8 changes: 8 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,14 @@ def agency_request_summary_released(self):
and self.agency_request_summary_release_date < datetime.utcnow()
)

@property
def requester_name(self):
try:
request_doc = es.get(index=current_app.config["ELASTICSEARCH_INDEX"], doc_type="request", id=self.id)
return request_doc['_source']['requester_name']
except Exception as e:
print(e)

def es_update(self):
if current_app.config["ELASTICSEARCH_ENABLED"]:
if self.agency.is_active:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% if request.custom_metadata %}
<li>Request Type: {{ request.description }}</li>
{% endif %}
<li>Requester Last Name: {{ request.requester.last_name | title | safe }}</li>
<li>Requester Name: {{ request.requester_name | title | safe }}</li>
<li>Request Opened: {{ request.date_submitted.strftime('%m/%d/%y') }}</li>
</ul>
</li>
Expand All @@ -29,7 +29,7 @@
{% if request.custom_metadata %}
<li>Request Type: {{ request.description }}</li>
{% endif %}
<li>Requester Last Name: {{ request.requester.last_name | title | safe }}</li>
<li>Requester Name: {{ request.requester_name | title | safe }}</li>
<li>Request Opened: {{ request.date_submitted.strftime('%m/%d/%y') }}</li>
</ul>
</li>
Expand All @@ -55,7 +55,7 @@
{% if request.custom_metadata %}
<li>Request Type: {{ request.description }}</li>
{% endif %}
<li>Requester Last Name: {{ request.requester.last_name | title | safe }}</li>
<li>Requester Name: {{ request.requester_name | title | safe }}</li>
<li>Request Opened: {{ request.date_submitted.strftime('%m/%d/%y') }}</li>
</ul>
</li>
Expand All @@ -81,7 +81,7 @@
{% if request.custom_metadata %}
<li>Request Type: {{ request.description }}</li>
{% endif %}
<li>Requester Last Name: {{ request.requester.last_name | title | safe }}</li>
<li>Requester Name: {{ request.requester_name | title | safe }}</li>
<li>Request Opened: {{ request.date_submitted.strftime('%m/%d/%y') }}</li>
</ul>
</li>
Expand Down

0 comments on commit 516e251

Please sign in to comment.