Create custom Flux functions

This page documents an earlier version of InfluxDB. InfluxDB v2.7 is the latest stable version. View this page in the v2.7 documentation.

Flux’s functional syntax lets you create custom functions. This guide walks through the basics of creating your own function.

Function definition syntax

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

  1. // Basic function definition syntax
  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

Use piped-forward data in a custom function

Most Flux functions process piped-forward data. 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) => tables
  3. |> map(fn: (r) => ({r with _value: r._value * x}))
  4. // Function usage
  5. from(bucket: "example-bucket")
  6. |> range(start: -1m)
  7. |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
  8. |> 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 a list of leaders

The example below defines a leaderBoard function that returns a limited number of records sorted by values in specified columns. It uses the sort() function to sort records in either descending or ascending order. It then uses the limit() function to return a specified number of records from the sorted table.

  1. // Function definition
  2. leaderBoard = (tables=<-, limit=4, columns=["_value"], desc=true) => tables
  3. |> sort(columns: columns, desc: desc)
  4. |> limit(n: limit)
  5. // Function usage
  6. // Get the 4 highest scoring players
  7. from(bucket: "example-bucket")
  8. |> range(start: -1m)
  9. |> filter(fn: (r) => r._measurement == "player-stats" and r._field == "total-points")
  10. |> leaderBoard()
  11. // Get the 10 shortest race times
  12. from(bucket: "example-bucket")
  13. |> range(start: -1m)
  14. |> filter(fn: (r) => r._measurement == "race-times" and r._field == "elapsed-time")
  15. |> leaderBoard(limit: 10, desc: false)

Define functions with scoped variables

To create custom functions with variables scoped to the function, place your function operations and variables inside of a block ({}) and use a return statement to return a specific variable.

  1. functionName = (functionParameters) => {
  2. exampleVar = "foo"
  3. return exampleVar
  4. }

Example functions with scoped variables

Return an alert level based on a value

The following function uses conditional logic to return an alert level based on a numeric input value:

  1. alertLevel = (v) => {
  2. level = if float(v: v) >= 90.0 then
  3. "crit"
  4. else if float(v: v) >= 80.0 then
  5. "warn"
  6. else if float(v: v) >= 65.0 then
  7. "info"
  8. else
  9. "ok"
  10. return level
  11. }
  12. alertLevel(v: 87.3)
  13. // Returns "warn"

Convert a HEX color code to a name

The following function converts a hexadecimal (HEX) color code to the equivalent HTML color name. The functions uses the Flux dictionary package to create a dictionary of HEX codes and their corresponding names.

  1. import "dict"
  2. hexName = (hex) => {
  3. hexNames = dict.fromList(
  4. pairs: [
  5. {key: "#00ffff", value: "Aqua"},
  6. {key: "#000000", value: "Black"},
  7. {key: "#0000ff", value: "Blue"},
  8. {key: "#ff00ff", value: "Fuchsia"},
  9. {key: "#808080", value: "Gray"},
  10. {key: "#008000", value: "Green"},
  11. {key: "#00ff00", value: "Lime"},
  12. {key: "#800000", value: "Maroon"},
  13. {key: "#000080", value: "Navy"},
  14. {key: "#808000", value: "Olive"},
  15. {key: "#800080", value: "Purple"},
  16. {key: "#ff0000", value: "Red"},
  17. {key: "#c0c0c0", value: "Silver"},
  18. {key: "#008080", value: "Teal"},
  19. {key: "#ffffff", value: "White"},
  20. {key: "#ffff00", value: "Yellow"},
  21. ],
  22. )
  23. name = dict.get(dict: hexNames, key: hex, default: "No known name")
  24. return name
  25. }
  26. hexName(hex: "#000000")
  27. // Returns "Black"
  28. hexName(hex: "#8b8b8b")
  29. // Returns "No known name"

functions custom flux