Skip to content

Commit

Permalink
update dps insert examples with instance_id (#2036)
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonvt authored Nov 29, 2024
1 parent f8302f4 commit 9783088
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
27 changes: 18 additions & 9 deletions cognite/client/_api/datapoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1525,12 +1525,13 @@ def insert(
The timestamp can be given by datetime as above, or in milliseconds since epoch. Status codes can also be
passed as normal integers; this is necessary if a subcategory or modifier flag is needed, e.g. 3145728: 'GoodClamped':
>>> from cognite.client.data_classes.data_modeling import NodeId
>>> datapoints = [
... (150000000000, 1000),
... (160000000000, 2000, 3145728),
... (170000000000, 2000, 2147483648), # Same as StatusCode.Bad
... ]
>>> client.time_series.data.insert(datapoints, id=2)
>>> client.time_series.data.insert(datapoints, instance_id=NodeId("my-space", "my-ts-xid"))
Or they can be a list of dictionaries:
Expand Down Expand Up @@ -1595,24 +1596,32 @@ def insert_multiple(self, datapoints: list[dict[str, str | int | list | Datapoin
>>> from cognite.client import CogniteClient
>>> from cognite.client.data_classes.data_modeling import NodeId
>>> from cognite.client.data_classes import StatusCode
>>> from datetime import datetime, timezone
>>> client = CogniteClient()
>>> to_insert = [
... {"id": 1, "datapoints": [
... (datetime(2018,1,1, tzinfo=timezone.utc), 1000),
... (datetime(2018,1,2, tzinfo=timezone.utc), 2000, StatusCode.Good),
... (datetime(2018,1,3, tzinfo=timezone.utc), 3000, StatusCode.Uncertain),
... (datetime(2018,1,4, tzinfo=timezone.utc), None, StatusCode.Bad),
... ]}]
... (datetime(2018,1,2, tzinfo=timezone.utc), 2000, StatusCode.Good)],
... },
... {"external_id": "foo", "datapoints": [
... (datetime(2018,1,3, tzinfo=timezone.utc), 3000),
... (datetime(2018,1,4, tzinfo=timezone.utc), 4000, StatusCode.Uncertain)],
... },
... {"instance_id": NodeId("my-space", "my-ts-xid"), "datapoints": [
... (datetime(2018,1,5, tzinfo=timezone.utc), 5000),
... (datetime(2018,1,6, tzinfo=timezone.utc), None, StatusCode.Bad)],
... }
... ]
Passing datapoints using the dictionary format with timestamp given in milliseconds since epoch:
>>> import math
>>> to_insert.append(
... {"external_id": "foo", "datapoints": [
... {"timestamp": 170000000, "value": 4000},
... {"timestamp": 180000000, "value": 5000, "status": {"symbol": "Uncertain"}},
... {"external_id": "bar", "datapoints": [
... {"timestamp": 170000000, "value": 7000},
... {"timestamp": 180000000, "value": 8000, "status": {"symbol": "Uncertain"}},
... {"timestamp": 190000000, "value": None, "status": {"code": StatusCode.Bad}},
... {"timestamp": 200000000, "value": math.inf, "status": {"code": StatusCode.Bad, "symbol": "Bad"}},
... ]})
Expand All @@ -1625,7 +1634,7 @@ def insert_multiple(self, datapoints: list[dict[str, str | int | list | Datapoin
>>> client.time_series.data.insert_multiple(to_insert)
"""
if not isinstance(datapoints, Sequence):
raise ValueError("Input must be a list of dictionaries")
raise TypeError("Input to 'insert_multiple' must be a list of dictionaries")
DatapointsPoster(self).insert(datapoints)

def delete_range(
Expand Down
15 changes: 11 additions & 4 deletions tests/tests_integration/test_api/test_three_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
from cognite.client import CogniteClient
from cognite.client.data_classes import ThreeDModel, ThreeDModelRevisionUpdate, ThreeDModelUpdate, ThreeDModelWrite
from cognite.client.exceptions import CogniteAPIError
from cognite.client.utils._text import random_string


@pytest.fixture(scope="class")
def test_model_name():
return f"NewTestModel-{random_string(6)}"


@pytest.fixture(scope="class")
Expand All @@ -17,8 +23,8 @@ def test_revision(cognite_client):


@pytest.fixture(scope="class")
def new_model(cognite_client: CogniteClient) -> ThreeDModel:
res = cognite_client.three_d.models.create(name="NewTestModel")
def new_model(cognite_client: CogniteClient, test_model_name: str) -> ThreeDModel:
res = cognite_client.three_d.models.create(name=test_model_name)
yield res
cognite_client.three_d.models.delete(id=res.id)
assert cognite_client.three_d.models.retrieve(id=res.id) is None
Expand All @@ -30,11 +36,12 @@ def test_nodes_tree_index_order(cognite_client):
return cognite_client.three_d.list_nodes(model_id=model_id, revision_id=revision.id)


@pytest.mark.usefixtures("new_model")
class TestThreeDModelsAPI:
def test_list_and_retrieve(self, cognite_client):
def test_list_and_retrieve(self, cognite_client, test_model_name):
res = cognite_client.three_d.models.list(limit=1)
assert 1 == len(res)
res = next(r for r in cognite_client.three_d.models(limit=None) if r.name == "MyModel775")
res = next(r for r in cognite_client.three_d.models(limit=None) if r.name == test_model_name)
assert res == cognite_client.three_d.models.retrieve(res.id)

def test_create_update_delete(self, cognite_client) -> None:
Expand Down

0 comments on commit 9783088

Please sign in to comment.