Skip to content

Commit 4862607

Browse files
authored
Support running analysis from agent via SDK (#126)
* support running analysis from agent * lint
1 parent 997ffd7 commit 4862607

File tree

6 files changed

+1009
-790
lines changed

6 files changed

+1009
-790
lines changed

cirro/cirro_client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from cirro.auth.base import AuthInfo
55
from cirro.config import AppConfig
66
from cirro.services import FileService, DatasetService, ProjectService, ProcessService, ExecutionService, \
7-
MetricsService, MetadataService, BillingService, ReferenceService, UserService
7+
MetricsService, MetadataService, BillingService, ReferenceService, UserService, ComputeEnvironmentService
88

99

1010
class CirroApi:
@@ -51,6 +51,7 @@ def __init__(self, auth_info: AuthInfo = None, base_url: str = None):
5151
self._project_service = ProjectService(self._api_client)
5252
self._process_service = ProcessService(self._api_client)
5353
self._execution_service = ExecutionService(self._api_client)
54+
self._compute_environment_service = ComputeEnvironmentService(self._api_client)
5455
self._metrics_service = MetricsService(self._api_client)
5556
self._metadata_service = MetadataService(self._api_client)
5657
self._billing_service = BillingService(self._api_client)
@@ -85,6 +86,13 @@ def execution(self) -> ExecutionService:
8586
"""
8687
return self._execution_service
8788

89+
@property
90+
def compute_environments(self) -> ComputeEnvironmentService:
91+
"""
92+
List and update compute environments
93+
"""
94+
return self._compute_environment_service
95+
8896
@property
8997
def metrics(self) -> MetricsService:
9098
"""

cirro/sdk/dataset.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ def run_analysis(
173173
description: str = "",
174174
process: Union[DataPortalProcess, str] = None,
175175
params=None,
176-
notifications_emails=None
176+
notifications_emails=None,
177+
compute_environment=None
177178
) -> str:
178179
"""
179180
Runs an analysis on a dataset, returns the ID of the newly created dataset.
@@ -187,6 +188,8 @@ def run_analysis(
187188
process (DataPortalProcess or str): Process to run
188189
params (dict): Analysis parameters
189190
notifications_emails (List[str]): Notification email address(es)
191+
compute_environment (str): Name or ID of compute environment to use,
192+
if blank it will run in AWS
190193
191194
Returns:
192195
dataset_id (str): ID of newly created dataset
@@ -203,6 +206,18 @@ def run_analysis(
203206
# If the process is a string, try to parse it as a process name or ID
204207
process = parse_process_name_or_id(process, self._client)
205208

209+
if compute_environment:
210+
compute_environments = self._client.compute_environments.list_environments_for_project(
211+
project_id=self.project_id
212+
)
213+
compute_environment = next(
214+
(env for env in compute_environments
215+
if env.name == compute_environment or env.id == compute_environment),
216+
None
217+
)
218+
if compute_environment is None:
219+
raise DataPortalInputError(f"Compute environment '{compute_environment}' not found")
220+
206221
resp = self._client.execution.run_analysis(
207222
project_id=self.project_id,
208223
request=RunAnalysisRequest(
@@ -211,7 +226,8 @@ def run_analysis(
211226
process_id=process.id,
212227
source_dataset_ids=[self.id],
213228
params=RunAnalysisRequestParams.from_dict(params),
214-
notification_emails=notifications_emails
229+
notification_emails=notifications_emails,
230+
compute_environment_id=compute_environment.id if compute_environment else None
215231
)
216232
)
217233
return resp.id

cirro/services/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .billing import BillingService
2+
from .compute_environment import ComputeEnvironmentService
23
from .dataset import DatasetService
34
from .execution import ExecutionService
45
from .file import FileService
@@ -13,6 +14,7 @@
1314
'BillingService',
1415
'DatasetService',
1516
'ExecutionService',
17+
'ComputeEnvironmentService',
1618
'FileService',
1719
'MetadataService',
1820
'MetricsService',
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List
2+
3+
from cirro_api_client.v1.api.compute_environment import get_compute_environments
4+
from cirro_api_client.v1.models import ComputeEnvironmentConfiguration
5+
6+
from cirro.services.base import BaseService
7+
8+
9+
class ComputeEnvironmentService(BaseService):
10+
"""
11+
Service for interacting with the Compute Environment endpoints
12+
"""
13+
14+
def list_environments_for_project(self, project_id: str) -> List[ComputeEnvironmentConfiguration]:
15+
"""
16+
List of custom compute environments for a project (i.e., an agent)
17+
18+
Args:
19+
project_id (str): Project ID
20+
21+
Returns:
22+
List of compute environments that are available for the project
23+
"""
24+
return get_compute_environments.sync(project_id=project_id,
25+
client=self._api_client)

0 commit comments

Comments
 (0)