forked from multiservicio/sonarqube-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonar-client.py
109 lines (90 loc) · 3.18 KB
/
sonar-client.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
import requests
import os
import datetime
from influxdb import InfluxDBClient
BASE_URL = 'http://local.dev:9000'
USER = os.environ['SONAR_USER']
PASSWORD = os.environ['SONAR_PASSWORD']
INFLUX_URL = 'local.dev'
INFLUX_USER = os.environ['INFLUX_USER']
INFLUX_PASSWORD = os.environ['INFLUX_PASSWORD']
INFLUX_DB = os.environ['INFLUX_DB']
class SonarApiClient:
def __init__(self, user, passwd):
self.user = user
self.passwd = passwd
def _make_request(self, endpoint):
r = requests.get(BASE_URL + endpoint, auth=(self.user, self.passwd))
return r.json()
def get_all_ids(self, endpoint):
data = self._make_request(endpoint)
ids = []
for component in data['components']:
dict = {
'id': component['id'],
'key': component['key']
}
ids.append(dict)
return ids
def get_all_available_metrics(self, endpoint):
data = self._make_request(endpoint)
metrics = []
for metric in data['metrics']:
metrics.append(metric['key'])
return metrics
def get_measures_by_component_id(self, endpoint):
data = self._make_request(endpoint)
return data['component']['measures']
class Project:
def __init__(self, identifier, key):
self.id = identifier
self.key = key
self.metrics = None
self.timestamp = datetime.datetime.utcnow().isoformat()
def set_metrics(self, metrics):
self.metrics = metrics
def export_metrics(self):
influx_client = InfluxDBClient(
host=INFLUX_URL,
port=8086,
username=INFLUX_USER,
password=INFLUX_PASSWORD,
database=INFLUX_DB
)
influx_client.write_points(self._prepare_metrics())
def _prepare_metrics(self):
json_to_export = []
for metric in self.metrics:
one_metric = {
"measurement": metric['metric'],
"tags": {
"id": self.id,
"key": self.key
},
"time": self.timestamp,
"fields": {
"value": metric['value'] if ('value' in metric) else 0
}
}
json_to_export.append(one_metric)
return json_to_export
# Fetch all projects IDs
client = SonarApiClient(USER, PASSWORD)
ids = client.get_all_ids('/api/components/search?qualifiers=TRK')
# Fetch all available metrics
metrics = client.get_all_available_metrics('/api/metrics/search')
comma_separated_metrics = ''
for metric in metrics:
comma_separated_metrics += metric + ','
# Collect metrics per project
uri = '/api/measures/component'
for item in ids:
project_id = item['id']
project_key = item['key']
print(project_key, project_id)
project = Project(identifier=project_id, key=project_key)
component_id_query_param = 'componentId=' + project_id
metric_key_query_param = 'metricKeys=' + comma_separated_metrics
measures = client.get_measures_by_component_id(uri + '?' + component_id_query_param + '&' + metric_key_query_param)
project.set_metrics(measures)
project.export_metrics()