aboutsummaryrefslogtreecommitdiffstats
path: root/vnfs/DAaaS/lib/promql_api/prom_ql_api.py
blob: 2ee273a052768d6225f3affb1493f098aa01bb21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# -------------------------------------------------------------------------
#   Copyright (c) 2019 Intel Corporation Intellectual Property
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# -------------------------------------------------------------------------


from __future__ import print_function
from os import environ
import logging
import requests
from requests.exceptions import HTTPError


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__)


def set_log_config():
    logging.basicConfig(format='%(asctime)s ::%(filename)s :: %(funcName)s :: %(levelname)s :: %(message)s',
                    datefmt='%m-%d-%Y %I:%M:%S%p',
                    level=logging.DEBUG,
                    filename='promql_api.log',
                    filemode='a')
    LOG.info("Set the log configs.")


def load_and_validate_env_vars(list_of_env_vars):
    LOG.info("Loading the env variables ...")
    for env_var in list_of_env_vars:
        if env_var in environ:
            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:9090' # to be deleted
            LOG.error("Env var: {} not found ! ".format(env_var.upper()))
            raise KeyError("Env variable: {} not found ! ".format(env_var.upper()))


def query(QUERY_STRING):
    """
    Input parameters:
        QUERY_STRING : a list of the query strings like ['irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])']
    Return:
        returns a list of  result sets corresponding to each of the query strings..
        SAMPLE O/P:
        [{'metric': {'cpufreq': '1',
             'endpoint': 'collectd-prometheus',
             'exported_instance': 'otconap7',
             'instance': '172.25.103.1:9103',
             'job': 'collectd',
             'namespace': 'edge1',
             'pod': 'plundering-liger-collectd-wz7xg',
             'service': 'collectd'},
        'value': [1559177169.415, '119727200']}]
    """
    set_log_config()
    load_and_validate_env_vars(LIST_OF_ENV_VARIABLES)
    LOG.info("Forming the get request ...")
    list_of_substrings = []
    params_map = {}
    list_of_result_sets = []
    list_of_substrings.append(MAP_ENV_VARIABLES['DATA_ENDPOINT'])
    list_of_substrings.append(QUERY_API_VERSION)
    url = ''.join(list_of_substrings)

    for each_query_string in QUERY_STRING:
        params_map['query'] = each_query_string
        try:
            LOG.info('API request::: URL: {} '.format(url))
            LOG.info('API request::: params: {} '.format(params_map))
            response = requests.get(url, params=params_map)
            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']))
                list_of_result_sets.append(dict({'error':response.json()['error'],
                                                'errorType' : response.json()['errorType']}))
            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::::::::::::: {}'.format(each_query_string))
                for each_result in results:
                    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