InfluxDB:如何创建连续查询来计算delta值?

时间:2018-02-02 13:06:47

标签: influxdb

我想计算存储在InfluxDB中的一系列测量值的delta值。这些值是每5分钟取一次电表的读数。值随时间增加。以下是为您提供想法的数据子集(下面显示的命令在InfluxDB CLI中执行):

> SELECT "Haushaltstromzaehler - cnt" FROM "myhome_measurements" WHERE time >= '2018-02-02T10:00:00Z' AND time < '2018-02-02T11:00:00Z'
name: myhome_measurements
time                           Haushaltstromzaehler - cnt
----                           --------------------------
2018-02-02T10:00:12.610811904Z 11725.638
2018-02-02T10:05:11.242021888Z 11725.673
2018-02-02T10:10:10.689827072Z 11725.707
2018-02-02T10:15:12.143326976Z 11725.736
2018-02-02T10:20:10.753357056Z 11725.768
2018-02-02T10:25:11.18448512Z  11725.803
2018-02-02T10:30:12.922032896Z 11725.837
2018-02-02T10:35:10.618788096Z 11725.867
2018-02-02T10:40:11.820355072Z 11725.9
2018-02-02T10:45:11.634203904Z 11725.928
2018-02-02T10:50:11.10436096Z  11725.95
2018-02-02T10:55:10.753853952Z 11725.973

使用difference()函数计算InfluxDB CLI中的差异非常简单。这给了我5分钟间隔内消耗的电力:

> SELECT difference("Haushaltstromzaehler - cnt") FROM "myhome_measurements" WHERE time >= '2018-02-02T10:00:00Z' AND time < '2018-02-02T11:00:00Z'
name: myhome_measurements
time                           difference
----                           ----------
2018-02-02T10:05:11.242021888Z 0.03499999999985448
2018-02-02T10:10:10.689827072Z 0.033999999999650754
2018-02-02T10:15:12.143326976Z 0.02900000000045111
2018-02-02T10:20:10.753357056Z 0.0319999999992433
2018-02-02T10:25:11.18448512Z  0.03499999999985448
2018-02-02T10:30:12.922032896Z 0.033999999999650754
2018-02-02T10:35:10.618788096Z 0.030000000000654836
2018-02-02T10:40:11.820355072Z 0.03299999999944703
2018-02-02T10:45:11.634203904Z 0.028000000000247383
2018-02-02T10:50:11.10436096Z  0.02200000000084401
2018-02-02T10:55:10.753853952Z 0.02299999999922875

我奋斗的地方是让它在连续查询中工作。这是我用来设置连续查询的命令:

CREATE CONTINUOUS QUERY cq_Haushaltstromzaehler_cnt ON myhomedb 
BEGIN
    SELECT difference(sum("Haushaltstromzaehler - cnt")) AS "delta" INTO "Haushaltstromzaehler_delta" FROM "myhome_measurements" GROUP BY time(1h)
END

查看InfluxDB日志文件,我发现在连续查询执行的新“delta”度量中没有写入数据:

...finished continuous query cq_Haushaltstromzaehler_cnt, 0 points(s) written...

经过多次故障排除和实验后,我现在明白了为什么没有生成数据。设置连续查询需要使用GROUP BY time()语句。这又需要在differences()函数中使用聚合函数。现在的问题是聚合函数只返回GROUP BY time()指定的时间段内的一个值。显然,differences()函数无法仅计算一个值的差异。本质上,连续查询执行如下命令:

> SELECT difference(sum("Haushaltstromzaehler - cnt")) FROM "myhome_measurements" WHERE time >= '2018-02-02T10:00:00Z' AND time < '2018-02-02T11:00:00Z' GROUP BY time(1h)
>

我现在对如何完成这项工作毫无头绪并感谢你提出的任何建议。

2 个答案:

答案 0 :(得分:0)

DIFFERENCE() would calculate delta from the "aggregated" value taken from previous group, not within current group.

所以请在那里免费使用选择器功能 - 因为你的计数器似乎是累积的,LAST()应该运行良好。

答案 1 :(得分:0)

使用上一个汇总函数是否有帮助?尚未将其作为CQ进行测试。

//Get Edit
[HttpGet]
public ActionResult Edit(Roles model)
{
    return View(); 
}

//Put Edit
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Comtrex_ICU.Models.Roles model, int RoleId, 
    Roles RoleName)
{
    try
    {
        Roles db = new Roles();

        db.Entry(RoleName).State = EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("RolesIndex");
    }
    catch 
    { 
        return View(); 
    }
}