Hello, I have a metric and I have been asked to do some calculations before producing the graphs, so I was thinking there is this cool Fluxlang, let’s use it. this is what I came out with
import "math"
import "experimental"
data = from(bucket: "latest")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "moving_averages")
meanData = data
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(fn: (r) => ({ r with _value: (r.fast + r.rapid) / 2 }))
|> drop(columns: ["fast", "rapid", "slow", "standard"])
moving1 = meanData
|> movingAverage(n: 15)
moving5 = meanData
|> movingAverage(n: 30)
moving10 = meanData
|> movingAverage(n: 60)
moving20 = meanData
|> movingAverage(n: 120)
firstJoin = experimental.join(left: moving5, right: moving10, fn: (left, right) => ({
left with
moving5: left._value,
moving10: right._value,
}))
secondJoin = experimental.join(left: firstJoin, right: moving20, fn: (left, right) => ({
left with
moving5: left.moving5,
moving10: left.moving10,
moving20: right._value
}))
thirdJoin = experimental.join(left: secondJoin, right: moving1, fn: (left, right) => ({
left with
moving5: left.moving5,
moving10: left.moving10,
moving20: left.moving20,
moving1: right._value
}))
|> map(fn: (r) => {
min1 = math.mMin(x: r.moving5, y: r.moving10)
min2 = math.mMin(x: min1, y: r.moving20)
min3 = math.mMin(x: min2, y: r.moving1)
return { r with _value: min3 }
})
|> yield(name: "min")
meanData
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean")
The calculation is to sum up 2 values, then do some moving averages, then take out the min of them.
The query runs quite slow on my small VM, I think there must be some basic mistake that multiplies the row or something.
Help! thanks