Grafana中踩过的坑

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012111465/article/details/83650725

1、时间戳

遇到将过去某一时间范围内的放到今天显示的需求,琢磨的一阵,是可以的。
timeSeriestimeSeries以及timeFilter的变化技巧

SELECT
    $timeSeries+86400*7*1000 as t,
    domain,
    sum(hit) as hit_2
FROM $table
WHERE date = today() - 7
AND datetime >= toDateTime($timeFilter-toDateTime(86400*7))
GROUP BY t,domain
ORDER BY t,domain

2、group by 的技巧

$colums()的功能有限,当需要多个select或group by多列时。其实查询出来是想要的结果,但在图上未表现出来,即没有聚合(一条线)
例如:本来应该写成:

SELECT
    $timeSeries+86400*7*1000 as t,
    domain,
    sum(hit) as hit_2
FROM $table
WHERE date = today() - 7
AND datetime >= toDateTime($timeFilter-toDateTime(86400*7))
GROUP BY t,domain
ORDER BY t,domain

最后需要写成(显示多条线):

SELECT t,groupArray((domain, diff)) as groupArr 
FROM
(SELECT
    $timeSeries+86400*7*1000 as t,
    domain,
    sum(hit) as hit_2
FROM $table
WHERE date = today() - 7
AND datetime >= toDateTime($timeFilter-toDateTime(86400*7))
GROUP BY t,domain
ORDER BY t,domain)
GROUP BY t ORDER BY t

3、表格

操作表格时,需要将Metrics下方的Format as选择Time series改为Table。表格的group by多列可以直接写SQL。

4、时间datetime/ts/time

在选择数据库时,检查Metrics下方的date有没有需要填写的时间列。

5、pie/donut图

pie/donut图需要有一个额外的sum(1) as tp,这样图形才能正常显示 。

SELECT
sum(1) as tp,
sum(duration/60 <= 1) as  le1min,
sum(duration/60 > 1 AND duration/60<=2) as in1_2min,
sum(duration/60 > 2) as gt2min
FROM $table
WHERE $timeFilter

6、图例或者变量等中文显示

图例或者变量等中文显示需要反引号就可以 。

SELECT
sum(1) as tp,
sum(duration/60 <= 1) as  `小于1min`,
sum(duration/60 > 1 AND duration/60<=2) as `1-2min`,
sum(duration/60 > 2) as `大于2min`
FROM $table
WHERE $timeFilter
展开阅读全文

没有更多推荐了,返回首页