Query: Calculate % using multiple fields


I have this query working in overwriting my original hrStorageUsed and hrStorageSize fields with disk allocation units by using map function:

from(bucket: “db-snmp-synology”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “hrStorageTable”)
|> filter(fn: (r) => r[“_field”] == “hrStorageUsed” or r[“_field”] == “hrStorageSize”)
|> filter(fn: (r) => r[“hrStorageIndex”] == “53”)
|> group()
|> pivot(rowKey: [“_time”], columnKey: [“_field”], valueColumn: “_value”)
|> map(fn: (r) => ({r with hrStorageSize: r.hrStorageSize * 4096}))
|> map(fn: (r) => ({r with hrStorageUsed: r.hrStorageUsed * 4096}))
|> drop(columns: [“_start”, “_stop”])

How could I calculate the used percentage from the above query?

if I use:

|> map(fn: (r) => ({r with percent_used: r.hrStorageUsed / r.hrStorageSize * 100}))

would return 0 and would probably be some kind of integer overflow.

if I go with float, it won’t work either:

|> map(fn: (r) => ({r with percent_used: float(v: r.hrStorageUsed / r.hrStorageSize * 100)}))


How could I make this work?

Hello @a-neagoe,
Can you please navigate to the raw data view? Lets verify that you’re getting actual percentages and that the reason you’re seeing 0 values is because of rounding.

Next, go to customize and change the number of decimal places pleaseee and see if that helps? Thank you.

thank you very much for the help.

the values are actually all longs after seeing the raw data. if I manually adjust one of the values to less decimals, I would get some return values:

If I just adjust the outputs just to be readable in grafana, I could work the percentage out with two transformation but is less ideal than directly returning all values directly from flux.

|> map(fn: (r) => ({r with _value: r.hrStorageUsed/ r.hrStorageSize * 100}))
this would still return 0

I’m confused it seems like its working?