From 4071f68b67711dac36d1c2c4d6e737633d1c11d3 Mon Sep 17 00:00:00 2001 From: PascalRepond Date: Wed, 5 Feb 2025 10:11:39 +0100 Subject: [PATCH] fix(pricing stats): make active patrons period 12 months - The correct date range for `number_of_active_patrons` in the pricing stats should be 12 months and not the default `RERO_ILS_STATS_BILLING_TIMEFRAME_IN_MONTHS`. Co-Authored-by: Pascal Repond --- rero_ils/modules/stats/api/pricing.py | 9 ++++++++- tests/ui/stats/test_stats_pricing.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/rero_ils/modules/stats/api/pricing.py b/rero_ils/modules/stats/api/pricing.py index ae6d297a0d..7596b51550 100644 --- a/rero_ils/modules/stats/api/pricing.py +++ b/rero_ils/modules/stats/api/pricing.py @@ -58,6 +58,7 @@ def __init__(self, to_date=None): fmt="YYYY-MM-DDT00:00:00" ) _to = to_date.format(fmt="YYYY-MM-DDT23:59:59") + self.to_date = to_date self.date_range = {"gte": _from, "lte": _to} @classmethod @@ -171,11 +172,17 @@ def number_of_active_patrons(self, library_pid): :return: the number of matched active patrons :rtype: integer """ + # this should always count patrons that were active in the past year + _from = (self.to_date - relativedelta(months=12)).format( + fmt="YYYY-MM-DDT00:00:00" + ) + _to = self.date_range.get("lte") + activity_period = {"gte": _from, "lte": _to} op_logs_query = ( LoanOperationLogsSearch() .get_logs_by_trigger( triggers=[ItemCirculationAction.CHECKOUT, ItemCirculationAction.EXTEND], - date_range=self.date_range, + date_range=activity_period, ) .filter("term", loan__item__library_pid=library_pid) ) diff --git a/tests/ui/stats/test_stats_pricing.py b/tests/ui/stats/test_stats_pricing.py index 155c3412dc..2d8c20de78 100644 --- a/tests/ui/stats/test_stats_pricing.py +++ b/tests/ui/stats/test_stats_pricing.py @@ -19,6 +19,8 @@ """Stats Pricing tests.""" import mock +from arrow import utcnow +from dateutil.relativedelta import relativedelta from invenio_db import db from rero_ils.modules.ill_requests.models import ILLRequestStatus @@ -81,6 +83,19 @@ def test_stats_pricing_number_of_active_patrons( assert stat_for_pricing.number_of_active_patrons("foo") == 0 assert stat_for_pricing.number_of_active_patrons(lib_martigny.pid) == 1 + # make sure that the class's date_range has no influence on this indicator + default_date_range = stat_for_pricing.date_range + _to = utcnow() + relativedelta(months=3) + _from = utcnow() + relativedelta(days=1) + stat_for_pricing.date_range = { + "gte": _from.format(fmt="YYYY-MM-DDT00:00:00"), + "lte": _to.format(fmt="YYYY-MM-DDT00:00:00"), + } + assert stat_for_pricing.number_of_active_patrons(lib_martigny.pid) == 1 + + # revert to default + stat_for_pricing.date_range = default_date_range + def test_stats_pricing_number_of_order_lines( stat_for_pricing, acq_order_line_fiction_martigny