Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use templateSrv to interpolate timeFrom and timeTo variables. #31

Merged
merged 1 commit into from
Nov 3, 2020

Conversation

carvid
Copy link
Contributor

@carvid carvid commented Oct 20, 2020

I realized that timeFrom and timeTo variables can be used to access the overridden time range values at the panel level (Query Options). Those variables were interpolated using a regex instead of using templateSrv, thus preventing custom date formats to be used. With this change, it's now possible to specify a custom date format for timeFrom and timeTo variables if running on Grafana >= v7.2. For example:

query {
  data:submissions(
    where: {
      submitTime: {_gte: "${timeFrom:date}", _lte: "${timeTo:date}"},
    }
  ) {
    Time:submitTime
  }
}

I realized that `timeFrom` and `timeTo` variables can be used to access the
overridden time range values at the panel level (Query Options). Those
variables were interpolated using a regex instead of using `templateSrv`, thus
preventing custom date formats to be used. With this change, it's now possible
to specify a custom date format for `timeFrom` and `timeTo` variables if
running on Grafana >= v7.2. For example:

```
query {
  data:submissions(
    where: {
      submitTime: {_gte: "${timeFrom:date}", _lte: "${timeTo:date}"},
    }
  ) {
    Time:submitTime
  }
}
```
@retrodaredevil
Copy link
Contributor

I meant to make this PR a long time ago but I never got around to it. It looks good to me, we might consider removing timeFrom and timeTo altogether unless we want to keep backwards compatibility with older queries since we just use __from and __to now.

But this is definitely much better than what we had before.

@retzkek
Copy link
Contributor

retzkek commented Oct 20, 2020

Date formats are fully supported using the builtin $__from and $__to variables, which are the recommended/documented variables to use now. I do think we should keep the legacy variables for backwards comparability, but I don't think we should add new features to them.

@carvid
Copy link
Contributor Author

carvid commented Oct 20, 2020

@retrodaredevil and @retzkek I might getting this wrong since I'm not a Grafana expert, but Isn't timeFrom and timeTo meant to access the overridden time range from the panel's "Query Options"?. I'm asking that because the builtin $__from and $__to always return the dashboard's time range.

@retzkek
Copy link
Contributor

retzkek commented Oct 20, 2020

isn't timeFrom and timeTo meant to access the overridden time range from the panel's "Query Options"?.

No, they were just internal variables, from before the global builtins were added in Grafana 7.

the builtin $__from and $__to always return the dashboard's time range.

It looks like that's a bug grafana/grafana#20147

@carvid
Copy link
Contributor Author

carvid commented Oct 21, 2020

@retzkek thanks for the clarifying that. It seems that bug is there since Grafana v6.4.3. Other datasource plugins provide a different ways to access the time range so I guess that's the reason the bug is not fixed yet (e.g. MySQL and Postgres datasource provide __timeFilter, __timeFrom and __timeTo among other macros).

For now, I guess timeFrom and timeTo are the only workaround to get the panel's relative time for this plugin.

Copy link
Contributor

@retzkek retzkek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this, sorry for the delay. It seems to work OK, although unfortunately the date format options that are provided for __from and __to don't seem to work, but the behavior doesn't change from what we had before, so 🤷. Presumably this is some limitation of templateSrv, but documentation here is scarce.

src/DataSource.ts Show resolved Hide resolved
@retzkek
Copy link
Contributor

retzkek commented Nov 3, 2020

LGMT. Thanks for contributing!

@retzkek retzkek merged commit 608c1b8 into fifemon:master Nov 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants