Search code examples
influxdbdownsamplinginfluxdb-2

Influxdb2 : How to downsample data in a different measurement in same bucket?


I would like to create aggregation measurement into same bucket with monthly data. At the moment I have a task with this query:

import "strings"

option task = {name: "MTU Monthly", every: 1d}

from(bucket: "my_bucket")
    |> range(start: 1464943523, stop: 1654244985)
    |> filter(fn: (r) => r._measurement == "machine_time_utilization")
    |> aggregateWindow(every: 1mo, fn: sum)
    |> map(
        fn: (r) =>
            ({r with _measurement:
                    strings.replaceAll(
                        v: r._measurement,
                        t: "machine_time_utilization",
                        u: "machine_time_utilization_monthly",
                    ),
            }),
    )
    |> to(bucket: "my_bucket")

I don't like how it is done by using strings.replaceAll. Is there a way how to do it better?


Solution

  • If you need to set static value in the records, use set function, ie.

    from(bucket: "my_bucket")
        ...
        |> aggregateWindow(every: 1mo, fn: sum)
        |> set(key: "_measurement", value: "machine_time_utilization_monthly")
        |> to(bucket: "my_bucket")