Grafana:显示历史数据点的便捷方法

时间:2019-06-17 14:54:41

标签: postgresql group-by grafana

我手头有一个postgres数据库,其中包含几年前收集的数据集。这是桌子的样子。

postgres=# select * from urbansense.new_table;  
+---------------------------+-----------------+------------------+-----------+
|         recvtime          |   entitytype    |     attrname     | attrvalue |
+---------------------------+-----------------+------------------+-----------+
| 20014-04-18T12:05:42.00Z  | WeatherObserved | illuminance      | 20        |
| 20014-04-18T12:05:42.00Z  | WeatherObserved | temperature      | 15.2      |
| 20014-04-18T12:05:42.00Z  | WeatherObserved | relativehumidity | 64.3      |
| ....                      | ......          | .....            | .....     |
| 2015-07-10T11:47:02+01:00 | WeatherObserved | illuminance      | 29562.7   |
| 2015-07-10T11:47:02+01:00 | WeatherObserved | temperature      | 20.7      |
| 2015-07-10T11:47:02+01:00 | WeatherObserved | relativeHumidity | 78.2      |
+---------------------------+-----------------+------------------+-----------+

目标是在grafana图形面板上显示温度(attrvalue)的数据点。

我可以从postgres客户端检索值(使用下面的查询在面板上显示),但是grafana仅显示时间范围以外的数据点

postgres=# SELECT DISTINCT
  date_trunc('minute', to_timestamp(recvtime,'YYYY-MM-DD HH24:MI:SS')) AS "time",
  attrvalue::float AS temperature 
FROM
  urbansense.new_table 
WHERE  
  attrname = 'temperature' 
  AND attrvalue <> 'null' 
GROUP BY time, urbansense.new_table.attrvalue;
+------------------------+-------------+
|          time          | temperature |
+------------------------+-------------+
| 2015-07-10 07:49:00+00 | 24.1        |
| 2015-07-09 21:18:00+00 | 20.2        |
| 2015-07-09 23:52:00+00 | 19.8        |
| .....                  | ..          |
| 2015-07-08 23:56:00+00 | 29.1        |
| 2015-07-09 16:02:00+00 | 23.6        |
| 2015-07-09 09:30:00+00 | 32.3        |
| (31037 rows)           |             |
+------------------------+-------------+

我尝试再次使用grafana查询编辑器使用以下查询(使用$__timeFrom()),显示grafana:无数据点

 SELECT 
        COUNT(DISTINCT urbansense.new_table.attrvalue) as time
        FROM 
        urbansense.new_table
        WHERE  
        attrvalue <> 'null'
        AND to_timestamp(recvtime,'YYYY-MM-DD HH24:MI:SS') > $__timeFrom()

仅在recvtime列中可用的日期内显示数据点的正确方法是什么?我猜grafana正在尝试显示当前时间(或几个小时前),所以第一个错误Data points outside time range.

0 个答案:

没有答案