2019-01-22 05:31:27 +00:00
|
|
|
---
|
|
|
|
title: Create custom Flux functions
|
|
|
|
description: Create your own custom Flux functions to transform and manipulate data.
|
2019-02-20 00:04:06 +00:00
|
|
|
v2.0/tags: [functions, custom, flux]
|
2019-01-22 05:31:27 +00:00
|
|
|
menu:
|
2019-01-22 15:29:28 +00:00
|
|
|
v2_0:
|
|
|
|
name: Create custom functions
|
|
|
|
parent: How-to guides
|
2019-02-06 17:48:09 +00:00
|
|
|
weight: 208
|
2019-01-22 05:31:27 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Basic function definition structure
|
|
|
|
functionName = (functionParameters) => functionOperations
|
|
|
|
```
|
|
|
|
|
2019-01-22 15:54:31 +00:00
|
|
|
##### functionName
|
2019-01-22 05:31:27 +00:00
|
|
|
The name used to call the function in your Flux script.
|
|
|
|
|
2019-01-22 15:54:31 +00:00
|
|
|
##### functionParameters
|
2019-01-22 05:31:27 +00:00
|
|
|
A comma-separated list of parameters passed into the function and used in its operations.
|
|
|
|
[Parameter defaults](#define-parameter-defaults) can be defined for each.
|
|
|
|
|
2019-01-22 15:54:31 +00:00
|
|
|
##### functionOperations
|
2019-01-22 05:31:27 +00:00
|
|
|
Operations and functions that manipulate the input into the desired output.
|
|
|
|
|
|
|
|
#### Basic function examples
|
|
|
|
|
|
|
|
###### Example square function
|
|
|
|
```js
|
|
|
|
// Function definition
|
|
|
|
square = (n) => n * n
|
|
|
|
|
|
|
|
// Function usage
|
|
|
|
> square(n:3)
|
|
|
|
9
|
|
|
|
```
|
|
|
|
|
|
|
|
###### Example multiply function
|
|
|
|
```js
|
|
|
|
// Function definition
|
|
|
|
multiply = (x, y) => x * y
|
|
|
|
|
|
|
|
// Function usage
|
|
|
|
> multiply(x:2, y:15)
|
|
|
|
30
|
|
|
|
```
|
|
|
|
|
2019-01-22 15:54:31 +00:00
|
|
|
## 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
|
2019-01-22 05:31:27 +00:00
|
|
|
parameters must capture the input tables using the `<-` pipe-receive expression.
|
|
|
|
|
|
|
|
In the example below, the `tables` parameter is assigned to the `<-` expression,
|
2019-01-22 15:54:31 +00:00
|
|
|
which represents all data piped-forward into the function.
|
|
|
|
`tables` is then piped-forward into other operations in the function definition.
|
2019-01-22 05:31:27 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
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.
|
2019-02-08 16:41:28 +00:00
|
|
|
It uses the [`map()` function](/v2.0/reference/flux/functions/built-in/transformations/map)
|
2019-01-22 15:54:31 +00:00
|
|
|
to modify each `_value`.
|
2019-01-22 05:31:27 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
// Function definition
|
|
|
|
multByX = (tables=<-, x) =>
|
|
|
|
tables
|
|
|
|
|> map(fn: (r) => r._value * x)
|
|
|
|
|
|
|
|
// Function usage
|
|
|
|
from(bucket: "telegraf/autogen")
|
|
|
|
|> range(start: -1m)
|
|
|
|
|> filter(fn: (r) =>
|
|
|
|
r._measurement == "mem" and
|
|
|
|
r._field == "used_percent"
|
|
|
|
)
|
|
|
|
|> multByX(x:2.0)
|
|
|
|
```
|
|
|
|
|
|
|
|
## Define parameter defaults
|
2019-01-22 15:54:31 +00:00
|
|
|
Use the `=` assignment operator to assign a default value to function parameters
|
|
|
|
in your function definition:
|
2019-01-22 05:31:27 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
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`.
|
2019-02-08 16:41:28 +00:00
|
|
|
It uses the [`sort()` function](/v2.0/reference/flux/functions/built-in/transformations/sort)
|
2019-01-22 15:54:31 +00:00
|
|
|
to sort records in either descending or ascending order.
|
2019-02-08 16:41:28 +00:00
|
|
|
It then uses the [`limit()` function](/v2.0/reference/flux/functions/built-in/transformations/limit)
|
2019-01-22 15:54:31 +00:00
|
|
|
to return the first record from the sorted table.
|
2019-01-22 05:31:27 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
// Function definition
|
|
|
|
getWinner = (tables=<-, noSarcasm:true) =>
|
|
|
|
tables
|
|
|
|
|> sort(desc: noSarcasm)
|
|
|
|
|> limit(n:1)
|
|
|
|
|
|
|
|
// Function usage
|
|
|
|
// Get the winner
|
|
|
|
from(bucket: "telegraf/autogen")
|
|
|
|
|> range(start: -1m)
|
|
|
|
|> filter(fn: (r) =>
|
|
|
|
r._measurement == "mem" and
|
|
|
|
r._field == "used_percent"
|
|
|
|
)
|
|
|
|
|> getWinner()
|
|
|
|
|
|
|
|
// Get the "winner"
|
|
|
|
from(bucket: "telegraf/autogen")
|
|
|
|
|> range(start: -1m)
|
|
|
|
|> filter(fn: (r) =>
|
|
|
|
r._measurement == "mem" and
|
|
|
|
r._field == "used_percent"
|
|
|
|
)
|
|
|
|
|> getWinner(noSarcasm: false)
|
|
|
|
```
|