Create custom Flux functions

Flux’s functional syntax allows for custom functions. This guide walks through the basics of creating your own function.

Function definition structure

The basic structure for defining functions in Flux is as follows:

  1. // Basic function definition structure
  2. functionName = (functionParameters) => functionOperations
functionName

The name used to call the function in your Flux script.

functionParameters

A comma-separated list of parameters passed into the function and used in its operations. Parameter defaults can be defined for each.

functionOperations

Operations and functions that manipulate the input into the desired output.

Basic function examples

Example square function
  1. // Function definition
  2. square = (n) => n * n
  3. // Function usage
  4. > square(n:3)
  5. 9
Example multiply function
  1. // Function definition
  2. multiply = (x, y) => x * y
  3. // Function usage
  4. > multiply(x:2, y:15)
  5. 30

Functions that manipulate piped-forward data

Most Flux functions manipulate data piped-forward into the function. In order for a custom function to process piped-forward data, one of the function parameters must capture the input tables using the <- pipe-receive expression.

In the example below, the tables parameter is assigned to the <- expression, which represents all data piped-forward into the function. tables is then piped-forward into other operations in the function definition.

  1. functionName = (tables=<-) => tables |> functionOperations

Pipe-forwardable function example

Multiply row values by x

The example below defines a multByX function that multiplies the _value column of each row in the input table by the x parameter. It uses the map() function to modify each _value.

  1. // Function definition
  2. multByX = (tables=<-, x) =>
  3. tables
  4. |> map(fn: (r) => ({ r with _value: r._value * x}))
  5. // Function usage
  6. from(bucket: "example-bucket")
  7. |> range(start: -1m)
  8. |> filter(fn: (r) =>
  9. r._measurement == "mem" and
  10. r._field == "used_percent"
  11. )
  12. |> multByX(x:2.0)

Define parameter defaults

Use the = assignment operator to assign a default value to function parameters in your function definition:

  1. functionName = (param1=defaultValue1, param2=defaultValue2) => functionOperation

Defaults are overridden by explicitly defining the parameter in the function call.

Example functions with defaults

Get the winner or the “winner”

The example below defines a getWinner function that returns the record with the highest or lowest _value (winner versus “winner”) depending on the noSarcasm parameter which defaults to true. It uses the sort() function to sort records in either descending or ascending order. It then uses the limit() function to return the first record from the sorted table.

  1. // Function definition
  2. getWinner = (tables=<-, noSarcasm:true) =>
  3. tables
  4. |> sort(desc: noSarcasm)
  5. |> limit(n:1)
  6. // Function usage
  7. // Get the winner
  8. from(bucket: "example-bucket")
  9. |> range(start: -1m)
  10. |> filter(fn: (r) =>
  11. r._measurement == "mem" and
  12. r._field == "used_percent"
  13. )
  14. |> getWinner()
  15. // Get the "winner"
  16. from(bucket: "example-bucket")
  17. |> range(start: -1m)
  18. |> filter(fn: (r) =>
  19. r._measurement == "mem" and
  20. r._field == "used_percent"
  21. )
  22. |> getWinner(noSarcasm: false)

functions custom flux