Skip to content

Commit 7ea7191

Browse files
authored
Merge pull request #424 from specklesystems/gergo/objectResultsWithApplicationIds
feat(automate): attach application id-s to automate result cases
2 parents 3417557 + a47f568 commit 7ea7191

File tree

2 files changed

+85
-36
lines changed

2 files changed

+85
-36
lines changed

src/speckle_automate/automation_context.py

Lines changed: 84 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# ignoring "line too long" check from linter
2-
# ruff: noqa: E501
31
"""This module provides an abstraction layer above the Speckle Automate runtime."""
42

53
import time
@@ -75,7 +73,7 @@ def initialize(
7573
speckle_client.authenticate_with_token(speckle_token)
7674
if not speckle_client.account:
7775
msg = (
78-
f"Could not autenticate to {automation_run_data.speckle_server_url}",
76+
f"Could not authenticate to {automation_run_data.speckle_server_url}",
7977
"with the provided token",
8078
)
8179
raise ValueError(msg)
@@ -109,18 +107,24 @@ def receive_version(self) -> Base:
109107
)
110108
except SpeckleException as err:
111109
raise ValueError(
112-
f"""\
113-
Could not receive specified version.
114-
Is your environment configured correctly?
115-
project_id: {self.automation_run_data.project_id}
116-
model_id: {self.automation_run_data.triggers[0].payload.model_id}
117-
version_id: {self.automation_run_data.triggers[0].payload.version_id}
118-
"""
110+
f"""Could not receive specified version.
111+
Is your environment configured correctly?
112+
project_id: {self.automation_run_data.project_id}
113+
model_id: {self.automation_run_data.triggers[0].payload.model_id}
114+
version_id: {self.automation_run_data.triggers[0].payload.version_id}
115+
"""
119116
) from err
120117

