0aa998fcbb2863bc8cbc59c13adfc0003c1407b8
[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 # configure a 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
23
24 # Generate visualizations
25 def generate_visualizations():
26     dir = './visualization/'
27     with open(os.path.join(dir, 'template.mustache'), 'r') as f:
28         template = f.read()
29
30     # test_names = tests.
31     for test_name in tests:
32         out_file = os.path.join(dir, test_name + '.json')
33         logger.info('Generating ' + out_file)
34
35         context = {
36            'title': test_name,
37            'query': "commit_info.project:soledad "
38                     "AND commit_info.branch='master' "
39                     "AND machine_info.host='weasel' "
40                     "AND name='" + test_name + "'"
41         }
42         rendered = pystache.render(template, context)
43
44         # verify we generated valid JSON visualizations
45         content = json.loads(rendered)
46         visState = content['visState']
47         searchSourceJSON = content['kibanaSavedObjectMeta']['searchSourceJSON']
48         assert json.loads(visState)
49         assert json.loads(searchSourceJSON)
50
51         with open(out_file, 'w') as out:
52             out.write(rendered)
53
54 # Generate Dashboard
55 def generate_dashboards():
56     dir = './dashboard/'
57     with open(os.path.join(dir, 'template.mustache'), 'r') as f:
58         template = f.read()
59
60     out_file = os.path.join(dir, 'soledad-benchmarks.json')
61     logger.info('Generating ' + out_file)
62
63     panels = []
64     count = 0
65     for test_name in tests:
66         count += 1
67         panels.append({
68             'id': test_name,
69             'panelIndex': count,
70             'col': 1,
71             'row': count,
72             'size_x': 6,
73             'size_y': 3,
74             'type': 'visualization',
75         })
76
77     panels_json = {'panels_json': json.dumps(panels)}
78     rendered = pystache.render(template, panels_json)
79
80     with open(out_file, 'w') as f:
81         f.write(rendered)
82
83
84 # Main
85
86 # Get all test names
87 with open('./query/test_names.json') as f:
88     response = requests.get(URL + '/_search', data=f)
89     buckets = response.json()['aggregations']['test_names']['buckets']
90
91 tests = []
92 for d in buckets:
93     name = d['key']
94     tests.append(name)
95 tests = sorted(tests)
96
97
98 if __name__ == '__main__':
99     generate_dashboards()
100     generate_visualizations()