Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search task added #177

Merged
merged 6 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions opensearch_py_ml/ml_commons/ml_commons_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# GitHub history for details.


import json
import time
from typing import Any, List, Union

Expand Down Expand Up @@ -376,6 +377,34 @@ def _get_task_info(self, task_id: str):
url=API_URL,
)

def search_task(self, json_input) -> object:
"""
This method searches a task from opensearch cluster (using ml commons api)
:param json: json input for the search request
:type json: string or dict
:return: returns a json object, with detailed information about the searched task
:rtype: object
"""

API_URL = f"{ML_BASE_URI}/tasks/_search"

if isinstance(json_input, str):
try:
json_obj = json.loads(json_input)
API_BODY = json.dumps(json_obj)
except json.JSONDecodeError:
return "Invalid JSON string passed as argument."
elif isinstance(json_input, dict):
API_BODY = json.dumps(json_input)
else:
return "Invalid JSON object passed as argument."
Copy link
Collaborator

@dhrubo-os dhrubo-os Jun 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we cover this line with testing? May be we can write separate test function just to cover this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean "else" block? I covered both dict and string test.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"return "Invalid JSON object passed as argument." this block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see maybe you mean try catch block missed for dict case. I will add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"return "Invalid JSON object passed as argument." this block

Ok


return self._client.transport.perform_request(
method="POST",
url=API_URL,
body=API_BODY,
)

def get_model_info(self, model_id: str) -> object:
"""
This method return information about a model registered in the opensearch cluster (using ml commons api)
Expand Down
20 changes: 20 additions & 0 deletions tests/ml_commons/test_ml_commons_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,26 @@ def test_integration_model_train_register_full_cycle():
print("Model Task Status:", ml_task_status)
raised = True
assert raised == False, "Raised Exception in pulling task info"

try:
search_task_obj = ml_client.search_task(json="{'_id':'{task_id}'}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw parameter name was: json_input. Then how can we invoke with json?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my bad sorry. I will change it.

assert search_task_obj["hits"]["hits"][0]["_id"] == task_id
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in searching task"

try:
search_task_obj = ml_client.search_task(json={"_id": task_id})
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in searching task"

try:
search_task_obj = ml_client.search_task(json=15)
assert search_task_obj == "Invalid JSON object passed as argument."
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in searching task"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make a separate function for this to test? This chain of flow test was to test integration test. We don't need to include this test here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make a separate function for this to test? This chain of flow test was to test integration test. We don't need to include this test here.

Ok.

# This is test is being flaky. Sometimes the test is passing and sometimes showing 500 error
# due to memory circuit breaker.
# Todo: We need to revisit this test.
Expand Down