def searches(): """ Lists existing cursors on the Ariel DB, including ones that have completed execution """ # HTTP GET to /api/ariel/searches response = qpylib.REST('GET', ARIEL_SEARCHES_ENDPOINT, headers=JSON_HEADERS).json() # Return the response return json.dumps(response)
def search_info(): """ Gets information about a cursor, such as status, progress and other meta data """ # Get cursor ID cursor_id = request.args.get('cursor_id') # HTTP GET to /api/ariel/searches/CURSOR_ID response = qpylib.REST('GET', '{0}/{1}'.format(ARIEL_SEARCHES_ENDPOINT, cursor_id), headers=JSON_HEADERS).json() # Return the response return json.dumps(response)
def results(): """ Retrieves the results of a cursor """ # Get cursor ID cursor_id = request.args.get('cursor_id') # HTTP GET to /api/ariel/searches/CURSOR_ID/results response = qpylib.REST('GET', '{0}/{1}/results'.format(ARIEL_SEARCHES_ENDPOINT, cursor_id), headers=JSON_HEADERS).json() # Return the response return json.dumps(response)
def poll(): """ Repeatedly call the Ariel API to check if a cursor has finished processing if it has, retrieve and return the results Poll only as long as the timeout defined """ # Get cursor ID cursor_id = request.args.get('cursor_id') # Start time that the polling began at init_time = time.time() while init_time + TIMEOUT_MILLISECONDS > time.time(): # While within the timeout # Poll with an HTTP GET request to the Ariel searches endpoint specifying # a cursor to retrieve the information of # /api/ariel/searches/CURSOR_ID response = qpylib.REST('GET', '{0}/{1}'.format(ARIEL_SEARCHES_ENDPOINT, cursor_id), headers=JSON_HEADERS).json() if 'http_response' in response: # If there's an 'http_response' attribute in the response # the request has failed, output the response and error return json.dumps(response) if response['status'] == 'COMPLETED': # If the status of the query is COMPLETED, the results can now be retrieved # Make an HTTP GET request to the Ariel searches endpoint specifying # a cursor to retrieve the results of # /api/ariel/searches/CURSOR_ID/results response = qpylib.REST('GET', '{0}/{1}/results'.format( ARIEL_SEARCHES_ENDPOINT, cursor_id), headers=JSON_HEADERS).json() # Return the results return json.dumps(response) # Wait for 1 second before polling again to avoid spamming the API time.sleep(1) # If the polling has timed out, return an error return json.dumps(TIMEOUT_RESPONSE)
def search(): """ Creates a new cursor with the query provided, returns a cursor ID to allow further cursor interaction, such as retrieving results """ # Get cursor ID query = request.args.get('query') # Parameter of ?query_expression=QUERY params = {'query_expression': query} # HTTP POST to /api/ariel/searches?query_expression=QUERY response = qpylib.REST('POST', ARIEL_SEARCHES_ENDPOINT, headers=JSON_HEADERS, params=params).json() # Return the response return json.dumps(response)
def delete(): """ Delete an Ariel cursor, removing it's results """ # Get the cursor ID from the request to this endpoint # /delete?cursor_id=CURSOR_ID cursor_id = request.args.get('cursor_id') # Make an HTTP DELETE request to the Ariel searches endpoint specifying # a cursor to delete # /api/ariel/searches/CURSOR_ID response = qpylib.REST('DELETE', '{0}/{1}'.format(ARIEL_SEARCHES_ENDPOINT, cursor_id), headers=JSON_HEADERS) if response.ok: # If the response is HTTP 200 OK # The request has been successful return json.dumps(SUCCESS_RESPONSE) # Otherwise the request has failed return json.dumps(FAILURE_RESPONSE)
def save_results(): """ Update an Ariel cursor to ensure that it's results are saved """ # Get cursor ID cursor_id = request.args.get('cursor_id') # Parameter of ?save_results=true params = {'save_results': 'true'} # Make an HTTP POST request to the Ariel searches endpoint specifying # a cursor to update to have the results of of it's search saved # /api/ariel/searches/CURSOR_ID?save_results=true response = qpylib.REST('POST', '{0}/{1}'.format(ARIEL_SEARCHES_ENDPOINT, cursor_id), headers=JSON_HEADERS, params=params) if response.ok: # Successful request return json.dumps(SUCCESS_RESPONSE) # Failed request return json.dumps(FAILURE_RESPONSE)
def cancel(): """ Mark an Ariel cursor as canceled, stopping it from processing further but retaining it's results """ # Get cursor ID cursor_id = request.args.get('cursor_id') # Parameter of ?status=CANCELED params = {'status': 'CANCELED'} # Make an HTTP POST request to the Ariel searches endpoint specifying # a cursor to update the status of to 'CANCELED' # /api/ariel/searches/CURSOR_ID?status=CANCELED response = qpylib.REST('POST', '{0}/{1}'.format(ARIEL_SEARCHES_ENDPOINT, cursor_id), headers=JSON_HEADERS, params=params) if response.ok: # Successful request return json.dumps(SUCCESS_RESPONSE) # Failed request return json.dumps(FAILURE_RESPONSE)
def progress(): """ Gets information about a cursor and returns details on the cursor status and progress of execution """ # Get cursor ID cursor_id = request.args.get('cursor_id') # HTTP GET to /api/ariel/searches/CURSOR_ID response = qpylib.REST('GET', '{0}/{1}'.format(ARIEL_SEARCHES_ENDPOINT, cursor_id), headers=JSON_HEADERS).json() if 'http_response' in response: # Request failed return json.dumps(response) # Request successful # Parse the response from the API for status and progress and # return them as a new JSON object return json.dumps({ 'status': str(response['status']), 'progress': str(response['progress']) })
def pollFoLatencyMetrics(): FN_NAME = 'pollFoLatencyMetrics' qpylib.log('Entered ' + FN_NAME, 'info') # get the results ... # firstly get a search ID qpylib.log(FN_NAME + ': Using search:' + gPoll_metrics_query, 'info') # Parameter of ?query_expression=QUERY params = {'query_expression': gPoll_metrics_query} # HTTP POST to /api/ariel/searches?query_expression=QUERY response = qpylib.REST('POST', ARIEL_SEARCHES_ENDPOINT, headers=JSON_HEADERS, params=params).json() time.sleep(0.5) # just in case #Check the respoonse code is not 401 if 'http_response' in response: if response['code'] == 401: qpylib.log(FN_NAME + ': Problem in the response. 401 returned', 'info') return FAILURE_RESPONSE # then something wrong #else: # qpylib.log(FN_NAME+': Problem with HTTP response. No http response found/returned.','info') # qpylib.log(FN_NAME+': Actual Response: '+json.dumps(response),'info') # return FAILURE_RESPONSE # Returns the response qpylib.log( FN_NAME + ': Search Query ID Response: ' + json.dumps(response), 'info') # We want the field in the JSON ... "search_id": "10094c6f-6190-419f-9f69-b4743a55ea52", qpylib.log(FN_NAME + ': Found search ID:' + response['search_id'], 'info') # then poll for the results search_id = response['search_id'] # Start time that the polling began at init_time = time.time() while init_time + TIMEOUT_MILLISECONDS > time.time(): # While within the timeout # Poll with an HTTP GET request to the Ariel searches endpoint specifying # a search to retrieve the information of # /api/ariel/searches/SEARCH_ID response = qpylib.REST('GET', '{0}/{1}'.format(ARIEL_SEARCHES_ENDPOINT, search_id), headers=JSON_HEADERS).json() if 'http_response' in response: # If there's an 'http_response' attribute in the response # the request has failed, output the response and error return json.dumps(response) if response['status'] == 'COMPLETED': # If the status of the query is COMPLETED, the results can now be retrieved # Make an HTTP GET request to the Ariel searches endpoint specifying # a search to retrieve the results of # /api/ariel/searches/SEARCH_ID/results final_response = qpylib.REST('GET', '{0}/{1}/results'.format( ARIEL_SEARCHES_ENDPOINT, search_id), headers=JSON_HEADERS).json() qpylib.log( FN_NAME + ': Output for search for search ID:' + response['search_id'] + ' is :' + json.dumps(final_response), 'info') # Return the results return final_response # Wait for 1 second before polling again to avoid spamming the API time.sleep(1) # If the polling has timed out, return an error qpylib.log( FN_NAME + ': Output for search for search ID:' + response['search_id'] + ' is :' + TIMEOUT_RESPONSE, 'info') return TIMEOUT_RESPONSE #-------------------------------------------------------------------------------