0

I'm using Grafana with JSON API data source and I'd like to build a query that depends on the selected period of time selected in the upper right corner of the screen.

Is there any variable (or something like that), which I can send the selected time range form Grafana and receive it in my backend?

In other words, If I select 24hs in Grafana I'd like to use that in my query and return only data in this period.

I tried to get request from Grafana, which should contain the time range. However I got error: Failed to decode JSON object: Expecting value: line 1 column 1 (char 0).

It's possible that I misunderstood something and it doesn't work that way.

This is my /query endpoint:

@app.route('/query', methods=['POST', 'GET'])
def query():

    req = request.get_json()   <- failed
    range = req['request']['range']
    
    json_data = get_from_database(range)

    return json_data

Are there any other options, like sending the time range ( with these variables {__from}&to=${__to}) in URL?

kamila
  • 71
  • 10
  • There is example which uses the request: https://github.com/otto-torino/grafana-json-backend/blob/d50e269b7d229f2d6ab49851640bb9edf3555fde/app/app.py – kamila Jul 15 '22 at 08:14

1 Answers1

1

You can use the global variables $__from and $__to. As explained in the docs, ${__from} will give you Unix millisecond epoch, but there are also other format options.

How to use variables in the JSON API plugin is explained in the docs. So you can either use them as params what will result in a URL like this /query?range_start=${__from}&range_end=${__to} or use them directly in your path like this /query/${__from}/${__to}.

For retrieving them using python: you will find a lot on that topic on SO. Basically, I think you don't need .get_json() (will not work if the request is not application/json). If you send them as params, use request.args.get('range_start') to get the value (short explanation).

dnnshssm
  • 1,057
  • 6
  • 17
  • 1
    So, I should set them in params - key: range_start, value: ${__from}? – kamila Jul 15 '22 at 08:36
  • Exactly, I clarified it in my answer – dnnshssm Jul 15 '22 at 08:44
  • Could you look if I set everything correct? My path in grafana: /query My params: key: from, value: ${__from:date} key: to, value: ${__to:date} and my code: `@app.route('/query', methods=methods) def query(): range_start = request.args.get('from') range_end = request.args.get('to') range = {'start': range_start, 'end': range_end} print(range) json_data = get_from_database(range) return json_data` – kamila Jul 15 '22 at 09:25
  • However when I print my time range, at first I get corret value, but it immediately changes to None: print(range) output: {'start': '2022-02-02T23:12:55.623Z', 'end': '2022-02-09T08:05:21.929Z'} {'start': None, 'end': None} – kamila Jul 15 '22 at 09:26
  • Looks good and according to you it's working. Are you still running another request from another panel to that endpoint? That could result in the 'None', since the params are not present. – dnnshssm Jul 15 '22 at 09:32
  • No, I only use one panel with this endpoint – kamila Jul 15 '22 at 09:35
  • 1
    I guess the grafana part is sorted, I would suggest you ask a seperate question with details about your endpoint – dnnshssm Jul 15 '22 at 09:49