From 3e1ccfa530eae55061672179bdb498bc7d116fed Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Mon, 26 Sep 2016 14:44:30 +0800 Subject: template-ize kibana visualization JIRA: FUNCTEST-492 Change-Id: I7235ff371ee16ae7f2e759d73735e1aa9b248c1c Signed-off-by: SerenaFeng --- dashboard/dashboard/elastic2kibana/main.py | 97 +++++++--------------- .../elastic2kibana/templates/duration.json | 45 ++++++++++ .../templates/success_percentage.json | 45 ++++++++++ .../elastic2kibana/templates/tests_failures.json | 45 ++++++++++ 4 files changed, 166 insertions(+), 66 deletions(-) create mode 100644 dashboard/dashboard/elastic2kibana/templates/duration.json create mode 100644 dashboard/dashboard/elastic2kibana/templates/success_percentage.json create mode 100644 dashboard/dashboard/elastic2kibana/templates/tests_failures.json (limited to 'dashboard/dashboard') diff --git a/dashboard/dashboard/elastic2kibana/main.py b/dashboard/dashboard/elastic2kibana/main.py index c1cbc30..37ce03e 100644 --- a/dashboard/dashboard/elastic2kibana/main.py +++ b/dashboard/dashboard/elastic2kibana/main.py @@ -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/dashboard/dashboard/elastic2kibana/templates/duration.json b/dashboard/dashboard/elastic2kibana/templates/duration.json new file mode 100644 index 0000000..f50a668 --- /dev/null +++ b/dashboard/dashboard/elastic2kibana/templates/duration.json @@ -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/dashboard/dashboard/elastic2kibana/templates/success_percentage.json b/dashboard/dashboard/elastic2kibana/templates/success_percentage.json new file mode 100644 index 0000000..9930708 --- /dev/null +++ b/dashboard/dashboard/elastic2kibana/templates/success_percentage.json @@ -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/dashboard/dashboard/elastic2kibana/templates/tests_failures.json b/dashboard/dashboard/elastic2kibana/templates/tests_failures.json new file mode 100644 index 0000000..01f9ba8 --- /dev/null +++ b/dashboard/dashboard/elastic2kibana/templates/tests_failures.json @@ -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": {} + } + } + ] +} -- cgit 1.2.3-korg