Skip to content

Commit 72f6ba7

Browse files
JakobMiesnerkpsherva
authored andcommitted
stats: generalize stat tracking loan extensions to track all transitions
1 parent b5da245 commit 72f6ba7

File tree

4 files changed

+45
-35
lines changed

4 files changed

+45
-35
lines changed

invenio_app_ils/config.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@
4343
from invenio_app_ils.patrons.indexer import PatronIndexer
4444
from invenio_app_ils.series.indexer import SeriesIndexer
4545
from invenio_app_ils.stats.event_builders import ils_record_changed_event_builder
46-
from invenio_app_ils.stats.processors import (
47-
LoansEventsIndexer,
48-
filter_extend_transitions,
49-
)
46+
from invenio_app_ils.stats.processors import LoansEventsIndexer
5047
from invenio_app_ils.vocabularies.indexer import VocabularyIndexer
5148

5249
from .document_requests.api import (
@@ -995,9 +992,8 @@ def _(x):
995992
"suffix": "%Y",
996993
},
997994
},
998-
# The following events are used to track loan state transitions and store additional data.
999-
# Only the "extend" transition will be aggregated and used in the way intended by invenio-stats.
1000-
# Other transitions, e.g. "request", are used to store additional information,
995+
# The following events are used to count loan state transitions.
996+
# Additionally, some transitions, e.g. "request", are used to store extra data,
1001997
# like the number of available items when a loan is requested.
1002998
# The loan indexer then later queries those events and adds the information to the loan.
1003999
"loan-transitions": {
@@ -1084,8 +1080,7 @@ def _(x):
10841080
index_interval="year",
10851081
copy_fields=dict(),
10861082
metric_fields=dict(),
1087-
# We only track extension transitions
1088-
query_modifiers=[filter_extend_transitions],
1083+
query_modifiers=[],
10891084
),
10901085
),
10911086
}
@@ -1162,13 +1157,13 @@ def _(x):
11621157
aggregated_fields=["user_id"],
11631158
),
11641159
),
1165-
"loan-extensions": dict(
1160+
"loan-transitions": dict(
11661161
cls=DateHistogramQuery,
11671162
permission_factory=backoffice_read_permission,
11681163
params=dict(
11691164
index="stats-loan-transitions",
11701165
copy_fields=dict(),
1171-
required_filters=dict(),
1166+
required_filters=dict(trigger="trigger"),
11721167
metric_fields=dict(
11731168
count=("sum", "count", {}),
11741169
),

invenio_app_ils/stats/event_builders.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,5 @@ def loan_transition_event_builder(
113113
},
114114
}
115115
)
116-
elif trigger == "extend":
117-
# Extensions are aggregated by invenio-stats and no extra information is required
118-
pass
119-
else:
120-
return None
121116

122117
return event

invenio_app_ils/stats/processors.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ def add_loan_transition_unique_id(doc):
4444
return doc
4545

4646

47-
def filter_extend_transitions(query):
48-
"""Filter for extend transitions only"""
49-
50-
return query.filter("term", trigger="extend")
51-
52-
5347
class LoansEventsIndexer(EventsIndexer):
5448
"""Events indexer for events related to loans.
5549

tests/api/ils/stats/test_loan_extension_stats.py renamed to tests/api/ils/stats/test_loan_transition_stats.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
from tests.helpers import user_login, user_logout
2020

2121

22-
def _query_loan_extensions_stats(client):
22+
def _query_loan_extensions_stats(client, trigger):
2323
"""Query stats via the HTTP API."""
2424
response = query_stats(
2525
client,
26-
"loan-extensions",
27-
{},
26+
"loan-transitions",
27+
{
28+
"trigger": trigger,
29+
},
2830
)
2931
assert response.status_code == 200
3032
buckets = extract_buckets_from_stats_query(response)
@@ -33,7 +35,7 @@ def _query_loan_extensions_stats(client):
3335
return total_count
3436

3537

36-
def test_loan_extensions_histogram(
38+
def test_loan_transition_histogram(
3739
client,
3840
json_headers,
3941
users,
@@ -43,21 +45,28 @@ def test_loan_extensions_histogram(
4345
loan_params,
4446
checkout_loan,
4547
):
46-
"""Test that loan extensions are tracked correctly."""
48+
"""Test that certain transitions are tracked correctly.
49+
50+
The following transitions are tested checkout, extend and checkin
51+
"""
4752

4853
process_and_aggregate_stats()
4954
user_login(client, "admin", users)
50-
initial_count = _query_loan_extensions_stats(client)
55+
initial_checkout_count = _query_loan_extensions_stats(client, "checkout")
56+
initial_extend_count = _query_loan_extensions_stats(client, "extend")
57+
initial_checkin_count = _query_loan_extensions_stats(client, "checkin")
5158

52-
# checkout and extend loan
59+
# checkout loan
5360
loan_pid = "loanid-1"
5461
params = deepcopy(loan_params)
5562
params["document_pid"] = "docid-1"
5663
params["item_pid"]["value"] = "itemid-2"
5764
del params["transaction_date"]
5865
loan = checkout_loan(loan_pid, params)
5966

60-
extend_url = loan["links"]["actions"]["extend"]
67+
# extend loan
68+
urls = loan["links"]["actions"]
69+
extend_url = urls["extend"]
6170
user_login(client, "admin", users)
6271
res = client.post(
6372
extend_url,
@@ -66,15 +75,30 @@ def test_loan_extensions_histogram(
6675
)
6776
assert res.status_code == 202
6877

78+
# checkin loan
79+
checkin_url = urls["checkin"]
80+
user_login(client, "librarian", users)
81+
res = client.post(
82+
checkin_url,
83+
headers=json_headers,
84+
data=json.dumps(params),
85+
)
86+
assert res.status_code == 202
87+
6988
process_and_aggregate_stats()
70-
final_count = _query_loan_extensions_stats(client)
71-
assert final_count == initial_count + 1
89+
final_checkout_count = _query_loan_extensions_stats(client, "checkout")
90+
final_extend_count = _query_loan_extensions_stats(client, "extend")
91+
final_checkin_count = _query_loan_extensions_stats(client, "checkin")
92+
93+
assert final_extend_count == initial_extend_count + 1
94+
assert final_checkout_count == initial_checkout_count + 1
95+
assert final_checkin_count == initial_checkin_count + 1
7296

7397

74-
def test_loan_extensions_stats_permissions(client, users):
98+
def test_loan_transition_stats_permissions(client, users):
7599
"""Test that only certain users can access the stats."""
76100

77-
stat = "loan-extensions"
101+
stat = "loan-transitions"
78102
tests = [
79103
("admin", 200),
80104
("patron1", 403),
@@ -83,7 +107,9 @@ def test_loan_extensions_stats_permissions(client, users):
83107
("anonymous", 401),
84108
]
85109

86-
params = {}
110+
params = {
111+
"trigger": "request",
112+
}
87113
for username, expected_resp_code in tests:
88114
user_login(client, username, users)
89115
response = query_stats(

0 commit comments

Comments
 (0)