Skip to content

Commit

Permalink
fix docs and add error handling for instances/inspect (#2058)
Browse files Browse the repository at this point in the history
Co-authored-by: Håkon V. Treider <[email protected]>
  • Loading branch information
andreavs and haakonvt authored Dec 5, 2024
1 parent fa7f991 commit bc8ae8a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
38 changes: 22 additions & 16 deletions cognite/client/_api/data_modeling/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,22 +718,31 @@ def inspect(
Args:
nodes (NodeId | Sequence[NodeId] | tuple[str, str] | Sequence[tuple[str, str]] | None): Node IDs.
edges (EdgeId | Sequence[EdgeId] | tuple[str, str] | Sequence[tuple[str, str]] | None): Edge IDs.
involved_views (InvolvedViews | None): Whether to include involved views.
involved_containers (InvolvedContainers | None): Whether to include involved containers.
involved_views (InvolvedViews | None): Whether to include involved views. Must pass at least one of involved_views or involved_containers.
involved_containers (InvolvedContainers | None): Whether to include involved containers. Must pass at least one of involved_views or involved_containers.
Returns:
InstanceInspectResults: List of instance inspection results.
Examples:
Look up the involved views and containers for a given node and edge:
Look up the involved views for a given node and edge:
>>> from cognite.client import CogniteClient
>>> from cognite.client.data_classes.data_modeling import NodeId, EdgeId
>>> from cognite.client.data_classes.data_modeling import NodeId, EdgeId, InvolvedViews
>>> client = CogniteClient()
>>> res = client.data_modeling.instances.inspect(
... nodes=NodeId("my-space", "foo1"),
... edges=EdgeId("my-space", "bar2"),
... involved_views=InvolvedViews(all_versions=False),
... )
Look up the involved containers:
>>> from cognite.client.data_classes.data_modeling import InvolvedContainers
>>> res = client.data_modeling.instances.inspect(
... nodes=[("my-space", "foo1"), ("my-space", "foo2")],
... involved_containers=InvolvedContainers(),
... )
"""
identifiers = self._load_node_and_edge_ids(nodes, edges)
Expand All @@ -744,24 +753,21 @@ def inspect(
inspect_operations["involvedViews"] = {"allVersions": involved_views.all_versions}
if involved_containers:
inspect_operations["involvedContainers"] = {}
options = {"inspectionOperations": inspect_operations}
if not inspect_operations:
raise ValueError("Must pass at least one of 'involved_views' or 'involved_containers'")

items = list(
itertools.chain.from_iterable(
self._post(self._RESOURCE_PATH + "/inspect", json={"items": chunk.as_dicts(), **options}).json()[
"items"
]
self._post(
self._RESOURCE_PATH + "/inspect",
json={"items": chunk.as_dicts(), "inspectionOperations": inspect_operations},
).json()["items"]
for chunk in identifiers.chunked(1000)
)
)
node_res = InstanceInspectResultList._load(
[node for node in items if node["instanceType"] == "node"], cognite_client=self._cognite_client
)
edge_res = InstanceInspectResultList._load(
[node for node in items if node["instanceType"] == "edge"], cognite_client=self._cognite_client
)
return InstanceInspectResults(
nodes=node_res,
edges=edge_res,
nodes=InstanceInspectResultList._load([node for node in items if node["instanceType"] == "node"]),
edges=InstanceInspectResultList._load([edge for edge in items if edge["instanceType"] == "edge"]),
)

def subscribe(
Expand Down
4 changes: 4 additions & 0 deletions docs/source/data_modeling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ Query instances
^^^^^^^^^^^^^^^^^
.. automethod:: cognite.client._api.data_modeling.instances.InstancesAPI.query

Inspect instances
^^^^^^^^^^^^^^^^^
.. automethod:: cognite.client._api.data_modeling.instances.InstancesAPI.inspect

Sync instances
^^^^^^^^^^^^^^^^^
.. automethod:: cognite.client._api.data_modeling.instances.InstancesAPI.sync
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,16 @@ def test_listing_global_nodes(self, cognite_client: CogniteClient) -> None:
)
assert len(nodes) == 5

def test_inspecting_instances__no_involved_arg(
self, cognite_client: CogniteClient, movie_nodes: NodeList, movie_edges: EdgeList
) -> None:
exp_err = "Must pass at least one of 'involved_views' or 'involved_containers'"
with pytest.raises(ValueError, match=exp_err):
cognite_client.data_modeling.instances.inspect(
nodes=movie_nodes[0].as_id(),
edges=movie_edges[:5].as_ids(),
)

def test_inspecting_instances(
self, cognite_client: CogniteClient, movie_nodes: NodeList, movie_edges: EdgeList
) -> None:
Expand Down

0 comments on commit bc8ae8a

Please sign in to comment.