From 1b63dd8c897e2444070b015e940d3f1b31fceefd Mon Sep 17 00:00:00 2001 From: Danni Moiseyev Date: Mon, 4 May 2020 11:03:29 +0300 Subject: [PATCH] Fix timezone issues, update Dockerfile --- Dockerfile | 12 +++++++----- GrafanaDatastoreServer.py | 10 +++------- README.md | 3 +++ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index b627184..fdd929a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,13 @@ -FROM python:alpine3.9 +FROM python:3-alpine + WORKDIR /app/ COPY requirements.txt /app/ -RUN apk add --no-cache --virtual build-deps gcc musl-dev libffi-dev \ - && pip install -r requirements.txt \ - && apk del build-deps + +RUN apk update && apk add --no-cache --virtual build-deps build-base gcc musl-dev libffi-dev libev-dev +RUN pip install -r requirements.txt +RUN apk del build-deps COPY GrafanaDatastoreServer.py /app/ @@ -13,4 +15,4 @@ EXPOSE 8080 ENV REDIS_HOST=localhost ENV REDIS_PORT=6379 -CMD /app/GrafanaDatastoreServer.py --redis-server $REDIS_HOST --redis-port $REDIS_PORT \ No newline at end of file +CMD /app/GrafanaDatastoreServer.py --redis-server $REDIS_HOST --redis-port $REDIS_PORT diff --git a/GrafanaDatastoreServer.py b/GrafanaDatastoreServer.py index 2bb7361..acbfc16 100755 --- a/GrafanaDatastoreServer.py +++ b/GrafanaDatastoreServer.py @@ -12,8 +12,6 @@ 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 = {} @@ -63,11 +61,9 @@ def query(): request = flask.request.get_json() response = [] - # !!! 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) + # dates 'from' and 'to' are expected to be in UTC, which is what Grafana provides here. + stime = dateutil.parser.parse(request['range']['from']).timestamp() * 1000 + etime = dateutil.parser.parse(request['range']['to']).timestamp() * 1000 redis_client = redis.Redis(connection_pool=REDIS_POOL) targets = process_targets([t['target'] for t in request['targets']], redis_client) diff --git a/README.md b/README.md index 94320ba..fbd7b43 100644 --- a/README.md +++ b/README.md @@ -51,3 +51,6 @@ optional arguments: --redis-port REDIS_PORT redis server port ``` + +#### Note about timezone +Grafana uses UTC timestamps to query its datastores. This datastore will use the same timestamps to query Redis, which means that it assumes all timestamps are UTC based. \ No newline at end of file