使用此转换按指定字段(列)值对数据进行分组,并对每个分组进行计算。单击可查看计算选项列表。有关可用计算的信息,请参阅 Grafana 计算类别。
下面是一个原始数据示例:
Time | Server ID | CPU Temperature | Server Status |
2020-07-07 11:34:20 | server 1 | 80 | Shutdown |
2020-07-07 11:34:20 | server 3 | 62 | OK |
2020-07-07 10:32:20 | server 2 | 90 | Overload |
2020-07-07 10:31:22 | server 3 | 55 | OK |
2020-07-07 09:30:57 | server 3 | 62 | Rebooting |
2020-07-07 09:30:05 | server 2 | 88 | OK |
2020-07-07 09:28:06 | server 1 | 80 | OK |
2020-07-07 09:25:05 | server 2 | 88 | OK |
2020-07-07 09:23:07 | server 1 | 86 | OK |
对应的 SQL 语句如下:
select UNIX_TIMESTAMP('2020-07-07 11:34:20') as time, 'server1' as server_id, 80 as cpu_temperature, 'Shutdown' as server_status from dual union all select UNIX_TIMESTAMP('2020-07-07 11:34:20') as time, 'server3' as server_id, 62 as cpu_temperature, 'OK' as server_status from dual union all select UNIX_TIMESTAMP('2020-07-07 10:32:20') as time, 'server2' as server_id, 90 as cpu_temperature, 'Overload' as server_status from dual union all select UNIX_TIMESTAMP('2020-07-07 10:31:22') as time, 'server3' as server_id, 55 as cpu_temperature, 'OK' as server_status from dual union all select UNIX_TIMESTAMP('2020-07-07 09:30:57') as time, 'server3' as server_id, 62 as cpu_temperature, 'Rebooting' as server_status from dual union all select UNIX_TIMESTAMP('2020-07-07 09:30:05') as time, 'server2' as server_id, 88 as cpu_temperature, 'OK' as server_status from dual union all select UNIX_TIMESTAMP('2020-07-07 09:28:06') as time, 'server1' as server_id, 80 as cpu_temperature, 'OK' as server_status from dual union all select UNIX_TIMESTAMP('2020-07-07 09:25:05') as time, 'server2' as server_id, 88 as cpu_temperature, 'OK' as server_status from dual union all select UNIX_TIMESTAMP('2020-07-07 09:23:07') as time, 'server1' as server_id, 86 as cpu_temperature, 'OK' as server_status from dual
将上述 SQL 语句作为 Grafana 的数据,如下图:
分组转换分为两个步骤:
(1)首先,指定一个或多个字段对数据进行分组。这将把这些字段的所有相同值分组在一起,就像排序一样。例如,如果我们按“Server ID”字段分组,那么数据就会这样分组:
Time | Server ID | CPU Temperature | Server Status |
2020-07-07 11:34:20 | server 1 | 80 | Shutdown |
2020-07-07 09:28:06 | server 1 | 80 | OK |
2020-07-07 09:23:07 | server 1 | 86 | OK |
2020-07-07 10:32:20 | server 2 | 90 | Overload |
2020-07-07 09:30:05 | server 2 | 88 | OK |
2020-07-07 09:25:05 | server 2 | 88 | OK |
2020-07-07 11:34:20 | server 3 | 62 | OK |
2020-07-07 10:31:22 | server 3 | 55 | OK |
2020-07-07 09:30:57 | server 3 | 62 | Rebooting |
所有服务器 ID 值相同的行都会被分组。
(2)其次,选择要按哪个字段对数据分组后,就可以在其他字段上添加各种计算,并将计算应用到每一组行。例如,我们可以计算每台服务器的平均 CPU 温度。因此,我们可以在 CPU 温度字段上添加平均值计算,得到以下结果:
我们还可以添加多个计算。例如:
对于“Time”字段,我们可以计算 “last”,以了解每台服务器接收到最后一个数据点的时间。
对于“Server Status”字段,我们可以计算“last”,以了解每台服务器的最后状态值。
对于“Temperature”字段,我们还可以计算 “last”,以了解每台服务器的最新监测温度。
这样我们就可以得到:
通过这种转换,您可以从时间序列中提取重要信息,并方便地将其呈现出来。