Skip to content

Commit

Permalink
Merge pull request #557 from nationalarchives/no-enrich-if-in-cooldown
Browse files Browse the repository at this point in the history
Only enrich if not recently called
  • Loading branch information
dragon-dxw authored Feb 14, 2024
2 parents 62d873c + 0477426 commit 73fd65c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
26 changes: 24 additions & 2 deletions src/caselawclient/models/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
uri_for_s3,
)

MINIMUM_ENRICHMENT_TIME = datetime.timedelta(minutes=20)


class UnparsableDate(Warning):
pass
Expand Down Expand Up @@ -480,9 +482,9 @@ def status(self) -> str:

return DOCUMENT_STATUS_NEW

def enrich(self) -> None:
def force_enrich(self) -> None:
"""
Request enrichment of the document
Request enrichment of the document, but do no checks
"""
now = datetime.datetime.now(datetime.timezone.utc)
self.api_client.set_property(
Expand All @@ -495,6 +497,26 @@ def enrich(self) -> None:
enrich=True,
)

def enrich(self) -> bool:
"""
Request enrichment of a document, if it's sensible to do so.
"""
if self.can_enrich:
self.force_enrich()
return True
return False

@cached_property
def can_enrich(self) -> bool:
"""
Is it sensible to enrich this document?
"""
last_enrichment = self.enrichment_datetime
now = datetime.datetime.now(tz=datetime.timezone.utc)
if last_enrichment and now - last_enrichment > MINIMUM_ENRICHMENT_TIME:
return True
return False

def publish(self) -> None:
"""
:raises CannotPublishUnpublishableDocument: This document has not passed the checks in `is_publishable`, and as
Expand Down
22 changes: 20 additions & 2 deletions tests/models/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,9 @@ def test_unpublish(
class TestDocumentEnrich:
@time_machine.travel(datetime.datetime(1955, 11, 5, 6))
@patch("caselawclient.models.documents.announce_document_event")
def test_enrich(self, mock_announce_document_event, mock_api_client):
def test_force_enrich(self, mock_announce_document_event, mock_api_client):
document = Document("test/1234", mock_api_client)
document.enrich()
document.force_enrich()

mock_api_client.set_property.assert_called_once_with(
"test/1234", "last_sent_to_enrichment", "1955-11-05T06:00:00+00:00"
Expand All @@ -433,6 +433,24 @@ def test_enrich(self, mock_announce_document_event, mock_api_client):
uri="test/1234", status="enrich", enrich=True
)

@patch("caselawclient.models.documents.Document.force_enrich")
def test_no_enrich_within_cooldown(self, force_enrich, mock_api_client):
document = Document("test/1234", mock_api_client)
document.enrichment_datetime = datetime.datetime.now(
tz=datetime.timezone.utc
) - datetime.timedelta(seconds=30)
document.enrich()
force_enrich.assert_not_called()

@patch("caselawclient.models.documents.Document.force_enrich")
def test_enrich_outside_cooldown(self, force_enrich, mock_api_client):
document = Document("test/1234", mock_api_client)
document.enrichment_datetime = datetime.datetime.now(
tz=datetime.timezone.utc
) - datetime.timedelta(days=2)
document.enrich()
force_enrich.assert_called()


class TestDocumentHold:
def test_hold(self, mock_api_client):
Expand Down

0 comments on commit 73fd65c

Please sign in to comment.