绘制时间序列时无法在Grafana中使用SQL案例语句?

发布于 2025-01-22 06:03:15 字数 1160 浏览 0 评论 0原文

试图在Grafana v8.3.3中使用SQL查询来绘制一个时间序列,以提醒是否在上一个小时内没有收到订单,但仅在本质上是在营业时间(即上午8点至晚上10点)。

由于警报只能每小时运行,因此我将在非企业时间(即上午11点至上午7点)入侵SQL查询以返回1个,因此警报不会发射。

尝试使用案例,但似乎并不认为该值返回为一个数字 - 关于如何最好地解决此问题或在Grafana中实现上述任何想法?

提前致谢!

没有案例:

SELECT $__timeGroup(DATE_CREATED, '1h', 0), 
1 AS "BOOKED" 
FROM CHANNEL_ORDER
WHERE STATUS = 'BOOKED'
GROUP BY $__timeGroup(DATE_CREATED, '1h', 0)
ORDER BY $__timeGroup(DATE_CREATED, '1h', 0)

有案例 ”

SELECT $__timeGroup(DATE_CREATED, '1h', 0), 
CASE WHEN TO_CHAR(DATE_CREATED,'HH24') >= 8 AND to_char(DATE_CREATED,'HH24') <= 22) THEN COUNT(1) ELSE 1 END) AS "BOOKED" 
FROM CHANNEL_ORDER
WHERE STATUS = 'BOOKED'
GROUP BY $__timeGroup(DATE_CREATED, '1h', 0)
ORDER BY $__timeGroup(DATE_CREATED, '1h', 0)

Trying to use a SQL query in Grafana v8.3.3 to plot a time-series to alert if no orders recieved in a previous hour, but only during essentially business hours (i.e 8am - 10pm).

Since the alert can only be run hourly, I'm going to hack the SQL query to return 1 during non-business hours (i.e 11pm - 7am) so alert will not fire.

Tried using CASE but doesn't seem to see the value returned as being a number - any thoughts on how best to fix this, or implement the above in Grafana?

Thanks in advance!

Without CASE:
Without CASE

SELECT $__timeGroup(DATE_CREATED, '1h', 0), 
1 AS "BOOKED" 
FROM CHANNEL_ORDER
WHERE STATUS = 'BOOKED'
GROUP BY $__timeGroup(DATE_CREATED, '1h', 0)
ORDER BY $__timeGroup(DATE_CREATED, '1h', 0)

With CASE
With CASE

SELECT $__timeGroup(DATE_CREATED, '1h', 0), 
CASE WHEN TO_CHAR(DATE_CREATED,'HH24') >= 8 AND to_char(DATE_CREATED,'HH24') <= 22) THEN COUNT(1) ELSE 1 END) AS "BOOKED" 
FROM CHANNEL_ORDER
WHERE STATUS = 'BOOKED'
GROUP BY $__timeGroup(DATE_CREATED, '1h', 0)
ORDER BY $__timeGroup(DATE_CREATED, '1h', 0)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2025-01-29 06:03:15

您的情况 表达 有两个问题。首先,您将date_created作为参数传递,但不在的组中,仅$ __ timeGroup(date_created,'1h',0)是。

  • 中的参考或汇总功能内部。

第二,您将日期转换为字符串,然后将其与整数进行比较;这种比较是不可能的,因此SQL Engine 隐式 基于数据类型顺序将两者之一转换为使它们具有相同的datatypeq。在您的情况下,整数转换为字符串。

由于$ __ TimeGroup()宏返回以整数秒为单位的时间,我会建议

SELECT
  $__timeGroup(DATE_CREATED, '1h', 0), 
  CASE
    WHEN $__timeGroup(DATE_CREATED, '1h', 0) % 86400 <  8*60*60 THEN 1
    WHEN $__timeGroup(DATE_CREATED, '1h', 0) % 86400 > 22*60*60 THEN 1
    ELSE COUNT(*)
  END AS "BOOKED" 
FROM CHANNEL_ORDER
WHERE STATUS = 'BOOKED'
GROUP BY $__timeGroup(DATE_CREATED, '1h', 0)
ORDER BY $__timeGroup(DATE_CREATED, '1h', 0)

您使用Modulo的,例如 ...我 推断 您的基础数据库是PostgreSQL吗?

另外,请记住,您对,0在宏调用中的使用意味着没有任何数据的任何小时将根据0的结果分配。 ,不是1。案例表达的替代方案可能是...

SELECT
  $__timeGroup(DATE_CREATED, '1h', 1), 
  COUNT(*) AS "BOOKED" 
FROM CHANNEL_ORDER
WHERE STATUS = 'BOOKED'
  AND $__timeGroup(DATE_CREATED, '1h', 1) % 86400 >=  8*60*60
  AND $__timeGroup(DATE_CREATED, '1h', 1) % 86400 <= 22*60*60
GROUP BY $__timeGroup(DATE_CREATED, '1h', 1)
ORDER BY $__timeGroup(DATE_CREATED, '1h', 1)

Your case expression has two problems. First, you pass DATE_CREATED in as a parameter, but it's not in the GROUP BY, only $__timeGroup(DATE_CREATED, '1h', 0) is.

  • All references in the SELECT must either be in the GROUP BY or inside an aggregate function.

Second you convert the date to a string, and compare it to an integer; this comparison is not possible and so the SQL engine implicitly converts one of the two, based on datatype order of precedence, to make them the same datatypeq. In your case, the integer is converted to a string.

As the $__timegroup() macro return the time rounded as an integer number of seconds, I would instead suggest something like...

SELECT
  $__timeGroup(DATE_CREATED, '1h', 0), 
  CASE
    WHEN $__timeGroup(DATE_CREATED, '1h', 0) % 86400 <  8*60*60 THEN 1
    WHEN $__timeGroup(DATE_CREATED, '1h', 0) % 86400 > 22*60*60 THEN 1
    ELSE COUNT(*)
  END AS "BOOKED" 
FROM CHANNEL_ORDER
WHERE STATUS = 'BOOKED'
GROUP BY $__timeGroup(DATE_CREATED, '1h', 0)
ORDER BY $__timeGroup(DATE_CREATED, '1h', 0)

Note: I used % for modulo, as I'm inferring the your underlying database is PostgreSQL?

Also, bear in mind that your use of , 0 in the macro call means that any hours without any data will be assigned a result of 0, not 1. An alternative to a CASE expression could be...

SELECT
  $__timeGroup(DATE_CREATED, '1h', 1), 
  COUNT(*) AS "BOOKED" 
FROM CHANNEL_ORDER
WHERE STATUS = 'BOOKED'
  AND $__timeGroup(DATE_CREATED, '1h', 1) % 86400 >=  8*60*60
  AND $__timeGroup(DATE_CREATED, '1h', 1) % 86400 <= 22*60*60
GROUP BY $__timeGroup(DATE_CREATED, '1h', 1)
ORDER BY $__timeGroup(DATE_CREATED, '1h', 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文