From fc72670dd8a6c492f0d95a287673e5aaa9812631 Mon Sep 17 00:00:00 2001 From: Petr Jasek Date: Fri, 3 Nov 2023 10:21:27 +0100 Subject: [PATCH] cache products and content filters for article so we only check each filter once per article when publishing SDCP-725 --- .../content_filter/content_filter_service.py | 11 ++++++++++- dev-requirements.txt | 2 +- .../formatters/ninjs_newsroom_formatter.py | 15 ++++++++++++--- .../formatters/ninjs_newsroom_formatter_test.py | 2 +- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/apps/content_filters/content_filter/content_filter_service.py b/apps/content_filters/content_filter/content_filter_service.py index 52ad229c1c..192c334abf 100644 --- a/apps/content_filters/content_filter/content_filter_service.py +++ b/apps/content_filters/content_filter/content_filter_service.py @@ -8,6 +8,7 @@ # AUTHORS and LICENSE files distributed with this source code, or # at https://www.sourcefabric.org/superdesk/license +import flask import logging from superdesk.services import CacheableService @@ -195,7 +196,15 @@ def _get_elastic_query(self, doc, matching=True): def does_match(self, content_filter, article, filters=None): if not content_filter: return True # a non-existing filter matches every thing - + cache_id = "filter-match-{filter}-{article}".format( + filter=content_filter.get("_id") or content_filter.get("name"), + article=article.get("_id") or article.get("guid"), + ) + if not hasattr(flask.g, cache_id): + setattr(flask.g, cache_id, self._does_match(content_filter, article, filters)) + return getattr(flask.g, cache_id) + + def _does_match(self, content_filter, article, filters): filter_condition_service = get_resource_service("filter_conditions") expressions = [] for index, expression in enumerate(content_filter.get("content_filter", [])): diff --git a/dev-requirements.txt b/dev-requirements.txt index d869766603..c1c3a3bb0f 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -17,5 +17,5 @@ typing_extensions>=3.7.4 moto[sqs] -e . --e git+https://github.com/superdesk/superdesk-planning.git@develop#egg=superdesk-planning +-e git+https://github.com/superdesk/superdesk-planning.git@v2.6.2#egg=superdesk-planning -e git+https://github.com/superdesk/sams.git@develop#egg=sams_client&subdirectory=src/clients/python/ diff --git a/superdesk/publish/formatters/ninjs_newsroom_formatter.py b/superdesk/publish/formatters/ninjs_newsroom_formatter.py index 328629a239..180be3fae9 100644 --- a/superdesk/publish/formatters/ninjs_newsroom_formatter.py +++ b/superdesk/publish/formatters/ninjs_newsroom_formatter.py @@ -9,10 +9,12 @@ # at https://www.sourcefabric.org/superdesk/license -from .ninjs_formatter import NINJSFormatter +import flask import superdesk import elasticapm +from .ninjs_formatter import NINJSFormatter + class NewsroomNinjsFormatter(NINJSFormatter): name = "Newsroom NINJS" @@ -32,8 +34,15 @@ def _format_products(self, article): :param article: :return: """ - result = superdesk.get_resource_service("product_tests").test_products(article) - return [{"code": p["product_id"], "name": p.get("name")} for p in result if p.get("matched", False)] + cache_id = "article-products-{_id}".format(_id=article.get("_id") or article.get("guid")) + if not hasattr(flask.g, cache_id): + matches = superdesk.get_resource_service("product_tests").test_products(article) + setattr( + flask.g, + cache_id, + [{"code": p["product_id"], "name": p.get("name")} for p in matches if p.get("matched", False)], + ) + return getattr(flask.g, cache_id) @elasticapm.capture_span() def _transform_to_ninjs(self, article, subscriber, recursive=True): diff --git a/tests/publish/formatters/ninjs_newsroom_formatter_test.py b/tests/publish/formatters/ninjs_newsroom_formatter_test.py index 3d45956d3a..fd48367199 100644 --- a/tests/publish/formatters/ninjs_newsroom_formatter_test.py +++ b/tests/publish/formatters/ninjs_newsroom_formatter_test.py @@ -150,7 +150,7 @@ def test_products(self): "products": [{"code": 1, "name": "p-1"}], } self.assertEqual(json.loads(doc), expected) - article["urgency"] = 1 + article.update(urgency=1, _id="v2") seq, doc = self.formatter.format(article, {"name": "Test Subscriber"})[0] expected = { "guid": "tag:aap.com.au:20150613:12345",