Skip to content

Commit

Permalink
fix(alerts): Restore send_es_search_alert_webhook to avoid conflicts …
Browse files Browse the repository at this point in the history
…due to scheduled task

- This can be removed after tasks in the queue have been processed.
  • Loading branch information
albertisfu committed Jul 25, 2024
1 parent 38d6884 commit b56f235
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
4 changes: 2 additions & 2 deletions cl/alerts/management/commands/cl_send_recap_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
recap_document_hl_matched,
)
from cl.api.models import WebhookEventType
from cl.api.tasks import send_es_search_alert_webhook
from cl.api.tasks import send_search_alert_webhook_es
from cl.lib.command_utils import VerboseCommand, logger
from cl.lib.date_time import dt_as_local_date
from cl.lib.elasticsearch_utils import do_es_sweep_alert_query
Expand Down Expand Up @@ -543,7 +543,7 @@ def send_search_alert_webhooks(
event_type=WebhookEventType.SEARCH_ALERT, enabled=True
)
for user_webhook in user_webhooks:
send_es_search_alert_webhook.delay(
send_search_alert_webhook_es.delay(
results_to_send, user_webhook.pk, alert_id
)

Expand Down
4 changes: 2 additions & 2 deletions cl/alerts/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from cl.api.models import WebhookEventType
from cl.api.tasks import (
send_docket_alert_webhook_events,
send_es_search_alert_webhook,
send_search_alert_webhook_es,
)
from cl.celery_init import app
from cl.custom_filters.templatetags.text_filters import best_case_name
Expand Down Expand Up @@ -458,7 +458,7 @@ def send_webhook_alert_hits(
event_type=WebhookEventType.SEARCH_ALERT, enabled=True
)
for user_webhook in user_webhooks:
send_es_search_alert_webhook.delay(
send_search_alert_webhook_es.delay(
documents,
user_webhook.pk,
alert.pk,
Expand Down
42 changes: 42 additions & 0 deletions cl/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,50 @@ def send_docket_alert_webhook_events(
send_webhook_event(webhook_event, json_bytes)


# TODO: Remove after scheduled OA alerts have been processed.
@app.task()
def send_es_search_alert_webhook(
results: list[dict[str, Any]],
webhook_pk: int,
alert: Alert,
) -> None:
"""Send a search alert webhook event containing search results from a
search alert object.
:param results: The search results returned by SOLR for this alert.
:param webhook_pk: The webhook endpoint ID object to send the event to.
:param alert: The search alert object.
"""

webhook = Webhook.objects.get(pk=webhook_pk)
serialized_alert = SearchAlertSerializerModel(alert).data
es_results = []
for result in results:
result["snippet"] = result["text"]
es_results.append(ResultObject(initial=result))
serialized_results = V3OAESResultSerializer(es_results, many=True).data

post_content = {
"webhook": generate_webhook_key_content(webhook),
"payload": {
"results": serialized_results,
"alert": serialized_alert,
},
}
renderer = JSONRenderer()
json_bytes = renderer.render(
post_content,
accepted_media_type="application/json;",
)
webhook_event = WebhookEvent.objects.create(
webhook=webhook,
content=post_content,
)
send_webhook_event(webhook_event, json_bytes)


@app.task()
def send_search_alert_webhook_es(
results: list[dict[str, Any]] | list[Hit],
webhook_pk: int,
alert_pk: int,
Expand Down

0 comments on commit b56f235

Please sign in to comment.