Skip to content

Commit

Permalink
Merge pull request #3748 from freelawproject/3630-index-document-chan…
Browse files Browse the repository at this point in the history
…ges-from-history-tables

3630 Index changed content from the history tables
  • Loading branch information
mlissner committed Feb 12, 2024
2 parents f111ee6 + 896ab75 commit 0a4e02a
Show file tree
Hide file tree
Showing 9 changed files with 1,075 additions and 117 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ jobs:
cl/users/management/commands/cl_delete_old_emails.py \
cl/users/management/commands/cl_retry_failed_email.py \
cl/users/tasks.py \
cl/recap/management/commands/remove_appellate_entries_with_long_numbers.py
cl/recap/management/commands/remove_appellate_entries_with_long_numbers.py \
cl/search/management/commands/cl_index_parent_and_child_docs.py
Expand Down
3 changes: 2 additions & 1 deletion cl/alerts/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from cl.recap.constants import COURT_TIMEZONES
from cl.search.models import Docket, DocketEntry
from cl.search.types import (
ESDocumentNameType,
PercolatorResponseType,
SaveDocumentResponseType,
SearchAlertHitType,
Expand Down Expand Up @@ -660,7 +661,7 @@ def send_or_schedule_alerts(
def es_save_alert_document(
self: Task,
alert_id: int,
es_document_name: str,
es_document_name: ESDocumentNameType,
) -> None:
"""Helper method to prepare and index an Alert object into Elasticsearch.
Expand Down
2 changes: 1 addition & 1 deletion cl/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ async def test_api_logged_correctly(self, mock_logging_prefix) -> None:

# Timings
self.assertAlmostEqual(
int(self.r.get("api:Test.timing")), 10, delta=500
int(self.r.get("api:Test.timing")), 10, delta=2000
)


Expand Down
76 changes: 52 additions & 24 deletions cl/lib/es_signal_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist
from django.db import transaction
from django.db.models.signals import m2m_changed, post_delete, post_save
from model_utils.tracker import FieldInstanceTracker

from cl.alerts.tasks import (
process_percolator_response,
Expand Down Expand Up @@ -57,6 +58,56 @@ def compose_app_label(instance: ESModelType) -> str:
return f"{instance._meta.app_label}.{instance.__class__.__name__}"


def check_fields_that_changed(
current_instance: ESModelType,
tracked_set: FieldInstanceTracker,
previous_instance: ESModelType | None = None,
) -> list[str]:
"""Identify which fields have changed between two instances of a model or
between an instance and its previous state.
:param current_instance: The current instance of the model to check for
changes.
:param tracked_set: An instance of FieldInstanceTracker for tracking field
changes.
:param previous_instance: Optional the previous instance of the model to
compare against. If None, the function compares against the tracked
previous values.
:return: A list of strings representing the names of the fields that have
changed.
"""
changed_fields = []
for field in tracked_set.fields:
current_value = getattr(current_instance, field)
if previous_instance:
previous_value = getattr(previous_instance, field)
else:
previous_value = tracked_set.previous(field)
try:
# If field is a ForeignKey relation, the current value is the
# related object, while the previous value is the ID, get the id.
# See https://django-model-utils.readthedocs.io/en/latest/utilities.html#field-tracker
field_type = current_instance.__class__._meta.get_field(field)
if (
field_type.get_internal_type() == "ForeignKey"
and current_value
and not field.endswith("_id")
):
current_value = current_value.pk
except FieldDoesNotExist:
# Support tracking for properties, only abort if it's not a model
# property
if not hasattr(current_instance, field) and not isinstance(
getattr(current_instance.__class__, field, None), property
):
continue

if current_value != previous_value:
changed_fields.append(field)

return changed_fields


def updated_fields(
instance: ESModelType, es_document: ESDocumentClassType
) -> list[str]:
Expand Down Expand Up @@ -85,30 +136,7 @@ def updated_fields(
return []

# Check each tracked field to see if it has changed
changed_fields = []
for field in tracked_set.fields:
current_value = getattr(instance, field)
try:
# If field is a ForeignKey relation, the current value is the
# related object, while the previous value is the ID, get the id.
# See https://django-model-utils.readthedocs.io/en/latest/utilities.html#field-tracker
field_type = instance.__class__._meta.get_field(field)
if (
field_type.get_internal_type() == "ForeignKey"
and current_value
and not field.endswith("_id")
):
current_value = current_value.pk
except FieldDoesNotExist:
# Support tracking for properties, only abort if it's not a model
# property
if not hasattr(instance, field) and not isinstance(
getattr(instance.__class__, field, None), property
):
continue

if current_value != tracked_set.previous(field):
changed_fields.append(field)
changed_fields = check_fields_that_changed(instance, tracked_set)
return changed_fields


Expand Down
Loading

0 comments on commit 0a4e02a

Please sign in to comment.