From 245e65a5a4287222a2f95036a03e80cacb450907 Mon Sep 17 00:00:00 2001 From: Travis Semple Date: Tue, 3 May 2022 15:55:58 -0700 Subject: [PATCH] 12032 - FAS - Performance - Query optimization (#926) * Fix business_identifier search. * Fix the pagination and limit for search. * Linting fixes + minor search tweak. --- pay-api/src/pay_api/models/routing_slip.py | 25 ++++++++++--------- .../src/pay_api/services/fas/routing_slip.py | 7 +----- .../tests/unit/models/test_routing_slip.py | 12 ++++----- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/pay-api/src/pay_api/models/routing_slip.py b/pay-api/src/pay_api/models/routing_slip.py index 740e6001f..3d2ad3fbd 100644 --- a/pay-api/src/pay_api/models/routing_slip.py +++ b/pay-api/src/pay_api/models/routing_slip.py @@ -101,7 +101,7 @@ def find_all_by_payment_account_id(cls, payment_account_id: str) -> List[Routing @classmethod def search(cls, search_filter: Dict, # pylint: disable=too-many-arguments - page: int, limit: int, return_all: bool, max_no_records: int = 0) -> (List[RoutingSlip], int): + page: int, limit: int, return_all: bool) -> (List[RoutingSlip], int): """Search for routing slips by the criteria provided.""" query = db.session.query(RoutingSlip).\ outerjoin(RoutingSlip.payments).\ @@ -146,6 +146,9 @@ def search(cls, search_filter: Dict, # pylint: disable=too-many-arguments if initiator := search_filter.get('initiator', None): query = query.filter(RoutingSlip.created_name.ilike('%' + initiator + '%')) + if business_identifier := search_filter.get('businessIdentifier', None): + query = query.filter(Invoice.business_identifier == business_identifier) + query = cls._add_receipt_number(query, search_filter) query = cls._add_folio_filter(query, search_filter) @@ -156,17 +159,15 @@ def search(cls, search_filter: Dict, # pylint: disable=too-many-arguments query = query.order_by(RoutingSlip.created_on.desc()) if not return_all: - pagination = query.paginate(per_page=limit, page=page) - result, count = pagination.items, pagination.total - if max_no_records > 0: - count = max_no_records if max_no_records < count else count - else: - if max_no_records > 0: - pagination = query.paginate(per_page=max_no_records, page=1) - result, count = pagination.items, max_no_records - else: - result = query.all() - count = len(result) + sub_query = query.with_entities(RoutingSlip.id).\ + group_by(RoutingSlip.id).\ + limit(limit).\ + offset((page - 1) * limit).\ + subquery() + query = query.filter(RoutingSlip.id.in_(sub_query)) + + result = query.all() + count = len(result) return result, count diff --git a/pay-api/src/pay_api/services/fas/routing_slip.py b/pay-api/src/pay_api/services/fas/routing_slip.py index bd9f6d4df..56aa66f2f 100644 --- a/pay-api/src/pay_api/services/fas/routing_slip.py +++ b/pay-api/src/pay_api/services/fas/routing_slip.py @@ -169,12 +169,7 @@ def asdict(self) -> Dict[str]: @classmethod def search(cls, search_filter: Dict, page: int, limit: int, return_all: bool = False): """Search for routing slip.""" - max_no_records: int = 0 - if not bool(search_filter) or not any(search_filter.values()): - max_no_records = current_app.config.get('ROUTING_SLIP_DEFAULT_TOTAL') - - routing_slips, total = RoutingSlipModel.search(search_filter, page, limit, return_all, - max_no_records) + routing_slips, total = RoutingSlipModel.search(search_filter, page, limit, return_all) data = { 'total': total, 'page': page, diff --git a/pay-api/tests/unit/models/test_routing_slip.py b/pay-api/tests/unit/models/test_routing_slip.py index 4a7ed3e54..68943e440 100644 --- a/pay-api/tests/unit/models/test_routing_slip.py +++ b/pay-api/tests/unit/models/test_routing_slip.py @@ -57,16 +57,16 @@ def test_routing_slip_find_search(session): routing_slip = RoutingSlip() search_dict = {'routingSlipNumber': rs.number} - res, count = routing_slip.search(search_dict, page=1, limit=1, max_no_records=50, return_all=True) - assert count == 50 + res, count = routing_slip.search(search_dict, page=1, limit=1, return_all=True) + assert count == 1 assert len(res) == 1, 'searched with routing slip.so only one record' - res, count = routing_slip.search({}, page=1, limit=1, max_no_records=50, return_all=True) - assert count == 50 + res, count = routing_slip.search({}, page=1, limit=1, return_all=True) + assert count == 21 assert len(res) == 21, 'retun all true ;so shud return all records' - res, count = routing_slip.search({}, page=1, limit=1, max_no_records=50, return_all=False) - assert count == 21 + res, count = routing_slip.search({}, page=1, limit=1, return_all=False) + assert count == 1 assert len(res) == 1, 'return all false'