Пример #1
0
    def __call__(
        self,
        chunk_size: int = None,
        start_time: Dict[str, Any] = None,
        end_time: Dict[str, Any] = None,
        type: str = None,
        subtype: str = None,
        metadata: Dict[str, Any] = None,
        asset_ids: List[int] = None,
        root_asset_ids: List[Dict[str, Any]] = None,
        source: str = None,
        created_time: Dict[str, Any] = None,
        last_updated_time: Dict[str, Any] = None,
        external_id_prefix: str = None,
        limit: int = None,
    ) -> Generator[Union[Event, EventList], None, None]:
        """Iterate over events

        Fetches events as they are iterated over, so you keep a limited number of events in memory.

        Args:
            chunk_size (int, optional): Number of events to return in each chunk. Defaults to yielding one event a time.
            start_time (Dict[str, Any]): Range between two timestamps
            end_time (Dict[str, Any]): Range between two timestamps
            type (str): Type of the event, e.g 'failure'.
            subtype (str): Subtype of the event, e.g 'electrical'.
            metadata (Dict[str, Any]): Customizable extra data about the event. String key -> String value.
            asset_ids (List[int]): Asset IDs of related equipments that this event relates to.
            root_asset_ids (List[Dict[str, Any]]): The IDs of the root assets that the related assets should be children of.
            source (str): The source of this event.
            created_time (Dict[str, Any]): Range between two timestamps
            last_updated_time (Dict[str, Any]): Range between two timestamps
            external_id_prefix (str): External Id provided by client. Should be unique within the project
            limit (int, optional): Maximum number of assets to return. Defaults to 25. Set to -1, float("inf") or None
                to return all items.

        Yields:
            Union[Event, EventList]: yields Event one by one if chunk is not specified, else EventList objects.
        """
        filter = EventFilter(
            start_time=start_time,
            end_time=end_time,
            metadata=metadata,
            asset_ids=asset_ids,
            root_asset_ids=root_asset_ids,
            source=source,
            created_time=created_time,
            last_updated_time=last_updated_time,
            external_id_prefix=external_id_prefix,
            type=type,
            subtype=subtype,
        ).dump(camel_case=True)
        return self._list_generator(method="POST",
                                    chunk_size=chunk_size,
                                    filter=filter,
                                    limit=limit)
Пример #2
0
 def test_search(self, mock_events_response):
     res = EVENTS_API.search(filter=EventFilter(external_id_prefix="abc"))
     assert mock_events_response.calls[0].response.json(
     )["items"] == res.dump(camel_case=True)
     assert {
         "search": {
             "description": None
         },
         "filter": {
             "externalIdPrefix": "abc"
         },
         "limit": 100
     } == jsgz_load(mock_events_response.calls[0].request.body)
Пример #3
0
 def test_search(self):
     res = COGNITE_CLIENT.events.search(filter=EventFilter(
         start_time={
             "min": cognite.client.utils._time.timestamp_to_ms("2d-ago")
         }))
     assert len(res) > 0
Пример #4
0
 def test_aggregation(self, new_event):
     res_aggregate = COGNITE_CLIENT.events.aggregate(filter=EventFilter(type="test__py__sdk"))
     assert res_aggregate[0].count > 0
