Skip to content

Commit 2377606

Browse files
SinaChavoshicopybara-github
authored andcommitted
feat: Add support for ordery_by in Metadata SDK list methods for Artifact, Execution and Context.
PiperOrigin-RevId: 488531299
1 parent a9a438a commit 2377606

File tree

8 files changed

+94
-26
lines changed

8 files changed

+94
-26
lines changed

Diff for: google/cloud/aiplatform/metadata/artifact.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ def _list_resources(
242242
cls,
243243
client: utils.MetadataClientWithOverride,
244244
parent: str,
245-
filter: Optional[str] = None,
245+
filter: Optional[str] = None, # pylint: disable=redefined-builtin
246+
order_by: Optional[str] = None,
246247
):
247248
"""List artifacts in the parent path that matches the filter.
248249
@@ -253,10 +254,21 @@ def _list_resources(
253254
Required. The path where Artifacts are stored.
254255
filter (str):
255256
Optional. filter string to restrict the list result
257+
order_by (str):
258+
Optional. How the list of messages is ordered. Specify the
259+
values to order by and an ordering operation. The default sorting
260+
order is ascending. To specify descending order for a field, users
261+
append a " desc" suffix; for example: "foo desc, bar". Subfields
262+
are specified with a ``.`` character, such as foo.bar. see
263+
https://google.aip.dev/132#ordering for more details.
264+
265+
Returns:
266+
List of artifacts.
256267
"""
257268
list_request = gca_metadata_service.ListArtifactsRequest(
258269
parent=parent,
259270
filter=filter,
271+
order_by=order_by,
260272
)
261273
return client.list_artifacts(request=list_request)
262274

Diff for: google/cloud/aiplatform/metadata/context.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ def _list_resources(
298298
cls,
299299
client: utils.MetadataClientWithOverride,
300300
parent: str,
301-
filter: Optional[str] = None,
301+
filter: Optional[str] = None, # pylint: disable=redefined-builtin
302+
order_by: Optional[str] = None,
302303
):
303304
"""List Contexts in the parent path that matches the filter.
304305
@@ -309,11 +310,22 @@ def _list_resources(
309310
Required. The path where Contexts are stored.
310311
filter (str):
311312
Optional. filter string to restrict the list result
313+
order_by (str):
314+
Optional. How the list of messages is ordered. Specify the
315+
values to order by and an ordering operation. The default sorting
316+
order is ascending. To specify descending order for a field, users
317+
append a " desc" suffix; for example: "foo desc, bar". Subfields
318+
are specified with a ``.`` character, such as foo.bar. see
319+
https://google.aip.dev/132#ordering for more details.
320+
321+
Returns:
322+
List of Contexts.
312323
"""
313324

314325
list_request = gca_metadata_service.ListContextsRequest(
315326
parent=parent,
316327
filter=filter,
328+
order_by=order_by,
317329
)
318330
return client.list_contexts(request=list_request)
319331

Diff for: google/cloud/aiplatform/metadata/execution.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,8 @@ def _list_resources(
458458
cls,
459459
client: utils.MetadataClientWithOverride,
460460
parent: str,
461-
filter: Optional[str] = None,
461+
filter: Optional[str] = None, # pylint: disable=redefined-builtin
462+
order_by: Optional[str] = None,
462463
):
463464
"""List Executions in the parent path that matches the filter.
464465
@@ -469,11 +470,21 @@ def _list_resources(
469470
Required. The path where Executions are stored.
470471
filter (str):
471472
Optional. filter string to restrict the list result
473+
order_by (str):
474+
Optional. How the list of messages is ordered. Specify the
475+
values to order by and an ordering operation. The default sorting
476+
order is ascending. To specify descending order for a field, users
477+
append a " desc" suffix; for example: "foo desc, bar". Subfields
478+
are specified with a ``.`` character, such as foo.bar. see
479+
https://google.aip.dev/132#ordering for more details.
480+
Returns:
481+
List of execution.
472482
"""
473483

474484
list_request = gca_metadata_service.ListExecutionsRequest(
475485
parent=parent,
476486
filter=filter,
487+
order_by=order_by,
477488
)
478489
return client.list_executions(request=list_request)
479490

