summaryrefslogtreecommitdiff
path: root/elastic/generate-config.py
blob: ec1a5e97a98b8db1090f7776225c189906d093de (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
#
# Todo:
#
# - Remove all json files from previous runs
import json
import logging
import os
import pystache
import requests


URL='https://moose.leap.se:9200'


def _get_logger():
    logger = logging.getLogger(__name__)
    ch = logging.StreamHandler()
    ch.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
    logger.addHandler(ch)
    logger.setLevel(logging.INFO)
    return logger


def _delete_jsons(directory):
    logger.info('removing *.json from %s' % directory)
    for f in os.listdir(directory):
        if f.endswith('.json'):
            os.unlink(os.path.join(directory, f))


def generate_visualizations(tests):
    with open(os.path.join('templates', 'visualization.mustache'), 'r') as f:
        template = f.read()
    with open(os.path.join('templates', 'visualization-visState-time.mustache')) as f:
        template_visState_time = f.read()
    with open(os.path.join('templates', 'visualization-visState-resources.mustache')) as f:
        template_visState_resources = f.read()
    with open(os.path.join('templates', 'visualization-searchSourceJSON.mustache')) as f:
        template_searchSourceJSON = f.read()

    out_dir = './visualization'
    _delete_jsons(out_dir)

    # remove all json files in output directory
    for f in os.listdir(out_dir):
        if f.endswith('.json'):
            os.unlink(os.path.join(out_dir, f))

    graphs = [
        ('time', 'Time taken by ', 'weasel', template_visState_time),
        ('resources', 'Resources consumed by ', 'weasel', template_visState_resources)
    ]

    for test_name in tests:

        for type, title, host, template_visState in graphs:

            out_file = os.path.join(out_dir, test_name + '_' + type + '.json')
            logger.info('Generating ' + out_file)

            context = {
               'title': title + test_name,
               'test_name': test_name,
               'query': "commit_info.project:soledad "
#                        "AND commit_info.branch='master' "
                        "AND machine_info.host='" + host + "' "
                        "AND name='" + test_name + "'"
            }

            visState = pystache.render(template_visState, context)
            assert json.loads(visState)
            context['visState'] = json.dumps(visState)

            searchSourceJSON = pystache.render(template_searchSourceJSON, context)
            assert json.loads(searchSourceJSON)
            context['searchSourceJSON'] = json.dumps(searchSourceJSON)

            rendered = pystache.render(template, context)
            assert json.loads(rendered)

            with open(out_file, 'w') as out:
                out.write(rendered)


def generate_dashboards(tests):
    with open(os.path.join('templates', 'dashboard.mustache'), 'r') as f:
        template = f.read()

    out_dir = './dashboard'
    _delete_jsons(out_dir)

    out_file = os.path.join(out_dir, 'soledad-benchmarks.json')
    logger.info('Generating ' + out_file)

    panels = []
    panelIndex = 1
    row = 1
    for test_name in tests:
        for type, col in [('time', 1), ('resources', 7)]:
            panels.append({
                'id': test_name + '_' + type,
                'panelIndex': panelIndex,
                'col': col,
                'row': row,
                'size_x': 6,
                'size_y': 3,
                'type': 'visualization',
            })
            panelIndex += 1
        row += 3

    context = {'panels_json': json.dumps(json.dumps(panels))}
    rendered = pystache.render(template, context)
    assert json.loads(rendered), rendered

    with open(out_file, 'w') as f:
        f.write(rendered)


def _get_test_names():
    with open('./query/test_names.json') as f:
        response = requests.get(URL + '/_search', data=f)
        buckets = response.json()['aggregations']['test_names']['buckets']
    tests = []
    for d in buckets:
        name = d['key']
        tests.append(name)
    return sorted(tests)


if __name__ == '__main__':
    logger = _get_logger()
    tests = _get_test_names()
    generate_dashboards(tests)
    generate_visualizations(tests)