Skip to content

Commit

Permalink
fix(alerts): Introduced SendAlertsResponse and PercolatorResponses
Browse files Browse the repository at this point in the history
  • Loading branch information
albertisfu committed Oct 18, 2024
1 parent 7dd1e43 commit 180727a
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 127 deletions.
16 changes: 9 additions & 7 deletions cl/alerts/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@
from cl.search.models import Docket, DocketEntry
from cl.search.types import (
ESDocumentNameType,
PercolatorResponsesType,
PercolatorResponseType,
SaveDocumentResponseType,
SaveESDocumentReturnType,
SearchAlertHitType,
SendAlertsResponse,
)
from cl.stats.utils import tally_stat
from cl.users.models import UserProfile
Expand Down Expand Up @@ -643,7 +643,7 @@ def process_percolator_response(response: PercolatorResponseType) -> None:


@app.task(ignore_result=True)
def percolator_response_processing(response: PercolatorResponsesType) -> None:
def percolator_response_processing(response: SendAlertsResponse) -> None:
"""Process the response from the percolator and handle alerts triggered by
the percolator query.
Expand Down Expand Up @@ -876,7 +876,7 @@ def send_or_schedule_alerts(
)
def send_or_schedule_search_alerts(
self: Task, response: SaveESDocumentReturnType | None
) -> PercolatorResponsesType | None:
) -> SendAlertsResponse | None:
"""Send real-time alerts based on the Elasticsearch search response.
Or schedule other rates alerts to send them later.
Expand All @@ -891,8 +891,10 @@ def send_or_schedule_search_alerts(
:param self: The celery task
:param response: A two tuple, the document ID to be percolated in
ES index and the document data that triggered the alert.
:return: A two tuple, a list of Alerts triggered and the document data that
triggered the alert.
:return: A SendAlertsResponse dataclass containing the main alerts
triggered, the recap documents alerts triggered, the docket alerts
triggered, the document content that triggered the alert, and the related
app label model.
"""

if not response or not settings.PERCOLATOR_SEARCH_ALERTS_ENABLED:
Expand All @@ -916,7 +918,7 @@ def send_or_schedule_search_alerts(
documents_to_percolate,
app_label,
)
if not percolator_responses[0]:
if not percolator_responses.main_response:
self.request.chain = None
return None

Expand All @@ -936,7 +938,7 @@ def send_or_schedule_search_alerts(
)
)

return PercolatorResponsesType(
return SendAlertsResponse(
main_alerts_triggered=main_alerts_triggered,
rd_alerts_triggered=rd_alerts_triggered,
d_alerts_triggered=d_alerts_triggered,
Expand Down
8 changes: 5 additions & 3 deletions cl/alerts/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2734,15 +2734,17 @@ def test_percolate_document_in_batches(self, mock_abort_audio):
AudioPercolator._index._name,
document_index,
)
ids_in_results = [result.id for result in percolator_responses[0].hits]
ids_in_results = [
result.id for result in percolator_responses.main_response.hits
]

# Update the first in the previous batch.
alert_to_modify = alerts_created[0]
alert_to_modify.rate = "dly"
alert_to_modify.save()

# Percolate the next page.
search_after = percolator_responses[0].hits[-1].meta.sort
search_after = percolator_responses.main_response.hits[-1].meta.sort
percolator_responses = percolate_es_document(
str(rt_oral_argument.pk),
AudioPercolator._index._name,
Expand All @@ -2752,7 +2754,7 @@ def test_percolate_document_in_batches(self, mock_abort_audio):

# The document updated shouldn't be retrieved again.
# Since documents are ordered by asc date_created instead of timestamp.
for result in percolator_responses[0].hits:
for result in percolator_responses.main_response.hits:
self.assertNotIn(result.id, ids_in_results)
ids_in_results.append(result.id)

Expand Down
87 changes: 52 additions & 35 deletions cl/alerts/tests/tests_recap_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1786,9 +1786,9 @@ def test_recap_document_cross_object_percolator_queries(
app_label, str(self.rd_att.pk)
)
expected_queries = 1
self.assertEqual(len(responses[0]), expected_queries)
self.assertEqual(len(responses.main_response), expected_queries)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id), True
self.confirm_query_matched(responses.main_response, query_id), True
)

# Test Percolate a RECAPDocument. It should match the query containing
Expand All @@ -1805,9 +1805,10 @@ def test_recap_document_cross_object_percolator_queries(
app_label, str(self.rd.pk)
)
expected_queries = 1
self.assertEqual(len(responses[0]), expected_queries)
self.assertEqual(len(responses.main_response), expected_queries)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_1), True
self.confirm_query_matched(responses.main_response, query_id_1),
True,
)

# Test Percolate a RECAPDocument. It should match the query containing
Expand All @@ -1823,12 +1824,14 @@ def test_recap_document_cross_object_percolator_queries(
app_label, str(self.rd.pk)
)
expected_queries = 2
self.assertEqual(len(responses[0]), expected_queries)
self.assertEqual(len(responses.main_response), expected_queries)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_1), True
self.confirm_query_matched(responses.main_response, query_id_1),
True,
)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_2), True
self.confirm_query_matched(responses.main_response, query_id_2),
True,
)

# Test Percolate a RECAPDocument. It should match the query containing
Expand All @@ -1845,18 +1848,21 @@ def test_recap_document_cross_object_percolator_queries(
)
expected_queries = 3
self.assertEqual(
len(responses[0]),
len(responses.main_response),
expected_queries,
msg="Wrong number of queries matched.",
)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_1), True
self.confirm_query_matched(responses.main_response, query_id_1),
True,
)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_2), True
self.confirm_query_matched(responses.main_response, query_id_2),
True,
)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_3), True
self.confirm_query_matched(responses.main_response, query_id_3),
True,
)

def test_recap_document_percolator(self, mock_prefix) -> None:
Expand All @@ -1881,12 +1887,12 @@ def test_recap_document_percolator(self, mock_prefix) -> None:
)
expected_queries = 1
self.assertEqual(
len(responses[0]),
len(responses.main_response),
expected_queries,
msg="Matched queries didn't match.",
)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id), True
self.confirm_query_matched(responses.main_response, query_id), True
)

# Test percolate only filters combination.
Expand All @@ -1904,12 +1910,12 @@ def test_recap_document_percolator(self, mock_prefix) -> None:
)
expected_queries = 1
self.assertEqual(
len(responses[0]),
len(responses.main_response),
expected_queries,
msg="Matched queries didn't match.",
)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id), True
self.confirm_query_matched(responses.main_response, query_id), True
)

# Test percolate a different document targeting a different filters
Expand All @@ -1927,12 +1933,12 @@ def test_recap_document_percolator(self, mock_prefix) -> None:
)
expected_queries = 1
self.assertEqual(
len(responses[0]),
len(responses.main_response),
expected_queries,
msg="Matched queries didn't match.",
)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id), True
self.confirm_query_matched(responses.main_response, query_id), True
)

# Test percolate the same document loosen the query.
Expand All @@ -1948,15 +1954,16 @@ def test_recap_document_percolator(self, mock_prefix) -> None:
)
expected_queries = 2
self.assertEqual(
len(responses[0]),
len(responses.main_response),
expected_queries,
msg="Matched queries didn't match.",
)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id), True
self.confirm_query_matched(responses.main_response, query_id), True
)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_2), True
self.confirm_query_matched(responses.main_response, query_id_2),
True,
)

def test_docket_percolator(self, mock_prefix) -> None:
Expand All @@ -1981,7 +1988,7 @@ def test_docket_percolator(self, mock_prefix) -> None:
document_index_alias,
)
expected_queries = 0
self.assertEqual(len(responses[0]), expected_queries)
self.assertEqual(len(responses.main_response), expected_queries)

# Test Percolate a docket object. It shouldn't match the query
# containing text query terms contained only in a RD.
Expand All @@ -1998,7 +2005,7 @@ def test_docket_percolator(self, mock_prefix) -> None:
document_index_alias,
)
expected_queries = 0
self.assertEqual(len(responses[0]), expected_queries)
self.assertEqual(len(responses.main_response), expected_queries)

# Test Percolate a docket object. Combining docket terms OR RECAPDocument
# fields. This query can be triggered only by the Docket document.
Expand All @@ -2015,9 +2022,12 @@ def test_docket_percolator(self, mock_prefix) -> None:
document_index_alias,
)
expected_queries = 1
self.assertEqual(len(responses[0]), expected_queries, msg="error 1")
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_2), True
len(responses.main_response), expected_queries, msg="error 1"
)
self.assertEqual(
self.confirm_query_matched(responses.main_response, query_id_2),
True,
)

# Test percolate text query + different filters.
Expand All @@ -2037,12 +2047,14 @@ def test_docket_percolator(self, mock_prefix) -> None:
document_index_alias,
)
expected_queries = 2
self.assertEqual(len(responses[0]), expected_queries)
self.assertEqual(len(responses.main_response), expected_queries)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_2), True
self.confirm_query_matched(responses.main_response, query_id_2),
True,
)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_3), True
self.confirm_query_matched(responses.main_response, query_id_3),
True,
)

# Test percolate text query + case_name filter.
Expand All @@ -2060,9 +2072,10 @@ def test_docket_percolator(self, mock_prefix) -> None:
document_index_alias,
)
expected_queries = 1
self.assertEqual(len(responses[0]), expected_queries)
self.assertEqual(len(responses.main_response), expected_queries)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_4), True
self.confirm_query_matched(responses.main_response, query_id_4),
True,
)

# Test percolate one filter.
Expand All @@ -2079,9 +2092,10 @@ def test_docket_percolator(self, mock_prefix) -> None:
document_index_alias,
)
expected_queries = 1
self.assertEqual(len(responses[0]), expected_queries)
self.assertEqual(len(responses.main_response), expected_queries)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_5), True
self.confirm_query_matched(responses.main_response, query_id_5),
True,
)

# Test percolate text query.
Expand All @@ -2098,15 +2112,18 @@ def test_docket_percolator(self, mock_prefix) -> None:
document_index_alias,
)
expected_queries = 3
self.assertEqual(len(responses[0]), expected_queries)
self.assertEqual(len(responses.main_response), expected_queries)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_2), True
self.confirm_query_matched(responses.main_response, query_id_2),
True,
)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_3), True
self.confirm_query_matched(responses.main_response, query_id_3),
True,
)
self.assertEqual(
self.confirm_query_matched(responses[0], query_id_6), True
self.confirm_query_matched(responses.main_response, query_id_6),
True,
)

def test_index_and_delete_recap_alerts_from_percolator(
Expand Down
Loading

0 comments on commit 180727a

Please sign in to comment.