def table_create(self, data): """ Create a table :param data: data to store :type data: dict :returns: the name of the created table :rtype: str :raises IkatsInputError: for any error present in the inputs :raises IkatsConflictError: if table already exist :raises IkatsException: for any other error during the request """ response = self.send(root_url=self.session.dm_url + self.root_url, verb=GenericClient.VERB.POST, template=TEMPLATES['create_table'], json_data=data, files=None) is_400(response=response, msg=response.json) is_409(response=response, msg="Table %s already exist in database" % data['table_desc']['name']) is_4xx(response, "Unexpected client error : {code}") is_5xx(response, "Unexpected server error : {code}") return response.json
def table_list(self, name=None, strict=True): """ List all tables If name is specified, filter by name name can contains "*", this character is considered as "any chars" (equivalent to regexp /.*/) :param name: name to find :param strict: consider name without any wildcards :type name: str or None :type strict: bool :returns: the list of tables matching the requirements :rtype: list :raises IkatsInputError: for any error present in the inputs :raises IkatsException: for any other error during the request """ response = self.send(root_url=self.session.dm_url + self.root_url, verb=GenericClient.VERB.GET, template=TEMPLATES['table_list'], uri_params={ 'name': name, 'strict': strict }, data=None, files=None) is_5xx(response, "Unexpected server error : {code}") return response.json
def rid_create(self, data, pid, name=None): """ Push new data to be considered as a result of the run identified by *pid* :param pid: process id to store information to :param data: data to store :param name: (option) label of the data :type pid: str or int :type data: object :type name: str or None :return: the RID of created result :rtype: int """ response = self.send(root_url=self.session.dm_url + self.root_url, verb=GenericClient.VERB.POST, template=TEMPLATES["rid_add"], data=data, uri_params={ 'process_id': pid, 'name': name, }) is_4xx(response, "Unexpected client error : {code}") is_5xx(response, "Unexpected server error : {code}") try: rid = int(response.text) return rid except ValueError: raise IkatsException("response couldn't be parsed correctly %s" % response)
def table_read(self, name): """ Reads the data blob content: for the unique table identified by id. :param name: the name of the raw table to get data from :type name: str :returns: the content data stored. :rtype: dict :raises IkatsNotFoundError: no resource identified by ID :raises IkatsException: any other error """ response = self.send(root_url=self.session.dm_url + self.root_url, verb=GenericClient.VERB.GET, template=TEMPLATES['table_read'], uri_params={'name': name}, data=None, files=None) is_400(response=response, msg="Wrong input: [%s]" % name) is_404(response=response, msg="Table %s not found" % name) is_4xx(response, "Unexpected client error : {code}") is_5xx(response, "Unexpected server error : {code}") return response.json
def table_delete(self, name): """ Delete a table :param name: the name of the table to delete :type name: str """ response = self.send(root_url=self.session.dm_url + self.root_url, verb=GenericClient.VERB.DELETE, template=TEMPLATES['table_delete'], uri_params={'name': name}) is_400(response, msg="Wrong input: [%s]" % name) is_404(response, msg="Table %s not found" % name) is_4xx(response, "Unexpected client error : {code}") is_5xx(response, "Unexpected server error : {code}")
def get_implementation_list(self): """ Get the list of all implementations from database :returns: the list of implementations :rtype: list """ response = self.send(root_url=self.session.catalog_url + self.root_url, verb=GenericClient.VERB.GET, template=TEMPLATES['implem_list']) if response.status_code == 200: return response.json if response.status_code == 404: return [] is_4xx(response, "Unexpected client error : {code}") is_5xx(response, "Unexpected server error : {code}") return []
def add_points(self, tsuid, data): """ :param tsuid: TSUID to use :param data: points as array :type tsuid: str :type data: list :returns: the start_date, end_date, nb_points """ metric, tags = self._get_metric_tags_from_tsuid(tsuid=tsuid) # Building body with points json_data = [] for point in data: json_data.append({ "metric": metric, "timestamp": str(point[0]).zfill(13), "value": point[1], "tags": tags }) response = self.send(root_url=self.session.tsdb_url, verb=GenericClient.VERB.POST, template=TEMPLATES['add_points'], data=json.dumps(json_data)) if "success" in response.data and response.data["success"] != len( data): self.session.log.debug(response.data) raise IkatsServerError( "Database wrote only %s points out of %s %s" % (response.data["success"], len(data), response.data)) is_4xx(response, "Unexpected client error: {code}") is_5xx(response, "Unexpected server error: {code}") return data[0][0], data[-1][0], len(data)
def rid_delete(self, rid, raise_exception=True): """ Delete a metadata Corresponding web app resource operation: **removeMetaData** :param rid: Result ID of the data to delete :param raise_exception: (optional) Indicates if Ikats exceptions shall be raised (True, default) or not (False) :type rid: str or int :type raise_exception: bool :returns: the status of the action :rtype: bool :raises TypeError: if *rid* not a str nor int :raises IkatsNotFoundError: if *rid* doesn't exist """ # Checks inputs check_type(value=rid, allowed_types=[str, int], var_name="rid", raise_exception=True) response = self.send(root_url=self.session.dm_url + self.root_url, verb=GenericClient.VERB.DELETE, template=TEMPLATES['rid_delete'], uri_params={'rid': rid}) try: is_404(response, "RID %s not found" % rid) is_4xx(response, msg="Unexpected client error : {code}") is_5xx(response, msg="Unexpected server error : {code}") except IkatsException: if raise_exception: raise return False return True
def get_implementation(self, name): """ Get the implementations matching the name from database :param name: identifier of the implementation to get :type name: str :returns: the dict of implementation :rtype: dict """ response = self.send(root_url=self.session.catalog_url + self.root_url, verb=GenericClient.VERB.GET, template=TEMPLATES['implem'], uri_params={"name": name}) if response.status_code == 200: return response.json is_404(response, "No implementation found matching " + name) is_4xx(response, "Unexpected client error : {code}") is_5xx(response, "Unexpected server error : {code}") raise IkatsException("Something wrong happened")
def metadata_delete(self, tsuid, name, raise_exception=True): """ Delete a metadata Corresponding web app resource operation: **removeMetaData** :param tsuid: TSUID of the Timeseries where is bound the metadata :param name: Metadata name :param raise_exception: (optional) Indicates if Ikats exceptions shall be raised (True, default) or not (False) :type tsuid: str :type name: str :type raise_exception: bool :returns: the status of the action :rtype: bool :raises TypeError: if *tsuid* not a str :raises TypeError: if *name* not a str :raises ValueError: if *tsuid* is empty :raises ValueError: if *name* is empty :raises IkatsNotFoundError: if metadata doesn't exist """ # Checks inputs check_type(value=tsuid, allowed_types=str, var_name="tsuid", raise_exception=True) check_type(value=name, allowed_types=str, var_name="name", raise_exception=True) if tsuid == "": self.session.log.error("tsuid must not be empty") raise ValueError("tsuid must not be empty") if name == "": self.session.log.error("name must not be empty") raise ValueError("name must not be empty") response = self.send(root_url=self.session.dm_url + self.root_url, verb=GenericClient.VERB.DELETE, template=TEMPLATES['metadata_delete'], uri_params={ 'tsuid': tsuid, 'name': name, }) try: is_404(response, "Metadata '%s' not found for TS '%s'" % (name, tsuid)) is_4xx(response, msg="Unexpected client error : {code}") is_5xx(response, msg="Unexpected server error : {code}") except IkatsException: if raise_exception: raise return False return True