Hi I am trying to automate a calculation I am doing in Flux with TICK script tasks. I am using InfluxDB v1.8.4 and can therefore not use flux tasks…
All I want to do is figure out a way to calculate the time difference between two timestamps. My data is always “on” and “off” (1 or 0) for the same signal.
I have the following flux script:
data = from(bucket: "plcview_4/autogen")
|> range(start: 2021-01-29T00:00:00.000Z, stop: now())
|> filter(fn: (r) => r._measurement == "FAULT_LASER")
events = data
|> map(fn: (r) => ({ r with event: if r._value == 1 then "start" else "stop", index: 1 }))
|> group(columns: ["event"])
startEvents = events |> filter(fn: (r) => r.event == "start") |> cumulativeSum(columns: ["index"])
stopEvents = events |> filter(fn: (r) => r.event == "stop") |> cumulativeSum(columns: ["index"])
faultEvents = join(tables: {start: startEvents, stop: stopEvents},
on: ["index", "equipmentNumber", "_measurement", "_field", "workplace"])
|> drop(columns: ["event_start", "event_stop", "index", "_start_start", "_start_stop", "_stop_start", "_stop_stop"])
|> rename(columns: {_time_start: "start", _time_stop: "stop"})
faultDurations = faultEvents
|> map(fn: (r) => ({ r with duration: (int(v: r.stop) - int(v: r.start))/1000000 }))
mttr = faultDurations
|> reduce(
fn: (r, accumulator) => ({
totalFaultTime: r.duration + accumulator.totalFaultTime,
nFaults: accumulator.nFaults + 1
}),
identity: { totalFaultTime: 0, nFaults: 0 }
)
|> map(fn: (r) => ({ _value: r.totalFaultTime / r.nFaults }))
|> yield(name: "mttr")
I can’t seem to get the join to work in TICK script…Here is what I have so far:
var events = batch
|query('SELECT * FROM "oneyear"."autogen".FAULT_LASER')
.period(10m)
.every(10s)
|eval(lambda: if("FAULT_LASER_ACTIVE" == 1, 'start', 'stop'))
.as('event')
.keep()
|eval(lambda: 1)
.as('index')
.keep()
var startEvents = events
|where(lambda: "event" == 'start')
|cumulativeSum('index')
|groupBy('index')
|httpOut('startEvents')
var stopEvents = events
|where(lambda: "event" == 'stop')
|cumulativeSum('index')
|groupBy('index')
|httpOut('stopEvents')
var faultDurations = startEvents
|join(stopEvents)
.as('start', 'stop')
|httpOut('join')
Is it even possible to translate what I have in Flux into TICK script?
@Emrys_Landivar Scott directed me to you