-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathdata_models.py
More file actions
279 lines (232 loc) · 11.3 KB
/
Copy pathdata_models.py
File metadata and controls
279 lines (232 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from __future__ import annotations
import asyncio
from collections.abc import AsyncIterator, Sequence
from typing import TYPE_CHECKING, Literal, cast, overload
from cognite.client._api_client import APIClient
from cognite.client.constants import DATA_MODELING_DEFAULT_LIMIT_READ
from cognite.client.data_classes.data_modeling.data_models import (
DataModel,
DataModelApply,
DataModelFilter,
DataModelList,
)
from cognite.client.data_classes.data_modeling.ids import DataModelId, DataModelIdentifier, ViewId, _load_identifier
from cognite.client.data_classes.data_modeling.views import View
if TYPE_CHECKING:
from cognite.client import AsyncCogniteClient
from cognite.client.config import ClientConfig
class DataModelsAPI(APIClient):
_RESOURCE_PATH = "/models/datamodels"
def __init__(self, config: ClientConfig, api_version: str | None, cognite_client: AsyncCogniteClient) -> None:
super().__init__(config, api_version, cognite_client)
self._DELETE_LIMIT = 100
self._RETRIEVE_LIMIT = 100
self._CREATE_LIMIT = 100
def _get_semaphore(self, operation: Literal["read_schema", "write_schema"]) -> asyncio.BoundedSemaphore:
from cognite.client import global_config
assert operation in ("read_schema", "write_schema"), "DataModels API should use schema semaphores"
return global_config.concurrency_settings.data_modeling._semaphore_factory(
operation, project=self._cognite_client.config.project
)
@overload
def __call__(
self,
chunk_size: None = None,
limit: int | None = None,
space: str | None = None,
inline_views: bool = False,
all_versions: bool = False,
include_global: bool = False,
) -> AsyncIterator[DataModel]: ...
@overload
def __call__(
self,
chunk_size: int,
limit: int | None = None,
space: str | None = None,
inline_views: bool = False,
all_versions: bool = False,
include_global: bool = False,
) -> AsyncIterator[DataModelList]: ...
async def __call__(
self,
chunk_size: int | None = None,
limit: int | None = None,
space: str | None = None,
inline_views: bool = False,
all_versions: bool = False,
include_global: bool = False,
) -> AsyncIterator[DataModel] | AsyncIterator[DataModelList]:
"""Iterate over data model
Fetches data model as they are iterated over, so you keep a limited number of data model in memory.
Args:
chunk_size (int | None): Number of data model to return in each chunk. Defaults to yielding one data_model a time.
limit (int | None): Maximum number of data model to return. Defaults to returning all items.
space (str | None): The space to query.
inline_views (bool): Whether to expand the referenced views inline in the returned result.
all_versions (bool): Whether to return all versions. If false, only the newest version is returned, which is determined based on the 'createdTime' field.
include_global (bool): Whether to include global views.
Yields:
DataModel | DataModelList: yields DataModel one by one if chunk_size is not specified, else DataModelList objects.
""" # noqa: DOC404
filter = DataModelFilter(space, inline_views, all_versions, include_global)
async for item in self._list_generator(
list_cls=DataModelList,
resource_cls=DataModel,
method="GET",
chunk_size=chunk_size,
limit=limit,
filter=filter.dump(camel_case=True),
semaphore=self._get_semaphore("read_schema"),
):
yield item
@overload
async def retrieve(
self, ids: DataModelIdentifier | Sequence[DataModelIdentifier], inline_views: Literal[True]
) -> DataModelList[View]: ...
@overload
async def retrieve(
self, ids: DataModelIdentifier | Sequence[DataModelIdentifier], inline_views: Literal[False] = False
) -> DataModelList[ViewId]: ...
async def retrieve(
self, ids: DataModelIdentifier | Sequence[DataModelIdentifier], inline_views: bool = False
) -> DataModelList[ViewId] | DataModelList[View]:
"""`Retrieve data_model(s) by id(s) <https://api-docs.cognite.com/20230101/tag/Data-models/operation/byExternalIdsDataModels>`_.
Args:
ids (DataModelIdentifier | Sequence[DataModelIdentifier]): Data Model identifier(s).
inline_views (bool): Whether to expand the referenced views inline in the returned result.
Returns:
DataModelList[ViewId] | DataModelList[View]: Requested data model(s) or empty if none exist.
Examples:
>>> from cognite.client import CogniteClient, AsyncCogniteClient
>>> client = CogniteClient()
>>> # async_client = AsyncCogniteClient() # another option
>>> res = client.data_modeling.data_models.retrieve(("mySpace", "myDataModel", "v1"))
"""
identifier = _load_identifier(ids, "data_model")
return await self._retrieve_multiple(
list_cls=DataModelList,
resource_cls=DataModel,
identifiers=identifier,
params={"inlineViews": inline_views},
override_semaphore=self._get_semaphore("read_schema"),
)
async def delete(self, ids: DataModelIdentifier | Sequence[DataModelIdentifier]) -> list[DataModelId]:
"""`Delete one or more data model <https://api-docs.cognite.com/20230101/tag/Data-models/operation/deleteDataModels>`_.
Args:
ids (DataModelIdentifier | Sequence[DataModelIdentifier]): Data Model identifier(s).
Returns:
list[DataModelId]: The data_model(s) which has been deleted. None if nothing was deleted.
Examples:
Delete data model by id:
>>> from cognite.client import CogniteClient, AsyncCogniteClient
>>> client = CogniteClient()
>>> # async_client = AsyncCogniteClient() # another option
>>> client.data_modeling.data_models.delete(("mySpace", "myDataModel", "v1"))
"""
deleted_data_models = cast(
list,
await self._delete_multiple(
identifiers=_load_identifier(ids, "data_model"),
wrap_ids=True,
returns_items=True,
override_semaphore=self._get_semaphore("write_schema"),
),
)
return [DataModelId(item["space"], item["externalId"], item["version"]) for item in deleted_data_models]
@overload
async def list(
self,
inline_views: Literal[True],
limit: int | None = DATA_MODELING_DEFAULT_LIMIT_READ,
space: str | None = None,
all_versions: bool = False,
include_global: bool = False,
) -> DataModelList[View]: ...
@overload
async def list(
self,
inline_views: Literal[False] = False,
limit: int | None = DATA_MODELING_DEFAULT_LIMIT_READ,
space: str | None = None,
all_versions: bool = False,
include_global: bool = False,
) -> DataModelList[ViewId]: ...
async def list(
self,
inline_views: bool = False,
limit: int | None = DATA_MODELING_DEFAULT_LIMIT_READ,
space: str | None = None,
all_versions: bool = False,
include_global: bool = False,
) -> DataModelList[View] | DataModelList[ViewId]:
"""`List data models <https://api-docs.cognite.com/20230101/tag/Data-models/operation/listDataModels>`_.
Args:
inline_views (bool): Whether to expand the referenced views inline in the returned result.
limit (int | None): Maximum number of data model to return. Defaults to 10. Set to -1, float("inf") or None to return all items.
space (str | None): The space to query.
all_versions (bool): Whether to return all versions. If false, only the newest version is returned, which is determined based on the 'createdTime' field.
include_global (bool): Whether to include global data models.
Returns:
DataModelList[View] | DataModelList[ViewId]: List of requested data models
Examples:
List 5 data model:
>>> from cognite.client import CogniteClient, AsyncCogniteClient
>>> client = CogniteClient()
>>> # async_client = AsyncCogniteClient() # another option
>>> data_model_list = client.data_modeling.data_models.list(limit=5)
Iterate over data model, one-by-one:
>>> for data_model in client.data_modeling.data_models():
... data_model # do something with the data model
Iterate over chunks of data model to reduce memory load:
>>> for data_model_list in client.data_modeling.data_models(chunk_size=10):
... data_model_list # do something with the data model
"""
filter = DataModelFilter(space, inline_views, all_versions, include_global)
return await self._list(
list_cls=DataModelList,
resource_cls=DataModel,
method="GET",
limit=limit,
filter=filter.dump(camel_case=True),
override_semaphore=self._get_semaphore("read_schema"),
)
@overload
async def apply(self, data_model: Sequence[DataModelApply]) -> DataModelList: ...
@overload
async def apply(self, data_model: DataModelApply) -> DataModel: ...
async def apply(self, data_model: DataModelApply | Sequence[DataModelApply]) -> DataModel | DataModelList:
"""`Create or update one or more data model <https://api-docs.cognite.com/20230101/tag/Data-models/operation/createDataModels>`_.
Args:
data_model (DataModelApply | Sequence[DataModelApply]): Data model(s) to create or update (upsert).
Returns:
DataModel | DataModelList: Created data model(s)
Examples:
Create new data model:
>>> from cognite.client import CogniteClient
>>> from cognite.client.data_classes.data_modeling import DataModelApply, ViewId
>>> client = CogniteClient()
>>> # async_client = AsyncCogniteClient() # another option
>>> data_models = [
... DataModelApply(
... space="mySpace",
... external_id="myDataModel",
... version="v1",
... views=[ViewId("mySpace", "myView", "v1")],
... ),
... DataModelApply(
... space="mySpace",
... external_id="myOtherDataModel",
... version="v1",
... views=[ViewId("mySpace", "myView", "v1")],
... ),
... ]
>>> res = client.data_modeling.data_models.apply(data_models)
"""
return await self._create_multiple(
list_cls=DataModelList,
resource_cls=DataModel,
items=data_model,
input_resource_cls=DataModelApply,
override_semaphore=self._get_semaphore("write_schema"),
)