template-ize kibana visualization 29/22429/3
authorSerenaFeng <feng.xiaowei@zte.com.cn>
Mon, 26 Sep 2016 06:44:30 +0000 (14:44 +0800)
committerSerenaFeng <feng.xiaowei@zte.com.cn>
Mon, 26 Sep 2016 07:41:59 +0000 (15:41 +0800)
JIRA: FUNCTEST-492

Change-Id: I7235ff371ee16ae7f2e759d73735e1aa9b248c1c
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
utils/test/dashboard/dashboard/elastic2kibana/main.py
utils/test/dashboard/dashboard/elastic2kibana/templates/duration.json [new file with mode: 0644]
utils/test/dashboard/dashboard/elastic2kibana/templates/success_percentage.json [new file with mode: 0644]
utils/test/dashboard/dashboard/elastic2kibana/templates/tests_failures.json [new file with mode: 0644]

index c1cbc30..37ce03e 100644 (file)
@@ -3,6 +3,7 @@ import json
 import urlparse
 
 import argparse
+from jinja2 import PackageLoader, Environment
 
 from common import logger_utils, elastic_access
 from conf import testcases
@@ -51,12 +52,13 @@ class KibanaDashboard(dict):
                                                                    scenario,
                                                                    self.visualization))
 
-        self._visualization_title = self._kibana_visualizations[0].vis_state_title
+        self._visualization_title = self._kibana_visualizations[0].vis_title
 
     def _publish_visualizations(self):
         for visualization in self._kibana_visualizations:
             url = urlparse.urljoin(base_elastic_url, '/.kibana/visualization/{}'.format(visualization.id))
             logger.debug("publishing visualization '{}'".format(url))
+            # logger.error("_publish_visualization: %s" % visualization)
             elastic_access.publish_json(visualization, es_creds, url)
 
     def _construct_panels(self):
@@ -163,67 +165,30 @@ class KibanaSearchSourceJSON(dict):
             self["filter"].append({"match": {"pod_name": {"query": pod, "type": "phrase"}}})
 
 
-class VisualizationState(dict):
+class VisualizationBuilder(object):
     def __init__(self, visualization):
-        super(VisualizationState, self).__init__()
-        name = visualization.get('name')
-        fields = visualization.get('fields')
-
-        if name == 'tests_failures':
-            mode = 'grouped'
-            metric_type = 'sum'
-            self['type'] = 'histogram'
-        else:
-            # duration or success_percentage
-            mode = 'stacked'
-            metric_type = 'avg'
-            self['type'] = 'line'
-
-        self['params'] = {
-            "shareYAxis": True,
-            "addTooltip": True,
-            "addLegend": True,
-            "smoothLines": False,
-            "scale": "linear",
-            "interpolate": "linear",
-            "mode": mode,
-            "times": [],
-            "addTimeMarker": False,
-            "defaultYExtents": False,
-            "setYExtents": False,
-            "yAxis": {}
-        }
+        super(VisualizationBuilder, self).__init__()
+        self.visualization = visualization
 
-        self['aggs'] = []
+    def build(self):
+        name = self.visualization.get('name')
+        fields = self.visualization.get('fields')
 
-        i = 1
+        aggs = []
+        index = 1
         for field in fields:
