How to show ratio among multiple values - Flux

Hello,
I’m running InfluxDB (Flux) as backend for Grafana.

For the following data, how could I get new values for ratio of a_unicast/a_total, b_unicast/b_total, c_unicast/c_total…

|_time | a_unicast | a_total | b_unicast | b_total | c_unicast | c_total |
|t1 | 697420 | 10220494 | 5520780 | 5522338 | 5429904 | 5431455|
|t2 | 697435 | 10220805 | 5520904 | 5522464 | 5430026 | 5431578|
|t3 | 697452 | 10221125 | 5521032 | 5522593 | 5430154 | 5431705|
|t4 | 697462 | 10221415 | 5521171 | 5522729 | 5430285 | 5431836|
|t5 | 697483 | 10221694 | 5521295 | 5522854 | 5430408 | 5431958|

Option 1: Using Flux’s map() function:

    |> map(fn: (r) => ({ r with a_ratio: float(v: r.a_unicast)/float(v: r.a_total),
                         b_ratio: float(v: r.b_unicast)/float(v: r.b_total),
                         c_ratio: float(v: r.c_unicast)/float(v: r.c_total) }))

Option 2: Using Grafana’s Add field from calculation transformation:

Hi ebabeshko,
Yes, it will work for the known amount and names for the fields.

But another challenge is that both the amount and names for the fields here are dynamic, except for the key word ‘_unicast’ and ‘_total’.
For example, sometimes the table header will look as following instead.
| kk_unicast | kk_total | yz_unicast | yz_total | hh_unicast | hh_total | bm_unicast | bm_total | mz_unicast | mz_total |

Then how could I get the ratio of each pair in this situation?

that breaks the recommended and documented schema design standard by influxdb. those should be tags or field properties imo check this out

1 Like