Skip to content

Commit

Permalink
Don't double-count department SLA days
Browse files Browse the repository at this point in the history
  • Loading branch information
wkeeling committed Oct 4, 2021
1 parent 9030b2e commit df614ab
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
17 changes: 12 additions & 5 deletions api/cases/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get_case_ids_with_active_ecju_queries(date):
# 2. Responded to in the last working day before cutoff time today
return (
EcjuQuery.objects.filter(
Q(responded_at__isnull=True, created_at__lt=today(time=SLA_UPDATE_CUTOFF_TIME),)
Q(responded_at__isnull=True, created_at__lt=today(time=SLA_UPDATE_CUTOFF_TIME))
| Q(responded_at__gt=yesterday(time=SLA_UPDATE_CUTOFF_TIME))
)
.values("case_id")
Expand Down Expand Up @@ -105,6 +105,9 @@ def update_cases_sla():
.exclude(status__in=terminal_case_status)
)
with transaction.atomic():
# Keep track of the department SLA updates.
# We only want to update a department SLA once per case assignment per day.
department_slas_updated = set()
for assignment in CaseQueue.objects.filter(case__in=cases):
# Update team SLAs
try:
Expand All @@ -118,13 +121,17 @@ def update_cases_sla():
if department is not None:
try:
department_sla = DepartmentSLA.objects.get(department=department, case=assignment.case)
department_sla.sla_days += 1
department_sla.save()
if department_sla.id not in department_slas_updated:
department_sla.sla_days += 1
department_sla.save()
except DepartmentSLA.DoesNotExist:
DepartmentSLA.objects.create(department=department, case=assignment.case, sla_days=1)
department_sla = DepartmentSLA.objects.create(
department=department, case=assignment.case, sla_days=1
)
department_slas_updated.add(department_sla.id)

results = cases.select_for_update().update(
sla_days=F("sla_days") + 1, sla_remaining_days=F("sla_remaining_days") - 1, sla_updated_at=date,
sla_days=F("sla_days") + 1, sla_remaining_days=F("sla_remaining_days") - 1, sla_updated_at=date
)

logging.info(f"{LOG_PREFIX} SLA Update Successful. Updated {results} cases")
Expand Down
64 changes: 43 additions & 21 deletions api/cases/tests/test_sla.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ def test_sla_update_exhibition_mod(
application = self.case_types[application_type]
case = self.submit_application(application)
_set_submitted_at(case, HOUR_BEFORE_CUTOFF)
CaseQueue.objects.create(
case=application.case_ptr, queue=self.queue,
)
CaseQueue.objects.create(case=application.case_ptr, queue=self.queue)
results = update_cases_sla.now()
sla = CaseAssignmentSLA.objects.get()
case.refresh_from_db()
Expand All @@ -160,10 +158,8 @@ def test_sla_update_F680_mod(
application = self.case_types[application_type]
case = self.submit_application(application)
_set_submitted_at(case, HOUR_BEFORE_CUTOFF)
CaseQueue.objects.create(
queue=self.queue, case=application.case_ptr,
)
sla = CaseAssignmentSLA.objects.create(sla_days=4, queue=self.queue, case=application.case_ptr,)
CaseQueue.objects.create(queue=self.queue, case=application.case_ptr)
sla = CaseAssignmentSLA.objects.create(sla_days=4, queue=self.queue, case=application.case_ptr)
results = update_cases_sla.now()

case.refresh_from_db()
Expand Down Expand Up @@ -196,9 +192,7 @@ def test_sla_update_gifting_mod(
self.assertEqual(case.sla_days, 1)
self.assertEqual(case.sla_remaining_days, target - 1)

@parameterized.expand(
[(CaseTypeSubTypeEnum.GOODS,), (CaseTypeSubTypeEnum.EUA,),]
)
@parameterized.expand([(CaseTypeSubTypeEnum.GOODS,), (CaseTypeSubTypeEnum.EUA,)])
def test_sla_doesnt_update_queries(self, query_type):
query = self.case_types[query_type]
case = self.submit_application(query)
Expand All @@ -218,11 +212,7 @@ class SlaRulesTests(DataTestClient):
def test_sla_cutoff_window(self, mock_is_weekend, mock_is_bank_holiday):
mock_is_weekend.return_value = False
mock_is_bank_holiday.return_value = False
times = [
HOUR_BEFORE_CUTOFF,
SLA_UPDATE_CUTOFF_TIME,
HOUR_AFTER_CUTOFF,
]
times = [HOUR_BEFORE_CUTOFF, SLA_UPDATE_CUTOFF_TIME, HOUR_AFTER_CUTOFF]
for submit_time in times:
application = self.create_draft_standard_application(self.organisation)
case = self.submit_application(application)
Expand Down Expand Up @@ -548,9 +538,7 @@ def test_sla_hours_does_not_appear_on_other_cases(self):
class TerminalCaseSlaTests(DataTestClient):
@mock.patch("api.cases.tasks.is_weekend")
@mock.patch("api.cases.tasks.is_bank_holiday")
def test_sla_not_update_for_terminal(
self, mock_is_weekend, mock_is_bank_holiday,
):
def test_sla_not_update_for_terminal(self, mock_is_weekend, mock_is_bank_holiday):
mock_is_weekend.return_value = False
mock_is_bank_holiday.return_value = False
application = self.create_draft_standard_application(self.organisation)
Expand All @@ -577,9 +565,7 @@ def test_sla_not_update_for_terminal(
class DepartmentSLATests(DataTestClient):
@mock.patch("api.cases.tasks.is_weekend")
@mock.patch("api.cases.tasks.is_bank_holiday")
def test_department_sla_updated(
self, mock_is_weekend, mock_is_bank_holiday,
):
def test_department_sla_updated(self, mock_is_weekend, mock_is_bank_holiday):
# The following is to ensure that this test doesn't fail on
# non-working days.
mock_is_weekend.return_value = False
Expand All @@ -600,3 +586,39 @@ def test_department_sla_updated(
update_cases_sla.now()
department_sla = DepartmentSLA.objects.get(department=test_department)
self.assertEqual(department_sla.sla_days, 1)

@mock.patch("api.cases.tasks.is_weekend")
@mock.patch("api.cases.tasks.is_bank_holiday")
def test_department_sla_updated_across_multiple_teams(self, mock_is_weekend, mock_is_bank_holiday):
# The following is to ensure that this test doesn't fail on
# non-working days.
mock_is_weekend.return_value = False
mock_is_bank_holiday.return_value = False
# Create & submit an application
application = self.create_draft_standard_application(self.organisation)
case = self.submit_application(application)
_set_submitted_at(case, HOUR_BEFORE_CUTOFF)

# Assign the application to our team
CaseQueue.objects.create(case=case, queue=self.queue)
# Create a test department
test_department = Department(name="test_department")
test_department.save()
# In order to move the department SLA counter, we need to assign
# our team to a department
self.team.department = test_department
self.team.save()

# Assign the application to another team that belongs to the same department
test_team = self.create_team("test_team")
test_queue = self.create_queue("test_queue", test_team)
test_team.department = test_department
test_team.save()
CaseQueue.objects.create(case=case, queue=test_queue)

update_cases_sla.now()

# We only expect the SLA counter to have been updated once for the two
# teams that are both associated with 'test_department'
test_department_sla = DepartmentSLA.objects.get(department=test_department)
self.assertEqual(test_department_sla.sla_days, 1)

0 comments on commit df614ab

Please sign in to comment.