Oban.Met.Values.Sketch (Oban Met v0.1.5)

A fast and fully mergeable quantile sketch with relative error guarantees.

Derived from DogSketch, based on DDSketch. This variant has a hard-coded error rate of 0.02 for the sake of simplicity.

Summary

Functions

Insert sample values into a sketch.

Initialize a sketch struct from a stringified map, e.g. encoded JSON.

Merge two sketch instances.

Create a new sketch instance with an optional error rate.

Compute the quantile value for a sketch.

Compute the sum for a sketch. Hardcoded to 0.

Convert a sketch into a list of bins and values.

Union two sketches into a single value. This is an alias for merge/2.

Types

@type t() :: %Oban.Met.Values.Sketch{
  data: %{optional(pos_integer()) => pos_integer()},
  size: non_neg_integer()
}

Functions

Link to this function

add(sketch, value)

@spec add(t(), pos_integer()) :: t()

Insert sample values into a sketch.

Examples

iex> Sketch.new()
...> |> Sketch.add(1)
...> |> Sketch.add(2)
...> |> Sketch.add(3)
...> |> Sketch.size()
3
@spec from_map(%{optional(String.t()) => term()}) :: t()

Initialize a sketch struct from a stringified map, e.g. encoded JSON.

Examples

iex> Sketch.new([1, 2])
...> |> Jason.encode!()
...> |> Jason.decode!()
...> |> Sketch.from_map()
...> |> Sketch.quantile(1.0)
...> |> floor()
2
Link to this function

merge(sketch_1, sketch_2)

@spec merge(t(), t()) :: t()

Merge two sketch instances.

Examples

iex> sketch_1 = Sketch.new([1])
...>
...> Sketch.new([2])
...> |> Sketch.merge(sketch_1)
...> |> Sketch.size()
2
Link to this function

new(values \\ [])

@spec new(pos_integer() | [pos_integer()]) :: t()

Create a new sketch instance with an optional error rate.

Examples

iex> sketch = Sketch.new()
...> Sketch.size(sketch)
0

iex> sketch = Sketch.new(1)
...> Sketch.size(sketch)
1

iex> sketch = Sketch.new([1, 2, 3])
...> Sketch.size(sketch)
3
Link to this function

quantile(sketch, quantile)

@spec quantile(t(), float()) :: nil | float()

Compute the quantile value for a sketch.

Examples

Without any values:

iex> Sketch.quantile(Sketch.new(), 0.5)
nil

With recorded values:

iex> Sketch.new()
...> |> Sketch.add(1)
...> |> Sketch.add(2)
...> |> Sketch.add(3)
...> |> Sketch.quantile(0.5)
...> |> trunc()
2
@spec sum(t()) :: float()

Compute the sum for a sketch. Hardcoded to 0.

Examples

iex> Sketch.sum(Sketch.new([1, 2, 3, 3]))
0.0
Link to this function

to_list(sketch)

@spec to_list(t()) :: [float()]

Convert a sketch into a list of bins and values.

Examples

iex> Sketch.new()
...> |> Sketch.add(1)
...> |> Sketch.add(2)
...> |> Sketch.add(3)
...> |> Sketch.to_list()
...> |> length()
3
Link to this function

union(sketch_1, sketch_2)

@spec union(t(), t()) :: t()

Union two sketches into a single value. This is an alias for merge/2.