Skip to content
This repository has been archived by the owner on Aug 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request #6 from streamnsight/master
Browse files Browse the repository at this point in the history
fix for redistimeseries breaking changes + pattern search
  • Loading branch information
Danni Moiseyev authored Aug 12, 2019
2 parents e7cc610 + 03a2f8f commit b0bfb8c
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions GrafanaDatastoreServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import argparse
import redis
import flask
import calendar
from datetime import timedelta, datetime
import dateutil.parser
from gevent.pywsgi import WSGIServer
from flask import Flask, jsonify
Expand All @@ -12,6 +12,8 @@
app = Flask(__name__)
CORS(app)

EPOCH = datetime(1970, 1, 1, 0, 0, 0)

REDIS_POOL = None
SCAN_TYPE_SCRIPT = """local cursor, pat, typ, cnt = ARGV[1], ARGV[2], ARGV[3], ARGV[4] or 100
local rep = {}
Expand Down Expand Up @@ -41,7 +43,7 @@ def search():
cursor = 0
while True:
cursor, keys = redis_client.eval(SCAN_TYPE_SCRIPT, 0, cursor, "*", "TSDB-TYPE", 100)
result.extend(keys)
result.extend([k.decode("ascii") for k in keys])
if cursor == 0:
break

Expand All @@ -51,7 +53,7 @@ def process_targets(targets, redis_client):
result = []
for target in targets:
if '*' in target:
result.extend(redis_client.keys(target))
result.extend([k.decode('ascii') for k in redis_client.keys(target)])
else:
result.append(target)
return result
Expand All @@ -61,19 +63,22 @@ def query():
request = flask.request.get_json()
response = []

stime = calendar.timegm(dateutil.parser.parse(request['range']['from']).timetuple())
etime = calendar.timegm(dateutil.parser.parse(request['range']['to']).timetuple())
# !!! dates 'from' and 'to' are expected to be in UTC, which is what Grafana provides here.
# If not in UTC, use pytz to set to UTC timezone and subtract the utcoffset().
# Time delta calculations should always be done in UTC to avoid pitfalls of daylight offset changes.
stime = (dateutil.parser.parse(request['range']['from']) - EPOCH) / timedelta(milliseconds=1)
etime = (dateutil.parser.parse(request['range']['to']) - EPOCH) / timedelta(milliseconds=1)

redis_client = redis.Redis(connection_pool=REDIS_POOL)
targets = process_targets([t['target'] for t in request['targets']], redis_client)

for target in targets:
args = ['ts.range', target, int(stime), int(etime)]
if 'intervalMs' in request and request['intervalMs'] > 0 and request['intervalMs']/1000 > 1:
args += ['avg', int(round(request['intervalMs']/1000))]
if 'intervalMs' in request and request['intervalMs'] > 0:
args += ['avg', int(request['intervalMs'])]
print(args)
redis_resp = redis_client.execute_command(*args)
datapoints = [(float(x2.decode("ascii")), x1*1000) for x1, x2 in redis_resp]
datapoints = [(float(x2.decode("ascii")), x1) for x1, x2 in redis_resp]
response.append(dict(target=target, datapoints=datapoints))
return jsonify(response)

Expand Down

0 comments on commit b0bfb8c

Please sign in to comment.