Skip to content

Commit

Permalink
Expose analyze endpoint (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
FedericoNegri committed Apr 23, 2024
1 parent 0dcbc15 commit 5bb88c8
Show file tree
Hide file tree
Showing 7 changed files with 542 additions and 274 deletions.
6 changes: 5 additions & 1 deletion doc/source/contribute.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ data model generator with this command:

.. code:: bash
datamodel-codegen --input .\rms_openapi.json --input-file-type openapi --output src/ansys/hps/client/rms/models.py --output-model-type pydantic_v2.BaseModel --base-class ansys.hps.client.common.DictModel
datamodel-codegen --input .\rms_openapi.json --input-file-type openapi \
--output src/ansys/hps/client/rms/models.py \
--output-model-type pydantic_v2.BaseModel \
--base-class ansys.hps.client.common.DictModel \
--custom-file-header-path rms_models.header
Post issues
-----------
Expand Down
3 changes: 3 additions & 0 deletions rms_models.header
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# DO NOT EDIT.
# This file is automatically generated from the RMS OpenApi Specification
# using datamodel-code-generator.
35 changes: 34 additions & 1 deletion src/ansys/hps/client/jms/api/project_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

from ansys.hps.client.client import Client
from ansys.hps.client.common import Object
from ansys.hps.client.exceptions import HPSError
from ansys.hps.client.exceptions import ClientError, HPSError
from ansys.hps.client.jms.resource import (
Algorithm,
File,
Expand All @@ -46,6 +46,8 @@
Task,
TaskDefinition,
)
from ansys.hps.client.rms.api import RmsApi
from ansys.hps.client.rms.models import AnalyzeRequirements, AnalyzeResponse

from .base import create_objects, delete_objects, get_objects, update_objects
from .jms_api import JmsApi, _copy_objects
Expand Down Expand Up @@ -278,6 +280,37 @@ def copy_task_definitions(
"""
return _copy_objects(self.client, self.url, task_definitions, wait=wait)

def analyze_task_definition(
self,
task_definition_id: str,
evaluator_ids: list[str] = None,
scaler_ids: list[str] = None,
analytics: bool = True,
as_object: bool = True,
) -> AnalyzeResponse:
"""Compare resource requirements against available compute resources."""

# Task definition is retrieved as a native dictionary to more easily translate
# the subobjects into RMS models
tds = self.get_task_definitions(id=task_definition_id, fields="all", as_objects=False)
if not tds:
raise ClientError(f"Could not retrieve task definition {task_definition_id}")
td = tds[0]

project_permissions = self.get_permissions(as_objects=False)

requirements = AnalyzeRequirements(
project_id=self.project_id,
software_requirements=td["software_requirements"],
resource_requirements=td["resource_requirements"],
evaluator_ids=evaluator_ids,
scaler_ids=scaler_ids,
project_permissions=project_permissions,
)

rms_api = RmsApi(self.client)
return rms_api.analyze(requirements=requirements, analytics=analytics, as_object=as_object)

################################################################
# Job definitions
def get_job_definitions(self, as_objects=True, **query_params) -> List[JobDefinition]:
Expand Down
24 changes: 16 additions & 8 deletions src/ansys/hps/client/rms/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ def _create_dynamic_list_model(name, field_name, field_type) -> BaseModel:
return create_model(name, **fields)


def object_to_json(
object: BaseModel,
exclude_unset: bool = True,
exclude_defaults: bool = False,
):
if pydantic_version.startswith("1."):
return object.json(exclude_unset=exclude_unset, exclude_defaults=exclude_defaults)
elif pydantic_version.startswith("2."):
return object.model_dump_json(
exclude_unset=exclude_unset, exclude_defaults=exclude_defaults
)
else:
raise RuntimeError(f"Unsupported Pydantic version {pydantic_version}")


def objects_to_json(
objects: List[BaseModel],
rest_name: str,
Expand All @@ -72,14 +87,7 @@ def objects_to_json(
args = {f"{rest_name}": objects}
objects_list = ListOfObjects(**args)

if pydantic_version.startswith("1."):
return objects_list.json(exclude_unset=exclude_unset, exclude_defaults=exclude_defaults)
elif pydantic_version.startswith("2."):
return objects_list.model_dump_json(
exclude_unset=exclude_unset, exclude_defaults=exclude_defaults
)
else:
raise RuntimeError(f"Unsupported Pydantic version {pydantic_version}")
return object_to_json(objects_list, exclude_unset, exclude_defaults)


def json_to_objects(data, obj_type):
Expand Down
27 changes: 26 additions & 1 deletion src/ansys/hps/client/rms/api/rms_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
from typing import List

from ansys.hps.client.client import Client
from ansys.hps.client.exceptions import ClientError
from ansys.hps.client.rms.models import (
AnalyzeRequirements,
AnalyzeResponse,
ClusterInfo,
ComputeResourceSet,
EvaluatorConfiguration,
Expand All @@ -33,7 +36,7 @@
ScalerRegistration,
)

from .base import create_objects, get_object, get_objects, get_objects_count
from .base import create_objects, get_object, get_objects, get_objects_count, object_to_json

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -174,3 +177,25 @@ def get_cluster_info(self, compute_resource_set_id, as_object=True) -> ClusterIn
ClusterInfo,
as_object=as_object,
)

################################################################
# Analyze

def analyze(
self, requirements: AnalyzeRequirements, analytics: bool = False, as_object: bool = True
) -> AnalyzeResponse:
"""Compare resource requirements against available compute resources."""
if requirements is None:
raise ClientError(f"Requirements can't be None.")

r = self.client.session.post(
f"{self.url}/analyze",
data=object_to_json(requirements),
params={"analytics": analytics},
)

data = r.json()
if not as_object:
return data

return AnalyzeResponse(**data)
Loading

0 comments on commit 5bb88c8

Please sign in to comment.