Define custom functions
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:
// 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:
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
Square a number
square = (n) => n * n
square(n:3)
// Returns 9
Multiple two values
multiply = (x, y) => x * y
multiply(x: 2, y: 15)
// Returns 30
Calculate n to the p power (with default parameters)
pow = (n, p=10) => n ^ p
pow(n: 2)
// Returns 1024
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.
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
multByX = (tables=<-, x) =>
tables
|> map(fn: (r) => ({r with _value: r._value * x}))
Example usage
srcID | _field | _value |
---|
t1a | foo | 1.2 |
t1a | foo | 3.4 |
t1a | foo | 5.6 |
srcID | _field | _value |
---|
t2b | foo | 0.8 |
t2b | foo | 1.9 |
t2b | foo | 2.7 |
The example above returns:
srcID | _field | _value |
---|
t1a | foo | 2.4 |
t1a | foo | 6.8 |
t1a | foo | 11.2 |
srcID | _field | _value |
---|
t2b | foo | 1.6 |
t2b | foo | 3.8 |
t2b | foo | 5.4 |
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
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
id | elapsed | distance |
---|
t1 | 1h15m | 101 |
t2 | 1h32m | 138 |
t3 | 56m | 112 |
The example above returns:
id | elapsed | distance | speed |
---|
t1 | 1h15m | 101 | 88.8 mph |
t2 | 1h32m | 138 | 90 mph |
t3 | 56m | 112 | 120 mph |
Define functions with scoped variables
To create custom functions with variables scoped to the function,
- Enclose your function body in a
block (
{}
). - Use a
return
statement to return a specific variable.
functionName = (param) => {
exampleVar = "foo"
return exampleVar
}
Example functions with scoped variables
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:
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"
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.
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"
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community!
We welcome and encourage your feedback and bug reports for Flux and this documentation.
To find support, use the following resources:
Customers with an annual or support contract can contact InfluxData Support.