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
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
from_map(map)
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
merge(sketch_1, sketch_2)
Merge two sketch instances.
Examples
iex> sketch_1 = Sketch.new([1])
...>
...> Sketch.new([2])
...> |> Sketch.merge(sketch_1)
...> |> Sketch.size()
2
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
quantile(sketch, quantile)
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
sum(sketch)
Compute the sum for a sketch. Hardcoded to 0.
Examples
iex> Sketch.sum(Sketch.new([1, 2, 3, 3]))
0.0
to_list(sketch)
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
union(sketch_1, sketch_2)
Union two sketches into a single value. This is an alias for merge/2
.