Ejemplo n.º 1
0
    def _fetch(self, obj=None):
        """ fetch data from the server """
        if obj is None:
            try:
                if self.__space_name__:
                    obj = pychronos._space_api_instance.app_api_space_get(
                        self.__space_name__)
                elif self.__space_id__:
                    obj = pychronos._raw_space_api_instance.app_api_space_raw_get(
                        self.__space_id__)
                else:
                    ValueError("space_name or space_id are missing")

            except MaxRetryError:
                raise ConnectionError(
                    f"Couldn't establish connection with "
                    f"{pychronos._configuration.host} on port {pychronos._configuration.port}"
                ) from None

            except ApiException as e:
                api_exception_helper(e)
                # if e.status == 401:
                #     raise ConnectionError("Login failed; login again") from None
                #
                # elif e.status == 404:
                #     raise NameError("Space, {}, not found".format(self.__space_name__)) from None
                #
                # else:
                #     raise RuntimeError(f"Error: {e}") from None

        self.__space_id__ = obj.space_id
        self.__space_title__ = obj.title
        self.__space_description__ = obj.description
        self.__real_start__ = obj.real_start
        self.__real_end__ = obj.real_end
Ejemplo n.º 2
0
    def history(self, real_start: datetime = None, real_end: datetime = None):
        """fetch historical collection object"""
        if self.__real_end__ != TIME_INF:
            raise TypeError("historical object can't be modified")

        if real_start and not isinstance(real_start, datetime):
            raise TypeError("real time must be datetime object")

        if real_start and not isinstance(real_start, datetime):
            raise TypeError("real time must be datetime object")

        try:
            hist = pychronos._raw_space_api_instance.app_api_space_object_history(
                self.__space_id__, real_start=real_start, real_end=real_end)

        except ApiException as e:
            api_exception_helper(e)

        out = []
        for h in hist:
            c = Space(h.name)
            c._fetch(obj=h)
            out += [c]

        return out
Ejemplo n.º 3
0
    def create(name,
               title=None,
               description=None,
               tags=None,
               public: bool = None):
        """
        create a new space
        :return:
        """
        if public is None:
            visibility = None
        else:
            visibility = "public" if public else "private"

        body = _Space(name=name,
                      title=title,
                      description=description,
                      tags=tags,
                      visibility=visibility)
        try:
            obj = pychronos._space_api_instance.app_api_space_post(body)

        except ApiException as e:
            api_exception_helper(e)

        sp = Space(name)
        sp._fetch(obj)
        return sp
Ejemplo n.º 4
0
    def login(self, username, password):
        """Login to ChronosDB"""

        # basic_auth
        pychronos._configuration.username = username
        pychronos._configuration.password = password

        try:
            response = pychronos._user_api_instance.app_api_user_login(
                _request_timeout=pychronos._timeout)
            # pychronos._configuration.access_token = response.jwt
            self.set_token(response.jwt)

        except MaxRetryError:
            raise ConnectionError(
                f"Couldn't establish connection with {pychronos._chronos_host} "
                f"on port {pychronos._chronos_port}") from None

        except ApiException as e:
            api_exception_helper(e)
            #
            # if e.status == 401:
            #     raise ValueError("invalid username and/or password") from None

        finally:
            pychronos._configuration.username = ""
            pychronos._configuration.password = ""
Ejemplo n.º 5
0
    def __getitem__(self, name: Text) -> TimeSeries:
        """ get TimeSeries """
        if self.__real_end__ != TIME_INF:
            raise TypeError("historical object can't be modified")

        try:
            if bson.objectid.ObjectId.is_valid(name):
                obj = pychronos._raw_timeseries_api_instance.app_api_timeseries_raw_get(coll_id=self.__coll_id__,
                                                                                        tsid=str(name))
            else:
                obj = pychronos._timeseries_api_instance.app_api_timeseries_get(self.__space_name__,
                                                                                self.__coll_name__,
                                                                                name)

        except MaxRetryError:
            raise ConnectionError(
                f"Couldn't establish connection with {pychronos._configuration.host} on port "
                f"{pychronos._configuration.port}") from None

        except ApiValueError as e:
            raise ValueError(e)

        except ApiException as e:
            api_exception_helper(e)

        ts = TimeSeries(self.__space_name__, self.__coll_name__, name)
        ts._fetch(obj)
        return ts
Ejemplo n.º 6
0
    def save(self,
             series: pd.Series,
             method: Text = 'update',
             vintage: Vintage = None,
             realtime: datetime = None) -> Vintage:
        """ save observations """
        if method not in ("update", "append", "overwrite"):
            raise ValueError(
                "invalid save method; must be either 'update', 'overwrite', or 'append'"
            )

        series_data = series_to_SingleTimeSeriesData(series,
                                                     tsid=self.__tsid__,
                                                     coll_id=self.__coll_id__)

        # TODO validate
        if series_data.dtype != self.dtype:
            raise ValueError(
                f"invalid data type, {series_data.dtype}; {self.dtype} is required"
            )
        if series_data.itype != self.itype:
            raise ValueError(
                f"invalid index type, {series_data.itype}; {self.itype} is required"
            )
        if series_data.freq != self.freq:
            raise ValueError(
                f"invalid frequency, {series_data.freq}; {self.freq} is required"
            )

        if vintage:
            _vintage = VintageUpdate(name=vintage.name,
                                     description=vintage.description,
                                     metadata=vintage.metadata)
        else:
            _vintage = None

        body = _RawDataPutRequest(series=[series_data], vintage=_vintage)
        try:
            res = pychronos._raw_api_instance.app_api_raw_timeseries_data_put(
                coll_id=self.__coll_id__,
                method=method,
                raw_data_put_request=body,
                realtime=realtime)
        except MaxRetryError:
            raise ConnectionError(
                f"Couldn't establish connection with {pychronos._configuration.host} "
                f"on port {pychronos._configuration.port}") from None

        except ApiException as e:
            api_exception_helper(e)

        except Exception as e:
            raise RuntimeError(e)

        return Vintage._from_response(res.vintage)
Ejemplo n.º 7
0
    def list_spaces():
        """
        list spaces in the database
        :return:
        """
        # check if initiated correctly
        if pychronos._client_instance is None:
            raise ConnectionError("connection not initiated")

        try:
            response = pychronos._space_api_instance.app_api_space_get_list()
        except ApiException as e:
            print(pychronos._configuration.access_token)
            api_exception_helper(e)
            # print("Exception when calling listing spaces: %s\n" % e)

        return [x.name for x in response]
Ejemplo n.º 8
0
    def annotations(self):
        """ list annotations in a collection """
        if self.__real_end__ != TIME_INF:
            raise TypeError("historical object can't be modified")

        try:
            res = pychronos._raw_annot_api_instance.app_api_annotation_raw_get_list(self.__coll_id__)

        except MaxRetryError:
            raise ConnectionError(
                f"Couldn't establish connection with "
                f"{pychronos._configuration.host} on port {pychronos._configuration.port}") from None

        except ApiException as e:
            api_exception_helper(e)

        return pychronos.annotation.construct_annotations(self.__coll_id__, res)
Ejemplo n.º 9
0
    def list_collections(self):
        """ list collection names """
        if self.__real_end__ != TIME_INF:
            raise TypeError("historical object can't be modified")

        try:
            response = pychronos._collection_api_instance.app_api_collection_get_list(
                self.__space_name__)

        except MaxRetryError:
            raise ConnectionError(
                f"Couldn't establish connection with "
                f"{pychronos._configuration.host} on port {pychronos._configuration.port}"
            ) from None

        except ApiException as e:
            api_exception_helper(e)

        return [x.name for x in response]
Ejemplo n.º 10
0
def construct_annotations(coll_id, annots: List[_Annotation]):
    # fetch time series for all annotations
    tsids = [x.tsid for r in annots for x in r.targets if x.tsid]

    if len(tsids) > 0:
        try:
            ts = pychronos._raw_timeseries_api_instance.app_api_timeseries_raw_get_list(
                coll_id=coll_id, tsids=tsids)

            ts = {x.tsid: x for x in ts}
        except MaxRetryError:
            raise ConnectionError(
                f"Couldn't establish connection with "
                f"{pychronos._configuration.host} on port {pychronos._configuration.port}"
            ) from None

        except ApiException as e:
            api_exception_helper(e)

    out = []
    for _a in annots:
        a = pychronos.annotation.Annotation(coll_id=_a.coll_id, aid=_a.aid)

        a.__symbol__ = _a.symbol
        a.__text__ = _a.text
        a.___format__ = _a.format
        a.__targets__ = [
            pychronos.annotation.AnnotationTarget._from_response(
                coll_id=_a.coll_id,
                aid=_a.aid,
                tsid=x.tsid,
                index=x.index,
                freq=x.freq,
                real_start=x.real_start) for x in _a.targets
        ]

        a.__real_start__ = _a.real_start
        a.__real_end__ = _a.real_end
        out.append(a)

    return out
Ejemplo n.º 11
0
    def history(self, real_time=None):
        """ history of time series properties changes """
        if self.__real_end__ != TIME_INF:
            raise TypeError("historical object can't be modified")

        if real_time and not isinstance(real_time, datetime.datetime):
            raise TypeError("real time must be datetime object")

        try:
            hist = pychronos._raw_timeseries_api_instance.app_api_timeseries_object_history(
                self.__coll_id__, real_start=real_time)

        except ApiException as e:
            api_exception_helper(e)

        out = []
        for h in hist:
            c = TimeSeries(self.__space_name__, self.__coll_name__,
                           self.__ts_name__)
            c._fetch(obj=h)
            out += [c]

        return out
Ejemplo n.º 12
0
    def create(self, name,
               freq: Freq,
               dtype: DType,
               itype: IType = IType.period,
               fparams: Optional[dict] = None,
               dparams: Optional[dict] = None,
               unit=None,
               title=None,
               description=None,
               attributes=None,
               legend=None,
               entity=None,
               variable=None):
        """create time series"""
        if self.__real_end__ != TIME_INF:
            raise TypeError("historical object can't be modified")

        # check dtype
        if isinstance(dtype, DType):
            dtype = dtype.value
        elif isinstance(dtype, str):
            dtype = DType(dtype).value
        else:
            raise TypeError(f"invalid dtype")

        # check itype
        if isinstance(itype, IType):
            itype = itype.value
        elif isinstance(itype, str):
            itype = IType(itype).value
        else:
            raise TypeError(f"invalid itype")

        # check freq
        if isinstance(freq, Freq):
            freq = freq.value
        elif isinstance(freq, str):
            freq = Freq(freq).value
        else:
            raise TypeError(f"invalid freq")

        # POST
        try:
            body = _TimeSeries(name=name,
                               title=title,
                               dtype=dtype,
                               dparams=dparams,
                               itype=itype,
                               freq=freq,
                               fparams=fparams,
                               unit=unit,
                               description=description,
                               attributes=attributes,
                               legend=legend,
                               entity=entity,
                               variable=variable
                               )
        except ApiValueError as e:
            api_exception_helper(e)

        try:
            obj = pychronos._raw_timeseries_api_instance.app_api_timeseries_raw_post(coll_id=self.__coll_id__,
                                                                                     time_series=body)

        except MaxRetryError:
            raise ConnectionError(
                f"Couldn't establish connection with "
                f"{pychronos._configuration.host} on port {pychronos._configuration.port}") from None

        except ApiException as e:
            api_exception_helper(e)

        ts = TimeSeries._from_core(space_name=self.__space_name__,
                                   coll_name=self.__coll_name__,
                                   ts_name=name,
                                   core=obj)

        return ts