-
Notifications
You must be signed in to change notification settings - Fork 1
/
esrally_csv_to_json.py
63 lines (52 loc) · 1.61 KB
/
esrally_csv_to_json.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
#!/usr/bin/python
DOCUMENTATION = '''
---
module: esrally_csv_to_json
version_added: "2.2"
short_description: Converts the resulting csv from the esrally benchmarking tool
to a JSON-formatted document.
author:
- "Samy Coenen ([email protected])"
'''
from ansible.errors import AnsibleError
import json
import collections
# Recursively loop through a dictionary to look for a key, if it's not found it
# gets added. Else this new key:value get's inserted into another
# key:$(key:value)
def update_dict(source_dict, update):
for key, value in update.iteritems():
if isinstance(value, collections.Mapping):
r = update_dict(source_dict.get(key, {}), value)
source_dict[key] = r
else:
source_dict[key] = update[key]
return source_dict
def convert(result_csv):
result = []
headers = ["Lap", "Metric", "Operation", "Value", "Unit"]
json_data = {}
# get array from csv without the headers
for line in result_csv[1:]:
complete_line = line.split(',')
result.append(complete_line)
# create new dict from list
for line in result:
new_dict = {}
operation = {}
try:
value = float(line[3])
except ValueError:
value = 0
metric = {
"%s in %s" % (line[1], line[4]): value
}
operation[line[2]] = metric
new_dict[line[0]] = operation
json_data = update_dict(json_data, new_dict)
return json.dumps(json_data)
class FilterModule(object):
def filters(self):
return {
'esrally_csv_to_json': convert
}