|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (C) 2025 CERN. |
| 4 | +# |
| 5 | +# invenio-app-ils is free software; you can redistribute it and/or modify it |
| 6 | +# under the terms of the MIT License; see LICENSE file for more details. |
| 7 | + |
| 8 | +"""Invenio App ILS acquisition stats views.""" |
| 9 | + |
| 10 | +from flask import Blueprint, request |
| 11 | +from invenio_records_rest.query import default_search_factory |
| 12 | +from invenio_rest import ContentNegotiatedMethodView |
| 13 | +from marshmallow.exceptions import ValidationError |
| 14 | + |
| 15 | +from invenio_app_ils.acquisition.api import ORDER_PID_TYPE |
| 16 | +from invenio_app_ils.acquisition.proxies import current_ils_acq |
| 17 | +from invenio_app_ils.errors import InvalidParameterError |
| 18 | +from invenio_app_ils.permissions import need_permissions |
| 19 | +from invenio_app_ils.stats.histogram import ( |
| 20 | + HistogramParamsSchema, |
| 21 | + create_histogram_view, |
| 22 | + get_record_statistics, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def create_acquisition_stats_blueprint(app): |
| 27 | + """Add statistics views to the blueprint.""" |
| 28 | + blueprint = Blueprint("invenio_app_ils_acquisition_stats", __name__, url_prefix="") |
| 29 | + |
| 30 | + create_histogram_view( |
| 31 | + blueprint, app, ORDER_PID_TYPE, OrderHistogramResource, "/acquisition" |
| 32 | + ) |
| 33 | + |
| 34 | + return blueprint |
| 35 | + |
| 36 | + |
| 37 | +class OrderHistogramResource(ContentNegotiatedMethodView): |
| 38 | + """Order stats resource.""" |
| 39 | + |
| 40 | + view_name = "order_histogram" |
| 41 | + |
| 42 | + def __init__(self, serializers, ctx, *args, **kwargs): |
| 43 | + """Constructor.""" |
| 44 | + super().__init__(serializers, *args, **kwargs) |
| 45 | + for key, value in ctx.items(): |
| 46 | + setattr(self, key, value) |
| 47 | + |
| 48 | + @need_permissions("stats-orders") |
| 49 | + def get(self, **kwargs): |
| 50 | + """Get order statistics.""" |
| 51 | + |
| 52 | + order_date_fields = [ |
| 53 | + "order_date", |
| 54 | + "expected_delivery_date", |
| 55 | + "received_date", |
| 56 | + "_created", |
| 57 | + "_updated", |
| 58 | + ] |
| 59 | + |
| 60 | + schema = HistogramParamsSchema(order_date_fields) |
| 61 | + try: |
| 62 | + parsed_args = schema.load(request.args.to_dict()) |
| 63 | + except ValidationError as e: |
| 64 | + raise InvalidParameterError(description=e.messages) from e |
| 65 | + |
| 66 | + # Construct search to allow for filtering with the q parameter |
| 67 | + search_cls = current_ils_acq.order_search_cls |
| 68 | + search = search_cls() |
| 69 | + search, _ = default_search_factory(self, search) |
| 70 | + |
| 71 | + aggregation_buckets = get_record_statistics( |
| 72 | + order_date_fields, |
| 73 | + search, |
| 74 | + parsed_args["group_by"], |
| 75 | + parsed_args["metrics"], |
| 76 | + ) |
| 77 | + |
| 78 | + response = { |
| 79 | + "buckets": aggregation_buckets, |
| 80 | + } |
| 81 | + |
| 82 | + return self.make_response(response, 200) |
0 commit comments