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

Commit

Permalink
Fix timezone issues, update Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Danni Moiseyev committed May 4, 2020
1 parent a29e747 commit 1b63dd8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
12 changes: 7 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
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/

EXPOSE 8080
ENV REDIS_HOST=localhost
ENV REDIS_PORT=6379

CMD /app/GrafanaDatastoreServer.py --redis-server $REDIS_HOST --redis-port $REDIS_PORT
CMD /app/GrafanaDatastoreServer.py --redis-server $REDIS_HOST --redis-port $REDIS_PORT
10 changes: 3 additions & 7 deletions GrafanaDatastoreServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 1b63dd8

Please sign in to comment.