Redis.Commands.TimeSeries (Redis v0.8.0)

Copy Markdown View Source

Command builders for Redis TimeSeries (TS.*) operations.

Redis TimeSeries is an append-only data structure optimized for storing timestamped numeric samples. It supports automatic downsampling via compaction rules, label-based indexing for multi-series queries, and built-in aggregation functions (avg, sum, min, max, count, etc.) over arbitrary time windows. Common use cases include metrics collection, IoT sensor data, financial tick data, and application monitoring.

All functions in this module are pure and return a command list (a list of strings) suitable for passing to Redis.command/2 or Redis.pipeline/2.

Examples

# Create a time series and add a sample
Redis.pipeline(conn, [
  TimeSeries.ts_create("temp:office", labels: %{"location" => "office"}),
  TimeSeries.ts_add("temp:office", "*", 22.5)
])

# Query a time range with 1-hour average aggregation
Redis.command(conn, TimeSeries.ts_range("temp:office", "-", "+",
  aggregation: {:avg, 3_600_000}
))

Summary

Functions

Appends a sample to a time series. Use "*" as the timestamp to let Redis assign the current server time. Accepts the same label and retention options as ts_create/2, plus :on_duplicate for handling duplicate timestamps.

Creates a new time series key. Accepts options including :retention (max age in milliseconds), :labels (a map or keyword list of label key-value pairs), :encoding, :chunk_size, and :duplicate_policy.

Queries samples in the time range from..to (inclusive). Use "-" and "+" for the minimum and maximum timestamps. Supports :count, :aggregation (e.g. {:avg, 60_000}), :filter_by_ts, and :filter_by_value options.

Functions

ts_add(key, timestamp, value, opts \\ [])

@spec ts_add(String.t(), String.t() | integer(), String.t() | number(), keyword()) ::
  [String.t()]

Appends a sample to a time series. Use "*" as the timestamp to let Redis assign the current server time. Accepts the same label and retention options as ts_create/2, plus :on_duplicate for handling duplicate timestamps.

ts_alter(key, opts \\ [])

@spec ts_alter(
  String.t(),
  keyword()
) :: [String.t()]

ts_create(key, opts \\ [])

@spec ts_create(
  String.t(),
  keyword()
) :: [String.t()]

Creates a new time series key. Accepts options including :retention (max age in milliseconds), :labels (a map or keyword list of label key-value pairs), :encoding, :chunk_size, and :duplicate_policy.

ts_decrby(key, value, opts \\ [])

@spec ts_decrby(String.t(), String.t() | number(), keyword()) :: [String.t()]

ts_del(key, from_timestamp, to_timestamp)

@spec ts_del(String.t(), String.t() | integer(), String.t() | integer()) :: [
  String.t()
]

ts_get(key, opts \\ [])

@spec ts_get(
  String.t(),
  keyword()
) :: [String.t()]

ts_incrby(key, value, opts \\ [])

@spec ts_incrby(String.t(), String.t() | number(), keyword()) :: [String.t()]

ts_info(key, opts \\ [])

@spec ts_info(
  String.t(),
  keyword()
) :: [String.t()]

ts_madd(entries)

@spec ts_madd([{String.t(), String.t() | integer(), String.t() | number()}]) :: [
  String.t()
]

ts_mget(filters, opts \\ [])

@spec ts_mget(
  [String.t()],
  keyword()
) :: [String.t()]

ts_mrange(from, to, filters, opts \\ [])

@spec ts_mrange(
  String.t() | integer(),
  String.t() | integer(),
  [String.t()],
  keyword()
) :: [String.t()]

ts_mrevrange(from, to, filters, opts \\ [])

@spec ts_mrevrange(
  String.t() | integer(),
  String.t() | integer(),
  [String.t()],
  keyword()
) :: [
  String.t()
]

ts_queryindex(filters)

@spec ts_queryindex([String.t()]) :: [String.t()]

ts_range(key, from, to, opts \\ [])

@spec ts_range(String.t(), String.t() | integer(), String.t() | integer(), keyword()) ::
  [String.t()]

Queries samples in the time range from..to (inclusive). Use "-" and "+" for the minimum and maximum timestamps. Supports :count, :aggregation (e.g. {:avg, 60_000}), :filter_by_ts, and :filter_by_value options.

ts_revrange(key, from, to, opts \\ [])

@spec ts_revrange(
  String.t(),
  String.t() | integer(),
  String.t() | integer(),
  keyword()
) :: [String.t()]