Grafana - 如何为 Mysql 数据源创建 sql 查询部分 variable/macro
Grafana - How to create sql query part variable/macro for Mysql datasource
我在 Grafana 中有以下查询,由 MySql DataSource 支持。
SELECT
$__timeGroupAlias(ts,$__interval),
sum(total) AS "total"
FROM hp
WHERE
$__timeFilter(ts)
AND customer_type IN ($CustomerType) AND age IN ($age) AND gender IN ($gender)
GROUP BY 1
ORDER BY $__timeGroup(ts,$__interval)
仪表板中有多个 singleStat/panel/graphs 使用不同的 select 参数,但 WHERE 条件在所有参数中都保持相同。
我想将条件保留为单独的常量变量,以便我可以在每个查询中只添加该变量。
我试过这样构建我的查询。
SELECT
$__timeGroupAlias(ts,$__interval),
sum(total) AS "total"
FROM hp
$where_condition
GROUP BY 1
ORDER BY $__timeGroup(ts,$__interval)
并声明 where_condition
为 WHERE $__timeFilter(ts) AND customer_type IN ($CustomerType) AND age IN ($age) AND gender IN ($gender)
。
但是查询失败了,因为查询生成器没有解析内部变量($CustomerType,$age,$gender),生成的查询看起来像这样。
SELECT
UNIX_TIMESTAMP(ts) DIV 900 * 900 AS "time",
sum(total) AS "total"
FROM hp
ts BETWEEN FROM_UNIXTIME(1548311714) AND FROM_UNIXTIME(1548398114)
AND customer_type IN ($CustomerType) AND age IN ($age) AND gender IN ($gender)
GROUP BY 1
ORDER BY UNIX_TIMESTAMP(ts) DIV 900 * 900
有没有办法解析包含在其他变量中的变量。或者有没有其他方法可以外部化包含变量的查询部分?
constant
变量类型只生成静态字符串。它没有替换任何变量。切换到 query
类型并使用 MySQL 到 return 字符串,这将为您的 where_condition
变量提供精确的字符串值。查询:
SELECT 'WHERE $__timeFilter(ts) AND customer_type IN ($CustomerType) AND age IN ($age) AND gender IN ($gender)'
恕我直言:变量替换应该也适用于 constant
类型。您可以打开 https://github.com/grafana/grafana/issues.
的功能请求
我也有引号问题,这是我的解决方案,支持 'All' 和许多选择:
SELECT '
task_run_table.is_system = false
AND
(
''__all__'' = ''${user:raw}''
OR
task_run_table.user = any ( string_to_array(''${user:raw}'', '','')::text[])
)
AND
(
''__all__'' = ''${job:raw}''
OR
task_run_table.job_name = any ( string_to_array(''${job:raw}'', '','')::text[])
)
'
All
值被覆盖为 __all__
。
它是这样使用的:
SELECT
$__timeGroup(task_run_table.start_time, '$interval', NULL),
task_run_table.name AS metric,
COUNT(*) AS value
FROM
task_run_table
WHERE
$__timeFilter(task_run_table.start_time)
AND
${default_filter:raw}
AND
task_run_table.state = $state
GROUP BY time, task_run_table.name
ORDER BY time, value DESC
我在 Grafana 中有以下查询,由 MySql DataSource 支持。
SELECT
$__timeGroupAlias(ts,$__interval),
sum(total) AS "total"
FROM hp
WHERE
$__timeFilter(ts)
AND customer_type IN ($CustomerType) AND age IN ($age) AND gender IN ($gender)
GROUP BY 1
ORDER BY $__timeGroup(ts,$__interval)
仪表板中有多个 singleStat/panel/graphs 使用不同的 select 参数,但 WHERE 条件在所有参数中都保持相同。
我想将条件保留为单独的常量变量,以便我可以在每个查询中只添加该变量。
我试过这样构建我的查询。
SELECT
$__timeGroupAlias(ts,$__interval),
sum(total) AS "total"
FROM hp
$where_condition
GROUP BY 1
ORDER BY $__timeGroup(ts,$__interval)
并声明 where_condition
为 WHERE $__timeFilter(ts) AND customer_type IN ($CustomerType) AND age IN ($age) AND gender IN ($gender)
。
但是查询失败了,因为查询生成器没有解析内部变量($CustomerType,$age,$gender),生成的查询看起来像这样。
SELECT
UNIX_TIMESTAMP(ts) DIV 900 * 900 AS "time",
sum(total) AS "total"
FROM hp
ts BETWEEN FROM_UNIXTIME(1548311714) AND FROM_UNIXTIME(1548398114)
AND customer_type IN ($CustomerType) AND age IN ($age) AND gender IN ($gender)
GROUP BY 1
ORDER BY UNIX_TIMESTAMP(ts) DIV 900 * 900
有没有办法解析包含在其他变量中的变量。或者有没有其他方法可以外部化包含变量的查询部分?
constant
变量类型只生成静态字符串。它没有替换任何变量。切换到 query
类型并使用 MySQL 到 return 字符串,这将为您的 where_condition
变量提供精确的字符串值。查询:
SELECT 'WHERE $__timeFilter(ts) AND customer_type IN ($CustomerType) AND age IN ($age) AND gender IN ($gender)'
恕我直言:变量替换应该也适用于 constant
类型。您可以打开 https://github.com/grafana/grafana/issues.
我也有引号问题,这是我的解决方案,支持 'All' 和许多选择:
SELECT '
task_run_table.is_system = false
AND
(
''__all__'' = ''${user:raw}''
OR
task_run_table.user = any ( string_to_array(''${user:raw}'', '','')::text[])
)
AND
(
''__all__'' = ''${job:raw}''
OR
task_run_table.job_name = any ( string_to_array(''${job:raw}'', '','')::text[])
)
'
All
值被覆盖为 __all__
。
它是这样使用的:
SELECT
$__timeGroup(task_run_table.start_time, '$interval', NULL),
task_run_table.name AS metric,
COUNT(*) AS value
FROM
task_run_table
WHERE
$__timeFilter(task_run_table.start_time)
AND
${default_filter:raw}
AND
task_run_table.state = $state
GROUP BY time, task_run_table.name
ORDER BY time, value DESC