Back to Influxdb

Define custom functions

content/flux/v0/define-functions.md

latest7.7 KB
Original Source

Flux's functional syntax lets you define custom functions. Learn the basics of creating your own functions.

On this page:

Function definition syntax

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

js
// Basic function definition syntax
functionName = (functionParameters) => functionBody
  • functionName: Name to use to execute the function.
  • functionParameters: Comma-separated list of parameters passed into the function.
  • functionBody: Operations on function parameters.

Define parameter defaults

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

js
functionName = (param1=defaultVal1, param2=defaultVal2) => functionBody

Defaults are overridden by explicitly defining the parameter in the function call. Parameters without default values are considered required parameters.

Custom function examples

{{< expand-wrapper >}} {{% expand "Square a number" %}}

js
square = (n) => n * n

square(n:3)
// Returns 9

{{% /expand %}} {{% expand "Multiple two values" %}}

js
multiply = (x, y) => x * y

multiply(x: 2, y: 15)
// Returns 30

{{% /expand %}} {{% expand "Calculate n to the p power (with default parameters)" %}}

js
pow = (n, p=10) => n ^ p

pow(n: 2)
// Returns 1024

{{% /expand %}} {{< /expand-wrapper >}}

Create a custom transformation

A transformation is a function that takes a stream of tables as input, operates on the input, and then outputs a new stream of tables.

The pipe-forward operator (|>) pipes data from the previous identifier or function forward into a transformation. To use piped-forward data, assign a function parameter to the pipe-receive operator (<-).

In the following example, the function x() receives piped-forwarded data and assigns it to the t parameter. In the function body, t is piped forward into other operations to generate output.

js
x = (t=<-) => t |> //...

Custom transformation examples

{{< expand-wrapper >}} {{% expand "Multiply values by x" %}}

Multiply values by x

The following example defines a multByX function that multiplies the _value column of each input row by the x parameter. The example uses the map() function to iterate over each row, modify the _value, and then return the updated row.

Function definition
js
multByX = (tables=<-, x) =>
    tables
        |> map(fn: (r) => ({r with _value: r._value * x}))
Example usage
js
data
    |> multByX(x: 2.0)

{{< flex >}} {{% flex-content %}}

Given the following input data:
srcID_field_value
t1afoo1.2
t1afoo3.4
t1afoo5.6
srcID_field_value
t2bfoo0.8
t2bfoo1.9
t2bfoo2.7
{{% /flex-content %}}
{{% flex-content %}}
The example above returns:
srcID_field_value
t1afoo2.4
t1afoo6.8
t1afoo11.2
srcID_field_value
t2bfoo1.6
t2bfoo3.8
t2bfoo5.4
{{% /flex-content %}}
{{< /flex >}}

{{% /expand %}} {{% expand "Calculate speed" %}}

Calculate speed

The following example defines a speed function that calculates speed using an elapsed and distance column in input tables. The example uses the map() function to iterate over each row, calculate the speed per specified unit of distance, and then return the updated row with a new speed column.

Function definition
js
speed = (tables=<-, unit="m") =>
    tables
        |> map(
            fn: (r) => {
                elapsedHours = float(v: int(v: duration(v: r.elapsed))) / float(v: int(v: 1h))
                distance = float(v: r.distance)
                speed = distance / elapsedHours

                return {r with speed: "${speed} ${unit}ph"}
            },
        )
Example usage
js
data
    |> speed()

{{< flex >}} {{% flex-content %}}

Given the following input data:
idelapseddistance
t11h15m101
t21h32m138
t356m112
{{% /flex-content %}}
{{% flex-content %}}
The example above returns:
idelapseddistancespeed
t11h15m10188.8 mph
t21h32m13890 mph
t356m112120 mph
{{% /flex-content %}}
{{< /flex >}}

{{% /expand %}} {{< /expand-wrapper >}}

Define functions with scoped variables

To create custom functions with variables scoped to the function,

  1. Enclose your function body in a block ({}).
  2. Use a return statement to return a specific variable.
js
functionName = (param) => {
    exampleVar = "foo"

    return exampleVar
}

Example functions with scoped variables

{{< expand-wrapper >}} {{% expand "Return an alert level based on a value" %}}

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:

js
alertLevel = (v) => {
    level =
        if float(v: v) >= 90.0 then
            "crit"
        else if float(v: v) >= 80.0 then
            "warn"
        else if float(v: v) >= 65.0 then
            "info"
        else
            "ok"

    return level
}

alertLevel(v: 87.3)
// Returns "warn"

{{% /expand %}}

{{% expand "Convert a HEX color code to a name" %}}

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.

js
import "dict"

hexName = (hex) => {
    hexNames =
        dict.fromList(
            pairs: [
                {key: "#00ffff", value: "Aqua"},
                {key: "#000000", value: "Black"},
                {key: "#0000ff", value: "Blue"},
                {key: "#ff00ff", value: "Fuchsia"},
                {key: "#808080", value: "Gray"},
                {key: "#008000", value: "Green"},
                {key: "#00ff00", value: "Lime"},
                {key: "#800000", value: "Maroon"},
                {key: "#000080", value: "Navy"},
                {key: "#808000", value: "Olive"},
                {key: "#800080", value: "Purple"},
                {key: "#ff0000", value: "Red"},
                {key: "#c0c0c0", value: "Silver"},
                {key: "#008080", value: "Teal"},
                {key: "#ffffff", value: "White"},
                {key: "#ffff00", value: "Yellow"},
            ],
        )
    name = dict.get(dict: hexNames, key: hex, default: "No known name")

    return name
}

hexName(hex: "#000000")
// Returns "Black"

hexName(hex: "#8b8b8b")
// Returns "No known name"

{{% /expand %}} {{< /expand-wrapper >}}