Пример #5
0
    def list(
        self,
        start_time: Union[Dict[str, Any], TimestampRange] = None,
        end_time: Union[Dict[str, Any], EndTimeFilter] = None,
        active_at_time: Union[Dict[str, Any], TimestampRange] = None,
        type: str = None,
        subtype: str = None,
        metadata: Dict[str, str] = None,
        asset_ids: List[int] = None,
        asset_external_ids: List[str] = None,
        root_asset_ids: List[int] = None,
        root_asset_external_ids: List[str] = None,
        asset_subtree_ids: List[int] = None,
        asset_subtree_external_ids: List[str] = None,
        data_set_ids: List[int] = None,
        data_set_external_ids: List[str] = None,
        source: str = None,
        created_time: Union[Dict[str, Any], TimestampRange] = None,
        last_updated_time: Union[Dict[str, Any], TimestampRange] = None,
        external_id_prefix: str = None,
        sort: List[str] = None,
        partitions: int = None,
        limit: int = 25,
    ) -> EventList:
        """`List events <https://docs.cognite.com/api/v1/#operation/advancedListEvents>`_

        Args:
            start_time (Union[Dict[str, Any], TimestampRange]): Range between two timestamps.
            end_time (Union[Dict[str, Any], TimestampRange]): Range between two timestamps.
            type (str): Type of the event, e.g 'failure'.
            subtype (str): Subtype of the event, e.g 'electrical'.
            metadata (Dict[str, str]): Customizable extra data about the event. String key -> String value.
            asset_ids (List[int]): Asset IDs of related equipments that this event relates to.
            asset_external_ids (List[str]): Asset External IDs of related equipment that this event relates to.
            root_asset_ids (List[int]): The IDs of the root assets that the related assets should be children of.
            root_asset_external_ids (List[str]): The external IDs of the root assets that the related assets should be children of.
            asset_subtree_ids (List[int]): List of asset subtrees ids to filter on.
            asset_subtree_external_ids (List[str]): List of asset subtrees external ids to filter on.
            data_set_ids (List[int]): Return only events in the specified data sets with these ids.
            data_set_external_ids (List[str]): Return only events in the specified data sets with these external ids.
            source (str): The source of this event.
            created_time (Union[Dict[str, int], TimestampRange]):  Range between two timestamps. Possible keys are `min` and `max`, with values given as time stamps in ms.
            last_updated_time (Union[Dict[str, int], TimestampRange]):  Range between two timestamps. Possible keys are `min` and `max`, with values given as time stamps in ms.
            external_id_prefix (str): External Id provided by client. Should be unique within the project.
            sort (List[str]): Sort by array of selected fields. Ex: ["startTime:desc']. Default sort order is asc when ommitted. Filter accepts following field names: startTime, endTime, createdTime, lastUpdatedTime. We only support 1 field for now.
            partitions (int): Retrieve events in parallel using this number of workers. Also requires `limit=None` to be passed.
            limit (int, optional): Maximum number of events to return. Defaults to 25. Set to -1, float("inf") or None
                to return all items.

        Returns:
            EventList: List of requested events

        Examples:

            List events and filter on max start time::

                >>> from cognite.client import CogniteClient
                >>> c = CogniteClient()
                >>> event_list = c.events.list(limit=5, start_time={"max": 1500000000})

            Iterate over events::

                >>> from cognite.client import CogniteClient
                >>> c = CogniteClient()
                >>> for event in c.events:
                ...     event # do something with the event

            Iterate over chunks of events to reduce memory load::

                >>> from cognite.client import CogniteClient
                >>> c = CogniteClient()
                >>> for event_list in c.events(chunk_size=2500):
                ...     event_list # do something with the events
        """
        if (root_asset_ids and not isinstance(root_asset_ids[0], dict)
            ) or root_asset_external_ids:
            root_asset_ids = self._process_ids(root_asset_ids,
                                               root_asset_external_ids,
                                               wrap_ids=True)
        if asset_subtree_ids or asset_subtree_external_ids:
            asset_subtree_ids = self._process_ids(asset_subtree_ids,
                                                  asset_subtree_external_ids,
                                                  wrap_ids=True)
        if data_set_ids or data_set_external_ids:
            data_set_ids = self._process_ids(data_set_ids,
                                             data_set_external_ids,
                                             wrap_ids=True)
        if end_time and ("max" in end_time
                         or "min" in end_time) and "isNull" in end_time:
            raise ValueError("isNull cannot be used with min or max values")

        filter = EventFilter(
            start_time=start_time,
            end_time=end_time,
            active_at_time=active_at_time,
            metadata=metadata,
            asset_ids=asset_ids,
            asset_external_ids=asset_external_ids,
            root_asset_ids=root_asset_ids,
            asset_subtree_ids=asset_subtree_ids,
            source=source,
            data_set_ids=data_set_ids,
            created_time=created_time,
            last_updated_time=last_updated_time,
            external_id_prefix=external_id_prefix,
            type=type,
            subtype=subtype,
        ).dump(camel_case=True)
        return self._list(method="POST",
                          limit=limit,
                          filter=filter,
                          partitions=partitions,
                          sort=sort)
