|
| 1 | +import logging |
| 2 | + |
| 3 | +from redash.query_runner import * |
| 4 | +from redash.utils import json_dumps |
| 5 | + |
| 6 | +logger = logging.getLogger(__name__) |
| 7 | + |
| 8 | +try: |
| 9 | + from arango import ArangoClient |
| 10 | + |
| 11 | + enabled = True |
| 12 | +except ImportError: |
| 13 | + enabled = False |
| 14 | + |
| 15 | + |
| 16 | +_TYPE_MAPPINGS = { |
| 17 | + "boolean": TYPE_BOOLEAN, |
| 18 | + "number": TYPE_FLOAT, |
| 19 | + "string": TYPE_STRING, |
| 20 | + "array": TYPE_STRING, |
| 21 | + "object": TYPE_STRING, |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +class Arango(BaseQueryRunner): |
| 26 | + noop_query = "RETURN {'id': 1}" |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def name(cls): |
| 30 | + return "ArangoDB" |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def configuration_schema(cls): |
| 34 | + return { |
| 35 | + "type": "object", |
| 36 | + "properties": { |
| 37 | + "user": {"type": "string"}, |
| 38 | + "password": {"type": "string"}, |
| 39 | + "host": {"type": "string", "default": "127.0.0.1"}, |
| 40 | + "port": {"type": "number", "default": 8529}, |
| 41 | + "dbname": {"type": "string", "title": "Database Name"}, |
| 42 | + "timeout": {"type": "number", "default": 0.0, "title": "AQL Timeout in seconds (0 = no timeout)"}, |
| 43 | + }, |
| 44 | + "order": ["host", "port", "user", "password", "dbname"], |
| 45 | + "required": ["host", "user", "password", "dbname"], |
| 46 | + "secret": ["password"], |
| 47 | + } |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def enabled(cls): |
| 51 | + try: |
| 52 | + import arango |
| 53 | + except ImportError: |
| 54 | + return False |
| 55 | + |
| 56 | + return True |
| 57 | + |
| 58 | + @classmethod |
| 59 | + def type(cls): |
| 60 | + return "arangodb" |
| 61 | + |
| 62 | + def run_query(self, query, user): |
| 63 | + client = ArangoClient(hosts='{}:{}'.format(self.configuration["host"], |
| 64 | + self.configuration.get("port", 8529))) |
| 65 | + db = client.db(self.configuration["dbname"], |
| 66 | + username=self.configuration["user"], |
| 67 | + password=self.configuration["password"]) |
| 68 | + |
| 69 | + try: |
| 70 | + cursor = db.aql.execute(query, max_runtime=self.configuration.get("timeout", 0.0)) |
| 71 | + result = [i for i in cursor] |
| 72 | + column_tuples = [ |
| 73 | + (i, TYPE_STRING) for i in result[0].keys() |
| 74 | + ] |
| 75 | + columns = self.fetch_columns(column_tuples) |
| 76 | + data = { |
| 77 | + "columns": columns, |
| 78 | + "rows": result, |
| 79 | + } |
| 80 | + |
| 81 | + json_data = json_dumps(data, ignore_nan=True) |
| 82 | + error = None |
| 83 | + except Exception: |
| 84 | + raise |
| 85 | + |
| 86 | + return json_data, error |
| 87 | + |
| 88 | + |
| 89 | +register(Arango) |
0 commit comments