-            self['aggs'].append({
-                "id": str(i),
-                "type": metric_type,
-                "schema": "metric",
-                "params": {
-                    "field": field.get('field')
-                }
-            })
-            i += 1
-
-        self['aggs'].append({
-                "id": str(i),
-                "type": 'date_histogram',
-                "schema": "segment",
-                "params": {
-                    "field": "start_date",
-                    "interval": "auto",
-                    "customInterval": "2h",
-                    "min_doc_count": 1,
-                    "extended_bounds": {}
-                }
+            aggs.append({
+                "id": index,
+                "field": field.get("field")
             })
+            index += 1
+
+        env = Environment(loader=PackageLoader('elastic2kibana', 'templates'))
+        env.filters['jsonify'] = json.dumps
+        template = env.get_template('{}.json'.format(name))
+        vis = template.render(aggs=aggs)
+        return json.loads(vis)
 
-        self['listeners'] = {}
-        self['title'] = ' '.join(['{} {}'.format(x['type'], x['params']['field']) for x in self['aggs']
-                                  if x['schema'] == 'metric'])
 
 
 class KibanaVisualization(dict):
@@ -243,24 +208,24 @@ class KibanaVisualization(dict):
         :return:
         """
         super(KibanaVisualization, self).__init__()
-        vis_state = VisualizationState(visualization)
-        self.vis_state_title = vis_state['title']
+        vis = VisualizationBuilder(visualization).build()
+        self.vis_title = vis['title']
         self['title'] = '{} {} {} {} {} {}'.format(project_name,
                                                    case_name,
-                                                   self.vis_state_title,
+                                                   self.vis_title,
                                                    installer,
                                                    pod,
                                                    scenario)
         self.id = self['title'].replace(' ', '-').replace('/', '-')
-        self['visState'] = json.dumps(vis_state, separators=(',', ':'))
+        self['visState'] = json.dumps(vis, separators=(',', ':'))
         self['uiStateJSON'] = "{}"
-        self['description'] = "Kibana visualization for project_name '{}', case_name '{}', data '{}', installer '{}'," \
+        self['description'] = "Kibana visualization for project_name '{}', case_name '{}', metric '{}', installer '{}'," \
                               " pod '{}' and scenario '{}'".format(project_name,
-                                                                  case_name,
-                                                                  self.vis_state_title,
-                                                                  installer,
-                                                                  pod,
-                                                                  scenario)
+                                                                   case_name,
+                                                                   self.vis_title,
+                                                                   installer,
+                                                                   pod,
+                                                                   scenario)
         self['scenario'] = 1
         self['kibanaSavedObjectMeta'] = {"searchSourceJSON": json.dumps(KibanaSearchSourceJSON(project_name,
                                                                                                case_name,
diff --git a/utils/test/dashboard/dashboard/elastic2kibana/templates/duration.json b/utils/test/dashboard/dashboard/elastic2kibana/templates/duration.json
new file mode 100644 (file)
index 0000000..f50a668
--- /dev/null
@@ -0,0 +1,45 @@
+{% set aggs = aggs|default([]) -%}
+
+{
+  "title": "duration",
+  "type": "line",
+  "listeners": {},
+  "params": {
+    "addLegend": true,
+    "shareYAxis": true,
+    "addTooltip": true,
+    "smoothLines": false,
+    "scale": "linear",
+    "interpolate": "linear",
+    "times": [],
+    "addTimeMarker": false,
+    "defaultYExtents": false,
+    "setYExtents": false,
+    "yAxis": {},
+    "mode": "stacked"
+  },
+  "aggs": [
+    {% for agg in aggs %}
+    {
+      "id": {{agg.id }},
+      "type": "avg",
+      "schema": "metric",
+      "params": {
+        "field": "{{agg.field}}"
+      }
+    },
+    {% endfor %}
+    {
+      "id": {{ aggs|length + 1 }},
+      "type": "date_histogram",
+      "schema": "segment",
+      "params": {
+        "field": "start_date",
+        "interval": "auto",
+        "customInterval": "2h",
+        "min_doc_count": 1,
+        "extended_bounds": {}
+      }
+    }
+  ]
+}
diff --git a/utils/test/dashboard/dashboard/elastic2kibana/templates/success_percentage.json b/utils/test/dashboard/dashboard/elastic2kibana/templates/success_percentage.json
new file mode 100644 (file)
index 0000000..9930708
--- /dev/null
@@ -0,0 +1,45 @@
+{% set aggs = aggs|default([]) -%}
+
+{
+  "title": "success_percentage",
+  "type": "line",
+  "listeners": {},
+  "params": {
+    "addLegend": true,
+    "shareYAxis": true,
+    "addTooltip": true,
+    "smoothLines": false,
+    "scale": "linear",
+    "interpolate": "linear",
+    "times": [],
+    "addTimeMarker": false,
+    "defaultYExtents": false,
+    "setYExtents": false,
+    "yAxis": {},
+    "mode": "stacked"
+  },
+  "aggs": [
+    {% for agg in aggs %}
+    {
+      "id": {{agg.id }},
+      "type": "avg",
+      "schema": "metric",
+      "params": {
+        "field": "{{agg.field}}"
+      }
+    },
+    {% endfor %}
+    {
+      "id": {{ aggs|length + 1 }},
+      "type": "date_histogram",
+      "schema": "segment",
+      "params": {
+        "field": "start_date",
+        "interval": "auto",
+        "customInterval": "2h",
+        "min_doc_count": 1,
+        "extended_bounds": {}
+      }
+    }
+  ]
+}
diff --git a/utils/test/dashboard/dashboard/elastic2kibana/templates/tests_failures.json b/utils/test/dashboard/dashboard/elastic2kibana/templates/tests_failures.json
new file mode 100644 (file)
index 0000000..01f9ba8
--- /dev/null
@@ -0,0 +1,45 @@
+{% set aggs = aggs|default([]) -%}
+
+{
+  "title": "tests_failures",
+  "type": "histogram",
+  "listeners": {},
+  "params": {
+    "addLegend": true,
+    "shareYAxis": true,
+    "addTooltip": true,
+    "smoothLines": false,
+    "scale": "linear",
+    "interpolate": "linear",
+    "times": [],
+    "addTimeMarker": false,
+    "defaultYExtents": false,
+    "setYExtents": false,
+    "yAxis": {},
+    "mode": "grouped"
+  },
+  "aggs": [
+    {% for agg in aggs %}
+    {
+      "id": {{agg.id }},
+      "type": "sum",
+      "schema": "metric",
+      "params": {
+        "field": "{{agg.field}}"
+      }
+    },
+    {% endfor %}
+    {
+      "id": {{ aggs|length + 1 }},
+      "type": "date_histogram",
+      "schema": "segment",
+      "params": {
+        "field": "start_date",
+        "interval": "auto",
+        "customInterval": "2h",
+        "min_doc_count": 1,
+        "extended_bounds": {}
+      }
+    }
+  ]
+}