Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions invenio_app_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@
from invenio_app_ils.patrons.indexer import PatronIndexer
from invenio_app_ils.series.indexer import SeriesIndexer
from invenio_app_ils.stats.event_builders import ils_record_changed_event_builder
from invenio_app_ils.stats.processors import (
LoansEventsIndexer,
filter_extend_transitions,
)
from invenio_app_ils.stats.processors import LoansEventsIndexer
from invenio_app_ils.vocabularies.indexer import VocabularyIndexer

from .document_requests.api import (
Expand Down Expand Up @@ -995,9 +992,8 @@ def _(x):
"suffix": "%Y",
},
},
# The following events are used to track loan state transitions and store additional data.
# Only the "extend" transition will be aggregated and used in the way intended by invenio-stats.
# Other transitions, e.g. "request", are used to store additional information,
# The following events are used to count loan state transitions.
# Additionally, some transitions, e.g. "request", are used to store extra data,
# like the number of available items when a loan is requested.
# The loan indexer then later queries those events and adds the information to the loan.
"loan-transitions": {
Expand Down Expand Up @@ -1084,8 +1080,7 @@ def _(x):
index_interval="year",
copy_fields=dict(),
metric_fields=dict(),
# We only track extension transitions
query_modifiers=[filter_extend_transitions],
query_modifiers=[],
),
),
}
Expand Down Expand Up @@ -1162,13 +1157,13 @@ def _(x):
aggregated_fields=["user_id"],
),
),
"loan-extensions": dict(
"loan-transitions": dict(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make sure: does this change influcence any existing stats in prod?

cls=DateHistogramQuery,
permission_factory=backoffice_read_permission,
params=dict(
index="stats-loan-transitions",
copy_fields=dict(),
required_filters=dict(),
required_filters=dict(trigger="trigger"),
metric_fields=dict(
count=("sum", "count", {}),
),
Expand Down
5 changes: 0 additions & 5 deletions invenio_app_ils/stats/event_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,5 @@ def loan_transition_event_builder(
},
}
)
elif trigger == "extend":
# Extensions are aggregated by invenio-stats and no extra information is required
pass
else:
return None

return event
6 changes: 0 additions & 6 deletions invenio_app_ils/stats/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ def add_loan_transition_unique_id(doc):
return doc


def filter_extend_transitions(query):
"""Filter for extend transitions only"""

return query.filter("term", trigger="extend")


class LoansEventsIndexer(EventsIndexer):
"""Events indexer for events related to loans.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
from tests.helpers import user_login, user_logout


def _query_loan_extensions_stats(client):
def _query_loan_extensions_stats(client, trigger):
"""Query stats via the HTTP API."""
response = query_stats(
client,
"loan-extensions",
{},
"loan-transitions",
{
"trigger": trigger,
},
)
assert response.status_code == 200
buckets = extract_buckets_from_stats_query(response)
Expand All @@ -33,7 +35,7 @@ def _query_loan_extensions_stats(client):
return total_count


def test_loan_extensions_histogram(
def test_loan_transition_histogram(
client,
json_headers,
users,
Expand All @@ -43,21 +45,28 @@ def test_loan_extensions_histogram(
loan_params,
checkout_loan,
):
"""Test that loan extensions are tracked correctly."""
"""Test that certain transitions are tracked correctly.

The following transitions are tested checkout, extend and checkin
"""

process_and_aggregate_stats()
user_login(client, "admin", users)
initial_count = _query_loan_extensions_stats(client)
initial_checkout_count = _query_loan_extensions_stats(client, "checkout")
initial_extend_count = _query_loan_extensions_stats(client, "extend")
initial_checkin_count = _query_loan_extensions_stats(client, "checkin")

# checkout and extend loan
# checkout loan
loan_pid = "loanid-1"
params = deepcopy(loan_params)
params["document_pid"] = "docid-1"
params["item_pid"]["value"] = "itemid-2"
del params["transaction_date"]
loan = checkout_loan(loan_pid, params)

extend_url = loan["links"]["actions"]["extend"]
# extend loan
urls = loan["links"]["actions"]
extend_url = urls["extend"]
user_login(client, "admin", users)
res = client.post(
extend_url,
Expand All @@ -66,15 +75,30 @@ def test_loan_extensions_histogram(
)
assert res.status_code == 202

# checkin loan
checkin_url = urls["checkin"]
user_login(client, "librarian", users)
res = client.post(
checkin_url,
headers=json_headers,
data=json.dumps(params),
)
assert res.status_code == 202

process_and_aggregate_stats()
final_count = _query_loan_extensions_stats(client)
assert final_count == initial_count + 1
final_checkout_count = _query_loan_extensions_stats(client, "checkout")
final_extend_count = _query_loan_extensions_stats(client, "extend")
final_checkin_count = _query_loan_extensions_stats(client, "checkin")

assert final_extend_count == initial_extend_count + 1
assert final_checkout_count == initial_checkout_count + 1
assert final_checkin_count == initial_checkin_count + 1


def test_loan_extensions_stats_permissions(client, users):
def test_loan_transition_stats_permissions(client, users):
"""Test that only certain users can access the stats."""

stat = "loan-extensions"
stat = "loan-transitions"
tests = [
("admin", 200),
("patron1", 403),
Expand All @@ -83,7 +107,9 @@ def test_loan_extensions_stats_permissions(client, users):
("anonymous", 401),
]

params = {}
params = {
"trigger": "request",
}
for username, expected_resp_code in tests:
user_login(client, username, users)
response = query_stats(
Expand Down
Loading