Botica.Check.Result (Botica v2.0.0)

Copy Markdown View Source

Logic for building and processing check results.

Summary

Functions

Builds a result map from a check definition and a check result tuple.

Builds a result from an exception.

Builds a result for a timeout situation.

Determines the overall health status from a summary.

Calculates a summary from a list of results.

Converts a raw check result tuple to a status and message.

Functions

build(check, status, message)

Builds a result map from a check definition and a check result tuple.

Examples

iex> check = %{id: :test, name: "Test", fix_command: "fix it"}
iex> Botica.Check.Result.build(check, :ok, "everything fine")
%{id: :test, name: "Test", status: :ok, message: "everything fine", fix_command: "fix it"}

from_exception(check, exception)

@spec from_exception(Botica.Types.check_def(), Exception.t()) :: Botica.Types.result()

Builds a result from an exception.

Examples

iex> check = %{id: :boom, name: "Boom", fix_command: nil}
iex> result = Botica.Check.Result.from_exception(check, %RuntimeError{message: "oops"})
iex> result.status
:error
iex> result.message
"exception: oops"

from_timeout(check, timeout_ms)

Builds a result for a timeout situation.

Examples

iex> check = %{id: :slow, name: "Slow", fix_command: nil}
iex> result = Botica.Check.Result.from_timeout(check, 5000)
iex> result.status
:error
iex> result.message
"timeout: check exceeded 5000ms"

health_status(summary)

@spec health_status(Botica.Types.summary()) :: :ok | :degraded | :fail

Determines the overall health status from a summary.

Examples

iex> Botica.Check.Result.health_status(%{error: 0, warning: 0})
:ok
iex> Botica.Check.Result.health_status(%{error: 0, warning: 1})
:degraded
iex> Botica.Check.Result.health_status(%{error: 1, warning: 0})
:fail

summarize(results)

@spec summarize([Botica.Types.result()]) :: Botica.Types.summary()

Calculates a summary from a list of results.

Examples

iex> results = [
...>   %{status: :ok},
...>   %{status: :ok},
...>   %{status: :warning},
...>   %{status: :error}
...> ]
iex> Botica.Check.Result.summarize(results)
%{ok: 2, warning: 1, error: 1, total: 4, passed?: false}

to_status(arg)

Converts a raw check result tuple to a status and message.

Examples

iex> Botica.Check.Result.to_status({:ok, "good"})
{:ok, "good"}

iex> Botica.Check.Result.to_status({:warning, "low memory"})
{:warning, "low memory"}

iex> Botica.Check.Result.to_status({:error, "connection failed"})
{:error, "connection failed"}