summaryrefslogtreecommitdiffstats
path: root/api/utils/influx.py
diff options
context:
space:
mode:
authorchenjiankun <chenjiankun1@huawei.com>2016-11-17 08:01:14 +0000
committerchenjiankun <chenjiankun1@huawei.com>2016-11-21 08:59:40 +0000
commit0e23c697e6329a57ba168cc57886b436ea87cdc4 (patch)
tree9344c63ed5296375dda7b0f0f2a3d85c06d7048e /api/utils/influx.py
parenteeeca4f0cdeff152ec061bb9b40ae305547c027e (diff)
Create API to run test cases
JIRA: YARDSTICK-413 Change-Id: Ibf58b50b568fae3f2eea985b25ee33be0a3666b7 Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
Diffstat (limited to 'api/utils/influx.py')
-rw-r--r--api/utils/influx.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/api/utils/influx.py b/api/utils/influx.py
new file mode 100644
index 000000000..52a90b61c
--- /dev/null
+++ b/api/utils/influx.py
@@ -0,0 +1,55 @@
+import logging
+from urlparse import urlsplit
+
+from influxdb import InfluxDBClient
+import ConfigParser
+
+from api import conf
+
+logger = logging.getLogger(__name__)
+
+
+def get_data_db_client():
+ parser = ConfigParser.ConfigParser()
+ try:
+ parser.read(conf.OUTPUT_CONFIG_FILE_PATH)
+ dispatcher = parser.get('DEFAULT', 'dispatcher')
+
+ if 'influxdb' != 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)
+ except ConfigParser.NoOptionError:
+ logger.error('can not find the key')
+ raise
+
+
+def _get_ip(url):
+ return urlsplit(url).netloc.split(':')[0]
+
+
+def _write_data(measurement, field, timestamp, tags):
+ point = {
+ 'measurement': measurement,
+ 'fields': field,
+ 'time': timestamp,
+ 'tags': tags
+ }
+
+ try:
+ client = get_data_db_client()
+
+ logger.debug('Start to write data: %s', point)
+ client.write_points([point])
+ 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)