1

I see many similar questions but couldn't find a good match. If we define a query and the result aught to be single value, is there a flux way to store as such? Example:

total = from(bucket: "xxx")
  |> range(start: 0)
  |> filter(fn: (r) => ...)
  |> keep(columns: ["_value"])
  |> sum()

consumed = from(bucket: "xxx")
  |> range(start: 0)
  |> filter(fn: (r) => ...)
  |> keep(columns: ["_value"])
  |> last()

total - consumed

Results in

invalid: error @18:1-18:40: [A] is not Subtractable

I can think of other ways to solve similar issues, but this example made me question whether flux actually supports easy working with single values or 1x1 relations.

Thanks

ThomDietrich
  • 79
  • 1
  • 7

2 Answers2

0

Not answering my original question but I want to provide the workaround I went with to solve this. I would still be interested in a more direct solution.

I've introduced a second column, then joined the two tables on that column:

total = from(bucket: "xxx")
  |> range(start: 0)
  |> filter(fn: (r) => ...)
  |> keep(columns: ["_value"])
  |> sum()
    // Added:
  |> map(fn: (r) => ({ age: "latest", _value:r._value }))

consumed = from(bucket: "xxx")
  |> range(start: 0)
  |> filter(fn: (r) => ...)
  |> keep(columns: ["_value"])
  |> last()
    // Added:
  |> map(fn: (r) => ({ age: "latest", _value:r._value }))

join(tables: {total: total, consumed: consumed}, on: ["age"])
  |> map(fn: (r) => ({_value: r._value_total - r._value_consumed}))

ThomDietrich
  • 79
  • 1
  • 7
0

In the query, total and consumed are tables. For how to extract and use scalar values, please see Extract scalar values in Flux

alespour
  • 397
  • 1
  • 5