I’m trying to do some slightly more advanced mathematics within conditional expressions of a map() function.
I’m using Influx 1.7.8 (i.e. the latest) running on Ubuntu 18.04.1 LTS
Below is a slightly redacted copy of my query, which just returns [object Object] in Chronograf:
from(bucket: "my_bucket")
|> range(start: dashboardTime)
|> filter(fn: (r) => r.controller_id == "304511028a28" and (r._field == "bms_batt_v" or r._field == "bms_batt_a" or r._field == "bms_soc" or r._field == "bms_soh" or r._field == "rectifier_1_v" or r._field == "rectifier_1_a" or r._field == "rectifier_2_v" or r._field == "rectifier_2_a"))
|> last()
|> keep(columns: ["_time", "_value", "_field"])
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(fn: (r) => ({
_time: r._time,
_value:
if r.bms_batt_a < 0 then
(100.0 * (r.bms_soh / 100.0) * (r.bms_soc / 100.0)) / (r.bms_batt_a * 0.95 * -1.0)
else
(100.0 * (r.bms_soh / 100.0) * (r.bms_soc / 100.0)) / ((((r.rectifier_1_a * r.rectifier_1_v) + (r.rectifier_2_a * r.rectifier_2_v))/r.bms_batt_v) * 0.95)
}))
If I remove the condition then everything works perfectly:
from(bucket: "my_bucket")
|> range(start: dashboardTime)
|> filter(fn: (r) => r.controller_id == "304511028a28" and (r._field == "bms_batt_v" or r._field == "bms_batt_a" or r._field == "bms_soc" or r._field == "bms_soh" or r._field == "rectifier_1_v" or r._field == "rectifier_1_a" or r._field == "rectifier_2_v" or r._field == "rectifier_2_a"))
|> last()
|> keep(columns: ["_time", "_value", "_field"])
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(fn: (r) => ({
_time: r._time,
_value: (100.0 * (r.bms_soh / 100.0) * (r.bms_soc / 100.0)) / (r.bms_batt_a * 0.95 * -1.0)
}))
If I simplify the calculations within the conditional statement then the query works too.
from(bucket: "my_bucket")
|> range(start: dashboardTime)
|> filter(fn: (r) => r.controller_id == "304511028a28" and (r._field == "bms_batt_v" or r._field == "bms_batt_a" or r._field == "bms_soc" or r._field == "bms_soh" or r._field == "rectifier_1_v" or r._field == "rectifier_1_a" or r._field == "rectifier_2_v" or r._field == "rectifier_2_a"))
|> last()
|> keep(columns: ["_time", "_value", "_field"])
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(fn: (r) => ({
_time: r._time,
_value:
if r.bms_batt_a < 0 then
1.0 * 2.0
else
3.0 + 4.0
}))
But if I put brackets around those 2 simple statements then the query doesn’t work, once again returning [object Object]
from(bucket: "my_bucket")
|> range(start: dashboardTime)
|> filter(fn: (r) => r.controller_id == "304511028a28" and (r._field == "bms_batt_v" or r._field == "bms_batt_a" or r._field == "bms_soc" or r._field == "bms_soh" or r._field == "rectifier_1_v" or r._field == "rectifier_1_a" or r._field == "rectifier_2_v" or r._field == "rectifier_2_a"))
|> last()
|> keep(columns: ["_time", "_value", "_field"])
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(fn: (r) => ({
_time: r._time,
_value:
if r.bms_batt_a < 0 then
(1.0 * 2.0)
else
(3.0 + 4.0)
}))
I have also tried calling a custom function for each condition but this also returns [object Object]
All help is greatly appreciated