Skip to content

Commit

Permalink
sort
Browse files Browse the repository at this point in the history
  • Loading branch information
lpereiracgn committed Dec 12, 2024
1 parent ebfe644 commit 433e585
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
15 changes: 13 additions & 2 deletions cognite/client/_api/simulators/simulation_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from cognite.client._api_client import APIClient
from cognite.client._constants import DEFAULT_LIMIT_READ
from cognite.client.data_classes.simulators.filters import SimulationRunsFilter
from cognite.client.data_classes.simulators.simulators import SimulationRun, SimulationRunsList
from cognite.client.data_classes.simulators.simulators import SimulationRun, SimulationRunsList, CreatedTimeSort, SimulationTimeSort
from cognite.client.utils._experimental import FeaturePreviewWarning

if TYPE_CHECKING:
Expand All @@ -22,7 +22,10 @@ def __init__(self, config: ClientConfig, api_version: str | None, cognite_client
)

def list(
self, limit: int = DEFAULT_LIMIT_READ, filter: SimulationRunsFilter | dict[str, Any] | None = None
self,
limit: int = DEFAULT_LIMIT_READ,
filter: SimulationRunsFilter | dict[str, Any] | None = None,
sort: CreatedTimeSort | SimulationTimeSort | None = None,
) -> SimulationRunsList:
"""`Filter simulation runs <https://developer.cognite.com/api#tag/Simulation-Runs/operation/filter_simulation_runs_simulators_runs_list_post>`_
Expand All @@ -44,13 +47,21 @@ def list(
>>> res = client.simulators.runs.list()
"""

sort_by = None
if isinstance(sort, CreatedTimeSort):
sort_by = [CreatedTimeSort.load(sort).dump()]
elif isinstance(sort, SimulationTimeSort):
sort_by = [SimulationTimeSort.load(sort).dump()]

self._warning.warn()
return self._list(
method="POST",
limit=limit,
url_path="/simulators/runs/list",
resource_cls=SimulationRun,
list_cls=SimulationRunsList,
sort=sort_by,
filter=filter.dump()
if isinstance(filter, SimulationRunsFilter)
else filter
Expand Down
22 changes: 13 additions & 9 deletions cognite/client/_api/simulators/simulator_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
SimulatorModelList,
SimulatorModelRevision,
SimulatorModelRevisionList,
CreatedTimeSort
)
from cognite.client.utils._experimental import FeaturePreviewWarning
from cognite.client.utils._identifier import IdentifierSequence
Expand All @@ -30,7 +31,10 @@ def __init__(self, config: ClientConfig, api_version: str | None, cognite_client
)

def list(
self, limit: int = DEFAULT_LIMIT_READ, filter: SimulatorModelRevisionsFilter | dict[str, Any] | None = None
self,
limit: int = DEFAULT_LIMIT_READ,
sort: CreatedTimeSort | None = None,
filter: SimulatorModelRevisionsFilter | dict[str, Any] | None = None
) -> SimulatorModelRevisionList:
"""`Filter simulator model revisions <https://developer.cognite.com/api#tag/Simulator-Models/operation/filter_simulator_model_revisions_simulators_models_revisions_list_post>`_
Expand Down Expand Up @@ -58,6 +62,7 @@ def list(
url_path="/simulators/models/revisions/list",
resource_cls=SimulatorModelRevision,
list_cls=SimulatorModelRevisionList,
sort=[CreatedTimeSort.load(sort).dump()] if sort else None,
filter=filter.dump()
if isinstance(filter, SimulatorModelRevisionsFilter)
else filter
Expand Down Expand Up @@ -100,7 +105,6 @@ def retrieve_multiple(
self,
ids: Sequence[int] | None = None,
external_ids: SequenceNotStr[str] | None = None,
ignore_unknown_ids: bool = False,
) -> SimulatorModelRevisionList:
"""`Retrieve simulator model revisions <https://developer.cognite.com/api#tag/Simulator-Models/operation/retrieve_simulator_model_revisions_simulators_models_revisions_byids_post>`_
Expand Down Expand Up @@ -129,7 +133,6 @@ def retrieve_multiple(
list_cls=SimulatorModelRevisionList,
resource_cls=SimulatorModelRevision,
identifiers=identifiers,
ignore_unknown_ids=ignore_unknown_ids,
)


Expand All @@ -144,7 +147,10 @@ def __init__(self, config: ClientConfig, api_version: str | None, cognite_client
)

