What am I doing wrong?
What here makes me literally a bad person?
Pls give feedback.
import "strings"
import "date"
import "math"
bepis = from(bucket: "mqtt_raw")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r.topic == "emon/ct1" or r.topic == "emon/ct2" or r.topic == "emon/ct3" or r.topic == "emon/ct4")
|> keep(columns: ["_time", "_value", "topic"])
|> elapsed(unit: 1ms)
|> map(fn: (r) => ({r with _value: math.abs(x: float(v: r._value)) * float(v: r.elapsed) / 3600000.0, topic: strings.trimPrefix(v: r.topic, prefix: "emon/")}))
|> aggregateWindow(every: duration(v: v.myWindowPeriod), fn: sum)
|> keep(columns: ["_time", "_value", "topic"])
total = bepis
|> group(columns: ["_time"], mode: "by")
|> sum(column: "_value")
|> group(columns: [], mode: "by")
|> map(fn: (r) => ({r with topic: "total"}))
union(tables: [bepis, total])
I’m also particularly curious:
- Is there a meaningful difference between returning one or many tables for graphing?
- Is there a way to create a Variable of type
Duration
? It doesn’t seem so. - Is it possible to create a Key-Value Variable with Query constructor?
It seems to only be possible to have separate keys and values in Map mode.
Or is there some record key like_key
that would allow renaming them in the dropdown? - Which of these operations should be reordered or regrouped for better performance?
For example, I know I shouldn’t put some kind of a substring predicate orcontains( value: r.topic, set: ["ct1", "ct2])
infilter
instead of concrete values, as that slows it down to a crawl. Or that leaving in the firstkeep
actually improves runtime. But perhaps I am missing some specific blessed string-prefix-match operator that has a fast path for filtering tags? - Is my
group|>sum|>group([])
actually the fastest/most idiomatic way of adding everything together? I would not be surprised if I am doing something very silly due to my poor understanding of the result/stream/tables model despite reading Flux data model | Flux 0.x Documentation and InfluxDB data elements | InfluxDB OSS 2.1 Documentation