Skip to content

Commit 47e23ec

Browse files
committed
[tracer][api] Improve list functions filtering
* search report types by name * order reports by name and time * filter reports by type * filter plots groups by methodology
1 parent 145da10 commit 47e23ec

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

src/picterra/tracer_client.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def list_plots_groups(
167167
search: Optional[str] = None,
168168
page_number: Optional[int] = None,
169169
include_archived: bool = False,
170+
methodology: Optional[str] = None
170171
) -> ResultsPage:
171172
"""
172173
List all the plots group the user can access, see `ResultsPage`
@@ -179,6 +180,7 @@ def list_plots_groups(
179180
search: The term used to filter by name
180181
page_number: Optional page (from 1) of the list we want to retrieve
181182
include_archived: If true, includes archived plot groups in the results
183+
methodology: If not None, filters the groups by the methodology (eg "Coffee - EUDR")
182184
183185
Returns:
184186
See https://app.picterra.ch/public/apidocs/plots_analysis/v1/#tag/plots-groups/operation/getPlotsGroupsList
@@ -190,6 +192,8 @@ def list_plots_groups(
190192
data["page_number"] = int(page_number)
191193
if include_archived:
192194
data["include_archived"] = include_archived
195+
if methodology is not None:
196+
data["methodology"] = methodology
193197
return self._return_results_page("plots_groups", data)
194198

195199
def analyze_plots_precheck(
@@ -304,6 +308,8 @@ def list_plots_analysis_reports(
304308
plots_group_id: Optional[str] = None,
305309
page_number: Optional[int] = None,
306310
include_archived: bool = False,
311+
search: Optional[str] = None,
312+
report_type: Optional[str] = None,
307313
) -> ResultsPage:
308314
"""
309315
List all the reports belonging to a given plots analysis, see `ResultsPage`
@@ -314,6 +320,9 @@ def list_plots_analysis_reports(
314320
page_number: Optional page (from 1) of the list we want to retrieve
315321
include_archived: Defaults to false. If true, includes archived analysis reports in the
316322
results
323+
search: Optional term to search report types by name
324+
report_type: Optional type of report to restrict the list by, use list_plots_analysis_report_types
325+
to know which the available report types are
317326
318327
Deprecated arguments:
319328
plots_group_id: ignored, do not provide it
@@ -329,6 +338,10 @@ def list_plots_analysis_reports(
329338
params["page_number"] = int(page_number)
330339
if include_archived:
331340
params["include_archived"] = include_archived
341+
if search is not None:
342+
params["search"] = search.strip()
343+
if report_type is not None:
344+
params["report_type"] = report_type
332345
return self._return_results_page(
333346
f"plots_analyses/{plots_analysis_id}/reports/", params
334347
)
@@ -337,12 +350,14 @@ def list_plots_analysis_report_types(
337350
self,
338351
plots_analysis_id: str,
339352
plots_group_id: Optional[str] = None,
353+
search: Optional[str] = None,
340354
) -> List[Dict[str, Any]]:
341355
"""
342356
List all the plots analyses report types the user can use (see create_plots_analysis_report)
343357
344358
Args:
345359
plots_analysis_id: id of the plots analysis
360+
search: optional term to search report types by name, if any
346361
347362
Deprecated arguments:
348363
plots_group_id: ignored, do not provide it
@@ -353,8 +368,12 @@ def list_plots_analysis_report_types(
353368
if plots_group_id is not None:
354369
warnings.warn("Passing plots_group_id is not needed anymore, remove it", DeprecationWarning)
355370

371+
params: Dict[str, Any] = {}
372+
if search is not None:
373+
params["search"] = search.strip()
356374
resp = self.sess.get(
357-
self._full_url(f"plots_analyses/{plots_analysis_id}/reports/types/")
375+
self._full_url(f"plots_analyses/{plots_analysis_id}/reports/types/"),
376+
params=params
358377
)
359378
_check_resp_is_ok(resp, "Couldn't list report types")
360379
return resp.json()

tests/test_tracer_client.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ def test_list_plots_groups(monkeypatch):
244244
add_mock_paginated_list_response(url, 2, "m_2", "spam")
245245
plots_groups = client.list_plots_groups(search="m_2", page_number=2)
246246
assert plots_groups[0]["name"] == "spam_1"
247+
# Filter list
248+
add_mock_paginated_list_response(url, qs={"methodology": "cocoa"})
249+
plots_groups = client.list_plots_groups(methodology="cocoa")
250+
assert plots_groups[0]["name"] == "a_1"
247251

248252

249253
@responses.activate
@@ -301,13 +305,17 @@ def test_list_plots_analysis_reports(monkeypatch):
301305
reports = client.list_plots_analysis_reports("my-analysis-id")
302306
assert len(reports) == 3
303307
assert reports[0]["name"] == "a_1" and reports[-1]["name"] == "a_3"
308+
# test search and filter
309+
add_mock_paginated_list_response(url, qs={"report_type": "type_1", "search": "spam"})
310+
reports = client.list_plots_analysis_reports("my-analysis-id", search="spam", report_type="type_1")
311+
assert len(reports) == 2
304312

305313

306314
@responses.activate
307315
def test_list_plots_analysis_report_types(monkeypatch):
308316
client: TracerClient = _client(monkeypatch, platform="plots_analysis")
309317
url = plots_analysis_api_url("plots_analyses/my-analysis-id/reports/types/")
310-
responses.get(
318+
_add_api_response(
311319
url,
312320
json=[
313321
{"report_type": "type_1", "name": "a_1"},
@@ -319,6 +327,16 @@ def test_list_plots_analysis_report_types(monkeypatch):
319327
reports = client.list_plots_analysis_report_types("my-analysis-id")
320328
assert len(reports) == 4
321329
assert reports[0]["report_type"] == "type_1" and reports[-1]["name"] == "a_4"
330+
_add_api_response(
331+
url,
332+
json=[
333+
{"report_type": "type_1", "name": "a_1"},
334+
{"report_type": "type_2", "name": "a_2"},
335+
],
336+
match=responses.matchers.query_param_matcher({"search": "spam"})
337+
)
338+
reports = client.list_plots_analysis_report_types("my-analysis-id", search="spam")
339+
assert len(reports) == 2
322340

323341

324342
@responses.activate

tests/utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ def plots_analysis_api_url(path):
5353
return urljoin(TEST_API_URL, urljoin("public/api/plots_analysis/v1/", path))
5454

5555

56-
def add_mock_paginated_list_response(endpoint: str, page: int = 1, search_string: str = None, name_prefix: str = "a", num_results: int = 2, include_archived: Optional[bool] = None):
56+
def add_mock_paginated_list_response(
57+
endpoint: str,
58+
page: int = 1,
59+
search_string: str = None,
60+
name_prefix: str = "a",
61+
num_results: int = 2,
62+
include_archived: Optional[bool] = None,
63+
qs: Optional[dict] = None
64+
):
5765
curr, next = str(page), str(page + 1)
5866
data1 = {
5967
"count": num_results * num_results,
@@ -67,11 +75,12 @@ def add_mock_paginated_list_response(endpoint: str, page: int = 1, search_string
6775
qs_params = {"page_number": curr}
6876
if search_string:
6977
qs_params["search"] = search_string
78+
if qs is not None:
79+
qs_params.update(qs)
7080
if include_archived is not None:
7181
qs_params["include_archived"] = str(include_archived).lower()
7282
_add_api_response(
73-
endpoint + "?page_number=" + curr + (("&search=" + search_string) if search_string else "") + (
74-
"&include_archived=" + str(include_archived).lower() if include_archived is not None else ""),
83+
endpoint,
7584
match=responses.matchers.query_param_matcher(qs_params),
7685
json=data1,
7786
)

0 commit comments

Comments
 (0)