X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=api%2Futils%2Finflux.py;h=275c63a246b8c87e38c0113cdd6753399e5b7d82;hb=8f91b0a5c1f47953dd1a14541b71505dbf8f869a;hp=1d56c95fddefca8872899b46527d69fd43ea6028;hpb=d491d77dea52e459dc6f1e1ec39703e26a4857e7;p=yardstick.git diff --git a/api/utils/influx.py b/api/utils/influx.py index 1d56c95fd..275c63a24 100644 --- a/api/utils/influx.py +++ b/api/utils/influx.py @@ -1,8 +1,18 @@ +############################################################################## +# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others. +# +# 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 +############################################################################## +from __future__ import absolute_import + import logging -from urlparse import urlsplit +import six.moves.configparser as ConfigParser +from six.moves.urllib.parse import urlsplit from influxdb import InfluxDBClient -import ConfigParser from api import conf @@ -13,43 +23,33 @@ def get_data_db_client(): parser = ConfigParser.ConfigParser() try: parser.read(conf.OUTPUT_CONFIG_FILE_PATH) - dispatcher = parser.get('DEFAULT', 'dispatcher') - if 'influxdb' != dispatcher: + if 'influxdb' != parser.get('DEFAULT', 'dispatcher'): raise RuntimeError - ip = _get_ip(parser.get('dispatcher_influxdb', 'target')) - username = parser.get('dispatcher_influxdb', 'username') - password = parser.get('dispatcher_influxdb', 'password') - db_name = parser.get('dispatcher_influxdb', 'db_name') - return InfluxDBClient(ip, conf.PORT, username, password, db_name) + return _get_client(parser) except ConfigParser.NoOptionError: logger.error('can not find the key') raise +def _get_client(parser): + ip = _get_ip(parser.get('dispatcher_influxdb', 'target')) + username = parser.get('dispatcher_influxdb', 'username') + password = parser.get('dispatcher_influxdb', 'password') + db_name = parser.get('dispatcher_influxdb', 'db_name') + return InfluxDBClient(ip, conf.PORT, username, password, db_name) + + def _get_ip(url): return urlsplit(url).hostname -def _write_data(measurement, field, timestamp, tags): - point = { - 'measurement': measurement, - 'fields': field, - 'time': timestamp, - 'tags': tags - } - +def query(query_sql): try: client = get_data_db_client() - - logger.debug('Start to write data: %s', point) - client.write_points([point]) + logger.debug('Start to query: %s', query_sql) + return list(client.query(query_sql).get_points()) except RuntimeError: - logger.debug('dispatcher is not influxdb') - - -def write_data_tasklist(task_id, timestamp, status, error=''): - field = {'status': status, 'error': error} - tags = {'task_id': task_id} - _write_data('tasklist', field, timestamp, tags) + logger.error('dispatcher is not influxdb') + raise