118+
if not version.referenced_object:
119+
raise Exception(
120+
"This version is past the version history limit,",
121+
" cannot execute an automation on it",
122+
)
123+
121124
base = operations.receive(
122125
version.referenced_object, self._server_transport, self._memory_transport
123126
)
127+
# self._closure_tree = base["__closure"]
124128
print(
125129
f"It took {self.elapsed():.2f} seconds to receive",
126130
f" the speckle version {version_id}",
@@ -242,7 +246,7 @@ def report_run_status(self) -> None:
242246
)
243247
if self.run_status in [AutomationStatus.SUCCEEDED, AutomationStatus.FAILED]:
244248
object_results = {
245-
"version": 1,
249+
"version": 2,
246250
"values": {
247251
"objectResults": self._automation_result.model_dump(by_alias=True)[
248252
"objectResults"
@@ -332,26 +336,24 @@ def _mark_run(
332336
def attach_error_to_objects(
333337
self,
334338
category: str,
335-
object_ids: Union[str, List[str]],
339+
affected_objects: Union[Base, List[Base]],
336340
message: Optional[str] = None,
337341
metadata: Optional[Dict[str, Any]] = None,
338342
visual_overrides: Optional[Dict[str, Any]] = None,
339343
) -> None:
340344
"""Add a new error case to the run results.
341-
342-
If the error cause has already created an error case,
343-
the error will be extended with a new case refering to the causing objects.
344345
Args:
345-
error_tag (str): A short tag for the error type.
346-
causing_object_ids (str[]): A list of object_id-s that are causing the error
347-
error_messagge (Optional[str]): Optional error message.
346+
category (str): A short tag for the event type.
347+
affected_objects (Union[Base, List[Base]]): A single object or a list of
348+
objects that are causing the error case.
349+
message (Optional[str]): Optional message.
348350
metadata: User provided metadata key value pairs
349351
visual_overrides: Case specific 3D visual overrides.
350352
"""
351353
self.attach_result_to_objects(
352354
ObjectResultLevel.ERROR,
353355
category,
354-
object_ids,
356+
affected_objects,
355357
message,
356358
metadata,
357359
visual_overrides,
@@ -360,16 +362,25 @@ def attach_error_to_objects(
360362
def attach_warning_to_objects(
361363
self,
362364
category: str,
363-
object_ids: Union[str, List[str]],
365+
affected_objects: Union[Base, List[Base]],
364366
message: Optional[str] = None,
365367
metadata: Optional[Dict[str, Any]] = None,
366368
visual_overrides: Optional[Dict[str, Any]] = None,
367369
) -> None:
368-
"""Add a new warning case to the run results."""
370+
"""Add a new warning case to the run results.
371+
372+
Args:
373+
category (str): A short tag for the event type.
374+
affected_objects (Union[Base, List[Base]]): A single object or a list of
375+
objects that are causing the warning case.
376+
message (Optional[str]): Optional message.
377+
metadata: User provided metadata key value pairs
378+
visual_overrides: Case specific 3D visual overrides.
379+
"""
369380
self.attach_result_to_objects(
370381
ObjectResultLevel.WARNING,
371382
category,
372-
object_ids,
383+
affected_objects,
373384
message,
374385
metadata,
375386
visual_overrides,
@@ -378,16 +389,25 @@ def attach_warning_to_objects(
378389
def attach_success_to_objects(
379390
self,
380391
category: str,
381-
object_ids: Union[str, List[str]],
392+
affected_objects: Union[Base, List[Base]],
382393
message: Optional[str] = None,
383394
metadata: Optional[Dict[str, Any]] = None,
384395
visual_overrides: Optional[Dict[str, Any]] = None,
385396
) -> None:
386-
"""Add a new success case to the run results."""
397+
"""Add a new success case to the run results.
398+
399+
Args:
400+
category (str): A short tag for the event type.
401+
affected_objects (Union[Base, List[Base]]): A single object or a list of
402+
objects that are causing the success case.
403+
message (Optional[str]): Optional message.
404+
metadata: User provided metadata key value pairs
405+
visual_overrides: Case specific 3D visual overrides.
406+
"""
387407
self.attach_result_to_objects(
388408
ObjectResultLevel.SUCCESS,
389409
category,
390-
object_ids,
410+
affected_objects,
391411
message,
392412
metadata,
393413
visual_overrides,
@@ -396,16 +416,25 @@ def attach_success_to_objects(
396416
def attach_info_to_objects(
397417
self,
398418
category: str,
399-
object_ids: Union[str, List[str]],
419+
affected_objects: Union[Base, List[Base]],
400420
message: Optional[str] = None,
401421
metadata: Optional[Dict[str, Any]] = None,
402422
visual_overrides: Optional[Dict[str, Any]] = None,
403423
) -> None:
404-
"""Add a new info case to the run results."""
424+
"""Add a new info case to the run results.
425+
426+
Args:
427+
category (str): A short tag for the event type.
428+
affected_objects (Union[Base, List[Base]]): A single object or a list of
429+
objects that are causing the info case.
430+
message (Optional[str]): Optional message.
431+
metadata: User provided metadata key value pairs
432+
visual_overrides: Case specific 3D visual overrides.
433+
"""
405434
self.attach_result_to_objects(
406435
ObjectResultLevel.INFO,
407436
category,
408-
object_ids,
437+
affected_objects,
409438
message,
410439
metadata,
411440
visual_overrides,
@@ -415,19 +444,39 @@ def attach_result_to_objects(
415444
self,
416445
level: ObjectResultLevel,
417446
category: str,
418-
object_ids: Union[str, List[str]],
447+
affected_objects: Union[Base, List[Base]],
419448
message: Optional[str] = None,
420449
metadata: Optional[Dict[str, Any]] = None,
421450
visual_overrides: Optional[Dict[str, Any]] = None,
422451
) -> None:
423-
if isinstance(object_ids, list):
424-
if len(object_ids) < 1:
452+
"""Add a new result case to the run results.
453+
454+
Args:
455+
level: Result level.
456+
category (str): A short tag for the event type.
457+
affected_objects (Union[Base, List[Base]]): A single object or a list of
458+
objects that are causing the info case.
459+
message (Optional[str]): Optional message.
460+
metadata: User provided metadata key value pairs
461+
visual_overrides: Case specific 3D visual overrides.
462+
"""
463+
if isinstance(affected_objects, list):
464+
if len(affected_objects) < 1:
425465
raise ValueError(
426-
f"Need atleast one object_id to report a(n) {level.value.upper()}"
466+
f"Need atleast one object to report a(n) {level.value.upper()}"
427467
)
428-
id_list = object_ids
468+
object_list = affected_objects
429469
else:
430-
id_list = [object_ids]
470+
object_list = [affected_objects]
471+
472+
ids: Dict[str, Optional[str]] = {}
473+
for o in object_list:
474+
# validate that the Base.id is not None. If its a None, throw an Exception
475+
if not o.id:
476+
raise Exception(
477+
f"You can only attach {level} results to objects with an id."
478+
)
479+
ids[o.id] = o.applicationId
431480
print(
432481
f"Created new {level.value.upper()}"
433482
f" category: {category} caused by: {message}"
@@ -436,7 +485,7 @@ def attach_result_to_objects(
436485
ResultCase(
437486
category=category,
438487
level=level,
439-
object_ids=id_list,
488+
object_app_ids=ids,
440489
message=message,
441490
metadata=metadata,
442491
visual_overrides=visual_overrides,

src/speckle_automate/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class ResultCase(AutomateBase):
8080

8181
category: str
8282
level: ObjectResultLevel
83-
object_ids: List[str]
83+
object_app_ids: Dict[str, Optional[str]]
8484
message: Optional[str]
8585
metadata: Optional[Dict[str, Any]]
8686
visual_overrides: Optional[Dict[str, Any]]

0 commit comments

Comments
 (0)