Diff for: google/cloud/aiplatform/metadata/resource.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,14 @@ def update(
313313
@classmethod
314314
def list(
315315
cls,
316-
filter: Optional[str] = None,
316+
filter: Optional[str] = None, # pylint: disable=redefined-builtin
317317
metadata_store_id: str = "default",
318318
project: Optional[str] = None,
319319
location: Optional[str] = None,
320320
credentials: Optional[auth_credentials.Credentials] = None,
321+
order_by: Optional[str] = None,
321322
) -> List["_Resource"]:
322-
"""List Metadata resources that match the list filter in target metadataStore.
323+
"""List resources that match the list filter in target metadataStore.
323324
324325
Args:
325326
filter (str):
@@ -339,6 +340,14 @@ def list(
339340
credentials (auth_credentials.Credentials):
340341
Custom credentials used to create this resource. Overrides
341342
credentials set in aiplatform.init.
343+
order_by (str):
344+
Optional. How the list of messages is ordered.
345+
Specify the values to order by and an ordering operation. The
346+
default sorting order is ascending. To specify descending order
347+
for a field, users append a " desc" suffix; for example: "foo
348+
desc, bar". Subfields are specified with a ``.`` character, such
349+
as foo.bar. see https://google.aip.dev/132#ordering for more
350+
details.
342351
343352
Returns:
344353
resources (sequence[_Resource]):
@@ -358,6 +367,7 @@ def list(
358367
location=location,
359368
credentials=credentials,
360369
parent=parent,
370+
order_by=order_by,
361371
)
362372

363373
@classmethod

Diff for: samples/model-builder/experiment_tracking/list_artifact_sample.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ def list_artifact_sample(
2222
project: str,
2323
location: str,
2424
display_name_fitler: Optional[str] = "display_name=\"my_model_*\"",
25-
create_date_filter: Optional[str] = "create_time>\"2022-06-11T12:30:00-08:00\"",
25+
create_date_filter: Optional[str] = "create_time>\"2022-06-11\"",
26+
order_by: Optional[str] = None,
2627
):
27-
aiplatform.init(
28-
project=project,
29-
location=location)
28+
aiplatform.init(project=project, location=location)
3029

3130
combined_filters = f"{display_name_fitler} AND {create_date_filter}"
32-
return aiplatform.Artifact.list(filter=combined_filters)
31+
return aiplatform.Artifact.list(
32+
filter=combined_filters,
33+
order_by=order_by,
34+
)
35+
3336

3437
# [END aiplatform_sdk_create_artifact_with_sdk_sample]

Diff for: samples/model-builder/experiment_tracking/list_artifact_sample_test.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ def test_list_artifact_with_sdk_sample(mock_artifact, mock_list_artifact):
2323
location=constants.LOCATION,
2424
display_name_fitler=constants.DISPLAY_NAME,
2525
create_date_filter=constants.CREATE_DATE,
26+
order_by=constants.ORDER_BY,
2627
)
2728

2829
mock_list_artifact.assert_called_with(
29-
filter=f"{constants.DISPLAY_NAME} AND {constants.CREATE_DATE}"
30+
filter=f"{constants.DISPLAY_NAME} AND {constants.CREATE_DATE}",
31+
order_by=constants.ORDER_BY,
3032
)
3133
assert len(artifacts) == 2
3234
# Returning list of 2 context to avoid confusion with get method

Diff for: samples/model-builder/test_constants.py

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
CREATE_DATE = "2022-06-11T12:30:00-08:00"
3232

33+
ORDER_BY = "CREATE_TIME desc"
34+
3335
STAGING_BUCKET = "gs://my-staging-bucket"
3436
EXPERIMENT_NAME = "fraud-detection-trial-72"
3537
CREDENTIALS = credentials.AnonymousCredentials()

Diff for: tests/unit/aiplatform/test_metadata_resources.py

+31-15
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"test-param2": "test-value-1",
6161
"test-param3": False,
6262
}
63+
_TEST_ORDER_BY = "test_order_by"
6364

