mirror of
https://github.com/valitydev/redash.git
synced 2024-11-07 09:28:51 +00:00
commit
32638aebed
57
redash/query_runner/mql.py
Normal file
57
redash/query_runner/mql.py
Normal file
@ -0,0 +1,57 @@
|
||||
import json
|
||||
|
||||
from . import BaseQueryRunner, register
|
||||
from .mongodb import TYPES_MAP, TYPE_STRING
|
||||
|
||||
try:
|
||||
import pymongo
|
||||
from ognom import query_to_plan
|
||||
from website.server.utils import simplify
|
||||
enabled = True
|
||||
except ImportError:
|
||||
enabled = False
|
||||
|
||||
def deduce_columns(rows):
|
||||
column_to_type = {}
|
||||
for row in rows:
|
||||
for column, value in row.iteritems():
|
||||
column_to_type[column] = TYPES_MAP.get(value.__class__, TYPE_STRING)
|
||||
return [{'name': column, 'friendly_name': column, 'type': type}
|
||||
for column, type in column_to_type.iteritems()]
|
||||
|
||||
class MQL(BaseQueryRunner):
|
||||
|
||||
def __init__(self, configuration_json):
|
||||
super(MQL, self).__init__(configuration_json)
|
||||
self.syntax = 'sql'
|
||||
|
||||
@classmethod
|
||||
def enabled(cls):
|
||||
return enabled
|
||||
|
||||
@classmethod
|
||||
def annotate_query(cls):
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def configuration_schema(cls):
|
||||
return {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'uri': {
|
||||
'type': 'string',
|
||||
'title': 'Connection String'
|
||||
}
|
||||
},
|
||||
'required': ['uri']
|
||||
}
|
||||
|
||||
def run_query(self, query):
|
||||
conn = pymongo.MongoClient(self.configuration['uri'])
|
||||
# execute() returns a generator (that wraps a cursor)
|
||||
gen = query_to_plan(query).execute(conn)
|
||||
# simplify converts special MongoDB data types (ObjectId, Date, etc') to strings
|
||||
result = simplify(list(gen))
|
||||
return json.dumps({'columns': deduce_columns(result), 'rows': result}), None
|
||||
|
||||
register(MQL)
|
@ -118,6 +118,7 @@ default_query_runners = [
|
||||
'redash.query_runner.google_spreadsheets',
|
||||
'redash.query_runner.graphite',
|
||||
'redash.query_runner.mongodb',
|
||||
'redash.query_runner.mql',
|
||||
'redash.query_runner.mysql',
|
||||
'redash.query_runner.pg',
|
||||
'redash.query_runner.url',
|
||||
|
14
tests/query_runner/test_mql.py
Normal file
14
tests/query_runner/test_mql.py
Normal file
@ -0,0 +1,14 @@
|
||||
from datetime import datetime
|
||||
from unittest import TestCase
|
||||
from redash.query_runner import TYPE_DATETIME, TYPE_INTEGER, TYPE_STRING
|
||||
from redash.query_runner.mql import deduce_columns
|
||||
|
||||
|
||||
class TestMQL(TestCase):
|
||||
def test_deduce_columns(self):
|
||||
self.assertEquals(deduce_columns([{'a': 1}]),
|
||||
[{'name': 'a', 'friendly_name': 'a', 'type': TYPE_INTEGER}])
|
||||
self.assertEquals(deduce_columns([{'a': 'foo'}]),
|
||||
[{'name': 'a', 'friendly_name': 'a', 'type': TYPE_STRING}])
|
||||
self.assertEquals(deduce_columns([{'a': datetime.now()}]),
|
||||
[{'name': 'a', 'friendly_name': 'a', 'type': TYPE_DATETIME}])
|
Loading…
Reference in New Issue
Block a user