aboutsummaryrefslogtreecommitdiffstats
path: root/vnfs/DAaaS/lib/promql_api/prom_ql_api.py
diff options
context:
space:
mode:
Diffstat (limited to 'vnfs/DAaaS/lib/promql_api/prom_ql_api.py')
-rw-r--r--vnfs/DAaaS/lib/promql_api/prom_ql_api.py66
1 files changed, 62 insertions, 4 deletions
diff --git a/vnfs/DAaaS/lib/promql_api/prom_ql_api.py b/vnfs/DAaaS/lib/promql_api/prom_ql_api.py
index 8a5ce508..1dc578e9 100644
--- a/vnfs/DAaaS/lib/promql_api/prom_ql_api.py
+++ b/vnfs/DAaaS/lib/promql_api/prom_ql_api.py
@@ -23,7 +23,8 @@ import requests
from requests.exceptions import HTTPError
-API_VERSION = '/api/v1/query'
+QUERY_API_VERSION = '/api/v1/query'
+QUERY_RANGE_API_VERSION = '/api/v1/query_range'
LIST_OF_ENV_VARIABLES = ["DATA_ENDPOINT"]
MAP_ENV_VARIABLES = dict()
LOG = logging.getLogger(__name__)
@@ -34,7 +35,7 @@ def set_log_config():
datefmt='%m-%d-%Y %I:%M:%S%p',
level=logging.DEBUG,
filename='promql_api.log',
- filemode='w')
+ filemode='a')
LOG.info("Set the log configs.")
@@ -45,7 +46,7 @@ def load_and_validate_env_vars(list_of_env_vars):
LOG.info("Found env variable: {} ".format(env_var.upper()))
MAP_ENV_VARIABLES[env_var.upper()] = environ.get(env_var)
else:
- #MAP_ENV_VARIABLES['DATA_ENDPOINT']='http://127.0.0.1:30090' # to be deleted
+ #MAP_ENV_VARIABLES['DATA_ENDPOINT']='http://127.0.0.1:9090' # to be deleted
LOG.error("Env var: {} not found ! ".format(env_var.upper()))
raise KeyError("Env variable: {} not found ! ".format(env_var.upper()))
@@ -74,7 +75,7 @@ def query(QUERY_STRING):
params_map = {}
list_of_result_sets = []
list_of_substrings.append(MAP_ENV_VARIABLES['DATA_ENDPOINT'])
- list_of_substrings.append(API_VERSION)
+ list_of_substrings.append(QUERY_API_VERSION)
url = ''.join(list_of_substrings)
for each_query_string in QUERY_STRING:
@@ -111,3 +112,60 @@ def query(QUERY_STRING):
LOG.info(each_result)
list_of_result_sets.append(results)
return list_of_result_sets
+
+
+def validate_parameters(map_of_parameters):
+ for k,v in map_of_parameters.items():
+ if k not in ['query', 'start', 'end', 'step', 'timeout']:
+ LOG.error('Parameter : \'{}\' not supported by query_range'.format(k))
+ LOG.info('Valid parameters :: \'query\', \'start\', \'end\', \'step\', \'timeout\'')
+ raise Exception('Parameter : \'{}\' not supported by query_range. Check logs'.format(k))
+ if not isinstance(k,str):
+ LOG.error(':: Key Paramter : \'{}\' NOT a string! Keys should be string ::'.format(k))
+ raise Exception(':: Key Paramter : \'{}\' NOT a string! Keys should be string ::'.format(k))
+ if not isinstance(v,str):
+ LOG.error(':: Value Paramter of key: \'{}\' NOT a string! Values should be string ::'.format(k))
+ raise Exception(':: Value Paramter of key: \'{}\' NOT a string! Values should be string ::'.format(k))
+ return True
+
+
+def query_range(map_of_parameters):
+ list_of_result_sets = []
+ set_log_config()
+ if validate_parameters(map_of_parameters):
+ LOG.info(':::Validation of map_of_parameters done::')
+ load_and_validate_env_vars(LIST_OF_ENV_VARIABLES)
+ LOG.info("Forming the query_range request ...")
+
+ list_of_substrings = []
+ list_of_substrings.append(MAP_ENV_VARIABLES['DATA_ENDPOINT'])
+ list_of_substrings.append(QUERY_RANGE_API_VERSION)
+ url = ''.join(list_of_substrings)
+
+ try:
+ LOG.info('API request::: URL: {} '.format(url))
+ LOG.info('API request::: params: {} '.format(map_of_parameters))
+ response = requests.get(url, params=map_of_parameters)
+ response.raise_for_status() # This might raise HTTPError which is handled in except block
+ except HTTPError as http_err:
+ if response.json()['status'] == "error":
+ LOG.error("::::ERROR OCCURED::::")
+ LOG.error("::::ERROR TYPE:::: {}".format(response.json()['errorType']))
+ LOG.error("::::ERROR:::: {}".format(response.json()['error']))
+ print(f'Check logs..HTTP error occurred: {http_err}')
+ except Exception as err:
+ print(f'Check logs..Other error occurred: {err}')
+ else:
+ if response.json()['status'] == "error":
+ LOG.error("::::ERROR OCCURED!::::")
+ LOG.error("::::ERROR TYPE:::: {}".format(response.json()['errorType']))
+ LOG.error("::::ERROR:::: {}".format(response.json()['error']))
+ list_of_result_sets.append(response.json()['error'])
+ list_of_result_sets.append(dict({'error':response.json()['error'],
+ 'errorType' : response.json()['errorType']}))
+ else:
+ results = response.json()['data']['result']
+ LOG.info('::::::::::RESULTS OF QUERY_RANGE::::::::::::: {}'.format(map_of_parameters))
+ list_of_result_sets.append(results)
+
+ return list_of_result_sets \ No newline at end of file