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 variable as value for database or table in Query Editor #141

Closed
splichy opened this issue Sep 17, 2019 · 1 comment · Fixed by #224, #250 or grafana/grafana-plugin-repository#690
Closed

Comments

@splichy
Copy link

splichy commented Sep 17, 2019

Hi,

It would be nice to be able to use variable as value in query editor for e.g. database (imagine you have multiple databases for more environments a you would like to switch dashboard to different DB by simply changing variable)
Now if I want to accomplish this I cannot simply use $table variable directly in query - as variable from Query Editor isn't evaluated.

Example:
$database = collectd
Query editor: database: $database table: metrics

Query: $perSecondColumns(hwid, value) FROM $table WHERE $timeFilter AND metric IN (....

Result:
SELECT t, groupArray((hwid, max_0_Rate)) AS groupArr FROM ( SELECT t, hwid, if(runningDifference(max_0) < 0, nan, runningDifference(max_0) / runningDifference(t/1000)) AS max_0_Rate FROM ( SELECT (intDiv(toUInt32(timestamp), 900) * 900) * 1000 AS t, hwid, max(value) AS max_0 FROM $database.metrics WHERE date >= toDate(...

Expected result:
SELECT t, groupArray((hwid, max_0_Rate)) AS groupArr FROM ( SELECT t, hwid, if(runningDifference(max_0) < 0, nan, runningDifference(max_0) / runningDifference(t/1000)) AS max_0_Rate FROM ( SELECT (intDiv(toUInt32(timestamp), 900) * 900) * 1000 AS t, hwid, max(value) AS max_0 FROM collectd.metrics WHERE date >= toDate(...

This can be workarounded with query like:
$perSecondColumns(hwid, value) FROM $database.metrics WHERE $timeFilter AND metric IN (....

But this is still workaround as table name must be 'hardcoded' into the query.

A bit strange is that when I will fill $database into database value then I will get correct table names recommendation in table field.

@Slach
Copy link
Collaborator

Slach commented Jul 7, 2020

Dear @splichy, sorry for the late response, I promise we will get more quickly and qualified feedback for our plugin users
unfortunately, the current plugin code base can't apply $table macro with recursive $templateVariable and i don't see how we can improve it without geoengineering, feel free make PR for it

as a workaround, instead of using $table macro, you can define couple template variables in your dashboard with type Query and source
$template_db with the following query: SELECT name FROM system.databases
and $template_table with the following query: SELECT name FROM system.tables WHERE database IN '${template_db}'

and use following SQL query

SELECT
    $timeSeries as t,
    count()
FROM $template_db.$template_table
WHERE $timeFilter
GROUP BY t
ORDER BY t

@Slach Slach closed this as completed in #224 Jul 7, 2020
Slach added a commit that referenced this issue Jul 7, 2020
add Dashboard database_as_template_variable.json, fix #141
Slach added a commit that referenced this issue Jul 24, 2020
# 2.0.3 (2020-07-24)

## Enhancements:
* add setup notes for Grafana 7.x to README
* add SQL preprocessing logic on browser side with <% js code subset %>, #186, thanks @fgbogdan
* improve alerts query processing for use case when `query(query_name, from, to)` time range is less than visible dashboard time range, see #237
* improve alerts json parsing in golang part for case when we have string fields in response which interprets as series name, see #230
* properly parsing POST queries in golang part of plugin, #228, thanks @it1804


## Fixes:
* add Vagrantfile for statefull environment and allow to upgrade scenario like  grafana 7.1.0 + grafana-cli upgrade-all
  * fix #244
  * fix #243
* add multiple dashboard examples for github issues:
  * fix #240 
  * fix #135 
  * fix #245 
  * fix #238   
  * fix #232
  * fix #127
  * fix #141
Slach added a commit to Altinity/grafana-plugin-repository that referenced this issue Jul 24, 2020
## Enhancements:
* add setup notes for Grafana 7.x to README
* add SQL preprocessing logic on browser side with <% js code subset %>, Altinity/clickhouse-grafana#186, thanks @fgbogdan
* improve alerts query processing for use case when `query(query_name, from, to)` time range is less than visible dashboard time range, see Altinity/clickhouse-grafana#237
* improve alerts json parsing in golang part for case when we have string fields in response which interprets as series name, see Altinity/clickhouse-grafana#230
* properly parsing POST queries in golang part of plugin, Altinity/clickhouse-grafana#228, thanks @it1804

## Fixes:
* add Vagrantfile for statefull environment and allow to upgrade scenario like  grafana 7.1.0 + grafana-cli upgrade-all
  * fix Altinity/clickhouse-grafana#244
  * fix Altinity/clickhouse-grafana#243
* add multiple dashboard examples for github issues:
  * fix Altinity/clickhouse-grafana#240
  * fix Altinity/clickhouse-grafana#135
  * fix Altinity/clickhouse-grafana#245
  * fix Altinity/clickhouse-grafana#238
  * fix Altinity/clickhouse-grafana#232
  * fix Altinity/clickhouse-grafana#127
  * fix Altinity/clickhouse-grafana#141

Signed-off-by: Eugene Klimov <eklimov@altinity.com>
Slach added a commit to Altinity/grafana-plugin-repository that referenced this issue Aug 13, 2020
# 2.1.0 (2020-08-13)

## Enhancement:
* add "Skip comments" checkbox to query editor to pass SQL comments to server, fix Altinity/clickhouse-grafana#265
* add setup notes for Grafana 7.x to README
* add SQL preprocessing logic on browser side with <% js code subset %>, Altinity/clickhouse-grafana#186, thanks @fgbogdan
* improve alerts query processing for use case when `query(query_name, from, to)` time range is less than visible dashboard time range, see Altinity/clickhouse-grafana#237
* improve alerts json parsing in golang part for case when we have string fields in response which interprets as series name, see Altinity/clickhouse-grafana#230
* properly parsing POST queries in golang part of plugin, Altinity/clickhouse-grafana#228, thanks @it1804

## Fixes:
* fix corner cases for $macro + subquery, see Altinity/clickhouse-grafana#276 and Altinity/clickhouse-grafana#277
* fix parallel query execution, see Altinity/clickhouse-grafana#273
* fix identifiers quotes, see Altinity/clickhouse-grafana#276, Altinity/clickhouse-grafana#277
* fix plugin.json for pass `grafana-plugin-repository` plugin validator
* fix multi-value variables behavior - Altinity/clickhouse-grafana#252
* add Vagrantfile for statefull environment and allow to upgrade scenario like  grafana 7.1.0 + grafana-cli upgrade-all
  * fix Altinity/clickhouse-grafana#244
  * fix Altinity/clickhouse-grafana#243
* add multiple dashboard examples for github issues:
  * fix Altinity/clickhouse-grafana#240
  * fix Altinity/clickhouse-grafana#135
  * fix Altinity/clickhouse-grafana#245
  * fix Altinity/clickhouse-grafana#238
  * fix Altinity/clickhouse-grafana#232
  * fix Altinity/clickhouse-grafana#127
  * fix Altinity/clickhouse-grafana#141

Signed-off-by: Eugene Klimov <eklimov@altinity.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants