def __init__(self, start=None, end=None): self.start = to_nanoseconds(start) if start else None self.end = to_nanoseconds(end) if end else None if self.start is None and self.end is None: raise BTRDBValueError("A valid `start` or `end` must be supplied") if self.start is not None and self.end is not None and self.start >= self.end: raise BTRDBValueError( "`start` must be strictly less than `end` argument")
def current(self): """ Returns the points of data in the streams closest to the current timestamp. If the current timestamp is outside of the filtered range of data, a ValueError is raised. Parameters ---------- None Returns ------- tuple The latest points of data found among all streams """ latest = [] params = self._params_from_filters() now = currently_as_ns() end = params.get("end", None) start = params.get("start", None) if (end is not None and end <= now) or (start is not None and start > now): raise BTRDBValueError( "current time is not included in filtered stream range") for s in self._streams: version = self.versions()[s.uuid] point, _ = s.nearest(now, version=version, backward=True) latest.append(point) return tuple(latest)
def _update_tags_collection(self, tags, collection): tags = self.tags() if tags is None else tags collection = self.collection if collection is None else collection if collection is None: raise BTRDBValueError( "collection must be provided to update tags or collection") self._btrdb.ep.setStreamTags(uu=self.uuid, expected=self._property_version, tags=tags, collection=collection)
def update(self, tags=None, annotations=None, collection=None, encoder=AnnotationEncoder, replace=False): """ Updates metadata including tags, annotations, and collection as an UPSERT operation. By default, the update will only affect the keys and values in the specified tags and annotations dictionaries, inserting them if they don't exist, or updating the value for the key if it does exist. If any of the update arguments (i.e. tags, annotations, collection) are None, they will remain unchanged in the database. To delete either tags or annotations, you must specify exactly which keys and values you want set for the field and set `replace=True`. For example: >>> annotations, _ = stream.anotations() >>> del annotations["key_to_delete"] >>> stream.update(annotations=annotations, replace=True) This ensures that all of the keys and values for the annotations are preserved except for the key to be deleted. Parameters ----------- tags : dict, optional Specify the tag key/value pairs as a dictionary to upsert or replace. If None, the tags will remain unchanged in the database. annotations : dict, optional Specify the annotations key/value pairs as a dictionary to upsert or replace. If None, the annotations will remain unchanged in the database. collection : str, optional Specify a new collection for the stream. If None, the collection will remain unchanged. encoder : json.JSONEncoder or None JSON encoder class to use for annotation serialization. Set to None to prevent JSON encoding of the annotations. replace : bool, default: False Replace all annotations or tags with the specified dictionaries instead of performing the normal upsert operation. Specifying True is the only way to remove annotation keys. Returns ------- int The version of the metadata (separate from the version of the data) also known as the "property version". """ if tags is None and annotations is None and collection is None: raise BTRDBValueError( "you must supply a tags, annotations, or collection argument") if tags is not None and isinstance(tags, dict) is False: raise BTRDBTypeError("tags must be of type dict") if annotations is not None and isinstance(annotations, dict) is False: raise BTRDBTypeError("annotations must be of type dict") if collection is not None and isinstance(collection, str) is False: raise InvalidCollection("collection must be of type string") if tags is not None or collection is not None: self._update_tags_collection(tags, collection) self.refresh_metadata() if annotations is not None: self._update_annotations(annotations, encoder, replace) self.refresh_metadata() return self._property_version