Skip to content

Commit 65a819d

Browse files
committed
Cleanup paginated search
1 parent ac6ffd6 commit 65a819d

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

sonar/portfolios.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
_CREATE_API = "views/create"
5151
_GET_API = "views/show"
5252

53-
MAX_PAGE_SIZE = 500
5453
_PORTFOLIO_QUALIFIER = "VW"
5554
_SUBPORTFOLIO_QUALIFIER = "SVW"
5655

@@ -89,6 +88,8 @@ class Portfolio(aggregations.Aggregation):
8988
SEARCH_API = "views/search"
9089
SEARCH_KEY_FIELD = "key"
9190
SEARCH_RETURN_FIELD = "components"
91+
MAX_PAGE_SIZE = 500
92+
MAX_SEARCH = 10000
9293

9394
_OBJECTS = {}
9495

@@ -302,7 +303,7 @@ def get_components(self) -> types.ApiPayload:
302303
"component": self.key,
303304
"metricKeys": "ncloc",
304305
"strategy": "children",
305-
"ps": 500,
306+
"ps": Portfolio.MAX_PAGE_SIZE,
306307
},
307308
).text
308309
)
@@ -571,7 +572,7 @@ def get_project_list(self) -> list[str]:
571572
log.debug("Search %s projects list", str(self))
572573
proj_key_list = []
573574
page = 0
574-
params = {"component": self.key, "ps": 500, "qualifiers": "TRK", "strategy": "leaves", "metricKeys": "ncloc"}
575+
params = {"component": self.key, "ps": Portfolio.MAX_PAGE_SIZE, "qualifiers": "TRK", "strategy": "leaves", "metricKeys": "ncloc"}
575576
while True:
576577
page += 1
577578
params["p"] = page
@@ -580,13 +581,16 @@ def get_project_list(self) -> list[str]:
580581
nbr_projects = util.nbr_total_elements(data)
581582
proj_key_list += [c["refKey"] for c in data["components"]]
582583
except HTTPError as e:
583-
log.critical("HTTP Error %s while collecting projects from %s, proceeding anyway", str(e), str(self))
584-
continue
584+
if e.response.status_code in (HTTPStatus.BAD_REQUEST, HTTPStatus.NOT_FOUND):
585+
log.warning("HTTP Error %s while collecting projects from %s, stopping collection", str(e), str(self))
586+
else:
587+
log.critical("HTTP Error %s while collecting projects from %s, proceeding anyway", str(e), str(self))
588+
break
585589
nbr_pages = util.nbr_pages(data)
586590
log.debug("Number of projects: %d - Page: %d/%d", nbr_projects, page, nbr_pages)
587-
if nbr_projects > 10000:
588-
log.critical("Can't collect more than 10000 projects from %s", str(self))
589-
if page >= nbr_pages:
591+
if nbr_projects > Portfolio.MAX_SEARCH:
592+
log.warning("Can't collect more than %d projects from %s", Portfolio.MAX_SEARCH, str(self))
593+
if page >= nbr_pages or page >= Portfolio.MAX_SEARCH / Portfolio.MAX_PAGE_SIZE:
590594
break
591595
log.debug("%s projects list = %s", str(self), str(proj_key_list))
592596
return proj_key_list

0 commit comments

Comments
 (0)