diff --git a/swh/search/elasticsearch.py b/swh/search/elasticsearch.py --- a/swh/search/elasticsearch.py +++ b/swh/search/elasticsearch.py @@ -36,6 +36,8 @@ "has_visits", "intrinsic_metadata", "visit_types", + "nb_visit", + "last_visit_date", ): if field_name in origin: res[field_name] = origin.pop(field_name) @@ -171,6 +173,8 @@ update_script = """ // backup current visit_types field value List visit_types = ctx._source.getOrDefault("visit_types", []); + int nb_visit = ctx._source.getOrDefault("nb_visit", 0); + ZonedDateTime last_visit_date = ZonedDateTime.parse(ctx._source.getOrDefault("last_visit_date", "0001-01-01T00:00:00")); // update origin document with new field values ctx._source.putAll(params); @@ -183,7 +187,23 @@ } } } - """ + + // Undo overwrite if incoming nb_visit is smaller + if (ctx._source.containsKey("nb_visit")) { + int incoming_nb_visit = ctx._source.getOrDefault("nb_visit", 0); + if(incoming_nb_visit < nb_visit){ + ctx._source.nb_visit = nb_visit; + } + } + + // Undo overwrite if incoming last_visit_date is older + if (ctx._source.containsKey("last_visit_date")) { + ZonedDateTime incoming_last_visit_date = ZonedDateTime.parse(ctx._source.getOrDefault("last_visit_date", "0001-01-01T00:00:00")); + if(incoming_last_visit_date < last_visit_date){ + ctx._source.last_visit_date = last_visit_date; + } + } + """ # noqa actions = [ { @@ -224,6 +244,7 @@ metadata_pattern: Optional[str] = None, with_visit: bool = False, visit_types: Optional[List[str]] = None, + nb_visit: int = None, page_token: Optional[str] = None, limit: int = 50, ) -> PagedResult[MinimalOriginDict]: @@ -277,6 +298,8 @@ if with_visit: query_clauses.append({"term": {"has_visits": True,}}) + if nb_visit: + query_clauses.append({"term": {"nb_visit": nb_visit,}}) if visit_types is not None: query_clauses.append({"terms": {"visit_types": visit_types}}) diff --git a/swh/search/in_memory.py b/swh/search/in_memory.py --- a/swh/search/in_memory.py +++ b/swh/search/in_memory.py @@ -4,6 +4,7 @@ # See top-level LICENSE file for more information from collections import defaultdict +from datetime import datetime import itertools import re from typing import Any, Dict, Iterable, Iterator, List, Optional @@ -65,6 +66,15 @@ document["visit_types"] = set(source_document["visit_types"]) if "visit_types" in self._origins[id_]: document["visit_types"].update(self._origins[id_]["visit_types"]) + if "nb_visit" in document: + document["nb_visit"] = max( + document["nb_visit"], self._origins[id_]["nb_visit"] + ) + if "last_visit_date" in document: + document["last_visit_date"] = max( + datetime.fromisoformat(document["last_visit_date"]), + self._origins[id_]["last_visit_date"], + ) self._origins[id_].update(document) if id_ not in self._origin_ids: @@ -78,6 +88,7 @@ with_visit: bool = False, visit_types: Optional[List[str]] = None, page_token: Optional[str] = None, + nb_visit: int = None, limit: int = 50, ) -> PagedResult[MinimalOriginDict]: hits: Iterator[Dict[str, Any]] = ( @@ -129,6 +140,8 @@ if with_visit: hits = filter(lambda o: o.get("has_visits"), hits) + if nb_visit: + hits = filter(lambda o: o.get("nb_visit") == nb_visit, hits) if visit_types is not None: visit_types_set = set(visit_types) diff --git a/swh/search/interface.py b/swh/search/interface.py --- a/swh/search/interface.py +++ b/swh/search/interface.py @@ -59,6 +59,7 @@ with_visit: bool = False, visit_types: Optional[List[str]] = None, page_token: Optional[str] = None, + nb_visit: int = None, limit: int = 50, ) -> PagedResult[MinimalOriginDict]: """Searches for origins matching the `url_pattern`. diff --git a/swh/search/journal_client.py b/swh/search/journal_client.py --- a/swh/search/journal_client.py +++ b/swh/search/journal_client.py @@ -59,7 +59,12 @@ logging.debug("processing origin visit statuses %r", visit_statuses) full_visit_status = [ - {"url": (visit_status["origin"]), "has_visits": True,} + { + "url": (visit_status["origin"]), + "has_visits": True, + "nb_visit": visit_status["visit"], + "last_visit_date": visit_status["date"].isoformat(), + } for visit_status in visit_statuses if visit_status["status"] == "full" ] diff --git a/swh/search/tests/test_cli.py b/swh/search/tests/test_cli.py --- a/swh/search/tests/test_cli.py +++ b/swh/search/tests/test_cli.py @@ -4,6 +4,7 @@ # See top-level LICENSE file for more information import copy +from datetime import datetime, timezone import tempfile from click.testing import CliRunner @@ -179,6 +180,7 @@ { "origin": origin_foobar["url"], "visit": 1, + "date": datetime.now(tz=timezone.utc), "snapshot": None, "status": "full", } diff --git a/swh/search/tests/test_journal_client.py b/swh/search/tests/test_journal_client.py --- a/swh/search/tests/test_journal_client.py +++ b/swh/search/tests/test_journal_client.py @@ -3,6 +3,7 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +from datetime import datetime, timezone import functools from unittest.mock import MagicMock @@ -42,23 +43,45 @@ search_mock = MagicMock() worker_fn = functools.partial(process_journal_objects, search=search_mock,) + current_datetime = datetime.now(tz=timezone.utc) worker_fn( { "origin_visit_status": [ - {"origin": "http://foobar.baz", "status": "full"} # full visits ok + { + "origin": "http://foobar.baz", + "status": "full", + "visit": 5, + "date": current_datetime, + } # full visits ok ] } ) search_mock.origin_update.assert_called_once_with( - [{"url": "http://foobar.baz", "has_visits": True},] + [ + { + "url": "http://foobar.baz", + "has_visits": True, + "nb_visit": 5, + "last_visit_date": current_datetime.isoformat(), + }, + ] ) search_mock.reset_mock() # non-full visits are filtered out worker_fn( - {"origin_visit_status": [{"origin": "http://foobar.baz", "status": "partial"}]} + { + "origin_visit_status": [ + { + "origin": "http://foobar.baz", + "status": "partial", + "visit": 5, + "date": current_datetime, + } + ] + } ) search_mock.origin_update.assert_not_called() diff --git a/swh/search/tests/test_search.py b/swh/search/tests/test_search.py --- a/swh/search/tests/test_search.py +++ b/swh/search/tests/test_search.py @@ -4,6 +4,7 @@ # See top-level LICENSE file for more information from hypothesis import given, settings, strategies +import pytest from swh.core.api.classes import stream_results @@ -184,6 +185,33 @@ ] ) + def test_origin_nb_visit_update_search(self): + origin_url = "http://foobar.baz" + self.search.origin_update([{"url": origin_url}]) + self.search.flush() + + def _update_nb_visit(nb_visit): + self.search.origin_update([{"url": origin_url, "nb_visit": nb_visit}]) + self.search.flush() + + def _check_nb_visit(nb_visit): + actual_page = self.search.origin_search( + url_pattern=origin_url, nb_visit=nb_visit, + ) + assert actual_page.next_page_token is None + results = [r["url"] for r in actual_page.results] + expected_results = [origin_url] + assert sorted(results) == sorted(expected_results) + + _update_nb_visit(5) + _check_nb_visit(5) + + _update_nb_visit(3) + + _check_nb_visit(5) # nb_visit shouldn't get updated and remain remain 5 + with pytest.raises(AssertionError): + _check_nb_visit(3) + def test_origin_update_with_no_visit_types(self): """ Update an origin with visit types first then with no visit types,