def execute(self, database_at_cluster, query, **kwargs): """Executes a query or management command. :param str database_at_cluster: name of database and cluster that a folder will be derived that contains all the files with the query results for this specific database. :param str query: Query to be executed. """ file_path = self._get_file_path(query, database_at_cluster) str_response = open(file_path, 'r').read() json_response = json.loads(str_response) if query.startswith('.') and json_response.get('tables') is not None: return KqlSchemaResponse(json_response) else: endpoint_version = self._get_endpoint_version(json_response) return KqlResponse(json_response, endpoint_version)
def execute(self, database_at_cluster, query, **options): """Executes a query or management command. :param str database_at_cluster: name of database and cluster that a folder will be derived that contains all the files with the query results for this specific database. :param str query: Query to be executed or a json file with query results. """ file_path = self._get_file_path(query, database_at_cluster, cache_folder=options.get("use_cache")) str_response = open(file_path, "r").read() json_response = json.loads(str_response) if query.startswith(".") and json_response.get("tables") is not None: return KqlSchemaResponse(json_response) else: endpoint_version = self._get_endpoint_version(json_response) return KqlQueryResponse(json_response, endpoint_version)
def execute(self, id: str, query: str, accept_partial_results: bool = False, **options) -> object: """ Execute a simple query or a metadata query Parameters ---------- id : str the workspaces (log analytics) or appid (application insights). query : str Query to be executed accept_partial_results : bool, optional Optional parameter. If query fails, but we receive some results, we consider results as partial. If this is True, results are returned to client, even if there are exceptions. If this is False, exception is raised. Default is False. oprions["timeout"] : float, optional Optional parameter. Network timeout in seconds. Default is no timeout. Returns ------- object KqlQueryResponse instnace if executed simple query request KqlSchemaResponse instnace if executed metadata request Raises ------ KqlError If request to draft failed. If response from draft contains exceptions. """ # # create API url # is_metadata = query == self._GET_SCHEMA_QUERY api_url = "{0}/{1}/{2}/{3}/{4}".format( self._data_source, self._API_VERSION, self._domain, id, "metadata" if is_metadata else "query") # # create Prefer header # prefer_list = [] if self._API_VERSION != "beta": prefer_list.append( "ai.response-thinning=false") # returns data as kusto v1 timeout = options.get("timeout") if timeout is not None: prefer_list.append("wait={0}".format(timeout)) # # create headers # request_headers = { "x-ms-client-version": "{0}.Python.Client:{1}".format(Constants.MAGIC_CLASS_NAME, self._WEB_CLIENT_VERSION), "x-ms-client-request-id": "{0}.execute;{1}".format(Constants.MAGIC_CLASS_NAME, str(uuid.uuid4())), } if self._aad_helper is not None: request_headers["Authorization"] = self._aad_helper.acquire_token() elif self._appkey is not None: request_headers["x-api-key"] = self._appkey if len(prefer_list) > 0: request_headers["Prefer"] = ", ".join(prefer_list) # # submit request # if is_metadata: response = requests.get(api_url, headers=request_headers) else: payload = {"query": query} response = requests.post(api_url, headers=request_headers, json=payload) # # handle response # if response.status_code != requests.codes.ok: # pylint: disable=E1101 raise KqlError([response.text], response) json_response = response.json() if is_metadata: kql_response = KqlSchemaResponse(json_response) else: kql_response = KqlQueryResponse(json_response) if kql_response.has_exceptions() and not accept_partial_results: raise KqlError(kql_response.get_exceptions(), response, kql_response) return kql_response
def execute(self, id, query: str, accept_partial_results=False, timeout=None): """ Execute a simple query or metadata query Parameters ---------- id : str the workspaces or apps id. query : str Query to be executed query_endpoint : str The query's endpoint accept_partial_results : bool Optional parameter. If query fails, but we receive some results, we consider results as partial. If this is True, results are returned to client, even if there are exceptions. If this is False, exception is raised. Default is False. timeout : float, optional Optional parameter. Network timeout in seconds. Default is no timeout. """ # https://api.applicationinsights.io/v1/apps/DEMO_APP/metadata?api_key=DEMO_KEY # https://api.loganalytics.io/v1/workspaces/DEMO_WORKSPACE/metadata?api_key=DEMO_KEY ismetadata = query.startswith('.') and query == ".show schema" request_payload = {"query": query} query_endpoint = "{0}/{1}/{2}/{3}/{4}".format( self.cluster, self._API_VERSION, self.domain, id, "metadata" if ismetadata else "query") # print('query_endpoint: ', query_endpoint) request_headers = { "x-ms-client-version": "{0}.Python.Client:{1}".format(Constants.MAGIC_CLASS_NAME, self._CLIENT_VERSION), "x-ms-client-request-id": "{0}PC.execute;{1}".format(Constants.MAGIC_CLASS_NAME[0], str(uuid.uuid4())), } if self.appkey is not None: request_headers["x-api-key"] = self.appkey else: request_headers["Authorization"] = self._aad_helper.acquire_token() # print('token: ' + request_headers["Authorization"]) prefer_list = [] if self._API_VERSION != "beta": prefer_list.append( "ai.response-thinning=false") # returns data as kusto v1 if timeout is not None: prefer_list.append("wait={0}".format(timeout)) if len(prefer_list) > 0: request_headers["Prefer"] = ", ".join(prefer_list) # print('request_headers: ', request_headers) response = requests.get( query_endpoint, headers=request_headers) if ismetadata else requests.post( query_endpoint, headers=request_headers, json=request_payload) if response.status_code == 200: json_response = response.json() # print('json_response:', json_response) query_response = KqlSchemaResponse( json_response) if ismetadata else KqlResponse(json_response) if query_response.has_exceptions() and not accept_partial_results: raise KqlError(query_response.get_exceptions(), response, query_response) return query_response else: raise KqlError([response.text], response)