-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
119 lines (98 loc) · 3.81 KB
/
handler.py
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
import os
import re
import json
import aiohttp
import validators
import logging
logging.basicConfig(level=os.getenv("LOG_LEVEL", logging.INFO), format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
async def get_data(url):
# json.loads() requires str beginning with a JSON document
json_body = json.loads('{}')
async with aiohttp.ClientSession() as client:
try:
async with client.get(url) as r:
status = r.status
logger.info(f'Requesting Gradle url {url}')
logger.debug(f'Full response: {r}')
if status == 200:
json_body = await r.json()
else:
logger.error(f'Cannot request Gradle url {url}! Response status: {status}')
except aiohttp.ClientError as error:
logger.error(f'Connection error to url {url}: {error}')
return json_body
def validate_json(json_data):
"""
:param json_data: any json
:return:
Example of expected json format received from Gradle server:
{
"pending" : 0,
"requested" : 0,
"ageMins" : 0,
"requestWaitTimeSecs" : 0,
"incomingRate1m" : 0.03221981766544038,
"incomingRate5m" : 0.02219163413405735,
"incomingRate15m" : 0.021373141599789678,
"processingRate1m" : 0.03399783025186821,
"processingRate5m" : 0.022374841163558885,
"processingRate15m" : 0.021459615070953553
}
Params validation may be added in the future
"""
if json_data == '{}':
logger.error('Empty json response from server! No metrics will be produced')
return None
def generate_metrics(json_data, url):
# get 'last-item' from 'https://domain.tld/path/last-item' string
last_item = url.rsplit('/', 1)[-1]
# replace '-' with '_'
last_item = last_item.replace('-', '_')
metrics_str = ''
for k, v in json_data.items():
# convert bool values into 0/1 representation to put into metric value
v = 1 if re.search('true', str(v), re.IGNORECASE) else 0
metrics_str += f'gradle_{last_item}{{entity="{k}",url="{url}"}} {v}\n'
return metrics_str
class HandleFileData:
def __init__(self, path):
self.path = path
def return_urls_list(self):
strs_list = self.read_file(path=self.path)
urls_list = self.normalize_urls_list(strs_list=strs_list)
return urls_list
def normalize_urls_list(self, strs_list):
invalid_urls_counter = 0
urls_list = []
for line in strs_list:
line_striped = line.strip()
if line_striped == '':
pass
# ignore line in the list if it doesn't look like a valid url
elif self.normalize_url(url=line_striped) is False:
invalid_urls_counter += 1
else:
urls_list.append(line_striped)
if invalid_urls_counter > 0:
logger.warning(f'{invalid_urls_counter} url(s) has been removed due to invalid format. Check file {self.path}')
logger.info('Read more about url format at https://validators.readthedocs.io/en/latest/#module-validators.url')
return urls_list
@staticmethod
def read_file(path):
try:
logger.info(f'Reading file {path}')
with open(path) as file:
# Return all lines in the file, as a list where each line is an item
strs_list = file.readlines()
except OSError as error:
logger.critical(f'Could not read file {path}: {error}')
strs_list = []
return strs_list
@staticmethod
def normalize_url(url):
result = True
if not validators.url(url):
logger.warning(f'String {url} is not a valid url. Skipping...')
result = False
return result