2121
2222from __future__ import annotations
2323
24+ import math
2425import json
2526import re
2627from http import HTTPStatus
7172SEVERITIES = ()
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