Пример #6
0
    def __call__(
        self,
        chunk_size: int = None,
        start_time: Union[Dict[str, Any], TimestampRange] = None,
        end_time: Union[Dict[str, Any], EndTimeFilter] = None,
        active_at_time: Union[Dict[str, Any], TimestampRange] = None,
        type: str = None,
        subtype: str = None,
        metadata: Dict[str, str] = None,
        asset_ids: List[int] = None,
        asset_external_ids: List[str] = None,
        root_asset_ids: List[int] = None,
        root_asset_external_ids: List[str] = None,
        asset_subtree_ids: List[int] = None,
        asset_subtree_external_ids: List[str] = None,
        data_set_ids: List[int] = None,
        data_set_external_ids: List[str] = None,
        source: str = None,
        created_time: Union[Dict[str, Any], TimestampRange] = None,
        last_updated_time: Union[Dict[str, Any], TimestampRange] = None,
        external_id_prefix: str = None,
        sort: List[str] = None,
        limit: int = None,
    ) -> Generator[Union[Event, EventList], None, None]:
        """Iterate over events

        Fetches events as they are iterated over, so you keep a limited number of events in memory.

        Args:
            chunk_size (int, optional): Number of events to return in each chunk. Defaults to yielding one event a time.
            start_time (Union[Dict[str, Any], TimestampRange]): Range between two timestamps
            end_time (Union[Dict[str, Any], TimestampRange]): Range between two timestamps
            type (str): Type of the event, e.g 'failure'.
            subtype (str): Subtype of the event, e.g 'electrical'.
            metadata (Dict[str, str]): Customizable extra data about the event. String key -> String value.
            asset_ids (List[int]): Asset IDs of related equipments that this event relates to.
            asset_external_ids (List[str]): Asset External IDs of related equipment that this event relates to.
            root_asset_ids (List[int]): The IDs of the root assets that the related assets should be children of.
            root_asset_external_ids (List[str]): The external IDs of the root assets that the related assets should be children of.
            asset_subtree_ids (List[int]): List of asset subtrees ids to filter on.
            asset_subtree_external_ids (List[str]): List of asset subtrees external ids to filter on.
            data_set_ids (List[int]): Return only events in the specified data sets with these ids.
            data_set_external_ids (List[str]): Return only events in the specified data sets with these external ids.
            source (str): The source of this event.
            created_time (Union[Dict[str, int], TimestampRange]):  Range between two timestamps. Possible keys are `min` and `max`, with values given as time stamps in ms.
            last_updated_time (Union[Dict[str, int], TimestampRange]):  Range between two timestamps. Possible keys are `min` and `max`, with values given as time stamps in ms.
            external_id_prefix (str): External Id provided by client. Should be unique within the project
            sort (List[str]): Sort by array of selected fields. Ex: ["startTime:desc']. Default sort order is asc when ommitted. Filter accepts following field names: startTime, endTime, createdTime, lastUpdatedTime. We only support 1 field for now.
            limit (int, optional): Maximum number of events to return. Defaults to return all items.

        Yields:
            Union[Event, EventList]: yields Event one by one if chunk is not specified, else EventList objects.
        """
        if (root_asset_ids and not isinstance(root_asset_ids[0], dict)
            ) or root_asset_external_ids:
            root_asset_ids = self._process_ids(root_asset_ids,
                                               root_asset_external_ids,
                                               wrap_ids=True)
        if asset_subtree_ids or asset_subtree_external_ids:
            asset_subtree_ids = self._process_ids(asset_subtree_ids,
                                                  asset_subtree_external_ids,
                                                  wrap_ids=True)
        if data_set_ids or data_set_external_ids:
            data_set_ids = self._process_ids(data_set_ids,
                                             data_set_external_ids,
                                             wrap_ids=True)

        filter = EventFilter(
            start_time=start_time,
            end_time=end_time,
            active_at_time=active_at_time,
            metadata=metadata,
            asset_ids=asset_ids,
            asset_external_ids=asset_external_ids,
            root_asset_ids=root_asset_ids,
            asset_subtree_ids=asset_subtree_ids,
            data_set_ids=data_set_ids,
            source=source,
            created_time=created_time,
            last_updated_time=last_updated_time,
            external_id_prefix=external_id_prefix,
            type=type,
            subtype=subtype,
        ).dump(camel_case=True)
        return self._list_generator(method="POST",
                                    chunk_size=chunk_size,
                                    filter=filter,
                                    limit=limit,
                                    sort=sort)
