def g():
     yield {
         'timestamp':
         datetime_to_timestamp_ms(
             datetime.combine(date.fromisoformat('1920-01-02'), time(),
                              timezone.utc)),
         'value':
         'Isaac Asimov',
     }
Example #2
0
 def get(
     self,
     ts_id: Union[TimeSeriesVertex, TIME_SERIES_ID_T],
     start: Optional[datetime] = None,
     end: Optional[datetime] = None
 ) -> Generator[TimeSeriesValue, None, None]:
     e_ts_id: str
     if isinstance(ts_id, TimeSeriesVertex):
         e_ts_id = ts_id.id
     elif isinstance(ts_id, TimeSeriesId):
         e_ts_id = str(ts_id)
     elif isinstance(ts_id, str):
         e_ts_id = ts_id
     else:
         raise TypeError(type(ts_id))
     items = self.__data_client.get(e_ts_id,
                                    datetime_to_timestamp_ms(start),
                                    datetime_to_timestamp_ms(end))
     transformer = data_to_model(items)
     yield from transformer
Example #3
0
    async def get_async_ts_values(
        self,
        vertex_id: VERTEX_ID_T_co,
        start: datetime,
        end: datetime,
        include_deleted: Optional[bool] = None,
        headers: Optional[Mapping[str, str]] = None,
    ) -> AsyncGenerator[TimeSeriesValue, None]:
        # <editor-fold name="effective headers">
        e_headers = {str(SystemAttribute.OGIT__ID): vertex_id}
        if include_deleted is not None:
            if isinstance(include_deleted, bool):
                e_headers['includeDeleted'] = str(include_deleted).lower()
            else:
                raise TypeError(type(include_deleted))

        if start is not None:
            if isinstance(start, datetime):
                e_headers['from'] = datetime_to_timestamp_ms(start)
            else:
                raise TypeError(type(include_deleted))

        if end is not None:
            if isinstance(start, datetime):
                e_headers['to'] = datetime_to_timestamp_ms(end)
            else:
                raise TypeError(type(include_deleted))

        if headers is not None:
            if isinstance(headers, Mapping):
                e_headers.update(headers)
            else:
                raise TypeError(type(headers))
        # </editor-fold>

        request = TimeSeriesGetValuesRequest(headers=e_headers, body={})

        async_generator = self.submit_request(request)
        async for value in async_generator:
            yield TimeSeriesValue(**value)
 def test_ts_add_rest(self, client: HiroClient, empty_ts_vertex: Vertex):
     storage = client.rest.storage
     body = json.dumps({
         'timestamp':
         datetime_to_timestamp_ms(
             datetime.combine(date.fromisoformat('1920-01-02'), time(),
                              timezone.utc)),
         'value':
         'Isaac Asimov',
     })
     res = storage.ts.add(empty_ts_vertex.id, body)
     res.raise_for_status()
     assert isinstance(res, Response)
Example #5
0
    def history(
        self,
        vertex_id: Union[VERTEX_T_co, VERTEX_ID_T_co, VERTEX_XID_T_co],
        start: Optional[datetime] = None,
        end: Optional[datetime] = None,
        offset: Optional[int] = None,
        limit: Optional[int] = None,
        res_format: Optional[HistoryFormat] = None,
        version: Optional[int] = None,
        params: Optional[Mapping[str, str]] = None,
        headers: Optional[Mapping[str, str]] = None
    ) -> Generator[Union[HistoryEntry, HistoryDiff, Vertex], None, None]:
        e_vertex_id = vertex_id_to_str(vertex_id)
        e_start = datetime_to_timestamp_ms(start) if isinstance(
            start, datetime) else None
        e_end = datetime_to_timestamp_ms(end) if isinstance(end,
                                                            datetime) else None
        e_format = res_format.value if isinstance(res_format,
                                                  HistoryFormat) else None
        if res_format is None or res_format is HistoryFormat.ELEMENT:

            def transform(src_item: Dict[str, Any]) -> Vertex:
                return Vertex(src_item, self.__base_client,
                              False)  # frozen=True
        elif res_format is HistoryFormat.DIFF:

            def transform_map(
                    mapping: Mapping[str, Any]) -> Dict[Attribute, Any]:
                result = {}
                for k, v in mapping.items():
                    a = to_attribute(k)
                    result[a] = v
                return result

            def transform(src_item: Dict[str, Any]) -> HistoryDiff:
                return HistoryDiff(added=MappingProxyType(
                    transform_map(src_item['add'])),
                                   replaced=MappingProxyType(
                                       transform_map(src_item['replace'])),
                                   removed=MappingProxyType(
                                       transform_map(src_item['remove'])))
        elif res_format is HistoryFormat.FULL:

            def transform(src_item: Dict[str, Any]) -> HistoryEntry:
                data_id = VertexId(src_item['identity'])
                action = HistoryAction[src_item['action']]
                data = to_vertex(src_item['data'], self.__base_client)
                meta = HistoryMeta(**src_item['meta'])
                return HistoryEntry(data_id, action, data, meta)
        elif not isinstance(res_format, HistoryFormat):
            raise TypeError(type(res_format))
        else:
            raise RuntimeError('Unreachable')

        for item in self.__data_client.history(e_vertex_id,
                                               start=e_start,
                                               end=e_end,
                                               offset=offset,
                                               limit=limit,
                                               res_format=e_format,
                                               version=version,
                                               params=params,
                                               headers=headers):
            yield transform(item)