Skip to content

Commit

Permalink
12032 - FAS - Performance - Query optimization (#926)
Browse files Browse the repository at this point in the history
* Fix business_identifier search.

* Fix the pagination and limit for search.

* Linting fixes + minor search tweak.
  • Loading branch information
seeker25 authored May 3, 2022
1 parent 6f5d3c9 commit 245e65a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
25 changes: 13 additions & 12 deletions pay-api/src/pay_api/models/routing_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).\
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
7 changes: 1 addition & 6 deletions pay-api/src/pay_api/services/fas/routing_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions pay-api/tests/unit/models/test_routing_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'


Expand Down

0 comments on commit 245e65a

Please sign in to comment.