Skip to content

Commit

Permalink
fix(pricing stats): make active patrons period 12 months
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
PascalRepond committed Feb 10, 2025
1 parent acc3dd9 commit 4071f68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion rero_ils/modules/stats/api/pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
)
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/stats/test_stats_pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4071f68

Please sign in to comment.