From 81423f6527d9df73fa07779b21ed3da77115995d Mon Sep 17 00:00:00 2001 From: Shashank Reddy Boyapally Date: Mon, 19 Aug 2024 16:27:06 -0400 Subject: [PATCH] lookback size Signed-off-by: Shashank Reddy Boyapally --- fmatch/matcher.py | 24 ++++++++++++++++-------- fmatch/tests/test_matcher.py | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/fmatch/matcher.py b/fmatch/matcher.py index 482f939..8a21d21 100644 --- a/fmatch/matcher.py +++ b/fmatch/matcher.py @@ -1,5 +1,4 @@ -""" metadata matcher -""" +"""metadata matcher""" # pylint: disable = invalid-name, invalid-unary-operand-type, no-member import os @@ -23,10 +22,10 @@ class Matcher: def __init__( self, - index: str ="ospst-perf-scale-ci", - level: int =logging.INFO, - ES_URL: str =os.getenv("ES_SERVER"), - verify_certs: bool =True, + index: str = "ospst-perf-scale-ci", + level: int = logging.INFO, + ES_URL: str = os.getenv("ES_SERVER"), + verify_certs: bool = True, ): self.index = index self.es_url = ES_URL @@ -68,7 +67,11 @@ def query_index(self, index: str, search: Search) -> Response: return search.execute() def get_uuid_by_metadata( - self, meta: Dict[str, Any], index: str = None, lookback_date: datetime = None + self, + meta: Dict[str, Any], + index: str = None, + lookback_date: datetime = None, + lookback_size: int = 10000, ) -> List[Dict[str, str]]: """get_uuid_by_metadata""" if index is None: @@ -98,7 +101,12 @@ def get_uuid_by_metadata( must=must_clause, filter=filter_clause, ) - s = Search(using=self.es, index=index).query(query).extra(size=self.search_size) + s = ( + Search(using=self.es, index=index) + .query(query) + .sort({"timestamp": {"order": "desc"}}) + .extra(size=lookback_size) + ) result = self.query_index(index, s) hits = result.hits.hits uuids_docs = [ diff --git a/fmatch/tests/test_matcher.py b/fmatch/tests/test_matcher.py index 95a6462..a3df17c 100644 --- a/fmatch/tests/test_matcher.py +++ b/fmatch/tests/test_matcher.py @@ -100,6 +100,30 @@ def test_get_uuid_by_metadata_lookback(matcher_instance): "buildUrl":"buildUrl1"}] assert result == expected +def test_get_uuid_by_metadata_lookback_size(matcher_instance): + matcher_instance.es.search = lambda *args, **kwargs: { + "hits": { + "hits": [{"_source": {"uuid": "uuid1", + "buildUrl":"buildUrl1", + "timestamp":"2024-07-10T13:46:24Z"}}, + {"_source": {"uuid": "uuid2", + "buildUrl":"buildUrl1", + "timestamp":"2024-07-08T13:46:24Z"}}] + } + } + meta = { + "field1": "value1", + "ocpVersion": "4.15", + } + date= datetime.datetime.strptime("2024-07-07T13:46:24Z","%Y-%m-%dT%H:%M:%SZ") + result = matcher_instance.get_uuid_by_metadata(meta=meta, lookback_date=date, lookback_size=2) + print(result) + expected= [{"uuid": "uuid1", + "buildUrl":"buildUrl1"}, + {"uuid": "uuid2", + "buildUrl":"buildUrl1"}] + assert result == expected + def test_match_kube_burner(matcher_instance): result = matcher_instance.match_kube_burner(["uuid1"],index="ospst-*")