InfluxEx.Flux (influx_ex v0.2.0)

Functions to build Flux queries

Current time related functionality only supports flux's duration types, for this is a value such has 15m. Where 15 is the number of the unit that follows, which in this case is m meaning minutes.

For more information about duration types see the Flux docs.

The %InfluxEx.Flux{} struct implements the String.Chars protocol which allows to_string/1 to be called on the structure.

Link to this section Summary

Functions

Add an aggregate window

Set which field the query is for

Fill missing values with the previous one

Set the bucket for the query

Set the measurement name to query for

Set the time range for the query

Set a tag to filter against

Link to this section Types

Link to this type

aggregate_selector()

@type aggregate_selector() :: :mean
Link to this type

aggregate_window()

@type aggregate_window() :: %{
  every: binary(),
  create_empty: false,
  fn: aggregate_selector()
}
Link to this type

aggregate_window_opt()

@type aggregate_window_opt() :: {:create_empty, boolean()}
@type t() :: %InfluxEx.Flux{
  aggregate_window: aggregate_window() | nil,
  bucket: InfluxEx.Bucket.name(),
  end: binary() | nil,
  field: binary() | nil,
  fill_value_use_previous: boolean(),
  measurement: binary() | nil,
  start: binary() | nil,
  tags: map()
}

Data structure for a flux query

Link to this section Functions

Link to this function

aggregate_window(f, every, opts \\ [])

@spec aggregate_window(t(), binary(), [aggregate_window_opt()]) :: t()

Add an aggregate window

Link to this function

field(f, field)

@spec field(t(), binary()) :: t()

Set which field the query is for

"my_bucket"
|> InfluxEx.Flux.from()
|> InfluxEx.Flux.range("-15m")
|> InfluxEx.Flux.measurement("cpu")
|> InfluxEx.Flux.field("average")
Link to this function

fill_value_previous(f)

Fill missing values with the previous one

This is useful for setting the :create_empty field to true when using Flux.aggregate_window/3.

"my bucket"
|> InfluxEx.Flux.from()
|> InfluxEx.Flux.range("-1d")
|> InfluxEx.Flux.measurement("cpu")
|> InfluxEx.Flux.field("average")
|> InfluxEx.Flux.aggregate_window("1h", create_empty: true)
|> InfluxEx.Flux.fill_value_previous()

The above query will return tables with data over the last day averaged in one hour intervals. Where there's missing data points, the query will fill the value with the last data point's value.

@spec from(InfluxEx.Bucket.name()) :: t()

Set the bucket for the query

This function starts a new flux query.

InfluxEx.Flux.from("my bucket")
Link to this function

measurement(f, measurement)

@spec measurement(t(), binary()) :: t()

Set the measurement name to query for

"my_bucket"
|> InfluxEx.Flux.from()
|> InfluxEx.Flux.range("-15m")
|> InfluxEx.Flux.measurement("cpu")
Link to this function

range(f, start, end_t \\ nil)

@spec range(t(), binary(), binary() | nil) :: t()

Set the time range for the query

To query a bucket over the last 15 minutes, you can use range/3:

"my_bucket"
|> InfluxEx.Flux.from()
|> InfluxEx.Flux.range("-15m")
Link to this function

run_query(flux_query, client, opts \\ [])

@spec run_query(t(), InfluxEx.Client.t(), [InfluxEx.query_opt()]) ::
  {:ok, InfluxEx.tables()} | {:error, InfluxEx.error()}

Run the flux query

Link to this function

tag(f, tag_name, tag_value)

@spec tag(t(), atom() | binary(), binary()) :: t()

Set a tag to filter against