[elastic] plot results for resources from weasel
[scripts.git] / elastic / generate-config.py
1 #!/usr/bin/env python3
2 #
3 # Todo:
4 #
5 # - Remove all json files from previous runs
6 import json
7 import logging
8 import os
9 import pystache
10 import requests
11
12
13 URL='https://moose.leap.se:9200'
14
15
16 def _get_logger():
17     logger = logging.getLogger(__name__)
18     ch = logging.StreamHandler()
19     ch.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
20     logger.addHandler(ch)
21     logger.setLevel(logging.INFO)
22     return logger
23
24
25 def _delete_jsons(directory):
26     logger.info('removing *.json from %s' % directory)
27     for f in os.listdir(directory):
28         if f.endswith('.json'):
29             os.unlink(os.path.join(directory, f))
30
31
32 def generate_visualizations(tests):
33     with open(os.path.join('templates', 'visualization.mustache'), 'r') as f:
34         template = f.read()
35     with open(os.path.join('templates', 'visualization-visState-time.mustache')) as f:
36         template_visState_time = f.read()
37     with open(os.path.join('templates', 'visualization-visState-resources.mustache')) as f:
38         template_visState_resources = f.read()
39     with open(os.path.join('templates', 'visualization-searchSourceJSON.mustache')) as f:
40         template_searchSourceJSON = f.read()
41
42     out_dir = './visualization'
43     _delete_jsons(out_dir)
44
45     # remove all json files in output directory
46     for f in os.listdir(out_dir):
47         if f.endswith('.json'):
48             os.unlink(os.path.join(out_dir, f))
49
50     graphs = [
51         ('time', 'Time taken by ', 'weasel', template_visState_time),
52         ('resources', 'Resources consumed by ', 'weasel', template_visState_resources)
53     ]
54
55     for test_name in tests:
56
57         for type, title, host, template_visState in graphs:
58
59             out_file = os.path.join(out_dir, test_name + '_' + type + '.json')
60             logger.info('Generating ' + out_file)
61
62             context = {
63                'title': title + test_name,
64                'test_name': test_name,
65                'query': "commit_info.project:soledad "
66 #                        "AND commit_info.branch='master' "
67                         "AND machine_info.host='" + host + "' "
68                         "AND name='" + test_name + "'"
69             }
70
71             visState = pystache.render(template_visState, context)
72             assert json.loads(visState)
73             context['visState'] = json.dumps(visState)
74
75             searchSourceJSON = pystache.render(template_searchSourceJSON, context)
76             assert json.loads(searchSourceJSON)
77             context['searchSourceJSON'] = json.dumps(searchSourceJSON)
78
79             rendered = pystache.render(template, context)
80             assert json.loads(rendered)
81
82             with open(out_file, 'w') as out:
83                 out.write(rendered)
84
85
86 def generate_dashboards(tests):
87     with open(os.path.join('templates', 'dashboard.mustache'), 'r') as f:
88         template = f.read()
89
90     out_dir = './dashboard'
91     _delete_jsons(out_dir)
92
93     out_file = os.path.join(out_dir, 'soledad-benchmarks.json')
94     logger.info('Generating ' + out_file)
95
96     panels = []
97     panelIndex = 1
98     row = 1
99     for test_name in tests:
100         for type, col in [('time', 1), ('resources', 7)]:
101             panels.append({
102                 'id': test_name + '_' + type,
103                 'panelIndex': panelIndex,
104                 'col': col,
105                 'row': row,
106                 'size_x': 6,
107                 'size_y': 3,
108                 'type': 'visualization',
109             })
110             panelIndex += 1
111         row += 3
112
113     context = {'panels_json': json.dumps(json.dumps(panels))}
114     rendered = pystache.render(template, context)
115     assert json.loads(rendered), rendered
116
117     with open(out_file, 'w') as f:
118         f.write(rendered)
119
120
121 def _get_test_names():
122     with open('./query/test_names.json') as f:
123         response = requests.get(URL + '/_search', data=f)
124         buckets = response.json()['aggregations']['test_names']['buckets']
125     tests = []
126     for d in buckets:
127         name = d['key']
128         tests.append(name)
129     return sorted(tests)
130
131
132 if __name__ == '__main__':
133     logger = _get_logger()
134     tests = _get_test_names()
135     generate_dashboards(tests)
136     generate_visualizations(tests)