6465
# context
6566
_TEST_CONTEXT_ID = "test-context-id"
@@ -473,9 +474,11 @@ def test_update_context(self, update_context_mock):
473474
def test_list_contexts(self, list_contexts_mock):
474475
aiplatform.init(project=_TEST_PROJECT)
475476

476-
filter = "test-filter"
477+
filter_query = "test-filter"
477478
context_list = context.Context.list(
478-
filter=filter, metadata_store_id=_TEST_METADATA_STORE
479+
filter=filter_query,
480+
metadata_store_id=_TEST_METADATA_STORE,
481+
order_by=_TEST_ORDER_BY,
479482
)
480483

481484
expected_context = GapicContext(
@@ -490,11 +493,14 @@ def test_list_contexts(self, list_contexts_mock):
490493
list_contexts_mock.assert_called_once_with(
491494
request={
492495
"parent": _TEST_PARENT,
493-
"filter": filter,
496+
"filter": filter_query,
497+
"order_by": _TEST_ORDER_BY,
494498
}
495499
)
496500
assert len(context_list) == 2
501+
# pylint: disable-next=protected-access
497502
assert context_list[0]._gca_resource == expected_context
503+
# pylint: disable-next=protected-access
498504
assert context_list[1]._gca_resource == expected_context
499505

500506
@pytest.mark.usefixtures("get_context_mock")
@@ -710,9 +716,11 @@ def test_update_execution(self, update_execution_mock):
710716
def test_list_executions(self, list_executions_mock):
711717
aiplatform.init(project=_TEST_PROJECT)
712718

713-
filter = "test-filter"
719+
filter_query = "test-filter"
714720
execution_list = execution.Execution.list(
715-
filter=filter, metadata_store_id=_TEST_METADATA_STORE
721+
filter=filter_query,
722+
metadata_store_id=_TEST_METADATA_STORE,
723+
order_by=_TEST_ORDER_BY,
716724
)
717725

718726
expected_execution = GapicExecution(
@@ -725,13 +733,16 @@ def test_list_executions(self, list_executions_mock):
725733
)
726734

727735
list_executions_mock.assert_called_once_with(
728-
request=dict(
729-
parent=_TEST_PARENT,
730-
filter=filter,
731-
)
736+
request={
737+
"parent": _TEST_PARENT,
738+
"filter": filter_query,
739+
"order_by": _TEST_ORDER_BY,
740+
}
732741
)
733742
assert len(execution_list) == 2
743+
# pylint: disable-next=protected-access
734744
assert execution_list[0]._gca_resource == expected_execution
745+
# pylint: disable-next=protected-access
735746
assert execution_list[1]._gca_resource == expected_execution
736747

737748
@pytest.mark.usefixtures("get_execution_mock", "get_artifact_mock")
@@ -1002,9 +1013,11 @@ def test_update_artifact(self, update_artifact_mock):
10021013
def test_list_artifacts(self, list_artifacts_mock):
10031014
aiplatform.init(project=_TEST_PROJECT)
10041015

1005-
filter = "test-filter"
1016+
filter_query = "test-filter"
10061017
artifact_list = artifact.Artifact.list(
1007-
filter=filter, metadata_store_id=_TEST_METADATA_STORE
1018+
filter=filter_query,
1019+
metadata_store_id=_TEST_METADATA_STORE,
1020+
order_by=_TEST_ORDER_BY,
10081021
)
10091022

10101023
expected_artifact = GapicArtifact(
@@ -1017,11 +1030,14 @@ def test_list_artifacts(self, list_artifacts_mock):
10171030
)
10181031

10191032
list_artifacts_mock.assert_called_once_with(
1020-
request=dict(
1021-
parent=_TEST_PARENT,
1022-
filter=filter,
1023-
)
1033+
request={
1034+
"parent": _TEST_PARENT,
1035+
"filter": filter_query,
1036+
"order_by": _TEST_ORDER_BY,
1037+
}
10241038
)
10251039
assert len(artifact_list) == 2
1040+
# pylint: disable-next=protected-access
10261041
assert artifact_list[0]._gca_resource == expected_artifact
1042+
# pylint: disable-next=protected-access
10271043
assert artifact_list[1]._gca_resource == expected_artifact

0 commit comments

Comments
 (0)