Flux query help : function calls

Hi. I am having a requirement, where i would need to do function call for each row.
So, firstly i would query a database and will fetch all tag values of 1. Then for each row, I would require to call a function and i would pass some arguments from current row, execute query and push data to different bucket. I am not able to achieve it using map function, also not able to use findrecord since we need to define index, which i am not able to do dynamically. Any way i can do that using flux only? Thanks

Could you share the query you tried, with map() function?

Hi, Basically i tried two approaches:
Approach 1:

mult = (x) => {
  from(bucket: src_bucket)
        |> range(start: 2022-06-23T00:00:00Z,stop: 2022-06-23T05:00:00Z)
        |> filter(fn: (r) => r["_measurement"] == src_measurement)
        |> filter(fn: (r) => r._field == "field" )
        |> to(bucket:dest_bucket,org:organization,timeColumn:"_time")
return 3 * x
}

from(bucket: "record_monitor_demo")
   |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
   |> filter(fn: (r) => r["_measurement"] == "record_monitor_per_hour")
   |> filter(fn: (r) => r["tagc"] == "1")
    |> map(fn: (r) => ({ r with _value: if r._value == 1 then mult(x:2) else 2 }))

Its correctly changing the value for all 1’s to 6. But it is not executing function defined in mult. I have checked the function, it’s working correctly when executed alone.

Approach 2:

mult = (tables=<-, x) => tables
        from(bucket: src_bucket)
        |> range(start: 2022-06-23T00:00:00Z,stop: 2022-06-23T05:00:00Z)
        |> filter(fn: (r) => r["_measurement"] == src_measurement)
        |> filter(fn: (r) => r._field == "field" )
        |> to(bucket:dest_bucket,org:organization,timeColumn:"_time")



from(bucket: "record_monitor_demo")
   |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
   |> filter(fn: (r) => r["_measurement"] == "record_monitor_per_hour")
   |> filter(fn: (r) => r["tagc"] == "1")
    |> map(fn: (r) => ({ r with _value: if r._value == 1 then mult(x:2) else 2 }))

Error i am getting: type error @55:63-55:72: missing required argument tables Basically for the last line the map() one, i am getting the error.

The requirement is like this:

  1. filter all 1’s from record_monitor_demo
  2. for all 1’s, we have to do function call, with arguments. For here, i was trying to do function call without any arguments. Arguments would be the coming from each record.
  3. Make all 1’s to 2’s

The problem is that the query inside a function will not execute until the result is actually used. You can try for example something like this as a workaround:

mult = (x) => {
  record = from(bucket: src_bucket)
        |> ...
        |> to(bucket:dest_bucket,org:organization,timeColumn:"_time")
        |> findRecord(fn: (key) => true, idx: 0)
return { result: 3 * x, unused: record }
}

from(bucket: "record_monitor_demo")
   |> ...
   |> map(fn: (r) => ({ r with _value: if r._value == 1 then mult(x:2).result else 2 }))

You just need to extract anything and use it, ie. return it.

1 Like