Creating Custom Functions With Tips from InfluxDB University

Navigate to:

Flux is InfluxDB’s functional data scripting language. It’s made to query, process, analyze, and act on data. It’s very powerful and is built and optimized for time series. There are so many things you can do with Flux it can be hard to know where to start. This August, InfluxDB University launched a free Intermediate Flux course taught by experts that can take your Flux skills up a notch. It includes lessons and hands-on exercises on window functions, moving averages, custom functions, joining data streams, and creating tasks and alerts. In this post we’re just going to explore custom functions.

Background on Flux

Flux queries do four basic things. They retrieve data, filter it based on time or column values, process and shape it, and return the result. You can also use Flux to create tasks, scripts that are scheduled to run at specified intervals, which are very helpful for automating downsampling and analysis. Several important factors were considered when Flux was originally designed. It’s meant to be simple to learn and easy for developers to read. Queries are code and you can run tests on them to find and correct bugs. Another key aspect is the importance of open source contributions. Flux was also built to be composable so developers can build their own functions and libraries onto Flux to tailor it to their application. There are many built-in functions included within Flux, from simple means to complex geospatial calculations. But developers and companies have unique needs, and the flexibility of Flux that allows for custom functions is one of its great strengths.

Example custom functions

Custom functions are useful when there’s an operation you need to do, often repeatedly throughout your code, that’s not included in Flux’s built-in functions. It could be a very complex calculation or something simple like converting Celsius to Farenheit. Creating a custom function saves time because you don’t have to type out the calculation repeatedly, makes code easier to read, and makes debugging simpler. Here we’re going to go over the syntax of a custom function using a basic squared calculation.

The syntax for a basic custom function is:

[function name] = ([variable]) => [implementation]

For example:

squared = (x) => x*x

After defining a custom function you can use it in Flux scripts, such as:

from(bucket: “foo”)
	|> range(start: -1hr)
	|> filter(fn: (r) => r._measurement == “samples”)
	|> map(fn: (r) => ({ _value: squared(x: r._value)}))
	|> filter(fn: (r) => r._value > 23.2)

In the example above, the custom function is used within the map() function to square all _value input rows.

You can also write custom pipe-forwardable functions in Flux. These can be pipelined together with other operators directly, rather than nested within other functions like the squared() function within the map() function above. Within the body of the function, you use standard Flux syntax.

The syntax for custom pipe forwardable functions is:

[function name] = ([table]="-,[variable]) => [table] |" [implementation]

To write a custom function to square every value in a table, instead of one variable at a time, you could write:

allSquared = (tables="-) => tables |" map(fn: (r) =>  squared(r._value))

You could use this function in a script such as

from(bucket: “foo”)
	|> range(start: -1hr)
	|> filter(fn: (r) => r._measurement == “samples”)
	|> allSquared()
	|> filter(fn: (r) => r._value > 23.2)

In this example, the variable used in the body of the function is left blank and it operates on all variables. Pipe-forwardable functions make clear code that’s simple to read and easier to maintain over time.

Become a Flux expert

The self-paced InfluxDB University course Intermediate Flux has more information and exercises exploring custom functions, conditional expressions, joining time streams, and more.

If you finish the Beginner Flux and Intermediate Flux courses on InfluxDB University, you’re eligible to win a free event ticket to the Advanced Flux Training this November 8-9 in London. This live training is geared towards data engineers and covers advanced topics like identifying correlation, handling seasonality, and forecasting time series. You can get more information and enter to win a free ticket here.

Flux Giveaway social tile