Fix whitespace issues in Python files
[demo.git] / vnfs / DAaaS / lib / promql_api / prom_ql_api.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2019 Intel Corporation Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17
18
19 from __future__ import print_function
20 from os import environ
21 import logging
22 import requests
23 from requests.exceptions import HTTPError
24
25
26 QUERY_API_VERSION = '/api/v1/query'
27 QUERY_RANGE_API_VERSION = '/api/v1/query_range'
28 LIST_OF_ENV_VARIABLES = ["DATA_ENDPOINT"]
29 MAP_ENV_VARIABLES = dict()
30 LOG = logging.getLogger(__name__)
31
32
33 def set_log_config():
34     logging.basicConfig(format='%(asctime)s ::%(filename)s :: %(funcName)s :: %(levelname)s :: %(message)s',
35                     datefmt='%m-%d-%Y %I:%M:%S%p',
36                     level=logging.DEBUG,
37                     filename='promql_api.log',
38                     filemode='a')
39     LOG.info("Set the log configs.")
40
41
42 def load_and_validate_env_vars(list_of_env_vars):
43     LOG.info("Loading the env variables ...")
44     for env_var in list_of_env_vars:
45         if env_var in environ:
46             LOG.info("Found env variable: {} ".format(env_var.upper()))
47             MAP_ENV_VARIABLES[env_var.upper()] = environ.get(env_var)
48         else:
49             #MAP_ENV_VARIABLES['DATA_ENDPOINT']='http://127.0.0.1:9090' # to be deleted
50             LOG.error("Env var: {} not found ! ".format(env_var.upper()))
51             raise KeyError("Env variable: {} not found ! ".format(env_var.upper()))
52
53
54 def query(QUERY_STRING):
55     """
56     Input parameters:
57         QUERY_STRING : a list of the query strings like ['irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])']
58     Return:
59         returns a list of  result sets corresponding to each of the query strings..
60         SAMPLE O/P:
61         [{'metric': {'cpufreq': '1',
62              'endpoint': 'collectd-prometheus',
63              'exported_instance': 'otconap7',
64              'instance': '172.25.103.1:9103',
65              'job': 'collectd',
66              'namespace': 'edge1',
67              'pod': 'plundering-liger-collectd-wz7xg',
68              'service': 'collectd'},
69         'value': [1559177169.415, '119727200']}]
70     """
71     set_log_config()
72     load_and_validate_env_vars(LIST_OF_ENV_VARIABLES)
73     LOG.info("Forming the get request ...")
74     list_of_substrings = []
75     params_map = {}
76     list_of_result_sets = []
77     list_of_substrings.append(MAP_ENV_VARIABLES['DATA_ENDPOINT'])
78     list_of_substrings.append(QUERY_API_VERSION)
79     url = ''.join(list_of_substrings)
80
81     for each_query_string in QUERY_STRING:
82         params_map['query'] = each_query_string
83         try:
84             LOG.info('API request::: URL: {} '.format(url))
85             LOG.info('API request::: params: {} '.format(params_map))
86             response = requests.get(url, params=params_map)
87             response.raise_for_status() # This might raise HTTPError which is handled in except block
88         except HTTPError as http_err:
89             if response.json()['status'] == "error":
90                 LOG.error("::::ERROR OCCURED::::")
91                 LOG.error("::::ERROR TYPE:::: {}".format(response.json()['errorType']))
92                 LOG.error("::::ERROR:::: {}".format(response.json()['error']))
93                 list_of_result_sets.append(dict({'error':response.json()['error'],
94                                                 'errorType' : response.json()['errorType']}))
95             print(f'Check logs..HTTP error occurred: {http_err}')
96
97         except Exception as err:
98             print(f'Check logs..Other error occurred: {err}')
99
100         else:
101             if response.json()['status'] == "error":
102                 LOG.error("::::ERROR OCCURED!::::")
103                 LOG.error("::::ERROR TYPE:::: {}".format(response.json()['errorType']))
104                 LOG.error("::::ERROR:::: {}".format(response.json()['error']))
105                 list_of_result_sets.append(response.json()['error'])
106                 list_of_result_sets.append(dict({'error':response.json()['error'],
107                                                 'errorType' : response.json()['errorType']}))
108             else:
109                 results = response.json()['data']['result']
110                 LOG.info('::::::::::RESULTS::::::::::::: {}'.format(each_query_string))
111                 for each_result in results:
112                     LOG.info(each_result)
113                 list_of_result_sets.append(results)
114     return list_of_result_sets
115
116
117 def validate_parameters(map_of_parameters):
118     for k,v in map_of_parameters.items():
119         if k not in ['query', 'start', 'end', 'step', 'timeout']:
120             LOG.error('Parameter : \'{}\' not supported by query_range'.format(k))
121             LOG.info('Valid parameters :: \'query\', \'start\', \'end\', \'step\', \'timeout\'')
122             raise Exception('Parameter : \'{}\' not supported by query_range. Check logs'.format(k))
123         if not isinstance(k,str):
124             LOG.error(':: Key Paramter : \'{}\' NOT a string! Keys should be string ::'.format(k))
125             raise Exception(':: Key Paramter : \'{}\' NOT a string! Keys should be string ::'.format(k))
126         if not isinstance(v,str):
127             LOG.error(':: Value Paramter of key: \'{}\' NOT a string! Values should be string ::'.format(k))
128             raise Exception(':: Value Paramter of key: \'{}\' NOT a string! Values should be string ::'.format(k))
129     return True
130
131
132 def query_range(map_of_parameters):
133     list_of_result_sets = []
134     set_log_config()
135     if validate_parameters(map_of_parameters):
136         LOG.info(':::Validation of map_of_parameters done::')
137     load_and_validate_env_vars(LIST_OF_ENV_VARIABLES)
138     LOG.info("Forming the query_range request ...")
139
140     list_of_substrings = []
141     list_of_substrings.append(MAP_ENV_VARIABLES['DATA_ENDPOINT'])
142     list_of_substrings.append(QUERY_RANGE_API_VERSION)
143     url = ''.join(list_of_substrings)
144
145     try:
146         LOG.info('API request::: URL: {} '.format(url))
147         LOG.info('API request::: params: {} '.format(map_of_parameters))
148         response = requests.get(url, params=map_of_parameters)
149         response.raise_for_status() # This might raise HTTPError which is handled in except block
150     except HTTPError as http_err:
151         if response.json()['status'] == "error":
152             LOG.error("::::ERROR OCCURED::::")
153             LOG.error("::::ERROR TYPE:::: {}".format(response.json()['errorType']))
154             LOG.error("::::ERROR:::: {}".format(response.json()['error']))
155         print(f'Check logs..HTTP error occurred: {http_err}')
156     except Exception as err:
157             print(f'Check logs..Other error occurred: {err}')
158     else:
159         if response.json()['status'] == "error":
160             LOG.error("::::ERROR OCCURED!::::")
161             LOG.error("::::ERROR TYPE:::: {}".format(response.json()['errorType']))
162             LOG.error("::::ERROR:::: {}".format(response.json()['error']))
163             list_of_result_sets.append(response.json()['error'])
164             list_of_result_sets.append(dict({'error':response.json()['error'],
165                                                 'errorType' : response.json()['errorType']}))
166         else:
167             results = response.json()['data']['result']
168             LOG.info('::::::::::RESULTS OF QUERY_RANGE::::::::::::: {}'.format(map_of_parameters))
169             list_of_result_sets.append(results)
170
171     return list_of_result_sets