Grafana-如何为Mysql数据源创建sql查询部分变量/宏
我在Grafana中有以下查询,它由MySql数据源支持Grafana-如何为Mysql数据源创建sql查询部分变量/宏,mysql,grafana,grafana-templating,grafana-variable,Mysql,Grafana,Grafana Templating,Grafana Variable,我在Grafana中有以下查询,它由MySql数据源支持 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,$
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/Graph,它们使用不同的select参数,但所有这些参数中的WHERE条件保持不变
我希望将条件作为单独的常量变量保留,以便在每个查询中只添加该变量
我试图这样构建我的查询
SELECT
$__timeGroupAlias(ts,$__interval),
sum(total) AS "total"
FROM hp
$where_condition
GROUP BY 1
ORDER BY $__timeGroup(ts,$__interval)
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
并声明where_condition
为where$\u时间过滤器(ts)和客户输入($CustomerType)和年龄输入($age)和性别输入($gender)
但查询失败,因为内部变量($CustomerType、$age、$gender)没有由查询生成器解析,生成的查询如下所示
SELECT
$__timeGroupAlias(ts,$__interval),
sum(total) AS "total"
FROM hp
$where_condition
GROUP BY 1
ORDER BY $__timeGroup(ts,$__interval)
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
是否有办法解决包含在其他变量中的变量。或者是否有其他方法可以将包含变量的查询部分外部化?
常量
变量类型只生成静态字符串。它没有替代任何变量。切换到query
类型并使用MySQL返回字符串,它将为您的where\u条件变量提供精确的字符串值。查询:
SELECT 'WHERE $__timeFilter(ts) AND customer_type IN ($CustomerType) AND age IN ($age) AND gender IN ($gender)'
IMHO:变量替换也适用于常量
类型。您可以为此打开功能请求。我的报价也有问题,以下是我的解决方案,它支持“全部”和多个选定项:
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
值已被重写为\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。
它是这样使用的:
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
您对where_condition
使用了哪种变量类型?您的Grafana版本是什么?Grafana版本=v5.4.3,我对where_condition
使用了常量类型。它有效。像这样使用这个变量时,我必须禁用引号${where\u condition:csv}
。我将提出同样的功能要求。