diff options
author | SerenaFeng <feng.xiaowei@zte.com.cn> | 2016-09-22 16:15:58 +0800 |
---|---|---|
committer | SerenaFeng <feng.xiaowei@zte.com.cn> | 2016-09-22 16:15:58 +0800 |
commit | 39d1381bb4b7d6041a8f05b198db911c9e583da5 (patch) | |
tree | 49faaca654d96fc9f47a7702ae9ae90cd985af2e /dashboard/dashboard/common | |
parent | 71edb3b92cbbf4a4f648dc4442efea29b76a157e (diff) |
rebuild directory structure of Kibana dashboard
JIRA: FUNCTEST-465
Change-Id: Icecd350b2f67105c8aaa9d71fd76d24827515545
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'dashboard/dashboard/common')
-rw-r--r-- | dashboard/dashboard/common/__init__.py | 0 | ||||
-rw-r--r-- | dashboard/dashboard/common/elastic_access.py | 64 | ||||
-rw-r--r-- | dashboard/dashboard/common/logger_utils.py | 65 |
3 files changed, 129 insertions, 0 deletions
diff --git a/dashboard/dashboard/common/__init__.py b/dashboard/dashboard/common/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/dashboard/dashboard/common/__init__.py diff --git a/dashboard/dashboard/common/elastic_access.py b/dashboard/dashboard/common/elastic_access.py new file mode 100644 index 0000000..e90a17f --- /dev/null +++ b/dashboard/dashboard/common/elastic_access.py @@ -0,0 +1,64 @@ +import json + +import urllib3 + +http = urllib3.PoolManager() + + +def delete_request(url, creds, body=None): + headers = urllib3.make_headers(basic_auth=creds) + http.request('DELETE', url, headers=headers, body=body) + + +def publish_json(json_ojb, creds, to): + json_dump = json.dumps(json_ojb) + if to == 'stdout': + print json_dump + return 200, None + else: + headers = urllib3.make_headers(basic_auth=creds) + result = http.request('POST', to, headers=headers, body=json_dump) + return result.status, result.data + + +def _get_nr_of_hits(elastic_json): + return elastic_json['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) + + # 2. get all results + elastic_json = json.loads(http.request('GET', elastic_url + '/_search?size={}'.format(nr_of_hits), headers=headers, body=body).data) + + elastic_docs = [] + for hit in elastic_json['hits']['hits']: + elastic_docs.append(hit[field]) + return elastic_docs + + +def get_elastic_docs_by_days(elastic_url, creds, days): + if days == 0: + body = '''{ + "query": { + "match_all": {} + } + }''' + elif days > 0: + body = '''{{ + "query" : {{ + "range" : {{ + "start_date" : {{ + "gte" : "now-{}d" + }} + }} + }} + }}'''.format(days) + else: + raise Exception('Update days must be non-negative') + return get_elastic_docs(elastic_url, creds, body) diff --git a/dashboard/dashboard/common/logger_utils.py b/dashboard/dashboard/common/logger_utils.py new file mode 100644 index 0000000..1830808 --- /dev/null +++ b/dashboard/dashboard/common/logger_utils.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# +# feng.xiaowei@zte.com.cn +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Logging levels: +# Level Numeric value +# CRITICAL 50 +# ERROR 40 +# WARNING 30 +# INFO 20 +# DEBUG 10 +# NOTSET 0 +# +# Usage: +# import functest_logger as fl +# logger = fl.Logger("script_name").getLogger() +# logger.info("message to be shown with - INFO - ") +# logger.debug("message to be shown with - DEBUG -") + +import logging +import os + + +class Logger(object): + file_path = '/var/log' + formatter = logging.Formatter('%(asctime)s - %(name)s - ' + '%(levelname)s - %(message)s') + + def __init__(self, logger_name): + + IF_DEBUG = os.getenv('IF_DEBUG') + + self.logger_name = logger_name + self.logger = logging.getLogger(logger_name) + self.logger.propagate = 0 + self.logger.setLevel(logging.DEBUG) + + ch = logging.StreamHandler() + ch.setFormatter(self.formatter) + if IF_DEBUG is not None and IF_DEBUG.lower() == "true": + ch.setLevel(logging.DEBUG) + else: + ch.setLevel(logging.INFO) + self.logger.addHandler(ch) + + hdlr = logging.FileHandler('%s/%s.log' % (self.file_path, logger_name)) + hdlr.setFormatter(self.formatter) + hdlr.setLevel(logging.DEBUG) + self.logger.addHandler(hdlr) + + @property + def get(self): + return self.logger + + +class DashboardLogger(Logger): + file_path = '/var/log/kibana_dashboard' + + def __init__(self, logger_name): + super(DashboardLogger, self).__init__(logger_name) + |