Skip to content

Commit

Permalink
Add data set scoping to time series subscriptions (#1506)
Browse files Browse the repository at this point in the history
  • Loading branch information
sighol authored Nov 21, 2023
1 parent 90d4ee9 commit 30090f9
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [7.3.0] - 2023-11-20
### Added
- Added Scope `DataSet` for `TimeSeriesSubscriptionsAcl`.
- Added `data_set_id` to `DatapointSubscription`.

## [7.2.1] - 2023-11-17
### Fixed
- The new compare methods for capabilities in major version 7, `IAMAPI.verify_capabilities` and `IAMAPI.compare_capabilities`
Expand Down
2 changes: 1 addition & 1 deletion cognite/client/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations

__version__ = "7.2.1"
__version__ = "7.3.0"
__api_subversion__ = "V20220125"
1 change: 1 addition & 0 deletions cognite/client/data_classes/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ class Action(Capability.Action):

class Scope:
All = AllScope
DataSet = DataSetScope


@dataclass
Expand Down
16 changes: 14 additions & 2 deletions cognite/client/data_classes/datapoints_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ def __init__(
filter: Filter | None = None,
name: str | None = None,
description: str | None = None,
data_set_id: int | None = None,
**_: Any,
) -> None:
self.external_id = external_id
self.partition_count = partition_count
self.filter = filter
self.name = name
self.description = description
self.data_set_id = data_set_id

@classmethod
def _load(
Expand Down Expand Up @@ -90,6 +92,7 @@ class DatapointSubscription(DatapointSubscriptionCore):
filter (Filter | None): If present, the subscription is defined by this filter.
name (str | None): No description.
description (str | None): A summary explanation for the subscription.
data_set_id (int | None): The id of the dataset this subscription belongs to.
**_ (Any): No description.
"""

Expand All @@ -103,9 +106,10 @@ def __init__(
filter: Filter | None = None,
name: str | None = None,
description: str | None = None,
data_set_id: int | None = None,
**_: Any,
) -> None:
super().__init__(external_id, partition_count, filter, name, description)
super().__init__(external_id, partition_count, filter, name, description, data_set_id)
self.time_series_count = time_series_count
self.created_time = created_time
self.last_updated_time = last_updated_time
Expand All @@ -124,6 +128,7 @@ class DataPointSubscriptionCreate(DatapointSubscriptionCore):
filter (Filter | None): A filter DSL (Domain Specific Language) to define advanced filter queries. Not compatible with time_series_ids.
name (str | None): No description.
description (str | None): A summary explanation for the subscription.
data_set_id (int | None): The id of the dataset this subscription belongs to.
"""

def __init__(
Expand All @@ -134,11 +139,12 @@ def __init__(
filter: Filter | None = None,
name: str | None = None,
description: str | None = None,
data_set_id: int | None = None,
) -> None:
if not exactly_one_is_not_none(time_series_ids, filter):
raise ValueError("Exactly one of time_series_ids and filter must be given")
_validate_filter(filter, _DATAPOINT_SUBSCRIPTION_SUPPORTED_FILTERS, "DataPointSubscriptions")
super().__init__(external_id, partition_count, filter, name, description)
super().__init__(external_id, partition_count, filter, name, description, data_set_id)
self.time_series_ids = time_series_ids

@classmethod
Expand All @@ -151,6 +157,7 @@ def _load(cls, resource: dict, cognite_client: CogniteClient | None = None) -> S
filter=filter,
name=resource.get("name"),
description=resource.get("description"),
data_set_id=resource.get("dataSetId"),
)


Expand Down Expand Up @@ -186,6 +193,10 @@ def remove(self, value: list) -> DataPointSubscriptionUpdate:
def name(self) -> _PrimitiveDataPointSubscriptionUpdate:
return DataPointSubscriptionUpdate._PrimitiveDataPointSubscriptionUpdate(self, "name")

@property
def data_set_id(self) -> _PrimitiveDataPointSubscriptionUpdate:
return DataPointSubscriptionUpdate._PrimitiveDataPointSubscriptionUpdate(self, "dataSetId")

@property
def time_series_ids(self) -> _ListDataPointSubscriptionUpdate:
return DataPointSubscriptionUpdate._ListDataPointSubscriptionUpdate(self, "timeSeriesIds")
Expand All @@ -200,6 +211,7 @@ def _get_update_properties(cls) -> list[PropertySpec]:
PropertySpec("name"),
PropertySpec("time_series_ids", is_container=True),
PropertySpec("filter", is_nullable=False),
PropertySpec("data_set_id"),
]


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "cognite-sdk"

version = "7.2.1"
version = "7.3.0"
description = "Cognite Python SDK"
readme = "README.md"
documentation = "https://cognite-sdk-python.readthedocs-hosted.com"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ def test_list_subscriptions(self, cognite_client: CogniteClient, subscription: D
def test_create_retrieve_delete_subscription(
self, cognite_client: CogniteClient, time_series_external_ids: list[str]
):
data_set = cognite_client.data_sets.list(limit=1)[0]
# Arrange
new_subscription = DataPointSubscriptionCreate(
external_id=f"PYSDKDataPointSubscriptionCreateRetrieveDeleteTest-{random_string(10)}",
name="PYSDKDataPointSubscriptionCreateRetrieveDeleteTest",
time_series_ids=time_series_external_ids,
partition_count=1,
data_set_id=data_set.id,
)
with create_subscription_with_cleanup(cognite_client, new_subscription) as created:
retrieved_subscription = cognite_client.time_series.subscriptions.retrieve(new_subscription.external_id)
Expand All @@ -98,6 +100,9 @@ def test_create_retrieve_delete_subscription(
assert created.last_updated_time
assert created.time_series_count == len(new_subscription.time_series_ids)
assert retrieved_subscription.external_id == new_subscription.external_id == created.external_id
assert retrieved_subscription.name == new_subscription.name == created.name
assert retrieved_subscription.description == new_subscription.description == created.description
assert retrieved_subscription.data_set_id == new_subscription.data_set_id == created.data_set_id

time_series_in_subscription = cognite_client.time_series.subscriptions.list_member_time_series(
new_subscription.external_id, limit=10
Expand All @@ -122,11 +127,13 @@ def test_update_subscription(self, cognite_client: CogniteClient, time_series_ex
time_series_ids=time_series_external_ids,
partition_count=1,
)
data_set = cognite_client.data_sets.list(limit=1)[0]
with create_subscription_with_cleanup(cognite_client, new_subscription):
update = (
DataPointSubscriptionUpdate(new_subscription.external_id)
.name.set("New Name")
.time_series_ids.remove([time_series_external_ids[0]])
.data_set_id.set(data_set.id)
)

# Act
Expand All @@ -135,6 +142,7 @@ def test_update_subscription(self, cognite_client: CogniteClient, time_series_ex
# Assert
assert updated.name == "New Name"
assert updated.time_series_count == len(time_series_external_ids) - 1
assert updated.data_set_id == data_set.id

def test_update_filter_defined_subscription(self, cognite_client: CogniteClient):
# Arrange
Expand Down

0 comments on commit 30090f9

Please sign in to comment.