DEV Community

Lourdes Suello
Lourdes Suello

Posted on

Sample of flux query

Flux is a functional data scripting language designed for querying and analyzing time series data. Here's a simple example to get you started with Flux, assuming you are working with InfluxDB:

Basic Flux Query Example

from(bucket: "example-bucket")
  |> range(start: -1h)
  |> filter(fn: (r) => r._measurement == "temperature" and r._field == "value")
  |> mean()
Enter fullscreen mode Exit fullscreen mode

Explanation:
from(bucket: "example-bucket"):

This specifies the bucket (database) you want to query data from.
range(start: -1h):

This defines the time range for your query. Here, -1h means data from the last hour.
filter(fn: (r) => r._measurement == "temperature" and r._field == "value"):

This filters the data to include only the points where the _measurement is "temperature" and the _field is "value".
mean():

This calculates the mean (average) value of the filtered data.

More Complex Example with Grouping and Aggregation

from(bucket: "example-bucket")
  |> range(start: -24h)
  |> filter(fn: (r) => r._measurement == "cpu" and r._field == "usage_user")
  |> aggregateWindow(every: 1h, fn: mean)
  |> group(columns: ["host"])
  |> sort(columns: ["_time"], desc: true)
Enter fullscreen mode Exit fullscreen mode

Explanation:
from(bucket: "example-bucket"):

Specifies the bucket (database) to query from.
range(start: -24h):

Queries data from the last 24 hours.
filter(fn: (r) => r._measurement == "cpu" and r._field == "usage_user"):

Filters the data to include only the CPU usage for user processes.
aggregateWindow(every: 1h, fn: mean):

Aggregates the data into 1-hour windows and calculates the mean for each window.
group(columns: ["host"]):

Groups the data by the host column, so you can see the CPU usage for each host.
sort(columns: ["_time"], desc: true):

Sorts the data by the _time column in descending order.

Query with Mathematical Operations and Joins
cpu = from(bucket: "example-bucket")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "cpu" and r._field == "usage_system")

mem = from(bucket: "example-bucket")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")

join(tables: {cpu: cpu, mem: mem}, on: ["_time", "host"])
|> map(fn: (r) => ({ r with cpu_mem_ratio: r.usage_system / r.used_percent }))

Explanation:
cpu and mem:

Defines two separate queries for CPU and memory usage data.
join(tables: {cpu: cpu, mem: mem}, on: ["_time", "host"]):

Joins the CPU and memory data on the _time and host columns.
map(fn: (r) => ({ r with cpu_mem_ratio: r.usage_system / r.used_percent })):

Adds a new field cpu_mem_ratio which is the ratio of CPU system usage to memory usage.
Useful Tips:
Documentation: Always refer to the official Flux documentation for the latest features and detailed explanations.
Experiment: Use the InfluxDB UI to experiment with different queries and see the results in real-time.
Functions: Explore various Flux functions like window(), derivative(), spread(), etc., to perform advanced data analysis.
These examples should help you get started with Flux queries in InfluxDB. Let me know if you need more specific queries or further assistance!

Top comments (0)