Skip to content

Commit 005e490

Browse files
committed
Use paging utility functions
1 parent 8af4620 commit 005e490

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

sonar/hotspots.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from __future__ import annotations
2323

24+
import math
2425
import json
2526
import re
2627
from http import HTTPStatus
@@ -71,7 +72,7 @@
7172
SEVERITIES = ()
7273

7374
# Filters for search of hotspots are different than for issues :-(
74-
_FILTERS_HOTSPOTS_REMAPPING = {"resolutions": "resolution", "statuses": "status", "componentsKey": PROJECT_FILTER_OLD, "components": PROJECT_FILTER}
75+
_FILTERS_HOTSPOTS_REMAPPING = {"resolutions": "resolution", "statuses": "status", "componentsKey": PROJECT_FILTER, "components": PROJECT_FILTER}
7576

7677
_OBJECTS = {}
7778

@@ -384,8 +385,7 @@ def search(endpoint: pf.Platform, filters: types.ApiParams = None) -> dict[str,
384385
:rtype: dict{<key>: <Hotspot>}
385386
"""
386387
hotspots_list = {}
387-
new_params = get_search_filters(endpoint=endpoint, params=filters)
388-
new_params = util.dict_remap(original_dict=new_params, remapping=_FILTERS_HOTSPOTS_REMAPPING)
388+
new_params = sanitize_search_filters(endpoint=endpoint, params=filters)
389389
filters_iterations = split_search_filters(new_params)
390390
ps = 500 if "ps" not in new_params else new_params["ps"]
391391
for inline_filters in filters_iterations:
@@ -395,16 +395,15 @@ def search(endpoint: pf.Platform, filters: types.ApiParams = None) -> dict[str,
395395
while True:
396396
inline_filters["p"] = p
397397
try:
398-
resp = endpoint.get("hotspots/search", params=inline_filters, mute=(HTTPStatus.NOT_FOUND,))
399-
data = json.loads(resp.text)
400-
nbr_hotspots = data["paging"]["total"]
398+
data = json.loads(endpoint.get("hotspots/search", params=inline_filters, mute=(HTTPStatus.NOT_FOUND,)).text)
399+
nbr_hotspots = util.nbr_total_elements(data)
401400
except HTTPError as e:
402401
if e.response.status_code == HTTPStatus.NOT_FOUND:
403402
log.warning("No hotspots found with search params %s", str(inline_filters))
404403
nbr_hotspots = 0
405404
return {}
406405
raise e
407-
nbr_pages = (nbr_hotspots + ps - 1) // ps
406+
nbr_pages = util.nbr_pages(data)
408407
log.debug("Number of hotspots: %d - Page: %d/%d", nbr_hotspots, inline_filters["p"], nbr_pages)
409408
if nbr_hotspots > 10000:
410409
raise TooManyHotspotsError(
@@ -432,7 +431,7 @@ def get_object(endpoint: pf.Platform, key: str, data: dict[str] = None, from_exp
432431
return _OBJECTS[uid]
433432

434433

435-
def get_search_filters(endpoint: pf.Platform, params: types.ApiParams) -> types.ApiParams:
434+
def sanitize_search_filters(endpoint: pf.Platform, params: types.ApiParams) -> types.ApiParams:
436435
"""Returns the filtered list of params that are allowed for api/hotspots/search"""
437436
log.debug("Sanitizing hotspot search criteria %s", str(params))
438437
if params is None:
@@ -446,6 +445,8 @@ def get_search_filters(endpoint: pf.Platform, params: types.ApiParams) -> types.
446445
criterias["status"] = "REVIEWED"
447446
if endpoint.version() <= (10, 2, 0):
448447
criterias = util.dict_remap(original_dict=criterias, remapping={PROJECT_FILTER: PROJECT_FILTER_OLD})
448+
else:
449+
criterias = util.dict_remap(original_dict=criterias, remapping={PROJECT_FILTER_OLD: PROJECT_FILTER})
449450
criterias = util.dict_subset(criterias, SEARCH_CRITERIAS)
450451
log.debug("Sanitized hotspot search criteria %s", str(criterias))
451452
return criterias

0 commit comments

Comments
 (0)