3

我有一个 InfluxDB(第 2 版),其中每秒将一个条目写入我的存储桶,以及一个identifier(源的 uuid)和一个side值(从 1 到 6 的一些特定于域的测量值)。而不是一长串这样的逐秒日志:

2020-05-18T15:57:18 side=1
2020-05-18T15:57:19 side=1
2020-05-18T15:57:20 side=3
2020-05-18T15:57:21 side=3
2020-05-18T15:57:22 side=3
2020-05-18T15:57:23 side=2
2020-05-18T15:57:24 side=2

我想压缩这些条目,以便我可以计算持续时间,持续多长时间side=x

from 2020-05-18T15:57:18 to 2020-05-18T15:57:19 side=1 duration=2s
from 2020-05-18T15:57:20 to 2020-05-18T15:57:22 side=3 duration=3s
from 2020-05-18T15:57:23 to 2020-05-18T15:57:24 side=2 duration=2s

我还通过从/到范围限制时间段。这是我当前的查询:

from(bucket: "mybucket")
    |>range(start:2020-01-01T00:00:00.000Z, stop:2020-12-31T00:00:00.000Z
    |>filter(fn:(r) => r.identifier == "3c583530-0152-4ed1-b15f-5bb0747e771e")

我的方法是读取原始数据,然后对其进行迭代,检测侧面的变化(类似于current.side != last.side),然后将其作为新条目报告给我的logbook. 当然,这种方法效率很低(JavaScript):

    const data = fetchFromInfluxDB(from, to):
    const terminator = { 'time': new Date(), 'identifier': '', 'side': -1 };
    data.push(terminator); // make sure last item is also reported
    const logbook = new Array();
    let lastSide = -1;
    let from = undefined;
    for (let e of data) {
        if (e.side != lastSide) {
            if (from != undefined) {
                let to = e.time;
                let diff = Math.floor((new Date(to) - new Date(from)) / 1000);
                if (diff > 0) {
                    logbook.push({'from': from, 'to': to, 'side': lastSide, 'seconds': diff});
                }
            }
            lastSide = e.side;
            from = e.time;
        }
    }

有没有办法使用 InfluxDB 查询语言对数据进行分组和汇总?

4

0 回答 0