71
71
)
72
72
from cognite .client .data_classes .data_modeling .views import View
73
73
from cognite .client .data_classes .filters import _BASIC_FILTERS , Filter , _validate_filter
74
- from cognite .client .utils ._auxiliary import load_yaml_or_json
74
+ from cognite .client .utils ._auxiliary import is_unlimited , load_yaml_or_json
75
75
from cognite .client .utils ._concurrency import ConcurrencySettings
76
76
from cognite .client .utils ._identifier import DataModelingIdentifierSequence
77
77
from cognite .client .utils ._retry import Backoff
78
78
from cognite .client .utils ._text import random_string
79
79
from cognite .client .utils .useful_types import SequenceNotStr
80
80
81
81
if TYPE_CHECKING :
82
- from cognite .client import CogniteClient
82
+ from cognite .client import ClientConfig , CogniteClient
83
83
84
84
_FILTERS_SUPPORTED : frozenset [type [Filter ]] = _BASIC_FILTERS .union (
85
85
{filters .Nested , filters .HasData , filters .MatchAll , filters .Overlaps , filters .InstanceReferences }
@@ -164,6 +164,11 @@ def _load(data: dict, cognite_client: CogniteClient | None = None) -> NodeApply
164
164
class InstancesAPI (APIClient ):
165
165
_RESOURCE_PATH = "/models/instances"
166
166
167
+ def __init__ (self , config : ClientConfig , api_version : str | None , cognite_client : CogniteClient ) -> None :
168
+ super ().__init__ (config , api_version , cognite_client )
169
+ self ._AGGREGATE_LIMIT = 1000
170
+ self ._SEARCH_LIMIT = 1000
171
+
167
172
@overload
168
173
def __call__ (
169
174
self ,
@@ -1042,7 +1047,7 @@ def search(
1042
1047
space : str | SequenceNotStr [str ] | None = None ,
1043
1048
filter : Filter | dict [str , Any ] | None = None ,
1044
1049
include_typing : bool = False ,
1045
- limit : int = DEFAULT_LIMIT_READ ,
1050
+ limit : int | None = DEFAULT_LIMIT_READ ,
1046
1051
sort : Sequence [InstanceSort | dict ] | InstanceSort | dict | None = None ,
1047
1052
) -> NodeList [Node ]: ...
1048
1053
@@ -1058,7 +1063,7 @@ def search(
1058
1063
space : str | SequenceNotStr [str ] | None = None ,
1059
1064
filter : Filter | dict [str , Any ] | None = None ,
1060
1065
include_typing : bool = False ,
1061
- limit : int = DEFAULT_LIMIT_READ ,
1066
+ limit : int | None = DEFAULT_LIMIT_READ ,
1062
1067
sort : Sequence [InstanceSort | dict ] | InstanceSort | dict | None = None ,
1063
1068
) -> EdgeList [Edge ]: ...
1064
1069
@@ -1074,7 +1079,7 @@ def search(
1074
1079
space : str | SequenceNotStr [str ] | None = None ,
1075
1080
filter : Filter | dict [str , Any ] | None = None ,
1076
1081
include_typing : bool = False ,
1077
- limit : int = DEFAULT_LIMIT_READ ,
1082
+ limit : int | None = DEFAULT_LIMIT_READ ,
1078
1083
sort : Sequence [InstanceSort | dict ] | InstanceSort | dict | None = None ,
1079
1084
) -> NodeList [T_Node ]: ...
1080
1085
@@ -1090,7 +1095,7 @@ def search(
1090
1095
space : str | SequenceNotStr [str ] | None = None ,
1091
1096
filter : Filter | dict [str , Any ] | None = None ,
1092
1097
include_typing : bool = False ,
1093
- limit : int = DEFAULT_LIMIT_READ ,
1098
+ limit : int | None = DEFAULT_LIMIT_READ ,
1094
1099
sort : Sequence [InstanceSort | dict ] | InstanceSort | dict | None = None ,
1095
1100
) -> EdgeList [T_Edge ]: ...
1096
1101
@@ -1104,7 +1109,7 @@ def search(
1104
1109
space : str | SequenceNotStr [str ] | None = None ,
1105
1110
filter : Filter | dict [str , Any ] | None = None ,
1106
1111
include_typing : bool = False ,
1107
- limit : int = DEFAULT_LIMIT_READ ,
1112
+ limit : int | None = DEFAULT_LIMIT_READ ,
1108
1113
sort : Sequence [InstanceSort | dict ] | InstanceSort | dict | None = None ,
1109
1114
) -> NodeList [T_Node ] | EdgeList [T_Edge ]:
1110
1115
"""`Search instances <https://developer.cognite.com/api/v1/#tag/Instances/operation/searchInstances>`_
@@ -1118,7 +1123,8 @@ def search(
1118
1123
space (str | SequenceNotStr[str] | None): Restrict instance search to the given space (or list of spaces).
1119
1124
filter (Filter | dict[str, Any] | None): Advanced filtering of instances.
1120
1125
include_typing (bool): Whether to include typing information.
1121
- limit (int): Maximum number of instances to return. Defaults to 25.
1126
+ limit (int | None): Maximum number of instances to return. Defaults to 25. Will return the maximum number
1127
+ of results (1000) if set to None, -1, or math.inf.
1122
1128
sort (Sequence[InstanceSort | dict] | InstanceSort | dict | None): How you want the listed instances information ordered.
1123
1129
1124
1130
Returns:
@@ -1166,7 +1172,11 @@ def search(
1166
1172
else :
1167
1173
raise ValueError (f"Invalid instance type: { instance_type } " )
1168
1174
1169
- body : dict [str , Any ] = {"view" : view .dump (camel_case = True ), "instanceType" : instance_type_str , "limit" : limit }
1175
+ body : dict [str , Any ] = {
1176
+ "view" : view .dump (camel_case = True ),
1177
+ "instanceType" : instance_type_str ,
1178
+ "limit" : self ._SEARCH_LIMIT if is_unlimited (limit ) else limit ,
1179
+ }
1170
1180
if query :
1171
1181
body ["query" ] = query
1172
1182
if properties :
@@ -1202,7 +1212,7 @@ def aggregate(
1202
1212
target_units : list [TargetUnit ] | None = None ,
1203
1213
space : str | SequenceNotStr [str ] | None = None ,
1204
1214
filter : Filter | dict [str , Any ] | None = None ,
1205
- limit : int = DEFAULT_LIMIT_READ ,
1215
+ limit : int | None = DEFAULT_LIMIT_READ ,
1206
1216
) -> AggregatedNumberedValue : ...
1207
1217
1208
1218
@overload
@@ -1217,7 +1227,7 @@ def aggregate(
1217
1227
target_units : list [TargetUnit ] | None = None ,
1218
1228
space : str | SequenceNotStr [str ] | None = None ,
1219
1229
filter : Filter | dict [str , Any ] | None = None ,
1220
- limit : int = DEFAULT_LIMIT_READ ,
1230
+ limit : int | None = DEFAULT_LIMIT_READ ,
1221
1231
) -> list [AggregatedNumberedValue ]: ...
1222
1232
1223
1233
@overload
@@ -1232,7 +1242,7 @@ def aggregate(
1232
1242
target_units : list [TargetUnit ] | None = None ,
1233
1243
space : str | SequenceNotStr [str ] | None = None ,
1234
1244
filter : Filter | dict [str , Any ] | None = None ,
1235
- limit : int = DEFAULT_LIMIT_READ ,
1245
+ limit : int | None = DEFAULT_LIMIT_READ ,
1236
1246
) -> InstanceAggregationResultList : ...
1237
1247
1238
1248
def aggregate (
@@ -1246,7 +1256,7 @@ def aggregate(
1246
1256
target_units : list [TargetUnit ] | None = None ,
1247
1257
space : str | SequenceNotStr [str ] | None = None ,
1248
1258
filter : Filter | dict [str , Any ] | None = None ,
1249
- limit : int = DEFAULT_LIMIT_READ ,
1259
+ limit : int | None = DEFAULT_LIMIT_READ ,
1250
1260
) -> AggregatedNumberedValue | list [AggregatedNumberedValue ] | InstanceAggregationResultList :
1251
1261
"""`Aggregate data across nodes/edges <https://developer.cognite.com/api/v1/#tag/Instances/operation/aggregateInstances>`_
1252
1262
@@ -1260,7 +1270,8 @@ def aggregate(
1260
1270
target_units (list[TargetUnit] | None): Properties to convert to another unit. The API can only convert to another unit if a unit has been defined as part of the type on the underlying container being queried.
1261
1271
space (str | SequenceNotStr[str] | None): Restrict instance aggregate query to the given space (or list of spaces).
1262
1272
filter (Filter | dict[str, Any] | None): Advanced filtering of instances.
1263
- limit (int): Maximum number of instances to return. Defaults to 25.
1273
+ limit (int | None): Maximum number of instances to return. Defaults to 25. Will return the maximum number
1274
+ of results (1000) if set to None, -1, or math.inf.
1264
1275
1265
1276
Returns:
1266
1277
AggregatedNumberedValue | list[AggregatedNumberedValue] | InstanceAggregationResultList: Node or edge aggregation results.
@@ -1282,7 +1293,11 @@ def aggregate(
1282
1293
1283
1294
self ._validate_filter (filter )
1284
1295
filter = self ._merge_space_into_filter (instance_type , space , filter )
1285
- body : dict [str , Any ] = {"view" : view .dump (camel_case = True ), "instanceType" : instance_type , "limit" : limit }
1296
+ body : dict [str , Any ] = {
1297
+ "view" : view .dump (camel_case = True ),
1298
+ "instanceType" : instance_type ,
1299
+ "limit" : self ._AGGREGATE_LIMIT if is_unlimited (limit ) else limit ,
1300
+ }
1286
1301
is_single = isinstance (aggregates , (dict , MetricAggregation ))
1287
1302
aggregate_seq : Sequence [MetricAggregation | dict ] = (
1288
1303
[aggregates ] if isinstance (aggregates , (dict , MetricAggregation )) else aggregates
0 commit comments