[elastic] remove inline json
authordrebs <drebs@riseup.net>
Thu, 27 Apr 2017 10:09:04 +0000 (12:09 +0200)
committerdrebs <drebs@riseup.net>
Fri, 5 May 2017 07:17:34 +0000 (09:17 +0200)
elastic/generate-config.py

index 0841f5c..0aa998f 100755 (executable)
@@ -3,27 +3,42 @@
 # Todo:
 #
 # - Remove all json files from previous runs
+import json
+import logging
+import os
 import pystache
 import requests
-import json
+import sys
+
 
 URL='https://moose.leap.se:9200'
 
+# configure a logger
+logger = logging.getLogger(__name__)
+ch = logging.StreamHandler()
+ch.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
+logger.addHandler(ch)
+logger.setLevel(logging.INFO)
+
 
 # Generate visualizations
 def generate_visualizations():
     dir = './visualization/'
-    template = open(dir + 'template.mustache', 'r').read()
+    with open(os.path.join(dir, 'template.mustache'), 'r') as f:
+        template = f.read()
 
     # test_names = tests.
     for test_name in tests:
-        out_file = dir + test_name + '.json'
-        print('Generating ' + out_file)
+        out_file = os.path.join(dir, test_name + '.json')
+        logger.info('Generating ' + out_file)
+
         context = {
            'title': test_name,
-           'query': "commit_info.project:soledad AND commit_info.branch='master' AND machine_info.host='weasel' AND name='" + test_name + "'"
+           'query': "commit_info.project:soledad "
+                    "AND commit_info.branch='master' "
+                    "AND machine_info.host='weasel' "
+                    "AND name='" + test_name + "'"
         }
-
         rendered = pystache.render(template, context)
 
         # verify we generated valid JSON visualizations
@@ -33,32 +48,37 @@ def generate_visualizations():
         assert json.loads(visState)
         assert json.loads(searchSourceJSON)
 
-        out = open(out_file, 'w')
-        out.write(rendered)
-        out.flush()
+        with open(out_file, 'w') as out:
+            out.write(rendered)
 
 # Generate Dashboard
 def generate_dashboards():
     dir = './dashboard/'
-    template = open(dir + 'template.mustache', 'r').read()
-    out_file = dir + 'soledad-benchmarks.json'
-    print('Generating ' + out_file)
+    with open(os.path.join(dir, 'template.mustache'), 'r') as f:
+        template = f.read()
+
+    out_file = os.path.join(dir, 'soledad-benchmarks.json')
+    logger.info('Generating ' + out_file)
 
-    panels = '['
+    panels = []
     count = 0
     for test_name in tests:
         count += 1
-        panels += '{\\"id\\":\\"' + test_name + '\\",\\"panelIndex\\":' + str(count) + ',\\"col\\":1,\\"row\\":' + str(count) + ',\\"size_x\\":6,\\"size_y\\":3,\\"type\\":\\"visualization\\"}, '
-    panels = panels[:-2]
-    panels += ']'
-
-    panels_json = { 'panels_json': panels }
+        panels.append({
+            'id': test_name,
+            'panelIndex': count,
+            'col': 1,
+            'row': count,
+            'size_x': 6,
+            'size_y': 3,
+            'type': 'visualization',
+        })
+
+    panels_json = {'panels_json': json.dumps(panels)}
     rendered = pystache.render(template, panels_json)
-    #print(rendered)
 
-    out = open(out_file, 'w')
-    out.write(rendered)
-    out.flush()
+    with open(out_file, 'w') as f:
+        f.write(rendered)
 
 
 # Main
@@ -74,6 +94,7 @@ for d in buckets:
     tests.append(name)
 tests = sorted(tests)
 
-generate_dashboards()
-print()
-generate_visualizations()
+
+if __name__ == '__main__':
+    generate_dashboards()
+    generate_visualizations()