Transform data with mathematic operations

If you’re just getting started with Flux queries, check out the following:

  • for a conceptual overview of Flux and parts of a Flux query.
  • Execute queries to discover a variety of ways to run your queries.
Basic mathematic operations

See Flux Read-Eval-Print Loop (REPL).

Operands must be the same type

Operands in Flux mathematic operations must be the same data type. For example, integers cannot be used in operations with floats. Otherwise, you will get an error similar to:

To convert operands to the same type, use type-conversion functions or manually format operands. The operand data type determines the output data type. For example:

  1. 100 // Parsed as an integer
  2. 100.0 // Parsed as a float
  3. // Example evaluations
  4. > 20 / 8
  5. 2
  6. > 20.0 / 8.0
  7. 2.5
Custom multiplication function
  1. multiply = (x, y) => x * y
  2. multiply(x: 10, y: 12)
  3. // Returns 120
Custom percentage function

To transform multiple values in an input stream, your function needs to:

  • .
  • Each operand necessary for the calculation exists in each row (see Pivot vs join below).
  • Use the to iterate over each row.

The example multiplyByX() function below includes:

  • A tables parameter that represents the input data stream (<-).
  • An x parameter which is the number by which values in the _value column are multiplied.
  • A map() function that iterates over each row in the input stream. It uses the with operator to preserve existing columns in each row. It also multiples the _value column by x.
  1. multiplyByX = (x, tables=<-) => tables
  2. data
  3. |> multiplyByX(x: 10)

To convert active memory from bytes to gigabytes (GB), divide the active field in the mem measurement by 1,073,741,824.

The map() function iterates over each row in the piped-forward data and defines a new _value by dividing the original _value by 1073741824.

  1. from(bucket: "example-bucket")
  2. |> range(start: -10m)
  3. |> filter(fn: (r) => r._measurement == "mem" and r._field == "active")
  4. |> map(fn: (r) => ({r with _value: r._value / 1073741824}))
  1. bytesToGB = (tables=<-) => tables
  2. |> map(fn: (r) => ({r with _value: r._value / 1073741824}))
  3. data

Include partial gigabytes

Because the original metric (bytes) is an integer, the output of the operation is an integer and does not include partial GBs. To calculate partial GBs, convert the _value column and its values to floats using the and format the denominator in the division operation as a float.

To calculate a percentage, use simple division, then multiply the result by 100.

  1. > 1.0 / 4.0 * 100.0
  2. 25.0

For an in-depth look at calculating percentages, see Calculate percentages.

To query and use values in mathematical operations in Flux, operand values must exists in a single row. Both pivot() and join() will do this, but there are important differences between the two:

Pivot is more performant

Use join for multiple data sources

Use join() when querying data from different buckets or data sources.

Pivot fields into columns for mathematic calculations
  1. data
  2. |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  3. |> map(fn: (r) => ({r with _value: (r.field1 + r.field2) / r.field3 * 100.0}))
Join multiple data sources for mathematic calculations
  1. import "sql"
  2. import "influxdata/influxdb/secrets"
  3. pgUser = secrets.get(key: "POSTGRES_USER")
  4. pgPass = secrets.get(key: "POSTGRES_PASSWORD")
  5. pgHost = secrets.get(key: "POSTGRES_HOST")
  6. t1 = sql.from(
  7. driverName: "postgres",
  8. dataSourceName: "postgresql://${pgUser}:${pgPass}@${pgHost}",
  9. query: "SELECT id, name, available FROM example_table",
  10. )
  11. t2 = from(bucket: "example-bucket")
  12. |> range(start: -1h)
  13. |> filter(fn: (r) => r._measurement == "example-measurement" and r._field == "example-field")
  14. join(tables: {t1: t1, t2: t2}, on: ["id"])
  15. |> map(fn: (r) => ({r with _value: r._value_t2 / r.available_t1 * 100.0}))