From fdd68181912a81249f33903e1e1e8cb765685dbb Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Tue, 27 Sep 2016 10:37:14 +0800 Subject: refactor elastic_access.py JIRA: FUNCTEST-495 Change-Id: I639c9b4838ba026cf48c01081810f663467163a8 Signed-off-by: SerenaFeng --- dashboard/dashboard/common/elastic_access.py | 54 ++++++++++++++++------------ dashboard/dashboard/elastic2kibana/main.py | 9 ++--- dashboard/dashboard/mongo2elastic/main.py | 2 +- 3 files changed, 37 insertions(+), 28 deletions(-) (limited to 'dashboard/dashboard') diff --git a/dashboard/dashboard/common/elastic_access.py b/dashboard/dashboard/common/elastic_access.py index e90a17f..b454e9a 100644 --- a/dashboard/dashboard/common/elastic_access.py +++ b/dashboard/dashboard/common/elastic_access.py @@ -5,41 +5,49 @@ import urllib3 http = urllib3.PoolManager() -def delete_request(url, creds, body=None): +def _request(method, url, creds=None, body=None): headers = urllib3.make_headers(basic_auth=creds) - http.request('DELETE', url, headers=headers, body=body) + return http.request(method, url, headers=headers, body=body) -def publish_json(json_ojb, creds, to): - json_dump = json.dumps(json_ojb) +def _post(url, creds=None, body=None): + return _request('POST', url, creds=creds, body=body) + + +def _get(url, creds=None, body=None): + return json.loads(_request('GET', url, creds=creds, body=body).data) + + +def delete_docs(url, creds=None, body=None): + return _request('DELETE', url, creds=creds, body=body) + + +def publish_docs(docs, creds, to): + json_docs = json.dumps(docs) if to == 'stdout': - print json_dump + print json_docs return 200, None else: - headers = urllib3.make_headers(basic_auth=creds) - result = http.request('POST', to, headers=headers, body=json_dump) + result = _post(to, creds=creds, body=json_docs) return result.status, result.data -def _get_nr_of_hits(elastic_json): - return elastic_json['hits']['total'] +def _get_docs_nr(url, creds=None, body=None): + res_data = _get('{}/_search?size=0'.format(url), creds=creds, body=body) + print type(res_data), res_data + return res_data['hits']['total'] -def get_elastic_docs(elastic_url, creds, body=None, field = '_source'): - - # 1. get the number of results - headers = urllib3.make_headers(basic_auth=creds) - elastic_json = json.loads(http.request('GET', elastic_url + '/_search?size=0', headers=headers, body=body).data) - print elastic_json - nr_of_hits = _get_nr_of_hits(elastic_json) +def get_docs(url, creds=None, body=None, field='_source'): - # 2. get all results - elastic_json = json.loads(http.request('GET', elastic_url + '/_search?size={}'.format(nr_of_hits), headers=headers, body=body).data) + docs_nr = _get_docs_nr(url, creds=creds, body=body) + res_data = _get('{}/_search?size={}'.format(url, docs_nr), + creds=creds, body=body) - elastic_docs = [] - for hit in elastic_json['hits']['hits']: - elastic_docs.append(hit[field]) - return elastic_docs + docs = [] + for hit in res_data['hits']['hits']: + docs.append(hit[field]) + return docs def get_elastic_docs_by_days(elastic_url, creds, days): @@ -61,4 +69,4 @@ def get_elastic_docs_by_days(elastic_url, creds, days): }}'''.format(days) else: raise Exception('Update days must be non-negative') - return get_elastic_docs(elastic_url, creds, body) + return get_docs(elastic_url, creds, body) diff --git a/dashboard/dashboard/elastic2kibana/main.py b/dashboard/dashboard/elastic2kibana/main.py index 37ce03e..38a49b6 100644 --- a/dashboard/dashboard/elastic2kibana/main.py +++ b/dashboard/dashboard/elastic2kibana/main.py @@ -5,7 +5,8 @@ import urlparse import argparse from jinja2 import PackageLoader, Environment -from common import logger_utils, elastic_access +from common import elastic_access +from common import logger_utils from conf import testcases from conf.config import APIConfig @@ -59,7 +60,7 @@ class KibanaDashboard(dict): 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) + elastic_access.publish_docs(visualization, es_creds, url) def _construct_panels(self): size_x = 6 @@ -137,7 +138,7 @@ class KibanaDashboard(dict): def _publish(self): url = urlparse.urljoin(base_elastic_url, '/.kibana/dashboard/{}'.format(self.id)) logger.debug("publishing dashboard '{}'".format(url)) - elastic_access.publish_json(self, es_creds, url) + elastic_access.publish_docs(self, es_creds, url) def publish(self): self._publish_visualizations() @@ -251,7 +252,7 @@ def _get_pods_and_scenarios(project_name, case_name, installer): } }) - elastic_data = elastic_access.get_elastic_docs(urlparse.urljoin(base_elastic_url, '/test_results/mongo2elastic'), + elastic_data = elastic_access.get_docs(urlparse.urljoin(base_elastic_url, '/test_results/mongo2elastic'), es_creds, query_json) pods_and_scenarios = {} diff --git a/dashboard/dashboard/mongo2elastic/main.py b/dashboard/dashboard/mongo2elastic/main.py index 25b5320..82b01e4 100644 --- a/dashboard/dashboard/mongo2elastic/main.py +++ b/dashboard/dashboard/mongo2elastic/main.py @@ -64,7 +64,7 @@ class DocumentPublisher: self._publish() def _publish(self): - status, data = elastic_access.publish_json(self.doc, self.creds, self.to) + status, data = elastic_access.publish_docs(self.doc, self.creds, self.to) if status > 300: logger.error('Publish record[{}] failed, due to [{}]' .format(self.doc, json.loads(data)['error']['reason'])) -- cgit 1.2.3-korg