Grafana: 使用$__timeGroup和dateadd()

huangapple go评论117阅读模式
英文:

Grafana: $__timeGroup with dateadd()

问题

将`DATETIME_FIELD`传递给 `$__timeGroup` 之前,我想在其中添加一定数量的秒数:

SELECT $__timeGroup(DATEADD(second, 10, DATETIME_FIELD), $aggr_interval)
FROM MY_TABLE

然而,这会导致失败:“插值失败:解析间隔时发生错误 10”,尽管如果它能够生成,SQL 查询将被 SQL 服务器接受。应该生成以下内容:

SELECT FLOOR(DATEDIFF(second, '1970-01-01', DATEADD(second, 10, DATETIME_FIELD))/604800)*604800
FROM MY_TABLE

如何将`DATEADD`函数作为参数传递给`$__timeGroup`?
英文:

Given a Grafana panel with the following (part of an) sql query on an SQL-server database:

SELECT $__timeGroup(DATETIME_FIELD, $aggr_interval)  -- $aggr_interval = '7d'
FROM MY_TABLE

which is translated into the following SQL query (according to the "Query inspector"):

SELECT FLOOR(DATEDIFF(second, '1970-01-01', DATETIME_FIELD)/604800)*604800
FROM MY_TABLE

Now, I'd like to add a number of seconds to the DATETIME_FIELD before passing it to $__timeGroup:

SELECT $__timeGroup(DATEADD(second, 10, DATETIME_FIELD), $aggr_interval)
FROM MY_TABLE

However, this results in a failure: interpolation failed: error parsing interval 10 even though the sql query would be acceptable to the sql server - if it would be generated. This is what should be generated:

SELECT FLOOR(DATEDIFF(second, '1970-01-01', DATEADD(second, 10, DATETIME_FIELD))/604800)*604800
FROM MY_TABLE

How to pass the DATEADD function as a parameter to $__timeGroup?

答案1

得分: 2

Grafana在$__timeGroup内部解析内容时出现问题:表达式内的逗号会导致解析错误。因此,它会将10视为$__timeGroup的第二个参数。最可能逗号和括号也不被允许。

据我所知,无法在$__timeGroup内部转义传递的表达式。您需要避免逗号和括号。

最简单的方法是使用类似于DATETIME_FIELD + 1./24/60/6的表达式,而不是使用DATEADD,如果可能的话。

如果将DATETIME_FIELD视为数字不可行,您需要使用子查询来准备数据。对于您的示例,可以按照以下方式进行操作:

SELECT $__timeGroup(mdate, $aggr_interval) 
FROM (SELECT DATEADD(second, 10, DATETIME_FIELD) mdate
        FROM MY_TABLE)

编辑:
对于OP的确切情况,更新的查询如下:

SELECT $__timeGroup(mdate, $aggr_interval) as time,
       count(*) as value,
       'Metric name' as metric
FROM (SELECT id, 
             DATEADD(second, 10, DATETIME_FIELD) as mdate from MY_TABLE
       WHERE $__timeFilter(DATETIME_FIELD)
)
GROUP BY $__timeGroup(mdate, $aggr_interval)
ORDER BY 1
英文:

Grafana has trouble parsing content inside $__timeGroup: a comma inside your expression throws it off. As a result, it thinks that 10 is the second parameter of $__timeGroup. Most likely parentheses are not welcome too.

AFAIK, there is no way to escape the expression passed inside $__timeGroup. You'll need to avoid commas and parentheses.

The simplest way is to use an expression like DATETIME_FIELD + 1./24/60/6 instead DATEADD, if possible.

if treating DATETIME_FIELD as a number is not possible - you'll need to use a sub-query to prepare your data. For your example something along the lines of:

SELECT $__timeGroup(mdate, $aggr_interval) 
FROM (SELECT DATEADD(second, 10, DATETIME_FIELD) mdate
        FROM MY_TABLE)

EDIT:
Updated query for exact case of OP:

SELECT $__timeGroup(mdate, $aggr_interval) as time,
       count(*) as value,
       'Metric name' as metric
FROM (SELECT id, 
             DATEADD(second, 10, DATETIME_FIELD) as mdate from MY_TABLE
       WHERE $__timeFilter(DATETIME_FIELD)
)
GROUP BY $__timeGroup(mdate, $aggr_interval)
ORDER BY 1

答案2

得分: 2

基于markalex的答案,可以使用FROM子句中的子选择语句来解决,该子选择语句准备数据:

SELECT $__timeGroup(a.mdate, $aggr_interval)
FROM MY_TABLE
inner join (select id, DATEADD(second, 10, DATETIME_FIELD) as mdate from MY_TABLE) as a on a.id = MY_TABLE.id

子选择语句与主查询相关联,使用id列,以便为每个主查询记录提供相同的记录,从而实现为DATETIME_FIELD准备修改后值的目标。

更大的背景:完整的Grafana面板查询

虽然不是问题的一部分,$__timeGroup经常被用作Grafana时间序列查询中的x轴值。以下是在查询中如何使用它,该查询提供x轴值、y轴值和指标名称:

SELECT $__timeGroup(mdate, $aggr_interval) as time, count(*), 'Metric name' as metric
FROM MY_TABLE
inner join (select id, DATEADD(second, 10, DATETIME_FIELD) as mdate from MY_TABLE) as a on a.id = MY_TABLE.id
WHERE $__timeFilter(DATETIME_FIELD)
GROUP BY $__timeGroup(mdate, $aggr_interval) order by 1
英文:

Based on markalex's answer there is a solution using a subselect in the from clause, which prepares the data:

SELECT $__timeGroup(a.mdate, $aggr_interval)
FROM MY_TABLE
inner join (select id, DATEADD(second, 10, DATETIME_FIELD) as mdate from MY_TABLE) as a on a.id = MY_TABLE.id

The subselect is correlated with the main query with the id column so that the subselect provides one and the same record for every main query record, in order to achieve the goal of preparing a modified value for DATETIME_FIELD.

Bigger picture: Full Grafana panel query

Although not part of the posted question, $__timeGroup is often used as x-axis value in a Grafana time-series query. Here is how it's being used in a query that provides x-axis value, y-axis value and metric name:

SELECT $__timeGroup(mdate, $aggr_interval) as time, count(*), 'Metric name' as metric
FROM MY_TABLE
inner join (select id, DATEADD(second, 10, DATETIME_FIELD) as mdate from MY_TABLE) as a on a.id = MY_TABLE.id
WHERE $__timeFilter(DATETIME_FIELD)
GROUP BY $__timeGroup(mdate, $aggr_interval) order by 1

答案3

得分: 0

我遇到了相同的错误,我创建了一个新列来执行 time_add

选择 
  $__timeGroup(日期, '3m', 0) 作为时间,
  计数(),
  date_add(日期, interval 1 ) 作为时间2
 表格

为了显示时间序列图,您必须添加一个转换来组织字段并隐藏时间列。

英文:

I had the same error, I created a new column to do the time_add.

Select 
  $__timeGroup(Date, '3m', 0) as time,
  count(rows),
  date_add(Date, interval 1 day) as time2
From table

To display the time series graph you have to add a transformation Organize fields and hide the time column.

huangapple
  • 本文由 发表于 2023年3月15日 18:05:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定