InfluxElixir.Query.ResponseParser (InfluxElixir v0.1.37)

Copy Markdown View Source

Parses InfluxDB query responses in JSON, JSONL, InfluxDB 3 CSV and Flux annotated CSV formats, and passes Parquet bodies through untouched.

Type Coercion

  • Timestamps → DateTime.t() with microsecond precision. JSON carries no column types, so this is by shape and by name: any string in InfluxDB 3's zone-less timestamp rendering (2023-11-14T22:13:20[.fraction]) is a timestamp whatever its column is called (DATE_BIN aliases, selector_*(...)['time'], MAX(time)); the zoned RFC3339 form is decoded only under time, _time, _start and _stop, where v2 emits it. A string field holding exactly that zone-less shape is decoded too — store zoned RFC3339 strings if the distinction matters.
  • JSON / JSONL numbers and booleans keep the types Jason decodes
  • Flux CSV cells are typed from the #datatype annotation row when the query requested one (double, long, unsignedLong, boolean, dateTime:RFC3339[Nano]); InfluxDB 3's format: :csv has no annotations, so every cell stays a string. An empty cell is left out of the row, as a null column is in JSON: CSV writes a null and an empty string alike (InfluxDB 3 writes a one-column row's as "", an empty line to the parser, and that row is %{}). A newline inside a value is restored: InfluxDB 2's CSV writer sends it as \r\n

Summary

Functions

Coerces known value types in a row map.

Parses a response body based on the specified format.

Functions

coerce_types(row)

@spec coerce_types(map()) :: map()

Coerces known value types in a row map.

Converts timestamp strings to DateTime (see "Type Coercion" in the moduledoc for which strings count as timestamps); leaves everything else as-is.

parse(body, format \\ :json)

@spec parse(binary(), atom()) :: {:ok, [map()] | binary()} | {:error, term()}

Parses a response body based on the specified format.

Parameters

  • body - response body binary
  • format - one of :json, :jsonl, :csv (InfluxDB 3: one header row, then one line per row), :flux_csv (InfluxDB 2's annotated CSV: tables separated by an empty line, each with its own header), :parquet

Returns

  • {:ok, [map()]} — list of row maps ({:ok, binary()} for :parquet)
  • {:error, reason} — parse failure