0

Can the Dashboard time range be used inside a query variable with MongoDB?

I want to extract the values of a variable of the data only inside the Time Range from the dashboard, but it seems that the global variabled corresponding to the Time-Range ($from, $to etc.) are not usable inside a query variable.

The Grafana documentation says about time range inside variables:

On Time Range Change - Queries the data source when the dashboard time range changes. Only use this option if your variable options query contains a time range filter or is dependent on the dashboard time range.

From that, I assume that it can be used, but not with MongoDB.

Any inside on how can this be solved? MongoDB query variables are incompatible with Time Range intervals?

Thanks in advance.

1 Answers1

1

I solved the issue. For some reason, $from and $to can be used in queries inside panels but not in variables of type query. But there exists some global variables equivalent to those, $__from and $__to that can be used (https://grafana.com/docs/grafana/latest/variables/variable-types/global-variables/).

Now, I have had problems comparing those variables to dates, my workaround was creating a new variable for each and transforming them to ms with the $toLong function, as comparing dates directly did not work.

Hope it helps someone.

  • 1
    Can you give us more details on the how-to? Thx. – lambruscoAcido Oct 25 '22 at 12:51
  • 1
    Unfortunetly I don't have access to the code anymore. But as I recall, I could not compare directly the variables $__from and $__to with the time variable from my data so I had to make a workaround and create a new variable that transformed $__from and $__to to Long with the $toLong function and then I could compare those variables to the time from the data. As far as I know, this was something unique to MongoDB and looked like a bug to me, I haven't had this problem with other databases. Hope it helps! – Aitor Domec Paz Oct 27 '22 at 07:29