Skip to content

Commit

Permalink
Fix prometheus query runner: get_schema and query range (#3471)
Browse files Browse the repository at this point in the history
  • Loading branch information
veerkat authored and arikfr committed Feb 25, 2019
1 parent d36e5ac commit 1a357df
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions redash/query_runner/prometheus.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import requests
import time
from datetime import datetime
from dateutil import parser
from urlparse import parse_qs
from redash.query_runner import BaseQueryRunner, register, TYPE_DATETIME, TYPE_STRING
from redash.utils import json_dumps
Expand Down Expand Up @@ -37,6 +39,30 @@ def get_range_rows(metrics_data):
return rows


# Convert datetime string to timestamp
def convert_query_range(payload):
query_range = {}

for key in ['start', 'end']:
if key not in payload.keys():
continue
value = payload[key][0]

if type(value) is str:
# Don't convert timestamp string
try:
int(value)
continue
except ValueError:
pass
value = parser.parse(value)

if type(value) is datetime:
query_range[key] = [int(time.mktime(value.timetuple()))]

payload.update(query_range)


class Prometheus(BaseQueryRunner):

@classmethod
Expand Down Expand Up @@ -69,8 +95,7 @@ def get_schema(self, get_stats=False):

schema = {}
for name in data:
schema[name] = {'name': name}

schema[name] = {'name': name, 'columns': []}
return schema.values()

def run_query(self, query, user):
Expand Down Expand Up @@ -117,7 +142,9 @@ def run_query(self, query, user):
# for the range of until now
if query_type == 'query_range' and ('end' not in payload.keys() or 'now' in payload['end']):
date_now = datetime.now()
payload.update({"end": [date_now.isoformat("T") + "Z"]})
payload.update({'end': [date_now]})

convert_query_range(payload)

api_endpoint = base_url + '/api/v1/{}'.format(query_type)

Expand Down

0 comments on commit 1a357df

Please sign in to comment.