def list(
self, limit: int = DEFAULT_LIMIT_READ, filter: SimulatorModelsFilter | dict[str, Any] | None = None
self,
limit: int = DEFAULT_LIMIT_READ,
filter: SimulatorModelsFilter | dict[str, Any] | None = None,
sort: CreatedTimeSort | None = None,
) -> SimulatorModelList:
"""`Filter simulator models <https://developer.cognite.com/api#tag/Simulator-Models/operation/filter_simulator_models_simulators_models_list_post>`_
Expand Down Expand Up @@ -173,6 +179,7 @@ def list(
url_path="/simulators/models/list",
resource_cls=SimulatorModel,
list_cls=SimulatorModelList,
sort=[CreatedTimeSort.load(sort).dump()] if sort else None,
filter=filter.dump()
if isinstance(filter, SimulatorModelsFilter)
else filter
Expand All @@ -194,15 +201,12 @@ def retrieve(self, id: int | None = None, external_id: str | None = None) -> Sim
Examples:
List simulator models:
Retrieve simulator model by id:
>>> from cognite.client import CogniteClient
>>> client = CogniteClient()
>>> res = client.simulators.models.list()
Get simulator model by id:
>>> res = client.simulators.models.retrieve(id=1)
Get simulator model by external id:
Retrieve simulator model by external id:
>>> res = client.simulators.models.retrieve(external_id="1")
"""
Expand Down
17 changes: 13 additions & 4 deletions cognite/client/_api/simulators/simulator_routines.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from collections.abc import Sequence
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Literal

from cognite.client._api_client import APIClient
from cognite.client._constants import DEFAULT_LIMIT_READ
Expand All @@ -11,6 +11,7 @@
SimulatorRoutineList,
SimulatorRoutineRevision,
SimulatorRoutineRevisionsList,
CreatedTimeSort,
)
from cognite.client.utils._experimental import FeaturePreviewWarning
from cognite.client.utils._identifier import IdentifierSequence
Expand All @@ -19,7 +20,6 @@
if TYPE_CHECKING:
from cognite.client import ClientConfig, CogniteClient


class SimulatorRoutineRevisionsAPI(APIClient):
_RESOURCE_PATH = "/simulators/routines/revisions"

Expand All @@ -30,7 +30,11 @@ def __init__(self, config: ClientConfig, api_version: str | None, cognite_client
)

def list(
self, limit: int = DEFAULT_LIMIT_READ, filter: SimulatorRoutineRevisionsFilter | dict[str, Any] | None = None
self,
limit: int = 20, # the maximum number of revisions to return is limited to 20 items.
sort: CreatedTimeSort | None = None,
filter: SimulatorRoutineRevisionsFilter | dict[str, Any] | None = None,
includeAllFields: bool = False,
) -> SimulatorRoutineRevisionsList:
"""`Filter simulator routine revisions <https://developer.cognite.com/api#tag/Simulator-Routines/operation/filter_simulator_routine_revisions_simulators_routines_revisions_list_post>`_
Expand Down Expand Up @@ -63,6 +67,8 @@ def list(
else filter
if isinstance(filter, dict)
else None,
sort=[CreatedTimeSort.load(sort).dump()] if sort else None,
other_params={"includeAllFields": includeAllFields},
)

def retrieve(self, id: int | None = None, external_id: str | None = None) -> SimulatorRoutineRevision | None:
Expand Down Expand Up @@ -143,7 +149,9 @@ def __init__(self, config: ClientConfig, api_version: str | None, cognite_client
)

def list(
self, limit: int = DEFAULT_LIMIT_READ, filter: SimulatorRoutinesFilter | dict[str, Any] | None = None
self,
limit: int = DEFAULT_LIMIT_READ, filter: SimulatorRoutinesFilter | dict[str, Any] | None = None,
sort: CreatedTimeSort | None = None,
) -> SimulatorRoutineList:
"""`Filter simulator routines <https://developer.cognite.com/api#tag/Simulator-Routines/operation/filter_simulator_routines_simulators_routines_list_post>`_
Expand Down Expand Up @@ -172,6 +180,7 @@ def list(
url_path="/simulators/routines/list",
resource_cls=SimulatorRoutine,
list_cls=SimulatorRoutineList,
sort=[CreatedTimeSort.load(sort).dump()] if sort else None,
filter=filter.dump()
if isinstance(filter, SimulatorRoutinesFilter)
else filter
Expand Down
28 changes: 27 additions & 1 deletion cognite/client/data_classes/simulators/simulators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from abc import ABC
from collections.abc import Sequence
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
from enum import auto
from typing import TYPE_CHECKING, Any, Literal, TypeAlias

from typing_extensions import Self

Expand All @@ -16,6 +17,8 @@
InternalIdTransformerMixin,
WriteableCogniteResource,
WriteableCogniteResourceList,
EnumProperty,
CogniteSort
)
from cognite.client.utils.useful_types import SequenceNotStr

Expand Down Expand Up @@ -1526,6 +1529,29 @@ def __hash__(self) -> int:
return hash(self.external_id)


class PropertySort(CogniteSort):

def dump(self, camel_case: bool = True) -> dict[str, Any]:
dumped = super().dump(camel_case=camel_case)
dumped["property"] = self.property
return dumped

class CreatedTimeSort(PropertySort):
def __init__(
self,
property: Literal["createdTime"] = "createdTime",
order: Literal["asc", "desc"] = "asc",
):
super().__init__(property, order)

class SimulationTimeSort(PropertySort):
def __init__(
self,
property: Literal["simulationTime"] = "simulationTime",
order: Literal["asc", "desc"] = "asc",
):
super().__init__(property, order)

class SimulatorRoutineRevisionWriteList(CogniteResourceList[SimulatorRoutineRevisionWrite], ExternalIDTransformerMixin):
_RESOURCE = SimulatorRoutineRevisionWrite

Expand Down

0 comments on commit 433e585

Please sign in to comment.