Пример #7
0
    def list(
        self,
        start_time: Dict[str, Any] = None,
        end_time: Dict[str, Any] = None,
        type: str = None,
        subtype: str = None,
        metadata: Dict[str, Any] = None,
        asset_ids: List[int] = None,
        root_asset_ids: List[int] = None,
        root_asset_external_ids: List[str] = None,
        source: str = None,
        created_time: Dict[str, Any] = None,
        last_updated_time: Dict[str, Any] = None,
        external_id_prefix: str = None,
        partitions: int = None,
        limit: int = 25,
    ) -> EventList:
        """List events

        Args:
            start_time (Dict[str, Any]): Range between two timestamps.
            end_time (Dict[str, Any]): Range between two timestamps.
            type (str): Type of the event, e.g 'failure'.
            subtype (str): Subtype of the event, e.g 'electrical'.
            metadata (Dict[str, Any]): Customizable extra data about the event. String key -> String value.
            asset_ids (List[int]): Asset IDs of related equipments that this event relates to.
            root_asset_ids (List[int]): The IDs of the root assets that the related assets should be children of.
            root_asset_external_ids (List[str]): The external IDs of the root assets that the related assets should be children of.
            source (str): The source of this event.
            created_time (Dict[str, Any]): Range between two timestamps.
            last_updated_time (Dict[str, Any]): Range between two timestamps.
            external_id_prefix (str): External Id provided by client. Should be unique within the project.
            partitions (int): Retrieve events in parallel using this number of workers. Also requires `limit=None` to be passed.
            limit (int, optional): Maximum number of events to return. Defaults to 25. Set to -1, float("inf") or None
                to return all items.

        Returns:
            EventList: List of requested events

        Examples:

            List events and filter on max start time::

                >>> from cognite.client import CogniteClient
                >>> c = CogniteClient()
                >>> file_list = c.events.list(limit=5, start_time={"max": 1500000000})

            Iterate over events::

                >>> from cognite.client import CogniteClient
                >>> c = CogniteClient()
                >>> for event in c.events:
                ...     event # do something with the event

            Iterate over chunks of events to reduce memory load::

                >>> from cognite.client import CogniteClient
                >>> c = CogniteClient()
                >>> for event_list in c.events(chunk_size=2500):
                ...     event_list # do something with the files
        """
        if (root_asset_ids and not isinstance(root_asset_ids[0], dict)
            ) or root_asset_external_ids:
            root_asset_ids = self._process_ids(root_asset_ids,
                                               root_asset_external_ids,
                                               wrap_ids=True)

        filter = EventFilter(
            start_time=start_time,
            end_time=end_time,
            metadata=metadata,
            asset_ids=asset_ids,
            root_asset_ids=root_asset_ids,
            source=source,
            created_time=created_time,
            last_updated_time=last_updated_time,
            external_id_prefix=external_id_prefix,
            type=type,
            subtype=subtype,
        ).dump(camel_case=True)
        return self._list(method="POST",
                          limit=limit,
                          filter=filter,
                          partitions=partitions)