InfluxDB:如何回填先前未测量的测量标签(如果可能的话)?

时间:2019-04-07 21:42:01

标签: database iot grafana influxdb sensors

大约一个月前,我开始使用Node-RED从Smart Meter记录数据,它看起来像是json数据(有效负载是重要的位):

{
  "topic":"stat/smartmeter/all",
  "payload":"{
    \"kwh_low_tarrif\":866.696,
    \"kwh_high_tarrif\":902.156,
    \"current_tarrif\":1,
    \"current_watt_draw\":485,
    \"gas_timestamp\":1554675307000,
    \"gas_total\":326.509,
    \"kwh_combined\":1768.852
  }",
  "qos":0,
  "retain":false,
  "_topic":"stat/smartmeter/all",
  "_msgid":"db4ebc0.72b9a48"
}

此数据的问题是我在Grafana仪表板中进行了电费和燃气费计算:

grafana smartmeter dashboard

我使用InfluxDB数据选择中的数学函数将成本硬编码到仪表板中:

grafana data query for InfluxDB (the math statement is the important bit)

您可以看到我使用的价值(或价格)是每kWh用电量0.230662欧元。现在愚蠢的我从来没有想过要能够在这个价格会波动的多年中进行计算,所以一旦我发现了电商的公共API端点,可以在其中读取我的特定计划的价格,便将其添加到了度量,所以现在json-data看起来像这样:

{
  "topic":"stat/smartmeter/all",
  "payload":"{
    \"kwh_low_tarrif\":866.696,
    \"kwh_high_tarrif\":902.156,
    \"kwh_low_price\":0.230662,
    \"kwh_high_price\":0.230662,
    \"current_tarrif\":1,
    \"current_watt_draw\":485,
    \"current_kwh_price\":0.230662,
    \"gas_timestamp\":1554675307000,
    \"gas_total\":326.509,
    \"gas_price\":0.804565,
    \"kwh_combined\":1768.852
  }",
  "qos":0,
  "retain":false,
  "_topic":"stat/smartmeter/all",
  "_msgid":"db4ebc0.72b9a48"
}

现在唯一的问题(以及我的主要问题)是:

1)如何编写一个在价格计算中使用该值的查询?我现在正在使用的查询(来自上面的屏幕截图)是:

SELECT distinct("kwh_combined")  * 0.230662 FROM "smartmeter" WHERE $timeFilter GROUP BY time($__interval) fill(linear)

2)我该如何回填数据?(从记录之初就将那些电价和燃气价写到数据库中,并将其添加到我当时取回的测量值中)

我宁愿将以前在面板中硬编码的值设置为我已经进行的测量,而不必为不存在或为“ null”的情况编写异常。我的意思是,数据由于价格没有变化,所以它本身是静态的,所以它不会那么难,不是吗?即使需要重建数据,我是否也可以将其重新插入到新的数据集中并自己添加字段?

请告诉我这对于InfluxDB是可行的...

我的意思是,在MySQL中,它将是一个简单的ALTER TABLE语句,也许在具有空值的记录上进行简单的插入。

..还是让我要求一个时间序列日志记录系统来更改其已记录数据的数据结构是不合理的,我是否要求过多的InfluxDB?

1 个答案:

答案 0 :(得分:2)

您可以将其他字段(price_1,price_2,...)添加到现有度量。将它们插入完全相同的时间戳和标记值,以使用这些新的价格列扩展现有数据记录,以准备在grafana中使用:

INSERT smartmeter,tagA=tagAvalue,tagB=tagBvalue,... price1=1234,price2=4321 1549983966469023105
...

另一种方法是导出现有数据,添加额外的价格列并重新导入。

这两种方法看起来都不简单。 使用select ... into语法似乎更容易运行(没有这个丑陋的0*last() + ... hack对我来说是行不通的):

    SELECT 
       last(kwh_combined) as kwh_combined,
       last(other_field) as other_field,
        ...add other fields here...
       0 * last(kwh_combined) + 1234 as price1,
       0 * last(kwh_combined) + 4321 as price2 
       INTO new_smartmeter
    FROM smartmeter GROUP BY *