Skip to content

Commit 7a0e15f

Browse files
committed
add docstrings with url, args & examples
1 parent aba9755 commit 7a0e15f

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed
Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from __future__ import annotations
22

33
import itertools
4-
from collections.abc import Sequence
5-
from typing import TYPE_CHECKING
4+
from typing import TYPE_CHECKING, Any, Literal, overload
65

76
from cognite.client._api_client import APIClient
87
from cognite.client.data_classes.data_modeling.ids import _load_space_identifier
9-
from cognite.client.data_classes.data_modeling.statistics import InstanceStatsList, ProjectStatsAndLimits
8+
from cognite.client.data_classes.data_modeling.statistics import (
9+
InstanceStatsList,
10+
InstanceStatsPerSpace,
11+
ProjectStatsAndLimits,
12+
)
13+
from cognite.client.utils.useful_types import SequenceNotStr
1014

1115
if TYPE_CHECKING:
1216
from cognite.client._cognite_client import CogniteClient
@@ -21,23 +25,77 @@ def __init__(self, config: ClientConfig, api_version: str | None, cognite_client
2125
self._RETRIEVE_LIMIT = 100 # may need to be renamed, but fine for now
2226

2327
def project(self) -> ProjectStatsAndLimits:
24-
"""Usage data and limits for a project's data modelling usage, including data model schemas and graph instances"""
28+
"""`Retrieve project-wide usage data and limits <https://developer.cognite.com/api#tag/Statistics/operation/getStatistics>`_
29+
30+
Returns the usage data and limits for a project's data modelling usage, including data model schemas and graph instances
31+
32+
Returns:
33+
ProjectStatsAndLimits: The requested statistics and limits
34+
35+
Examples:
36+
37+
Fetch project statistics (and limits) and check the current number of data models vs.
38+
and how many more can be created:
39+
40+
>>> from cognite.client import CogniteClient
41+
>>> client = CogniteClient()
42+
>>> stats = client.data_modeling.statistics.project()
43+
>>> num_dm = stats.data_models.current
44+
>>> num_dm_left = stats.data_models.limit - num_dm
45+
"""
2546
return ProjectStatsAndLimits._load(
2647
self._get(self._RESOURCE_PATH).json(), project=self._cognite_client._config.project
2748
)
2849

29-
def per_space(self, space: str | Sequence[str] | None = None, return_all: bool = False) -> InstanceStatsList:
30-
"""Statistics and limits for all spaces in the project, or for a select subset"""
50+
@overload
51+
def per_space(self, space: str, return_all: Literal[False]) -> InstanceStatsPerSpace: ...
52+
53+
@overload
54+
def per_space(self, space: Any, return_all: Literal[True]) -> InstanceStatsList: ...
55+
56+
@overload
57+
def per_space(self, space: SequenceNotStr[str], return_all: bool) -> InstanceStatsList: ...
58+
59+
def per_space(
60+
self, space: str | SequenceNotStr[str] | None = None, return_all: bool = False
61+
) -> InstanceStatsPerSpace | InstanceStatsList:
62+
"""`Retrieve usage data and limits per space <https://developer.cognite.com/api#tag/Statistics/operation/getSpaceStatisticsByIds>`_
63+
64+
See also: `Retrieve statistics and limits for all spaces <https://developer.cognite.com/api#tag/Statistics/operation/getSpaceStatistics>`_
65+
66+
Args:
67+
space (str | SequenceNotStr[str] | None): The space or spaces to retrieve statistics for.
68+
return_all (bool): If True, fetch statistics for all spaces. If False, fetch statistics for the specified space(s).
69+
70+
Returns:
71+
InstanceStatsPerSpace | InstanceStatsList: InstanceStatsPerSpace if a single space is given, else InstanceStatsList (which is a list of InstanceStatsPerSpace)
72+
73+
Examples:
74+
75+
Fetch statistics for a single space:
76+
77+
>>> from cognite.client import CogniteClient
78+
>>> client = CogniteClient()
79+
>>> res = client.data_modeling.statistics.per_space("my-space")
80+
81+
Fetch statistics for multiple spaces:
82+
>>> res = client.data_modeling.statistics.per_space(
83+
... ["my-space1", "my-space2"]
84+
... )
85+
86+
Fetch statistics for all spaces (ignores the 'space' argument):
87+
>>> res = client.data_modeling.statistics.per_space(return_all=True)
88+
"""
3189
if return_all:
3290
return InstanceStatsList._load(self._get(self._RESOURCE_PATH + "/spaces").json()["items"])
3391

34-
elif space is not None:
35-
# Statistics and limits for spaces by their space identifiers
36-
ids = _load_space_identifier(space)
37-
return InstanceStatsList._load(
38-
itertools.chain.from_iterable(
39-
self._post(self._RESOURCE_PATH + "/spaces/byids", json={"items": chunk.as_dicts()}).json()["items"]
40-
for chunk in ids.chunked(self._RETRIEVE_LIMIT)
41-
)
92+
elif space is None:
93+
raise ValueError("Either 'space' or 'return_all' must be specified")
94+
95+
ids = _load_space_identifier(space)
96+
return InstanceStatsList._load(
97+
itertools.chain.from_iterable(
98+
self._post(self._RESOURCE_PATH + "/spaces/byids", json={"items": chunk.as_dicts()}).json()["items"]
99+
for chunk in ids.chunked(self._RETRIEVE_LIMIT)
42100
)
43-
raise ValueError("Either 'space' or 'return_all' must be specified")
101+
)

tests/tests_unit/test_docstring_examples.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
units,
2626
workflows,
2727
)
28-
from cognite.client._api.data_modeling import containers, data_models, graphql, instances, spaces, views
28+
from cognite.client._api.data_modeling import containers, data_models, graphql, instances, spaces, statistics, views
2929
from cognite.client._api.hosted_extractors import destinations, jobs, mappings, sources
3030
from cognite.client._api.postgres_gateway import tables as postgres_gateway_tables
3131
from cognite.client._api.postgres_gateway import users as postgres_gateway_users
@@ -114,6 +114,7 @@ def test_data_modeling(self):
114114
run_docstring_tests(data_models)
115115
run_docstring_tests(spaces)
116116
run_docstring_tests(graphql)
117+
run_docstring_tests(statistics)
117118

118119
def test_datapoint_subscriptions(self):
119120
run_docstring_tests(datapoints_subscriptions)

0 commit comments

Comments
 (0)