Barograph.Query (barograph v0.1.0)

Copy Markdown View Source

Generated SQL for time-bucketed queries (spec §9.1).

Elixir functions returning SQL fragments — no C extension, no custom query language. Level 1 of the query layer: the common case as a single call. Everything here degrades gracefully to level 3 — the generated SQL is plain SQLite, runnable by hand.

Summary

Functions

Bucket width of {n, unit} expressed in the database's time unit.

Runs a bucketed aggregate query against a metric (spec §9.2 level 1).

The time_bucket hyperfunction as a SQL fragment (spec §9.1): integer division and multiply on epoch timestamps.

Functions

bucket_width(arg, time_unit)

@spec bucket_width(
  {pos_integer(), atom()},
  atom()
) :: pos_integer()

Bucket width of {n, unit} expressed in the database's time unit.

run(db, metric, opts)

@spec run(Barograph.db(), String.t(), keyword()) :: {:ok, [map()]} | {:error, term()}

Runs a bucketed aggregate query against a metric (spec §9.2 level 1).

Options

  • :labels - label filter map; matched with json_extract on the series' labels. A series matches if it carries at least the given pairs.
  • :from, :to - DateTime or integer epoch in the database's time unit. from is inclusive, to exclusive.
  • :bucket - {n, unit} where unit is :second, :minute, :hour, :day, or :week. Required when :agg is given.
  • :agg - one of :avg, :min, :max, :sum, :count.

Without :bucket, returns raw samples as %{ts, value} maps, ordered by time. With :bucket, returns %{bucket, value} maps where bucket is the bucket start epoch in the database's time unit.

time_bucket(ts_expr, width)

@spec time_bucket(String.t(), pos_integer()) :: String.t()

The time_bucket hyperfunction as a SQL fragment (spec §9.1): integer division and multiply on epoch timestamps.