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