Skip to content

Commit

Permalink
Merge pull request #475 from hakobera/support-infuxdb
Browse files Browse the repository at this point in the history
Feature: Support InfluxDB v0.9+
  • Loading branch information
arikfr committed Jul 5, 2015
2 parents 05c2c21 + 00edc29 commit 39db74f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
83 changes: 83 additions & 0 deletions redash/query_runner/influx_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import json
import logging

from redash.utils import JSONEncoder
from redash.query_runner import *

logger = logging.getLogger(__name__)

try:
from influxdb import InfluxDBClusterClient
enabled = True

except ImportError:
logger.warning("Missing dependencies. Please install influxdb.")
logger.warning("You can use pip: pip install influxdb")
enabled = False

def _transform_result(results):
result_columns = []
result_rows = []

for result in results:
if not result_columns:
for c in result.raw['series'][0]['columns']:
result_columns.append({ "name": c })

for point in result.get_points():
result_rows.append(point)

return json.dumps({
"columns" : result_columns,
"rows" : result_rows
}, cls=JSONEncoder)

class InfluxDB(BaseQueryRunner):
@classmethod
def configuration_schema(cls):
return {
'type': 'object',
'properties': {
'url': {
'type': 'string'
}
},
'required': ['url']
}

@classmethod
def enabled(cls):
return enabled

@classmethod
def annotate_query(cls):
return False

@classmethod
def type(cls):
return "influxdb"

def __init__(self, configuration_json):
super(InfluxDB, self).__init__(configuration_json)

def run_query(self, query):
client = InfluxDBClusterClient.from_DSN(self.configuration['url'])

logger.debug("influxdb url: %s", self.configuration['url'])
logger.debug("influxdb got query: %s", query)

try:
results = client.query(query)
if not isinstance(results, list):
results = [results]

json_data = _transform_result(results)
error = None
except Exception, ex:
json_data = None
error = ex.message

return json_data, error


register(InfluxDB)
1 change: 1 addition & 0 deletions redash/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def parse_boolean(str):
'redash.query_runner.pg',
'redash.query_runner.script',
'redash.query_runner.url',
'redash.query_runner.influx_db',
])))

# Features:
Expand Down

0 comments on commit 39db74f

Please sign in to comment.